【发布时间】:2020-04-13 12:32:50
【问题描述】:
我正在使用 Python 模块 Pygame 制作 2D 平台游戏。我已经为所述玩家制作了背景、角色和动作。您可以使用键盘上的 a & d 移动并使用 SPACE 跳跃。但是我不知道如何为玩家制作空闲动画。我有跑步动画,但是我还没有制作精灵,所以我只是使用了空闲的图片集。
这是我的代码:
import pygame, time, itertools
pygame.init()
# background image
walkRight = pygame.image.load('idle1.png')
walkLeft = pygame.image.load('idle1.png')
bg = pygame.image.load("background.png")
idle = [pygame.image.load('idle1.png'), pygame.image.load('idle2.png'), pygame.image.load('idle3.png')]
standcount = True
clock = pygame.time.Clock()
# jump
isJump = False
jumpcount = 10
# window
display_width = 1000
display_height = 600
win = pygame.display.set_mode((display_width, display_height))
# title & icon
pygame.display.set_caption("Grand Theft Ewok")
icon = pygame.image.load('bear.png')
pygame.display.set_icon(icon)
# player creds
x = 50
y = 430
vel = 10
# playerIMG
playerIMG = pygame.image.load('player.png')
def player(x, y):
global standcount
global walkcount
win.blit(bg, (0, 0))
# win.blit(playerIMG, (x,y))
if walkcount + 1 >= 9:
walkcount = 0
if standcount + 1 >= 9:
standcount = 0
if left:
win.blit(idle[walkcount // 3], (x, y))
walkcount += 1
elif right:
win.blit(idle[walkcount // 3], (x, y))
walkcount += 1
elif standcount:
p = 0
for frame in idle:
win.blit(idle[p], (x, y))
p += 1
if p >= 2:
p = 0
continue
pygame.display.update()
# game loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# movement
keys = pygame.key.get_pressed()
if keys[pygame.K_a] and x > (vel - 25):
x -= vel
left = True
right = False
elif keys[pygame.K_d] and x < 835:
x += vel
right = True
left = False
else:
right = False
left = False
walkcount = 0
standcount = True
if not (isJump):
if keys[pygame.K_SPACE]:
isJump = True
left = False
right = False
else:
if jumpcount >= -10:
neg = 1
if jumpcount < 0:
neg = -1
y -= (jumpcount ** 2) * 0.5 * neg
jumpcount -= 1
else:
isJump = False
jumpcount = 10
player(x, y)
pygame.display.update()
pygame.quit()
在播放器功能下,您可以看到我尝试使用 for 循环的位置,但它不起作用。
我正在使用 Python 3.8。
祝大家好运。
待在室内,谢谢。
编辑 我已经找到了如何使用这个来制作空闲动画:
elif standcount:
p = 0
for frame in idle:
win.blit(idle[standcount], (x, y))
standcount += 1
#pygame.display.update()
if standcount >= 2:
standcount = 0
continue
pygame.display.update()
但是,它迭代列表的速度非常快。我想不出一种不使用 time.sleep 来减慢它的方法,因为这样每次我停止移动时它都会冻结游戏。
谢谢
【问题讨论】: