这段代码有什么问题?我似乎无法从火箭中射出子弹

时间:2017-03-31 22:56:03

标签: python pygame

基本上火箭上下移动,当火箭到达顶部或底部时停止并且识别出空格键被按下。然而,子弹不会从船上射击。我是python和pygame的新手。无论如何这里是代码,谢谢你的帮助!:

import pygame
import sys
from pygame.locals import*

bg_color = (0, 191, 255)
d_w = 1200
d_h= 800
class Rocket(object):
    def __init__(self):
        self.image = pygame.image.load('Rocket.bmp')
        self.x = 0
        self.y = d_h/2

    def handle_keys(self):
        key = pygame.key.get_pressed()
        dist = 6.5
        if key[pygame.K_UP]:
            if self.y > 0:
                self.y -= dist
        elif key[pygame.K_DOWN]:
            if self.y < 775:
                self.y += dist
        elif key[pygame.K_SPACE]:
            Bullet.update(Bullet)
    def draw(self, surface):
        surface.blit(self.image, (self.x, self.y))
class Bullet(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()

        self.image = pygame.Surface([2, 5])
        self.image.fill(0, 0, 0)

        self.rect = self.image.get_rect()

    def update(self):
        """ Move the bullet. """
        self.image = pygame.Surface([2, 5])
        self.image.fill((0, 0, 0))
        self.rect = self.image.get_rect()
        self.rect.x += 3
pygame.init()
screen = pygame.display.set_mode((d_w, d_h))
pygame.display.set_caption("Game Character")
Mario = pygame.image.load('Mario_Sprite.bmp')
rocket = Rocket()
clock = pygame.time.Clock()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
    rocket.handle_keys()
    screen.fill(bg_color)
    rocket.draw(screen)
    pygame.display.update()
    clock.tick(72)

1 个答案:

答案 0 :(得分:0)

Bullet.update(Bullet)

您正在静态呼叫update。您需要创建该类的实例,然后更新该实例,如:

bullet = Bullet()
...
bullet.update()