2012-04-27 88 views
1

我需要讓這個代碼中的球隨機移動。我不確定我做錯了什麼。我是編程新手,這可能是嚴重的編碼,所以任何幫助將不勝感激。現在,代碼會在屏幕中間創建一個角色,您可以向上或向下移動,並在屏幕左上角移動一個不移動的球。我希望屏幕上有多個球,最終我想讓球隨機移動,如果他們與角色相撞,就會失去這個簡單的遊戲。在pygame中移動多個球

from pygame import * 
import random 

class Ball(sprite.Sprite): 
    def __init__(self, numballs, balls = []): 
     sprite.Sprite.__init__(self) 
     self.image = image.load('ball.png') 
     self.rect = self.image.get_rect() 
     self.numballs = numballs 
     self.balls = balls 


    def multipleBalls(self): 
     for count in range(self.numballs): 
      self.balls.append(dict) 
      self.balls[count] = {'x': 0, 'y': 0, 'xmove': random.randint(1,2), 'ymove':random.randint(1,2)} 

    def ballMove(self): 
     for count in range(self.numballs): 
      self.balls[count]['x'] = self.balls[count]['x'] + self.balls[count]['xmove'] 
      self.balls[count]['y'] = self.balls[count]['y'] + self.balls[count]['ymove'] 

    def ballsOnScreen(self): 
     for count in range(self.numballs): 
      self.screen.blit(self.image, (self.balls[count]['x'], self.balls[count]['y'])) 


    def ballBarrier(self): 
     for count in range(self.numballs): 
      if self.balls[count]['x'] > 620: 
       self.balls[count]['xmove'] = random.randint(-2, 0) 
      if self.balls[count]['x'] < -10: 
       self.balls[count]['xmove'] = random.randint(0, 2) 
      if self.balls[count]['y'] > 470: 
       self.balls[count]['ymove'] = random.randint(-2, 0) 
      if self.balls[count]['y'] < -10: 
       self.balls[count]['ymove'] = random.randint(0, 2) 

    def manageBall(self): 
     self.multipleBalls() 
     self.ballsOnScreen() 
     self.ballMove() 
     self.ballBarrier() 

class Character(sprite.Sprite): 
    def __init__(self, xy): 
     sprite.Sprite.__init__(self) 
     self.image = image.load('character.png') 
     self.rect = self.image.get_rect() 
     self.rect.centerx, self.rect.centery = xy 
     self.movementspeed = 1 
     self.velocity = 0 


    def down(self): 
     self.velocity += self.movementspeed 

    def up(self): 
     self.velocity -= self.movementspeed 

    def characterMove(self, dy): 
     if self.rect.bottom + dy > 480: 
      self.rect.bottom = 480 
     elif self.rect.top + dy < 0: 
      self.rect.top = 0 
     else: 
      self.rect.y += dy 

    def update(self): 
     self.characterMove(self.velocity) 


class Game(object): 
    def __init__(self): 
     init() 
     key.set_repeat(1, 30) 
     self.screen = display.set_mode((640, 480)) 
     self.clock = time.Clock() 
     display.set_caption('Game') 
     event.set_allowed([QUIT, KEYDOWN, KEYUP]) 
     self.background = Surface((640, 480)) 
     self.background.fill((0,0,0)) 
     self.screen.blit(self.background, (0,0)) 
     display.flip() 
     self.sprites = sprite.RenderUpdates() 
     self.character = Character((320, 240)) 
     self.sprites.add(self.character) 
     self.ball = Ball(5) 
     self.sprites.add(self.ball) 

    def run(self): 
     running = True 
     while running == True: 
      self.clock.tick(60) 
      running = self.handleEvents() 
      display.set_caption('game %d fps' % self.clock.get_fps()) 
      for sprite in self.sprites: 
       sprite.update() 
      self.sprites.clear(self.screen, self.background) 
      dirty = self.sprites.draw(self.screen) 
      display.update(dirty) 
      self.ball.manageBall() 

    def handleEvents(self): 
     for e in event.get(): 
      if e.type == QUIT: 
       return False 

      elif e.type == KEYDOWN: 
       if e.key == K_ESCAPE: 
        return False 
       if e.key == K_UP: 
        self.character.up() 
       if e.key == K_DOWN: 
        self.character.down() 

     return True 



def main(): 
    game = Game() 
    game.run() 
main() 
+3

你應該解釋你的代碼實際上做了什麼,以及如何比較你想要做的。 – ajwood 2012-04-27 21:04:48

回答

2

我從來沒有使用過Sprite類,所以這是一個很好的練習。 :)

我修正了你的代碼,只是將圖像路徑改回到你的圖標位置,你應該很好去。如果您運行的是較早版本的Python,則打印語句可能需要重新格式化。

我拿出被迫調用球和字符類雪碧初始化方法,它不會讓我將它們添加到game.sprites渲染組。不知道爲什麼。

我正在運行Python 3.2.2和Pygame 1.9.2pre所以YMMV。 :)

控制:

UP/DOWN =移動你的傢伙。

R =增加一個新的隨機球。

from pygame import * 
import random 

SCREEN_WIDTH = 640 
SCREEN_HEIGHT = 480 

FPS = 60 

class Ball(sprite.Sprite): 
    def __init__(self, xy = (0,0), xm = 0, ym = 0): 
     sprite.Sprite.__init__(self) 
     self.img_load('evil_balloon_32x32.png') 
     self.rect.centerx, self.rect.centery = xy 

     self.xmove = xm 
     self.ymove = ym 

    def update(self): 
     self.move() 
     self.ballBarrier() 

    def move(self): 
     self.rect.x += self.xmove 
     self.rect.y += self.ymove 

    def img_load(self, filename): 
     self.image = image.load(filename) 
     self.rect = self.image.get_rect() 

    def ballBarrier(self): 
     """ 
     Checks to make sure ball is within bounds, adjusts movement speed if it's not 
     """ 
     if self.rect.right > SCREEN_WIDTH: 
      self.xmove = random.randint(-2, 0) 
     if self.rect.left < 0: 
      self.xmove = random.randint(0, 2) 
     if self.rect.bottom > SCREEN_HEIGHT: 
      self.ymove = random.randint(-2, 0) 
     if self.rect.top < 0: 
      self.ymove = random.randint(0, 2) 

class ball_manager(): 
    def __init__(self, numballs = 5, balls = []):  
     self.blist = balls 

     if numballs > 0: 
      self.multipleBalls(numballs) # moved this here so balls get init'd only once 

    def update(self): 
     """ 
     Update position of all balls 
     """ 
     for ball in self.blist: 
      self.ballMove(ball) 

    def add_ball(self, xy = (0,0), xm = 0, ym = 0): 
     self.blist.append(Ball(xy, xm, ym)) # appends a random ball 

    def multipleBalls(self, numballs): 
     for i in range(numballs): 
      self.add_ball((random.randint(0, SCREEN_WIDTH), 
          random.randint(0, SCREEN_HEIGHT)), 
          random.randint(-2,2), 
          random.randint(-2,2)) 

class Character(sprite.Sprite): 
    def __init__(self, xy): 
     sprite.Sprite.__init__(self) 
     self.img_load() 

     self.rect.centerx, self.rect.centery = xy 

     self.movementspeed = 1 
     self.velocity = 0 

    def down(self): 
     self.velocity += self.movementspeed 

    def up(self): 
     self.velocity -= self.movementspeed 

    def characterMove(self, dy): 
     if self.rect.bottom + dy > SCREEN_HEIGHT: 
      self.rect.bottom = SCREEN_HEIGHT 
      self.velocity = 0 
     elif self.rect.top + dy < 0: 
      self.rect.top = 0 
      self.velocity = 0 
     else: 
      self.rect.y += dy 

    def update(self): 
     self.characterMove(self.velocity) 

    def img_load(self): 
     self.image = image.load("scary_clown_32x32.png") 
     self.rect = self.image.get_rect() 

class Game(object): 
    def __init__(self): 
     init() 
     key.set_repeat(1, 30) 
     self.screen = display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) 
     self.clock = time.Clock() 
     display.set_caption('Game') 

     event.set_allowed([QUIT, KEYDOWN, KEYUP]) 
     self.background = Surface((SCREEN_WIDTH, SCREEN_HEIGHT)) 
     self.background.fill((0,0,0)) 
     self.screen.blit(self.background, (0,0)) 
     display.flip() 

     self.sprites = sprite.RenderUpdates() 

     self.character = Character((SCREEN_WIDTH/2, SCREEN_HEIGHT/2)) 

     self.sprites.add(self.character) 

     self.balls = ball_manager(5) 

     for ball in self.balls.blist: 
      self.sprites.add(ball) 

    def run(self): 
     running = True 
     while running == True: 
      self.clock.tick(FPS) 
      running = self.handleEvents() 
      display.set_caption('game %d fps' % self.clock.get_fps()) 

      self.sprites.clear(self.screen, self.background) 

      for sprite in self.sprites: 
       sprite.update() 

      dirty = self.sprites.draw(self.screen) 
      display.update(dirty) 


    def handleEvents(self): 
     for e in event.get(): 
      if e.type == QUIT: 
       return False 

      elif e.type == KEYDOWN: 
       if e.key == K_ESCAPE: 
        return False 
       if e.key == K_UP: 
        self.character.up() 
       if e.key == K_DOWN: 
        self.character.down() 
       if e.key == K_r: 
        self.sprites.add(Ball((random.randint(0, SCREEN_WIDTH), 
          random.randint(0, SCREEN_HEIGHT)), 
          random.randint(-2,2), 
          random.randint(-2,2))) 


     return True 

def main(): 
    game = Game() 
    game.run() 
    quit() 

main() 
sys.exit() 
+1

非常感謝。這工作完美,它幫助我更好地理解編碼。 – Igotagood1 2012-04-28 18:47:05

+0

美好時光!很高興我可以幫助,雪碧類非常酷。 – Technohazard 2012-04-28 21:32:24