在pygame上开发游戏时出现错误

时间:2020-06-05 18:09:01

标签: python pygame

我有一个很长一段时间无法解决的问题。
一般而言:
我想为我的游戏制作一个播放器动画。 我已经下载了一些图片:
1:5个精灵用于左手动画
2:5个用于右手动画的精灵
3:玩家静止不动时1个精灵
我的代码中有一个错误:当玩家向右或向左移动时,会显示玩家静止不动的图片。
而当玩家跳跃并向右/向左移动时,一切正常工作
这是指向github仓库的链接:github.com/Fierce-Wolf/Game
代码:

import pygame

pygame.init()

win = pygame.display.set_mode((500, 500))  # Create a window
pygame.display.set_caption('Gangster Diplomat')  # Give name of the application

# Loading sprites:

# Sprites walk to right
walkRight = [
    pygame.image.load('right_1.png'), 
    pygame.image.load('right_2.png'), 
    pygame.image.load('right_3.png'), 
    pygame.image.load('right_4.png'), 
    pygame.image.load('right_5.png')
]

# Sprites walk to left
walkLeft = [
    pygame.image.load('left_1.png'), 
    pygame.image.load('left_2.png'), 
    pygame.image.load('left_3.png'), 
    pygame.image.load('left_4.png'), 
    pygame.image.load('left_5.png')
]

bg = pygame.image.load('bg.jpg')    # Background

# This is where the sprite of the player standing still is stored
playerStand = pygame.image.load('idle.png') 

# INITIAL COORDINATES OF PLAYER
x = 50
y = 425

# Player parameters
width = 60  # Ширина
height = 71  # Высота
speed = 5  # Скорость

# Game physics parameters
isJump = False  # Jump status (the player jumped or DIDN't jump)
jumpCount = 10 # This variable determines the height of the jump

# For animating objects:
Left = False
Right = False
animCount = 0

clock = pygame.time.Clock()

def draw_window():
    '''This function redraws and updates the game window
    During each iteration of the game cycle
    '''
    global animCount
    win.blit(bg, (0, 0)) # Displaying the background image on our window

    # Create the animation of the game
    if (animCount + 1) >= 25:
        animCount = 0

    if Left:
        win.blit(walkLeft[animCount // 5], (x, y))
        animCount += 1

    elif Right:
        win.blit(walkRight[animCount // 5], (x, y))
        animCount += 1
    else:
        win.blit(playerStand, (x, y))

    pygame.display.update()  # Обновляем окно игры

# Создаем игровой цикл
my_game = True
while my_game:
    clock.tick(25)  # fps

    # ↓ Этот цикл (for) обрабатывает все игровые события.
    # Но в данном случае, нам нужен только один тип событий.
    for event in pygame.event.get():
        # Если юзер нажал на 'красный крестик' в углу окна приложения, то
        if event.type == pygame.QUIT:
            # Приложение закрывается т.к цикл больше не выполняется
            my_game = False

    keys = pygame.key.get_pressed()
    # Move left
    if (keys[pygame.K_LEFT] or keys[pygame.K_a]) and x > 5:
        x -= speed

        Left = True
        Right = False

    # Move right
    if (keys[pygame.K_RIGHT] or keys[pygame.K_d]) and x < (500 - width - 5):
        x += speed

        Left = False
        Right = True

    if not(isJump): # If player didn't jump

        # jump
        if keys[pygame.K_SPACE]:
            isJump = True

        # if player doesn't move
        else:
            Left = False
            Right = False
            animCount = 0

    # If player jump
    else:
        # Create the physics of jump
        if jumpCount >= -10:

            # ↓ This operator is necessary for the player to go down
            # after climbing up
            if jumpCount < 0:
                y += (jumpCount ** 2) / 2
            else:
                # Raising the player up
                y -= (jumpCount ** 2) / 2

            # Уменьшаем еденицу прыжка
            jumpCount -= 1

            # This way the player will go up and down 
            # the same number of units
            # => вверх = вниз

        # Jump 'over'
        else:
            # Returning the isjump variable to its original value,
            isJump = False

            # Returning the old value to the jumpcount variable
            jumpCount = 10

    draw_window()

pygame.quit()

1 个答案:

答案 0 :(得分:0)

Left = FalseRight = False设置在错误的位置:

  • 在评估移动方向之前,先全部设置False
  • 根据移动方向进行更改
  • 如果玩家跳动,请重置
while my_game:

    Left, Right = False, False # <------------ 

    # Move left
    if (keys[pygame.K_LEFT] or keys[pygame.K_a]) and x > 5:
        x -= speed
        Left = True # <------------ 
    # Move right
    if (keys[pygame.K_RIGHT] or keys[pygame.K_d]) and x < (500 - width - 5):
        x += speed
        Right = True # <------------ 

    if not(isJump): # If player didn't jump
        # jump
        if keys[pygame.K_SPACE]:
            isJump = True
        # if player doesn't move
        else:
            animCount = 0

    # If player jump
    else:
        # Create the physics of jump
        if jumpCount >= -10:
            Left, Right = False, False # <------------ 

            # ↓ This operator is necessary for the player to go down
            # after climbing up
            if jumpCount < 0:
                y += (jumpCount ** 2) / 2
            else:
                # Raising the player up
                y -= (jumpCount ** 2) / 2

            # Уменьшаем еденицу прыжка
            jumpCount -= 1

            # This way the player will go up and down 
            # the same number of units
            # => вверх = вниз

        # Jump 'over'
        else:
            # Returning the isjump variable to its original value,
            isJump = False

            # Returning the old value to the jumpcount variable
            jumpCount = 10