2016-11-21 13 views
0

私はPythonで遊んでいますが、(非常に)シンプルな空間侵略ゲームを作成しようとしていますが、弾丸スプライトは描画されていません。私は現時点で同じグラフィックを使用しています。他のすべてのものが動作するとすぐにグラフィックスをあらかじめ確認します。これは私のコードです:Pygameスプライトが描画されていません

# !/usr/bin/python 

import pygame 

bulletDelay = 40 

class Bullet(object): 
    def __init__(self, xpos, ypos, filename): 
     self.image = pygame.image.load(filename) 
     self.rect = self.image.get_rect() 
     self.x = xpos 
     self.y = ypos 

    def draw(self, surface): 
     surface.blit(self.image, (self.x, self.y)) 


class Player(object): 
    def __init__(self, screen): 
     self.image = pygame.image.load("spaceship.bmp")  # load the spaceship image 
     self.rect = self.image.get_rect()      # get the size of the spaceship 
     size = screen.get_rect() 
     self.x = (size.width * 0.5) - (self.rect.width * 0.5) # draw the spaceship in the horizontal middle 
     self.y = size.height - self.rect.height    # draw the spaceship at the bottom 

    def current_position(self): 
     return self.x 

    def draw(self, surface): 
     surface.blit(self.image, (self.x, self.y))   # blit to the player position 


pygame.init() 
screen = pygame.display.set_mode((640, 480)) 
clock = pygame.time.Clock() 
player = Player(screen)          # create the player sprite 
missiles = []             # create missile array 
running = True 
counter = bulletDelay 

while running: # the event loop 
    counter=counter+1 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      running = False 
    key = pygame.key.get_pressed() 
    dist = 1     # distance moved for each key press 
    if key[pygame.K_RIGHT]: # right key 
     player.x += dist 
    elif key[pygame.K_LEFT]: # left key 
     player.x -= dist 
    elif key[pygame.K_SPACE]: # fire key 
     if counter > bulletDelay: 
      missiles.append(Bullet(player.current_position(),1,"spaceship.bmp")) 
      counter=0 

    for m in missiles: 
     if m.y < (screen.get_rect()).height and m.y > 0: 
      m.draw(screen) 
      m.y += 1 
     else: 
      missiles.pop(0) 

    screen.fill((255, 255, 255)) # fill the screen with white 
    player.draw(screen)   # draw the spaceship to the screen 
    pygame.display.update()  # update the screen 
    clock.tick(40) 

私の箇条書きが描かれない理由はありますか?

あなたが手伝ってくれた指が交差しており、事前に感謝します。

答えて

1

弾丸が描かれています。しかし、あなたのコードを書いた方法のために、あなたはそれを見ることはありません!最初にすべての箇条書きを描画し、その直後に画面を白く塗りつぶします。これは非常に速く起こり、見ることができません。これを試してみて、あなたは私が何を意味するかが表示されます:

for m in missiles: 
    if m.y < (screen.get_rect()).height and m.y > 0: 
     m.draw(screen) 
     m.y += 1 
    else: 
     missiles.pop(0) 

# screen.fill((255, 255, 255)) # fill the screen with white 
player.draw(screen)   # draw the spaceship to the screen 
pygame.display.update()  # update the screen 
clock.tick(40) 

一つの解決策は、あなたがミサイルを描画する前にscreen.fillを移動することです。

+0

Aww、damn。私はあまりにも長くなってしまった。それは、茶色の紙袋のエラーの本当に厄介なことです。どうもありがとうございます。私は行って自分を叩いてコーヒーを飲む。 – headbanger

関連する問題