2013-01-11 30 views
1

好的,我正在为这个学校的项目工作。我应该制作一个太空侵略者类型的游戏。我完成了使我的船移动和射击。现在是问题所在,当我尝试多火时,它会清除之前被解雇的子弹并且会触发一个新的子弹,而这根本不是一个好站点。我如何才能真正发射多个镜头?我怎样才能让我的船发射多枪?

while (running == 1): 
    screen.fill(white) 
    for event in pygame.event.get(): 
     if (event.type == pygame.QUIT): 
      running = 0 
     elif (event.type == pygame.KEYDOWN): 
      if (event.key == pygame.K_d): 
       dir = "R" 
       move = True 
      elif (event.key == pygame.K_a): 
       dir = "L" 
       move = True 
      elif (event.key == pygame.K_s): 
       dir = "D" 
       move = True 
      elif (event.key == pygame.K_w): 
       dir = "U" 
       move = True 
      elif (event.key == pygame.K_ESCAPE): 
       sys.exit(0) 
      elif (event.key == pygame.K_SPACE): 
       shot=True 
       xbul=xgun + 18 
       ybul=ygun 
      #if key[K_SPACE]: 
       #shot = True 
     if (event.type == pygame.KEYUP): 
      move = False 

    #OBJECT'S MOVEMENTS 
    if ((dir == "R" and xgun<460) and (move == True)): 
     xgun = xgun + 5 
     pygame.event.wait 
    elif ((dir == "L" and xgun>0) and (move == True)): 
     xgun = xgun - 5 
     pygame.event.wait 
    elif ((dir == "D" and ygun<660) and (move == True)): 
     ygun = ygun + 5 
     pygame.event.wait 
    elif ((dir == "U" and ygun>0) and (move == True)): 
     ygun = ygun - 5 

    screen.blit(gun, (xgun,ygun)) 

    #PROJECTILE MOTION 
    #key = pygame.key.get_pressed() 

    if shot == True: 
     ybul = ybul - 10 
     screen.blit(bullet, (xbul, ybul)) 

    if xbul>640: 
     shot=False 

    pygame.display.flip() 
    time.sleep(0.012) 
+0

你必须第一行和第二行之间的缩进错误... – mgilson

+0

您需要保留屏幕项目符号的['list'](http://docs.python.org/3.3/tutorial/introduction.html#lists),并且移动bl它们全部在重画期间等等。 – millimoose

+0

或Sprite的精灵组 – ninMonkey

回答

7

只有一个项目符号变量 - xbul和ybul。如果你想要多个子弹,那么你应该让每一个列表。您可以附加到每个列表以添加新的项目符号,弹出以删除旧的项目符号,并在绘图时迭代列表。

+0

我该怎么做?我知道如何制作基本列表,但不是这种类型。你能给我一段可以做到的代码吗?我真的很新 – Whosyourdaddy

1

您可以为包含x和y坐标以及与子弹相关的其他事物创建子类。然后为每个消防按钮按下,创建并追加一个新实例到列表中。这样你就可以拥有你想要的子弹。
(对于新move功能改变的代码)

class Bullet: 
    def __init__(self,x,y,vx,vy):# you can add other arguments like colour,radius 
     self.x = x 
     self.y = y 
     self.vx = vx # velocity on x axis 
     self.vy = vy # velocity on y axis 
    def move(self): 
     self.x += self.vx 
     self.y += self.vy 

示例代码添加到使用的list和子弹的更新现在的位置(move()是以上):

if shot == True: # if there are bullets on screen (True if new bullet is fired). 
    if new_bullet== True: # if a new bullet is fired 
     bullet_list.append(Bullet(n_x,n_y,0,10)) # n_x and n_y are the co-ords 
               # for the new bullet. 

    for bullet in bullet_list: 
     bullet.move() 
+0

你的意思是什么?*其他论点?我怎样才能为它列出一个清单?我知道该怎么做的列表的类型将不会工作 – Whosyourdaddy

+0

@Whosyourdaddy *其他参数是你想要为子弹提供的其他参数,如颜色等,我忘了提及..(改变)和每次一个子弹被触发,你可以创建一个Bullet类的新实例,并将它追加到列表中。将列表实现代码部分添加到答案中。 – pradyunsg