因此,我使用another answer帮助我找到了一种在pygame中移动玩家精灵的简单方法。唯一的问题是,必须为移动按钮设置垃圾邮件,以使Sprite每次按下该按钮时,其移动量超过指定的五个像素。
这是完整的代码。有问题的区域是Player()类和“玩家”。循环中的区域:
import pygame
player_path = "downChara.png" #specifies image path
class Player(object): #representitive of the player's overworld sprite
def __init__(self):
self.image = pygame.image.load(player_path).convert_alpha() #creates image, the player_path variable allowing it to be updated
self.X = 284; # x co-ord of player
self.Y = 184; # y co-ord of player
def handle_keys(self): #handling the keys/inputs
key = pygame.key.get_pressed()
dist = 5 #distance travelled in one frame of the program
if key[pygame.K_DOWN]: #if down
self.Y += dist #move down the length of dist
player_path = "downChara.png" #change image to down
self.image = pygame.image.load(player_path).convert_alpha()
elif key[pygame.K_UP]: #if up
self.Y -= dist #move up the length of dist
player_path = "upChara.png" #change to up
self.image = pygame.image.load(player_path).convert_alpha()
if key[pygame.K_RIGHT]: #etc.
self.X += dist
player_path = "rightChara.png"
self.image = pygame.image.load(player_path).convert_alpha()
elif key[pygame.K_LEFT]:
self.X -= dist
player_path = "leftChara.png"
self.image = pygame.image.load(player_path).convert_alpha()
def draw(self, surface): #draw to the surface/screen
surface.blit(self.image, (self.X, self.Y))
pygame.init() (width, height) = (600, 400) #specify window resolution bg_colour = (100,20,156) #specify bg colour
screen = pygame.display.set_mode((width, height)) #create window pygame.display.set_caption('EduGame') #specify window name
player = Player() clock = pygame.time.Clock()
pygame.display.flip() #paints screen gameRun = True #allow game events to loop/be carried out more than once
while gameRun: #while game is running:
for event in pygame.event.get(): #all the events (movement, etc) to be here.
if event.type == pygame.QUIT: #if the "x" is pressed
pygame.quit() #quit game
gameRun = False #break the loop.
quit()
player.handle_keys() #handle keys
screen.fill(bg_colour) #draw background colour
player.draw(screen) #draws player
pygame.display.update()
clock.tick(40)
以下是我提到的具体领域:
class Player(object): #representitive of the player's overworld sprite
def __init__(self):
self.image = pygame.image.load(player_path).convert_alpha() #creates image, the player_path variable allowing it to be updated
self.X = 284; # x co-ord of player
self.Y = 184; # y co-ord of player
def handle_keys(self): #handling the keys/inputs
key = pygame.key.get_pressed()
dist = 5 #distance travelled in one frame of the program
if key[pygame.K_DOWN]: #if down
self.Y += dist #move down the length of dist
player_path = "downChara.png" #change image to down
self.image = pygame.image.load(player_path).convert_alpha()
elif key[pygame.K_UP]: #if up
self.Y -= dist #move up the length of dist
player_path = "upChara.png" #change to up
self.image = pygame.image.load(player_path).convert_alpha()
if key[pygame.K_RIGHT]: #etc.
self.X += dist
player_path = "rightChara.png"
self.image = pygame.image.load(player_path).convert_alpha()
elif key[pygame.K_LEFT]:
self.X -= dist
player_path = "leftChara.png"
self.image = pygame.image.load(player_path).convert_alpha()
def draw(self, surface): #draw to the surface/screen
surface.blit(self.image, (self.X, self.Y))
和:
player.handle_keys() #handle keys
screen.fill(bg_colour) #draw background colour
player.draw(screen) #draws player
“玩家”。不是在任何event.type if / elif循环中,但是当我尝试将其放在“ event.type == pygame.KEYDOWN”下时,它似乎没有任何作用。
这不一定是一个严重的错误,因为我可以尝试调整自己的游戏,但是我认为,如果玩家能够按住相应的键而不是垃圾邮件,那么该游戏将运行并看起来更加流畅-轻按他们十年来尝试导航。
(作为奖励,可以尝试找到一种比我在“ def handle_keys(self)”函数下所做的更好的更新精灵路径/位置的方法)
欢呼
答案 0 :(得分:0)
原来是缩进才是问题所在。由于某些原因,不同的区域在缩进方面有所不同,因此抛弃了所有内容。感谢@sloth提到缩进导致我在修复它时不小心修复了:P