【发布时间】:2020-10-24 10:50:17
【问题描述】:
我创建了一个名为 JumpyMan 的游戏。
我创造了游戏机制,比如左右移动、跳跃和摔倒。
因为游戏不是很好看,所以我添加了一些角色动画(精灵),当你按下 Key D 时,动画开始并且角色朝着正确的方向移动。
但是,当我按下Key A 时,角色向左移动但动画没有开始。当我同时按下两个键时,左侧动画开始,但我的角色静止不动。
我能做些什么来修复这个错误?
这是我的代码:https://github.com/zewutz/jumpyman(所有需要的图片都在 github repo 中)
import pygame
import os
from pygame import Surface
from pygame.transform import scale
pygame.init()
#Screen, title/icon and clock
screen = pygame.display.set_mode((1366,650))
pygame.display.set_caption("JumpyMan")
iconImg = pygame.image.load('resources\images\jumpyman\stationary\statright.png')
iconImg = pygame.transform.scale(iconImg,(100,128))
pygame.display.set_icon(iconImg)
clock = pygame.time.Clock()
#backgroung
backgroundImage = pygame.image.load('resources\images\Imgbackground\jumpymanbackground.jpg')
backgroundCoord = (0,-350)
#Player sprites
left = [None]*10
for picIndex in range(1,10):
left[picIndex-1] = pygame.image.load(os.path.join('resources\images\jumpyman\imgleft', "L" + str(picIndex) + ".png"))
picIndex += 1
right = [None]*10
for picIndex in range(1,10):
right[picIndex-1] = pygame.image.load(os.path.join('resources\images\jumpyman\imgright', "Run__00" + str(picIndex) + ".png"))
picIndex += 1
# Player
playerImg = pygame.image.load('resources\images\jumpyman\stationary\statright.png')
smallerImage = pygame.transform.scale(playerImg, (50,100))
x = 10
y = 460
vel_x = 5
vel_y = 15
jump = False
move_left = False
move_right = False
stepIndex = 0
def characterMovementX():
global stepIndex
if stepIndex >= 9:
stepIndex = 0
if move_left:
screen.blit(left[stepIndex], (x,y))
stepIndex += 1
elif move_right:
screen.blit(right[stepIndex], (x,y))
stepIndex += 1
else:
screen.blit(smallerImage,(x,y))
#Game loop
game = True
while game:
screen.blit(backgroundImage,backgroundCoord)
for event in pygame.event.get():
if event.type == pygame.QUIT:
game = False
#Move left, right
userInput = pygame.key.get_pressed()
if userInput[pygame.K_a] and x > 5 :
move_left = True
x -= vel_x
if userInput[pygame.K_d] and x < 1280:
move_right = True
x += vel_x
else:
move_left = False
move_right = False
stepIndex = 0
#Jump
if jump is False and userInput[pygame.K_SPACE]:
jump = True
if jump:
y -= vel_y
vel_y -= 1
if vel_y < -15:
jump = False
vel_y = 15
characterMovementX()
clock.tick(30)
pygame.display.update()
pygame.quit()
【问题讨论】:
标签: python animation pygame character