如何使玩家角色移动?

时间:2019-11-17 18:00:01

标签: python python-3.x pygame

遇到障碍时,我开始改进代码。我的玩家角色可以跳跃,但不能左右移动。程序运行时好像没有语法错误。主要目的是试图使角色向左和向右移动

这是定义其属性和功能的播放器类

class player:
    def __init__(self,x,y):
        self.x = x
        self.y = y
        self.width = 64
        self.height = 64
        self.standing = True
        self.left = False
        self.right = True
        self.vel = 15
        self.jumping = False
        self.jumpCount = 10
        self.k = pygame.key.get_pressed()
    def move(self,x,y):
        if not self.standing:
            if self.k[pygame.K_LEFT] and self.x  > 0 - 150:
                self.left = True
                self.right = False            
                self.x -= self.vel
            elif self.k[pygame.K_RIGHT] and self.x  < 500 - 150 :
                self.right = True
                self.left = False
               self.x += self.vel
        else:
            self.standing = True

主循环

run = True
wizard = player(25,320)
while run:#main game loop
    pygame.time.delay(15)
    for event in pygame.event.get():#loops through a list of keyboard or mouse events
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                wizard.jumping = True    
    wizard.move(wizard.x,wizard.y)
    win.blit(bg,(0,0))
    wizard.jump(wizard.y)
    wizard.draw(win) 
    pygame.display.update()
pygame.quit()

2 个答案:

答案 0 :(得分:1)

好的,某些代码似乎有些混乱。我认为最好将您的player类作为一个向导来处理(这本身就是一个足够大的任务),并且您的主事件循环应注意用户输入。

主循环正在使用pygame.KEYDOWN事件。如果您想要那种按下键,按下键的“键入”动作,那就很好了。但是,更自然的方法是简单地检查pygame.key.get_pressed(),它返回所有按钮的状态。由于您的播放器已经保持了速度,因此请使用键状态来调整速度。

FPS=20
clock = pygame.time.Clock()   # clock to limit the FPS
while run: #main game loop
    #pygame.time.delay(15)   # <-- No need to delay here, see FPS limit below
    for event in pygame.event.get():  #loops through event list
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                wizard.goJump()

    # Handle keys pressed
    if ( keys[pygame.K_LEFT] ):
        wizard.goLeft()
    elif ( keys[pygame.K_RIGHT] ):
        wizard.goRight()

    # Update the player's position
    #wizard.move(wizard.x,wizard.y)
    wizard.update()

    # redraw the screen
    win.blit( bg, (0, 0) )
    wizard.draw( win ) 
    pygame.display.update()

    # Update the window, but at a useful FPS
    clock.tick_busy_loop( FPS )

pygame.quit() 

因此,这意味着对player进行了一些更改。我试图将所有“玩家处理”功能保留在播放器中,同时将所有用户输入的处理代码移到类之外。

class player:
    def __init__(self,x,y):
        self.x = x
        self.y = y
        self.width = 64
        self.height = 64
        #self.standing = True          <-- Standing is "not self.jumping"
        #self.left = False
        #self.right = True
        self.vel = 15                 # current player speed
        self.max_vel = 20             # Limit the player speed extremes
        self.mix_vel = -20
        self.jumping = False
        self.jumpCount = 10
        # self.k = pygame.key.get_pressed()  <-- Don't handle events inside the player

    def goLeft( self ):
        """ Handle the user-input to move left """      
        self.vel -= 1
        if ( self.vel <= self.min_vel ):
            self.vel = self.min_vel

    def goRight( self ):
        """ Handle the user-input to move right """
        self.vel += 1
        if ( self.vel >= self.max_vel ):
            self.vel = self.max_vel

    def goJump( self ):
        """ Handle the user-input to jump """
        if ( self.jumping == False ):
            self.jumping = True
            print( "TODO: Handle Jumping" )
        else:
            print( "TODO: Already Jumping" )

    def update( self ):
        # Move the character left/right
        self.x += self.vel   # (+) velocity is right, (-) is left
        # handle the progress of the jump
        if ( self.jumping == True ):
            print( "TODO: jump wizard - GO!" )

未实现跳转。一种实现方法是,不是简单地将self.jumping记录为布尔值,而是存储跳转开始的毫秒时间。然后在player.update()期间,使用实时差异使播放器在其(抛物线?)路径中上下移动。将player.jumptime重置为零后,他们可以使向导再次跳转。

答案 1 :(得分:0)

问题是您的播放器类的self.k只是创建播放器时按键的状态。尝试将self.k=pygame.key.get_pressed()放在播放器的移动功能中。