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

问题描述 投票:1回答:1

我正在创建一个目标射击游戏,玩家单击鼠标,子弹飞向它。我已经开始使用“ pygame.sprite”来简化事情。我目前设法加载目标和背景,并使目标移动。现在,我要执行子弹功能。理想情况下,我想再次使用“ pygame.sprite”命令,以便可以使用预制的碰撞检测系统。问题是我显然不能坐在这里实例化数百个单击时会产生的项目符号。我已经阅读了有关自动实例化的线程,但是没有一个与我需要将其合并到pygame中的问题类似。除了“ self.rect.x / y”以外,所有项目符号都具有相同的属性,但我以后可以处理。基本上,有没有办法使我可以在执行期间自动创建实例?请注意,“ refresh_window()”将包含所有绘图命令。

#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()
python function oop pygame sprite
1个回答
0
投票

您可以在每次单击鼠标时简单地创建一个新的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() 函数。这将使子弹沿着其轨迹移动。

© www.soinside.com 2019 - 2024. All rights reserved.