2016-11-21 2 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

Все, черт возьми. Я слишком долго. Это действительно шумная ошибка коричневого бумажного мешка. Огромное спасибо. Я пойду и похлопаю себя, а потом выпью кофе. – headbanger

Смежные вопросы