能够让图像在Pygame中移动;但我必须不断按下按钮使其移动

2024-03-28 08:11:51 发布

您现在位置:Python中文网/ 问答频道 /正文

所以,我用another answer来帮助我找到一种在pygame中移动我的玩家精灵的简单方法。唯一的问题是,移动按钮必须被垃圾邮件,以便精灵在每次按下按钮时移动超过指定的5个像素。你知道吗

这是完整的代码。有问题的区域是Player()类和循环中的“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

“玩家”不在任何地方事件类型if/elif循环,但是当我尝试将其置于事件类型== pygame.KEYDOWN键“好像没有什么效果。你知道吗

这不一定是一个严重的错误,因为我可以尝试和适应我的游戏围绕它,但我认为,游戏会运行和看起来更顺利,如果玩家能够只是按住各自的关键(s)而不是垃圾邮件点击他们十年来尝试和导航。你知道吗

(作为奖励,您可以尝试找到比“def handle\u keys(self)”功能下更新精灵路径/位置更好的方法)

干杯


Tags: ofthetopathkeyimageselfalpha
1条回答
网友
1楼 · 发布于 2024-03-28 08:11:51

原来是凹痕才是问题所在。由于某种原因,不同的区域的缩进不同于其他区域,这有点像是抛弃了一切。感谢@sloth提到的凹痕导致我在修好它时不小心修好了:P

相关问题 更多 >