我正在做一个平台游戏,它有一个水平,有一颗子弹不断朝同一个方向朝玩家发射。但是,我可以让一颗子弹发射,但我不能在列表中添加更多的子弹,以固定的间隔重复发射。任何帮助都将不胜感激!在
class Bullets(pygame.sprite.Sprite):
def __init__(self,x,y):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("Images/bullet.png")
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
def update(self):
self.rect.y += 4
if self.rect.y > 520:
BulletList.empty()
def main3():
moveX = 0
TrapList.empty()
player.rect.x,player.rect.y = 50,0
BulletList.add(bullet)
FiringBullet = pygame.USEREVENT + 1
pygame.time.set_timer(FiringBullet, 3000)
GameExit = False
while GameExit==False:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.move(-player.speed,0)
if event.key == pygame.K_RIGHT:
player.move(player.speed,0)
if event.key == pygame.K_UP:
player.move(0,-10)
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
player.move(player.speed,0)
if event.key == pygame.K_RIGHT:
player.move(-player.speed,0)
if event.key == pygame.K_UP:
player.move(0,0)
if event.type == FiringBullet:
BulletList.add(bullet)
print(BulletList)
screen.fill(BLACK)
level3.update()
if player.rect.x > 350:
for eachbullet in BulletList:
BulletList.draw(screen)
BulletList.update()
playergroup.update()
playergroup.draw(screen)
player.Level3PlatColl(BlockListDirt2)
pygame.display.update()
clock.tick(60)
bullet = Bullets(400,200)
BulletList = pygame.sprite.Group()
main3()
每次你开火的时候你都要创建一个新的子弹头实例。在
现在使用一个实例。将其添加到列表中,列表中有许多元素,但所有元素的位置相同。如果你改变了其中一个的位置,那么你就改变了所有的位置。在
参见可视化PythonTutor.com网站(这是与python代码的长链接)
http://www.pythontutor.com/visualize.html#code=%0Abullets_list+%3D+%5B%5D%0A%0Abullet+%3D+(400,200%29%0A%0Abullets_list.append(bullet%29%0Abullets_list.append(bullet%29%0Abullets_list.append(bullet%29%0A%0A%23+ %0A%0Aother_list+%3D+%5B%5D%0A%0Abullet+%3D+(400,200%29%0Aother_list.append(bullet%29%0A%0Abullet+%3D+(400,200%29%0Aother_list.append(bullet%29%0A%0Abullet+%3D+(400,200%29%0Aother_list.append(bullet%29&mode=display&origin=opt-frontend.js&cumulative=false&heapPrimitives=false&textReferences=false&py=2&rawInputLstJSON=%5B%5D&curInstr=12
顺便说一句:
我宁愿使用名称
Bullet
-它是单子弹位置。在有一个PEP8文档(Python代码的样式指南)建议
bullet_list
,trap_list
,game_exit
FIRING_BULLET
Bullet
,Sprite
,Group
甚至Stackoverflow编辑器也将CamelCase名称作为类名使用,并为它们使用蓝色。在
相关问题 更多 >
编程相关推荐