我可以通过类的自动生成实例创建bullet系统吗?(PYGAME)

2024-06-01 07:31:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建一个目标射击游戏,玩家点击鼠标,一颗子弹飞向它。我已经开始使用“pygame.sprite”让事情变得更简单。目前,我已成功加载目标和背景,并使目标移动。现在我想让子弹发挥作用。理想情况下,我想再次使用“pygame.sprite”命令,以便使用预先制作的碰撞检测系统。问题是,我显然不能坐在这里实例化数百个子弹,这些子弹会在点击后产生。我读过关于自动实例化的文章,但没有一篇与我的问题类似,我需要将它合并到pygame中。除了“self.rect.x/y”之外,所有项目符号都将共享相同的属性,但我可以稍后处理。基本上,是否有一种方法可以使实例在执行期间自动创建?请注意,“刷新窗口()”将包含所有绘图命令

#Using pygame.sprite to make a class for the bullet sprites.
bullet_img = pygame.image.load('bullet.png')

class Bullet(pygame.sprite.Sprite):
  def __init__(self, width, height):
    pygame.sprite.Sprite.__init__(self, bullet_sprites)
    self.image = pygame.Surface([width, height])
    self.image = bullet_img
    self.rect = self.image.get_rect()
    self.rect.center = (400,400)

bullet_sprites = pygame.sprite.Group()

#Creating a function which will deal with redrawing all sprites and updating the screen.
def refresh_window():
  window.blit(bgr, (0,0))
  player_sprites.draw(window)
  target_sprites.draw(window)
  bullet_sprites.draw(window)
  pygame.display.update()

Tags: the实例rectimage命令self目标window
1条回答
网友
1楼 · 发布于 2024-06-01 07:31:02

您只需在每次单击鼠标时创建一个新的Bullet。然后将其添加到您的组中。也许您只是在问题的代码中遗漏了这一部分,但是移动的精灵应该定义一个update()函数来移动它们自己(或者它们所做的任何其他事情)

bullet_img = pygame.image.load('bullet.png')

class Bullet( pygame.sprite.Sprite ):
    def __init__( self, from_x, from_y, to_x, to_y, speed=1.5 ):
        pygame.sprite.Sprite.__init__( self )
        global bullet_img
        self.image       = bullet_img
        self.rect        = self.image.get_rect()
        self.rect.center = ( from_x, from_y )
        self.speed       = speed

        # Calculate how far we should move in X and Y each update.
        # Basically this is just a pair of numbers that get added
        # to the co-ordinate each update to move the bullet towards
        # its destination.
        from_point  = pygame.math.Vector2( ( from_x, from_y ) )
        to_point    = pygame.math.Vector2( ( to_x, to_y ) )
        distance    = from_point.distance_to( to_point )     # 2D line length
        self.dir_x  = ( to_x - from_x ) / distance           # normalised direction in x
        self.dir_y  = ( to_y - from_y ) / distance           # normalised direction in y
        # Store the position as a float for slow-speed and small angles
        # Otherwise sub-pixel updates can be lost
        self.position = ( float( from_x ), float( from_y ) )

    def update( self ):
        # How far do we move this update?
        # TODO: maybe change to use millisecond timings instead of frame-speed
        x_movement = self.speed * self.dir_x
        y_movement = self.speed * self.dir_y
        self.position[0] += x_movement
        self.position[1] += y_movement
        # convert back to integer co-ordinates for the rect
        self.rect.center = ( int( self.position[0] ), int( self.position[1] ) )
        ### TODO: when ( offscreen ): self.kill()

在主循环中,每当玩家发射新子弹时,创建一个对象,并将其添加到精灵组

# ...

bullet_sprites = pygame.sprite.Group()

# ...

### MAIN Loop
done = False
while not done:
    for event in pygame.event.get():
        if ( event.type == pygame.QUIT ):
            done = True
        elif ( event.type == pygame.MOUSEBUTTONUP ):
             # Bullet from TURRET in the direction of the click
            start_x = WINDOW_WIDTH//2
            start_y = int( WINDOW_HEIGHT * 0.9 )  # where is this?
            end_pos = pygame.mouse.get_pos()
            to_x    = end_pos[0]
            to_y    = end_pos[1]
            # Create a new Bullet every mouse click!
            bullet_sprites.add( Bullet( start_x, start_y, to_x, to_y ) )

同样在主循环中,不要忘了打电话

bullet_sprites.update()

它调用组中每个精灵的update()函数。这将使子弹沿着其轨迹移动

相关问题 更多 >