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

问题描述 投票:0回答:1

我只是有一个很长一段时间无法解决的问题。一般来说:我想为我的游戏制作一个播放器动画。我已经下载了一些精灵: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()

python python-3.x pygame
1个回答
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
© www.soinside.com 2019 - 2024. All rights reserved.