2013-04-23 63 views
0

我是pygame的新手,我正在製作一個側面滾動的遊戲,它使用一個鳥作爲角色,它飛行,但我試圖讓它在屏幕上上下移動,但我無法圖如何。側面滾動Pygame

import pygame 
from pygame.locals import * 
import os 
import sys 
import time 
pygame.init() 

class Fly: 
    def __init__(self): 
     self.last_update = time.clock() 

     self.screen = pygame.display.set_mode((700, 400), 0, 32) 
     #Load bird 
     self.bird_state = 1 
     self.bird_frames = [] 
     for r in xrange(1, 5): 
      self.bird_frames.append(pygame.image.load('bird%s.png' % r)) 
     self.bg = pygame.image.load('bg.png').convert() 

     self.Loop() 

    def eventLoop(self): 
     'Take and process input from perephirals' 
     for event in pygame.event.get(): 
      if event.type == QUIT: 
       sys.exit() 

    def Update(self): 
     self.bird_state += 1 
     if self.bird_state > 4: 
      self.bird_state = 1 

    def Draw(self): 
     self.screen.blit(self.bg, [0, 0]) 
     self.screen.blit(self.bird_frames[self.bird_state - 1], (150, 150)) 

    def Loop(self): 
     while 1: 
      self.eventLoop() 
      if time.clock() - self.last_update > 0.15: 
       self.Update() 
       self.last_update = time.clock() 
      self.Draw() 
      pygame.display.update() 

Fly() 
+0

你可以對你的問題更清楚,你是怎麼試圖ECT ECT – 2013-04-23 22:38:34

回答

2
import pygame 
from pygame.locals import * 
import os 
import sys 
import time 
pygame.init() 

class Fly: 
    def __init__(self): 
     self.last_update = time.clock() 

     self.screen = pygame.display.set_mode((700, 400), 0, 32) 
     #Load bird 
     self.bird_state = 1 
     self.bird_frames = [] 
     for r in xrange(1, 5): 
      self.bird_frames.append(pygame.image.load('bird%s.png' % r)) 
     self.bg = pygame.image.load('bg.png').convert() 

     self.Loop() 

    def eventLoop(self): 
     'Take and process input from perephirals' 
     for event in pygame.event.get(): 
      if event.type == QUIT: 
       sys.exit() 

    def Update(self): 
     self.bird_state += 1 
     if self.bird_state > 4: 
      self.bird_state = 1 

    def Draw(self): 
     self.screen.blit(self.bg, [0, 0]) 
     self.screen.blit(self.bird_frames[self.bird_state - 1], (150, 150)) 

    def Loop(self): 
     while 1: 
      self.eventLoop() 

      k = pygame.key.get_pressed() 
      if k[K_DOWN]: 
       # Do something with your bird 
      if k[K_UP]: 
       # Do something with your bird 

      if time.clock() - self.last_update > 0.15: 
       self.Update() 
       self.last_update = time.clock() 
      self.Draw() 
      pygame.display.update() 

Fly() 

pygame.key.get_pressed將檢查是否按一個按鈕。檢查你在那裏有什麼樣的選擇參考。 http://www.pygame.org/docs/ref/key.html

將它放在循環中,因爲它的「實時性」。

k = pygame.key.get_pressed() 
if k[K_DOWN]: 
    # Do somthing with your bird 
    if k[K_UP]: 
    # Do somthing with your bird 
+0

我怎樣才能改變時,關鍵是現在按我的鳥的座標?對不起,我不是很擅長這個 – Serial 2013-04-23 23:16:44

+0

@ChristianCareaga然後它更好地做這個教程:http://www.pygame.org/docs/tut/tom/MakeGames.html他們解釋你想要完成的一切。 – 2013-04-23 23:20:48

0

如果你認爲你的播放器爲矩形(這可能適用於所有在你的遊戲你的精靈的),那麼所有你需要做的是設置兩個參數爲您的矩形稱爲self.speedx和自我。迅速。既然你只是尋找上下運動,speedx不會改變,但你會根據按下哪個按鈕來快速調整。以下是我的一款遊戲的代碼片段。它是我的玩家類更新def的一部分。不幸的是,在這個遊戲中,我只是想要橫向(左右)移動,但你會明白。如果您需要任何幫助,請告訴我。下面的代碼:

def update(self): 
    # Ensures default motion is 0 
    self.speedx = 0 
    self.speedy = 0 
    # This checks to see what key's are pressed 
    keystate = pygame.key.get_pressed() 

    # This gives control for movement (Can use WASD or arrow keys) 
    if keystate[pygame.K_a] or keystate[pygame.K_LEFT]: 
     self.speedx = -5 
    if keystate[pygame.K_d] or keystate[pygame.K_RIGHT]: 
     self.speedx = 5 
    if keystate[pygame.K_SPACE]: 
     self.shoot(bullet_img) 

    # Gives movement based on keys that are pressed 
    self.rect.x += self.speedx 
    self.rect.y += self.speedy