2013-04-23 2 views
0

나는 파이 게임에 익숙하지 않으며 캐릭터로 새를 사용하는 사이드 스크롤링 게임을 만들고 있지만 화면에서 위아래로 움직이기 위해 노력하고 있지만 그림을 그릴 수는 없다. 어떻게. 당신이 버튼을 누를 경우사이드 스크롤링 게임

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

당신이 당신의 질문에 대해 더 명확 할 수있다, 당신이 요법은 –

답변

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

"실시간"때문에 루프에 넣으십시오. 당신이 당신의 사각형이라고 self.speedx과 자기에 대한 다음 당신이 할 일은 두 개의 매개 변수를 설정하는 사각형 같은 플레이어 (이 게임에서 스프라이트 모두에 적용 할 수있는) 생각하면

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

+0

@ChristianCareaga이 튜토리얼을 수행하는 것이 좋습니다. http://www.pygame.org/docs/tut/tom/MakeGames.html 그들은 당신이 성취하고자하는 모든 것을 설명합니다. –

0

. 빠른. 당신은 단지 위아래 운동만을 찾고 있기 때문에 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 
관련 문제