我正在使用PyGame创建游戏。
我想让我的角色看起来一直在运行。要做到这一点,我必须在两个精灵之间切换。但是这样做通常会导致图像变化太快,如果我添加
time.sleep(1)
暂停整个程序。如果这样做,则不允许我在暂停时间内输入任何内容。
import pygame
import time
pygame.init()
white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
green = (0,155,0)
display_width = 500
display_height = 500
gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption("Sword")
gameDisplay.fill(white)
pygame.display.update()
sUp = pygame.image.load("C:/Users/michael/Google Drive/PyGame/Images/image4.png")
sUp = pygame.transform.scale(sUp, (500,500))
sUp1 = pygame.image.load("C:/Users/michael/Google Drive/PyGame/Images/image5.png")
sUp1 = pygame.transform.scale(sUp1, (500,500))
sDown = pygame.image.load("C:/Users/michael/Google Drive/PyGame/Images/image6.png")
sDown = pygame.transform.scale(sDown, (500,500))
def SUP(x=0, y=0):
gameDisplay.blit(sUp, (x, y))
pygame.display.update()
def SDOWN(x=0, y=0):
gameDisplay.blit(sDown, (x, y))
pygame.display.update()
def running(x=0, y=0):
gameDisplay.blit(sUp, (x, y))
pygame.display.update()
time.sleep(1)
gameDisplay.blit(sUp1, (x, y))
pygame.display.update()
time.sleep(1)
def gameLoop():
game = True
while game == True:
running()
for event in pygame.event.get():
print event
if event.type == pygame.QUIT:
game = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
SDOWN()
if event.type == pygame.KEYUP:
if event.key == pygame.K_SPACE:
SUP()
pygame.quit()
quit()
gameLoop()
这是我的代码。如果有更好的方法,请告诉我。
由于
我已使用您的建议更新了它,但它返回了我在评论中提到的错误。我检查了代码,我很确定它是正确的。
frames = [sUp, sUp1] # Defined at the top of the code to define the frames
frameid = 0 # The current frame
lastframe = None
def SUP(x=0, y=0):
gameDisplay.blit(sUp, (x, y))
pygame.display.update()
def SDOWN(x=0, y=0):
gameDisplay.blit(sDown, (x, y))
pygame.display.update()
def running(x=0, y=0):
global frameid
gameDisplay.blit(frames[frameid], (x, y))
pygame.display.update()
frameid = 1 if frameid == 0 else 0
def gameLoop():
game = True
lastframe = time.time()
while game == True:
if time.time() - lastframe() > 0.25: # Ensure we only update animation every 1/4 second
running()
lastframe = time.time()
time = datetime.now()
print time
running()
for event in pygame.event.get():
print event
if event.type == pygame.QUIT:
game = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
SDOWN()
if event.type == pygame.KEYUP:
if event.key == pygame.K_SPACE:
SUP()
pygame.quit()
quit()
time.sleep(0.1) # Run the game at 10 loops per second.
gameLoop()
答案 0 :(得分:1)
我的部分意见......
在模块的顶部:
frames = [sup, sup1] # Defined at the top of the code to define the frames
frameid = 0 # The current frame
lastframe = None
将运行功能更改为:
def running(x=0, y=0):
global frameid
gameDisplay.blit(frames[frameid], (x, y))
pygame.display.update()
frameid = 1 if frameid == 0 else 0
游戏循环函数中的:
lastframe = time.time()
while game == True:
if time.time() - lastframe > 0.25: # Ensure we only update animation every 1/4 second
running()
lastframe = time.time()
在循环结束时:
time.sleep(0.1) # Run the game at 10 loops per second.
这些都没有经过测试。