簡體   English   中英

如何讓玩家角色移動?

[英]How do I get the player character to move?

當我遇到障礙時,我開始改進我的代碼。 我的玩家角色可以跳躍但不能左右移動。程序運行就像沒有語法錯誤一樣。 主要目的是試圖讓角色左右移動

這里是播放器 class 定義了它的屬性和功能

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()

好的,有些代碼似乎有點混亂。 我認為如果您的player class 只是處理作為向導(這本身就是一項足夠大的任務),並且您的主事件循環應該注意用戶輸入,那會更好。

主循環使用pygame.KEYDOWN事件。 如果您想要那種按下鍵、向上鍵“打字”的運動,這很好。 但更自然的方法是簡單地檢查pygame.key.get_pressed() ,它返回所有按鈕的 state。 由於您的播放器已經保持了速度,因此請使用鍵狀態來調整速度。

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 之外。

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記錄為 boolean,而是存儲跳轉開始的毫秒時間。 然后在player.update()期間,使用實時差異來移動玩家通過他們的(拋物線?)路徑上下移動。 一旦player.jumptime重置為零,他們的用戶可以讓向導再次跳轉。

問題是您的播放器self.k的 self.k 只是創建播放器時鍵的 state 。 嘗試將self.k=pygame.key.get_pressed()放在玩家的移動 function 中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM