Skip to main content
 首页 » 编程设计

python之Pygame:子弹粘在屏幕上

2024年12月31日9langtianya

我的问题很简单。如果我快速射击,我发射的子弹会粘在屏幕上。如果我慢慢射击,它们就不会粘住。有人知道这种现象是如何发生的吗?

粘在屏幕上的子弹截图

下面我输入了代码。我遵循这个默认的游戏流程图:

我很好奇问题的根源。是代码还是硬件?

import sys 
import pygame 
from pygame.sprite import Sprite 
from pygame.sprite import Group 
 
# pygame initializing 
pygame.init() 
 
#create the screen surface 
screen =  pygame.display.set_mode((800, 700)) 
 
class Color(): 
    def __init__(self): 
        self.black = (0, 0, 0) 
        self.white = (255, 255, 255) 
        self.red = (255, 0, 0) 
        self.green = (0, 255, 0) 
        self.green_lambda = (10, 255, 150) 
        self.blue = (0, 0, 255) 
 
# set up the colors 
color = Color() # make an instance of this class - this makes some colors available 
 
class Spaceship(Sprite): 
    """ 
    This class represents the Spaceship. 
    It derives from the "Sprite" class in Pygame. 
    """ 
    def __init__(self): 
        """ Constructor""" 
        # Call the parent class (Sprite) constructor 
        super().__init__() 
 
        width = 22 
        height = 32 
        self.screen = screen 
        self.image = pygame.Surface((width, height)) 
        self.image.fill(color.black) 
        self.image.set_colorkey(color.black) 
 
        pygame.draw.polygon(self.image, color.green_lambda, [[10,0],[15,22],[20,30],[10,27],[0,30],[5,22]],2) 
 
        self.rect = self.image.get_rect() 
 
        self.screen_rect = self.screen.get_rect() 
        self.rect.centerx = self.screen_rect.centerx 
        self.rect.bottom = self.screen_rect.bottom 
 
        # As the rect method only take integers we store a 
        # This value is only used at the beginning, i.e. before the game loop starts 
        self.center_x = self.rect.centerx 
        self.center_y = self.rect.centery 
 
 
class Bullet(Sprite): 
    """ 
        This class represents the bullets. 
        It derives from the "Sprite" class in Pygame. 
    """ 
    def __init__(self): 
        # Call the parent class (Sprite) constructor 
        super().__init__() 
 
        self.image = pygame.Surface((8,10)) 
        self.image.fill(color.red) 
        self.image.set_colorkey((color.red)) 
        pygame.draw.ellipse(self.image, color.green, [1, 0, 5, 8], 2) 
 
        self.rect = self.image.get_rect() 
 
        self.rect.centerx = defender.rect.centerx 
        self.rect.bottom = defender.rect.top 
 
    # def function to move the bullets 
    def update_pos(self): 
        self.rect.y -= bullet_speed 
 
 
# create spaceship instance 
defender = Spaceship() 
 
# create group to store sprites in 
all_sprites_list = Group() 
all_sprites_list.add(defender) 
 
ship_speed = 0.5 
bullet_speed = 3 
 
 
def run_game(): 
    m_right = False 
    m_left = False 
    m_up = False 
    m_down = False 
    new_bullet = False 
 
    while True: 
        """This is the user interaction section""" 
        for event in pygame.event.get(): 
            if event.type == pygame.KEYDOWN: 
                if event.key == pygame.K_ESCAPE: 
                    sys.exit() 
                elif event.key == pygame.K_RIGHT: 
                    m_right = True 
                elif event.key == pygame.K_LEFT: 
                    m_left = True 
                elif event.key == pygame.K_UP: 
                    m_up = True 
                elif event.key == pygame.K_DOWN: 
                    m_down = True 
                elif event.key == pygame.K_SPACE: 
                    new_bullet = Bullet() 
                    #print(dir(new_bullet)) 
                    all_sprites_list.add(new_bullet) 
            if event.type == pygame.KEYUP: 
                if event.key == pygame.K_RIGHT: 
                    m_right = False 
                elif event.key == pygame.K_LEFT: 
                    m_left = False 
                elif event.key == pygame.K_UP: 
                    m_up = False 
                elif event.key == pygame.K_DOWN: 
                    m_down = False 
 
        """Below is the game logic, which gets input from the user interaction  
        section and more""" 
        # Movement of spaceship depending on the flag boolean value and on screen width and height 
        if m_right and defender.rect.right < defender.screen_rect.right: 
            defender.center_x += ship_speed 
        if m_left and defender.rect.left > defender.screen_rect.left: 
            defender.center_x -= ship_speed 
        if m_up and defender.rect.top > defender.screen_rect.top: 
            defender.center_y -= ship_speed 
        if m_down and defender.rect.bottom < defender.screen_rect.bottom: 
            defender.center_y += ship_speed 
 
        # The cumulative value (which is a float number) for the spaceships movement 
        # is given to the spaceship rect variable (which can only be integer) now. 
        # This enables fine adjusting of the speed 
        defender.rect.centerx = defender.center_x 
        defender.rect.centery = defender.center_y 
        all_sprites_list.update() 
        screen.fill(color.black) 
        if new_bullet: 
            new_bullet.update_pos() 
            # Below the bullets which leaves the screen display are deleted 
            if new_bullet.rect.bottom < defender.screen_rect.top: 
                all_sprites_list.remove(new_bullet) 
        all_sprites_list.draw(screen) 
        print(all_sprites_list) 
        pygame.display.flip() 
 
run_game() 

请您参考如下方法:

而不是仅仅更新 new_bullet 的位置

    # if new_bullet: 
    #     new_bullet.update_pos() 
    #     # Below the bullets which leaves the screen display are deleted 
    #     if new_bullet.rect.bottom < defender.screen_rect.top: 
    #         all_sprites_list.remove(new_bullet) 

更新所有子弹的位置

    for bullet in all_sprites_list: 
        if isinstance(bullet,Bullet): 
            bullet.update_pos() 
            if bullet.rect.bottom < defender.screen_rect.top: 
                all_sprites_list.remove(bullet) 
                del bullet