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



我正在创建一个射击游戏,玩家点击鼠标,一颗子弹飞向它。我已经开始使用"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()

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

最新更新