我的代码没有按照我想要的方式工作。我试图在移动时将我的var编程为面向方向。它搞砸了。请尽力帮助我。谢谢。当我试图向上移动时,动画不显示,但是当我向上和向右移动时,它就会起作用。然而,这是唯一一个以任何方式工作的人.......
这是我的代码:
import pygame
from pygame.locals import *
a = 20
# 2 - Initialize the game
pygame.init()
width, height = 720,540
keys = [False, False, False, False]
playerpos=[100,100]
screen=pygame.display.set_mode((width, height))
pygame.display.set_caption("Zelda")
Background = (245,204,153)
# 3 - Load images
down1 = pygame.image.load('link_down1.png')
down2 = pygame.image.load('link_down2.png')
up1 = pygame.image.load('link_up1.png')
up2 = pygame.image.load('link_up2.png')
left1 = pygame.image.load('link_left1.png')
left2 = pygame.image.load('link_left2.png')
player = pygame.image.load("resources/images/link.png")
tree = pygame.image.load("resources/images/tree.png")
right = pygame.transform.flip(left1,True,False)
# 4 - keep looping through
while 1:
# 5 - clear the screen before drawing it again
screen.fill(Background)
# 6 - draw the screen elements
screen.blit(player, playerpos)
screen.blit(tree, (0,0))
screen.blit(tree, (0,20))
screen.blit(tree, (0,40))
screen.blit(tree, (20,0))
screen.blit(tree, (20,20))
screen.blit(tree, (20,40))
screen.blit(tree, (40,0))
screen.blit(tree, (40,20))
screen.blit(tree, (40,40))
pygame.display.flip()
# 7 - loop through the events
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key==K_w:
keys[0]=True
elif event.key==K_a:
keys[1]=True
elif event.key==K_s:
keys[2]=True
elif event.key==K_d:
keys[3]=True
if event.type == pygame.KEYUP:
if event.key==pygame.K_w:
keys[0]=False
elif event.key==pygame.K_a:
keys[1]=False
elif event.key==pygame.K_s:
keys[2]=False
elif event.key==pygame.K_d:
keys[3]=False
# check if the event is the X button
if event.type==pygame.QUIT:
# if it is quit the game
pygame.quit()
exit(0)
# 8 - Move player
if keys[0]:
screen.blit(up1, playerpos)
playerpos[1]-=0.2
screen.fill(Background)
screen.blit(up1,playerpos)
screen.blit(tree, (0,0))
screen.blit(tree, (0,20))
screen.blit(tree, (0,40))
screen.blit(tree, (20,0))
screen.blit(tree, (20,20))
screen.blit(tree, (20,40))
screen.blit(tree, (40,0))
screen.blit(tree, (40,20))
screen.blit(tree, (40,40))
elif keys[2]:
screen.blit(down1, playerpos)
playerpos[1]+=0.2
screen.fill(Background)
screen.blit(up1,playerpos)
screen.blit(tree, (0,0))
screen.blit(tree, (0,20))
screen.blit(tree, (0,40))
screen.blit(tree, (20,0))
screen.blit(tree, (20,20))
screen.blit(tree, (20,40))
screen.blit(tree, (40,0))
screen.blit(tree, (40,20))
screen.blit(tree, (40,40))
if keys[1]:
screen.blit(left1, playerpos)
playerpos[0]-=0.2
screen.fill(Background)
screen.blit(up1,playerpos)
screen.blit(tree, (0,0))
screen.blit(tree, (0,20))
screen.blit(tree, (0,40))
screen.blit(tree, (20,0))
screen.blit(tree, (20,20))
screen.blit(tree, (20,40))
screen.blit(tree, (40,0))
screen.blit(tree, (40,20))
screen.blit(tree, (40,40))
elif keys[3]:
screen.blit(right, playerpos)
playerpos[0]+=0.2
screen.fill(Background)
screen.blit(up1,playerpos)
screen.blit(tree, (0,0))
screen.blit(tree, (0,20))
screen.blit(tree, (0,40))
screen.blit(tree, (20,0))
screen.blit(tree, (20,20))
screen.blit(tree, (20,40))
screen.blit(tree, (40,0))
screen.blit(tree, (40,20))
screen.blit(tree, (40,40))
#9
pygame.display.flip()