2017-04-27 79 views
1

我正在嘗試做一個簡單的遊戲,但移動不能正常工作 我希望它在按鈕被按下時不斷移動,但是尊重屏幕的邊界當前按鈕將總體工作但它使矩形口吃,這裏是我的代碼,pygame不斷移動與if語句

import pygame 

pygame.init() 

SD = pygame.display.set_mode((640,480)) 
x = 16 
y = 16 

while True: 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      pygame.quit() 
      quit() 
     keys = pygame.key.get_pressed() 
     if keys[pygame.K_w]: 
      if y > 0: 
       y -= 8 
     if keys[pygame.K_a]: 
      if x > 0: 
       x -= 8 
     if keys[pygame.K_s]: 
      if y < 448: 
       y += 8 
     if keys[pygame.K_d]: 
      if x < 608: 
       x += 8 
    SD.fill((255,255,255)) 
    pygame.draw.rect(SD, (255,0,0), (x,y, 30, 30)) 
    pygame.display.update() 

回答

1

pygame.event.get()如果硬件發生事件就像按下一個鍵將只返回一個事件。所以你的if keys[...]不會評估什麼時候沒有發生(一個按下的關鍵事件不會重複)

移動你的ifs一個級別,它會工作沒有口吃,但你必須放慢你的運動(sleep(0.1)會做的例子,但你可能想移動到更先進的東西,因爲你不想睡在你的繪製循環)

+0

是的,不要在遊戲循環中使用sleep()。 Pygame有一個內置的時鐘。使用'clock = pygame.time.Clock()'創建它,然後在遊戲循環中使用'clock.tick(30)' - 這將確保您的循環以30 fps運行。 – Chris

1

按鍵的方式是在pygame(和大多數其他遊戲引擎)是你只有在按下或釋放按鍵時纔會得到一個事件。你的角色運動看起來如此跳躍的原因是因爲按鍵操作就像在文本編輯器中按住一個鍵一樣。如果你按下這個鍵,一封信就會顯示出來,一會兒你就會得到很多重複的信。

你真正想要做的是爲每個鍵設置一個布爾值,當你得到一個按鍵事件時你設置爲True,當你得到一個鍵釋放事件時False(請仔細看一下process_events函數)。

我修改代碼來做到這一點(有一些其他的變化一起,我會再具體解釋):

import pygame 


class Game(object): 
    def __init__(self): 
     """ 
     Initialize our game. 
     """ 
     # The initial position. 
     self.x = 16 
     self.y = 16 

     # The keyboard state. 
     self.keys = { 
      pygame.K_w: False, 
      pygame.K_a: False, 
      pygame.K_s: False, 
      pygame.K_d: False, 
     } 

     # Create the screen. 
     self.SD = pygame.display.set_mode((640,480)) 

    def move_character(self): 
     """ 
     Move the character according to the current keyboard state. 
     """ 
     # Process vertical movement. 
     if self.keys[pygame.K_w] and self.y > 0: 
      self.y -= 1 
     if self.keys[pygame.K_s] and self.y < 448: 
      self.y += 1 

     # Process horizontal movement. 
     if self.keys[pygame.K_a] and self.x > 0: 
      self.x -= 1 
     if self.keys[pygame.K_d] and self.x < 608: 
      self.x += 1 

    def process_events(self): 
     """ 
     Go through the pending events and process them. 
     """ 
     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       pygame.quit() 
       quit() 
      # If the event is a key press or release event, then register it 
      # with our keyboard state. 
      elif event.type == pygame.KEYDOWN: 
       self.keys[event.key] = True 
      elif event.type == pygame.KEYUP: 
       self.keys[event.key] = False 

    def draw(self): 
     """ 
     Draw the game character. 
     """ 
     self.SD.fill((255,255,255)) 
     pygame.draw.rect(self.SD, (255,0,0), (self.x, self.y, 30, 30)) 
     pygame.display.update() 

    def run(self): 
     while True: 
      self.process_events() 
      self.move_character() 
      self.draw() 


def main(): 
    pygame.init() 

    game = Game() 
    game.run() 


# This just means that the main function is called when we call this file 
# with python. 
if __name__ == '__main__': 
    main() 

我做了最大的變化就是你的遊戲進入一個類讓您更好地訪問您的函數中的變量。它還可以讓你將你的代碼分成不同的功能,使它更易於閱讀。