2017-06-05 54 views
1

我正在學習python和pygame,並且我想知道爲什麼子彈不會顯示,我很難說可能是screen.update_screen()會干擾但不是,情況並非如此,我需要幫助來了解Bullets如何在python中工作,因爲我的方法顯然不工作,我在其他帖子中看到了很多方法,並且他們使用有限的子彈數量來拍攝(不知道爲什麼,在我的情況下,我想要無限的子彈),所以我應該添加什麼來查看子彈,我知道我需要添加一個去除器來在顯示屏上顯示「運動」,但是我不知道如何幫助。瞭解並製作無窮子彈以在Pygame中拍攝

# -*- coding: utf-8 -*- 
import pygame 

class Screen(pygame.sprite.Sprite): 
    def __init__(self): 
     pygame.sprite.Sprite.__init__(self) 
     pygame.display.set_caption("Space Game") 
     self.screen_width = 800 
     self.screen_heigh = 600 
     self.picture = pygame.image.load("screen.png") 
     self.screen = pygame.display.set_mode((self.screen_width, self.screen_heigh)) 

    def update_screen(self): 
     self.screen.blit(self.picture, (0, 0)) 

    def update_obj(self, object): 
     self.screen.blit(object.picture, object.rect) 

    def update_shoot(self, object): 
     for y in range(object.rect.centery, 600, 10): 
      self.screen.blit(object.picture, (object.rect.centerx, object.rect.centery + y)) 

class Ship(pygame.sprite.Sprite): 
    def __init__(self): 
     pygame.sprite.Sprite.__init__(self) 
     self.picture = pygame.image.load("ship.png") 
     self.rect = self.picture.get_rect() 
     self.rect.centerx = 400 
     self.rect.centery = 500 

class Bullet(pygame.sprite.Sprite): 
    def __init__(self, object): 
     pygame.sprite.Sprite.__init__(self) 
     self.picture = pygame.image.load("shoot.png") 
     self.rect = self.picture.get_rect() 
     self.rect.centerx = object.rect.centerx 
     self.rect.centery = (object.rect.centery + 25) 

def main(): 
    pygame.init() 
    done = False 
    clock = pygame.time.Clock() 
    screen = Screen() 
    ship = Ship() 
    bullet = Bullet(ship) 

    while not done: 
     keys = pygame.key.get_pressed() 

     if keys[pygame.K_LEFT]: 
      ship.rect.centerx -= 5 
     if keys[pygame.K_RIGHT]: 
      ship.rect.centerx += 5 
     if keys[pygame.K_UP]: 
      ship.rect.centery -= 5 
     if keys[pygame.K_DOWN]: 
      ship.rect.centery += 5 

     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       done = True 
      elif event.type == pygame.KEYDOWN: 
       if event.key == pygame.K_ESCAPE: 
        done = True 
       if event.key == pygame.K_SPACE: 
        print ("shoot") 
        #for some weird reason the bullet doesn't display 
        screen.update_shoot(bullet) 

     screen.update_screen() 
     screen.update_obj(ship) 
     pygame.display.update() 
     clock.tick(10) 

    pygame.quit() 

if __name__ == "__main__": 
    main() 

回答

1

要拍攝子彈你平時創建Bullet類的實例,並將它們添加到列表,pygame.sprite.Group或其他容器中。然後,你遍歷這個容器,並呼籲在其位置改變子彈的update方法。要將精靈/對象的圖像複製到容器上,然後將圖像粘貼到屏幕上。通過精靈組,你可以調用sprite_group.update()sprite_group.draw(screen)而不是迭代自己。 BTW,Pygame的精靈必須有一個self.image屬性不是self.picture爲了與精靈羣體(看看Program Arcade Games瞭解更多信息)工作。

我開始修改你的例子中的一些東西,向你展示如何使用精靈組,但最終將整個Screen類改爲Game類(我將來推薦使用)。

import sys 
import pygame 


class Game: 

    def __init__(self): 
     self.done = False 
     self.screen_width = 800 
     self.screen_height = 600 
     self.image = pygame.Surface((800, 600)) 
     self.image.fill((30, 40, 50)) 
     self.screen = pygame.display.set_mode(
      (self.screen_width, self.screen_height)) 

     # all_sprites is used to update and draw all sprites together. 
     self.all_sprites = pygame.sprite.Group() 
     # You'll probably need a separate bullet_group 
     # later for collision detection with enemies. 
     self.bullet_group = pygame.sprite.Group() 

     self.ship = Ship() 
     self.all_sprites.add(self.ship) 

     bullet = Bullet(self.ship) 
     self.bullet_group.add(bullet) 
     self.all_sprites.add(bullet) 

    def handle_events(self): 
     keys = pygame.key.get_pressed() 

     if keys[pygame.K_LEFT]: 
      self.ship.rect.centerx -= 5 
     if keys[pygame.K_RIGHT]: 
      self.ship.rect.centerx += 5 
     if keys[pygame.K_UP]: 
      self.ship.rect.centery -= 5 
     if keys[pygame.K_DOWN]: 
      self.ship.rect.centery += 5 

     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       self.done = True 
      elif event.type == pygame.KEYDOWN: 
       if event.key == pygame.K_ESCAPE: 
        self.done = True 
       if event.key == pygame.K_SPACE: 
        bullet = Bullet(self.ship) 
        self.bullet_group.add(bullet) 
        self.all_sprites.add(bullet) 

    def update(self): 
     # Calls `update` methods of all contained sprites. 
     self.all_sprites.update() 

    def draw(self): 
     self.screen.blit(self.image, (0, 0)) 
     self.all_sprites.draw(self.screen) # Draw the contained sprites. 
     pygame.display.update() 


class Ship(pygame.sprite.Sprite): 

    def __init__(self): 
     pygame.sprite.Sprite.__init__(self) 
     self.image = pygame.Surface((20, 30)) 
     self.image.fill((50, 170, 230)) 
     # A nicer way to set the start pos with `get_rect`. 
     self.rect = self.image.get_rect(center=(400, 500)) 


class Bullet(pygame.sprite.Sprite): 

    def __init__(self, ship): 
     pygame.sprite.Sprite.__init__(self) 
     self.image = pygame.Surface((7, 7)) 
     self.image.fill((230, 140, 30)) 
     self.rect = self.image.get_rect() 
     self.rect.centerx = ship.rect.centerx 
     self.rect.centery = ship.rect.centery - 25 

    def update(self): 
     self.rect.y -= 5 # Move up 5 pixels per frame. 


def main(): 
    pygame.init() 
    pygame.display.set_caption('Space Game') 
    clock = pygame.time.Clock() 
    game = Game() 

    while not game.done: 
     game.handle_events() 
     game.update() 
     game.draw() 
     clock.tick(30) 


if __name__ == '__main__': 
    main() 
    pygame.quit() 
    sys.exit() 
+1

老兄你是一個生命的救星 –

+0

是我的榮幸!您還需要添加代碼,當他們離開屏幕移除子彈,否則你會耗盡內存...最終。 ;)只是檢查,如果他們的'rect.y <0',然後調用'self.kill()'殺精靈。 – skrx