pygame中取消跳转机制出现故障

2024-09-30 04:26:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我在pygame中编程smth类似于几何体dash,但我的跳跃机制有缺陷。每当我取消跳转时,它就会出现故障,有时你不能跳转

'''
2D Jump and Run
'''
import pygame, sys, math, random

pygame.init()  # Initializes Pygame module

window = pygame.display.set_mode((1150, 550))  # Creates the Game Window

obstacle = pygame.image.load('square.png')
obstacleX = 1214
obstacleY = 400
obstacleSpeed_x = -1

obstacle2 = pygame.image.load('square.png')
obstacleX2 = 1214 + 1150 / 2
obstacleY2 = 400
obstacleSpeed_x2 = -1

bg = pygame.image.load('bg.png')  # Sets bg to background image
bg_support = pygame.image.load('bg_support.png')  # 2nd half of background

char = pygame.image.load('Character.png')  # Picks a game character
charX = 100  # Determines x pos of character
charY = 400  # Determines y pos of character
charSpeed_y = 0  # Determines speed of characters y pos


def iscollision(x1, y1, x2, y2):  # Determines collision between player and obstacles
    distance = math.sqrt((math.pow(x1 - x2, 2))  # Pythagoras' distance formula
                         + (math.pow(y1 - y2, 2)))
    if distance < 40:
        return True  # Return whether collision or not
    else:
        return False


jump = False
run = True  # Determines if GameLoop is active
while run:  # Game Loop
    window.fill((0, 0, 0))  # Sets background color to black
    window.blit(bg, (0, 0))  # Overrides command above and sets a background image which is displayed
    window.blit(bg_support, (575, 0))  # 2nd half of background displaying
    if charY <= 400:
        jump = True
    else:
        jump = False
    for event in pygame.event.get():  # Event loop
        if event.type == pygame.QUIT:  # Makes sure close button works
            run = False  # GameLoop deactivated because the close button was pressed
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_DOWN and jump:
                charSpeed_y = 1
            elif charY >= 400:
                if event.key == pygame.K_UP:
                    charSpeed_y = -0.6

    charY += charSpeed_y
    if charY <= 250:
        charSpeed_y = 0.6
    elif charY >= 400:
        charSpeed_y = 0
    collision = iscollision(charX, charY, obstacleX, obstacleY)
    collision2 = iscollision(charX, charY, obstacleX2, obstacleY2)
    if collision or collision2:
        sys.exit()
    if obstacleX <= -40:
        a = random.randint(0, 1170)
        obstacleX = 1150 + a
        obstacleSpeed_x += -0.04
        obstacleSpeed_x2 += -0.04
    elif obstacleX2 <= -40:
        a = random.randint(0, 1170)
        obstacleX2 = 1150 + a
    window.blit(char, (charX, charY))  # Displays character
    window.blit(obstacle, (obstacleX, obstacleY))
    window.blit(obstacle2, (obstacleX2, obstacleY2))
    pygame.display.update()  # Updates screen after every iteration of the Game Loop

我试图改变你可以跳的高度,但角色有时还是会被卡在地板上。我不知道如何使用调试器,在这种情况下它可能不会有帮助


Tags: ofimageeventifpngloadwindowpygame
1条回答
网友
1楼 · 发布于 2024-09-30 04:26:45

取消跳跃时,玩家的y移动设置为1

charSpeed_y = 1

按下空格键时,播放机的y向移动设置为-0.6:

if event.key == pygame.K_UP:
   charSpeed_y = -0.6

可能会发生这样的情况:运动员放弃跳跃后的y位置低于400.6。在这种情况下,跳转不会在第一次尝试时启动。
确保球员位置不低于400:

while run:
    # [...]

    charY += charSpeed_y
    if charY <= 250:
        charSpeed_y = 0.6
    elif charY >= 400:
        charSpeed_y = 0
        charY = 400       # < -

相关问题 更多 >

    热门问题