2013-04-24 64 views
4

我嘗試讓我的圖像(鳥)來上下移動屏幕上,但我不能弄清楚如何在這裏做到這一點是我嘗試我確定它的路要走,但我嘗試以圖如果任何人都可以幫助,那就太棒了!如何Pygame的移動雪碧

import pygame 
import os 

screen = pygame.display.set_mode((640, 400)) 

running = 1 

while running: 
    event = pygame.event.poll() 
    if event.type == pygame.QUIT: 
     running = 0 
    screen.fill([255, 255, 255]) 
    clock = pygame.time.Clock() 
    clock.tick(0.5) 
    pygame.display.flip() 

    bird = pygame.image.load(os.path.join('C:\Python27', 'player.png')) 
    screen.blit(bird, (0, 0)) 

    pygame.display.update() 

class game(object): 
    def move(self, x, y): 
     self.player.center[0] += x 
     self.player.center[1] += y 


    if event.key == K_UP: 
     player.move(0,5) 
    if event.key == K_DOWN: 
     player.move(0,-5) 

game() 

即時試圖讓它下移的向下按下按鈕和最多的UP按鍵

+5

哇,鳥是你最擔心的在這一點上。你有一個循環,每次都加載鳥。你正在調用遊戲類,就像它是一個函數。你甚至還沒有初始化pygame。在理解遊戲循環之前,不要考慮移動和用戶輸入。 – ecline6 2013-04-24 04:28:45

+0

是啊我真的搞不清這一切m特很新,它 – Serial 2013-04-24 04:29:53

+1

我建議通過一些在pygame的網站上的教程去。 http://www.pygame.org/wiki/tutorials此外,在Python中,類一些清新,和控制流的想法是一個好主意了。順便提一句,你的K_UP和K_DOWN事件永遠不會被讀取,因爲你的遊戲被卡在上面的循環中。如果註釋掉while循環,腳本將不會運行。但是,不要專注於此。去找你一個教程吧! :) – ecline6 2013-04-24 04:33:07

回答

9

如前所述通過ecline6,鳥是你最擔心的在這一點上。

考慮閱讀this book ..

現在,首先,讓我們清理你的代碼...

import pygame 
import os 

# let's address the class a little later.. 

pygame.init() 
screen = pygame.display.set_mode((640, 400)) 
# you only need to call the following once,so pull them out of the while loop. 
bird = pygame.image.load(os.path.join('C:\Python27', 'player.png')) 
clock = pygame.time.Clock() 

running = True 
while running: 
    event = pygame.event.poll() 
    if event.type == pygame.QUIT: 
     running = False 

    screen.fill((255, 255, 255)) # fill the screen 
    screen.blit(bird, (0, 0)) # then blit the bird 

    pygame.display.update() # Just do one thing, update/flip. 

    clock.tick(40) # This call will regulate your FPS (to be 40 or less) 

現在,您的「鳥」是不動的原因是:
當你的blit圖像,即:screen.blit(bird, (0, 0))
(0,0)是恆定的,所以它不會移動。

下面是最後的代碼,你想要的輸出(嘗試)並閱讀註釋:

import pygame 
import os 

# it is better to have an extra variable, than an extremely long line. 
img_path = os.path.join('C:\Python27', 'player.png') 

class Bird(object): # represents the bird, not the game 
    def __init__(self): 
     """ The constructor of the class """ 
     self.image = pygame.image.load(img_path) 
     # the bird's position 
     self.x = 0 
     self.y = 0 

    def handle_keys(self): 
     """ Handles Keys """ 
     key = pygame.key.get_pressed() 
     dist = 1 # distance moved in 1 frame, try changing it to 5 
     if key[pygame.K_DOWN]: # down key 
      self.y += dist # move down 
     elif key[pygame.K_UP]: # up key 
      self.y -= dist # move up 
     if key[pygame.K_RIGHT]: # right key 
      self.x += dist # move right 
     elif key[pygame.K_LEFT]: # left key 
      self.x -= dist # move left 

    def draw(self, surface): 
     """ Draw on surface """ 
     # blit yourself at your current position 
     surface.blit(self.image, (self.x, self.y)) 


pygame.init() 
screen = pygame.display.set_mode((640, 400)) 

bird = Bird() # create an instance 
clock = pygame.time.Clock() 

running = True 
while running: 
    # handle every event since the last frame. 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      pygame.quit() # quit the screen 
      running = False 

    bird.handle_keys() # handle the keys 

    screen.fill((255,255,255)) # fill the screen with white 
    bird.draw(screen) # draw the bird to the screen 
    pygame.display.update() # update the screen 

    clock.tick(40) 
+2

如果你不明白這個部分,讀了這本書。它將幫助你(很多) – pradyunsg 2013-04-24 07:57:41

+0

+1,因爲它包括Invent with Python。如果我在開始時就已經瞭解這本書,那麼它會回答很多問題。 – ecline6 2013-04-24 14:21:40

+0

@ ecline6就是爲什麼我提到它.. – pradyunsg 2013-04-24 14:22:50