2015-10-04 53 views
0

我在做一個小遊戲。我控制一艘船,並拍攝其他小型敵艦。他們會放棄戰利品讓我接受。如果我射擊10艘戰艦,並且他們都扔掉戰利品,那10個戰利品可以追加到列表中,如果我拿起1個戰利品,我怎麼知道從列表中移除哪個戰利品?如果我在列表中追加多個相同的東西,我想擺脫其中的一個,我怎麼知道它是哪一個?

import pygame 
    import time 
    import random 

    pygame.init() 

    white = (255,255,255) 
    black = (0,0,0) 
    red = (255,0,0) 
    green = (0,255,0) 
    blue = (0,0,255) 

    display_width = 800 
    display_height = 600 

    gameDisplay = pygame.display.set_mode((display_width,display_height)) 
    pygame.display.set_caption('Destroyer') 

    clock = pygame.time.Clock() 
    FPS = 60 

    font = pygame.font.SysFont(None, 25) 

    #~~~~~~~~~~~~~~~~~~~Other Functions~~~~~~~~~~~~~~~~~# 

    def devMode(x, y, shipW, shipH, xcross, ycross, face): 
     devText = font.render("X: "+str(x) + "  Y: "+str(y) + "  shipW: "+str(shipW) + "  shipH: "+str(shipH) + "  xcross: "+str(xcross) + "  ycross: "+str(ycross) + "  Face: "+str(face), True, white) 
     gameDisplay.blit(devText,(0,display_height-17)) 

    def money(count): 
     text = font.render("Asteroids: "+str(count),True ,white) 
     gameDisplay.blit(text,(0,0)) 

    def rot_center(image, rect, angle): 
     rot_image = pygame.transform.rotate(image, angle) 
     rot_rect = rot_image.get_rect(center=rect.center) 
     return rot_image,rot_rect 


    #~~~~~~~~~~~~~~~~~~~~~~The Game~~~~~~~~~~~~~~~~~~~~~# 

    def game_loop(): 

     face = 1 

     asteroids = 0 

     fthrust = False 

     xcross = False 
     ycross = False 

     loot_spawnx = random.randrange(0,display_width) 
     loot_spawny = random.randrange(0,display_height) 
     loot_width = 20 
     loot_height = 20 

     x = (display_width * 0.45) 
     y = (display_height * 0.45) 

     x_change = 0 
     y_change = 0 

     shipspeed = 5 

     bulSpeed = 10 

     cooltime = 10000 
     duration = 5000 

     newGame = True 

     lastTime = 0 
     boost = True 
     cooldown = False 
     power = True 

     #Start of bullets 

     #1 

     lGun1 = -999 
     rGun1 = -999 

     yBul1 = 0 
     xBul1 = 0 

     fire1 = 0 

     b1 = False 

     ybul1_change = 0 
     xbul1_change = 0 

     #End of bullets 

     shipW = 69 
     shipH = 88 

     shipImg = pygame.image.load('ship1.png') 
     forwardthrust = pygame.image.load('ship1_forwardthrust.png') 
     reversethrust = pygame.image.load('ship1_reversethrust.png') 
     strafe = pygame.image.load('ship1_strafe.png') 

     gameExit = False 

     while not gameExit: 

      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: 
         #shipImg, newRect = rot_center(shipImg,oldRect,90) 
         #gameDisplay.blit(shipImg, newRect) 
         shipImg = pygame.transform.rotate(shipImg,90) 
         face -= 1 
         face = (face - 1) % 4 
         face += 1 

         if face == 2 or face == 4: 
          shipW = 88 
          shipH = 69 
         else: 
          shipW = 69 
          shipH = 88 

        elif event.key == pygame.K_RIGHT: 
         #shipImg, newRect = rot_center(shipImg,oldRect,-90) 
         #blit(shipImg, newRect) 
         shipImg = pygame.transform.rotate(shipImg,-90) 
         face -= 1 
         face = (face + 1) % 4 
         face += 1 

         if face == 2 or face == 4: 
          shipW = 88 
          shipH = 69 
         else: 
          shipW = 69 
          shipH = 88 

        elif event.key == pygame.K_UP: 
         if face == 1: 
          y_change = -shipspeed 
          x_change = 0 
         elif face == 2: 
          x_change = shipspeed 
          y_change = 0 
         elif face == 3: 
          y_change = shipspeed 
          x_change = 0 
         elif face == 4: 
          x_change = -shipspeed 
          y_change = 0 
        elif event.key == pygame.K_DOWN: 
         if face == 1: 
          y_change = shipspeed 
          x_change = 0 
         elif face == 2: 
          x_change = -shipspeed 
          y_change = 0 
         elif face == 3: 
          y_change = -shipspeed 
          x_change = 0 
         elif face == 4: 
          x_change = shipspeed 
          y_change = 0 
        elif event.key == pygame.K_s: 
         y_change = 0 
         x_change = 0 
        elif event.key == pygame.K_PAGEUP: 
         if face == 1: 
          x_change = -shipspeed 
          y_change = 0 
         elif face == 2: 
          y_change = -shipspeed 
          x_change = 0 
         elif face == 3: 
          x_change = shipspeed 
          y_change = 0 
         elif face == 4: 
          y_change = shipspeed 
          x_change = 0 
        elif event.key == pygame.K_PAGEDOWN: 
         if face == 1: 
          x_change = shipspeed 
          y_change = 0 
         elif face == 2: 
          y_change = shipspeed 
          x_change = 0 
         elif face == 3: 
          x_change = -shipspeed 
          y_change = 0 
         elif face == 4: 
          y_change = -shipspeed 
          x_change = 0 

        elif event.key == pygame.K_d: 
         if power == True: 
          power = False 
          if boost == True: 
           shipspeed = 10 
           start = pygame.time.get_ticks() 
           if now - start >= duration: 
            boost = False 
            shipspeed = 5 
            cooldown = True 
            lastTime = pygame.time.get_ticks() 

        elif event.key == pygame.K_f: 
         #1 
         if b1 == False: 
          fire1 = face 
          if fire1 == 1 or fire1 == 3: 
           xbul1_change = 0 
           xBul1 = 0 
           lGun1 = x + 9 
           rGun1 = x + 59 
           if fire1 == 1: 
            ybul1_change = -bulSpeed 
            yBul1 = y - 10 
           else: 
            ybul1_change = bulSpeed 
            yBul1 = y + 88 
          else: 
           ybul1_change = 0 
           yBul1 = 0 
           lGun1 = y + 9 
           rGun1 = y + 59 
           if fire1 == 2: 
            xbul1_change = bulSpeed 
            xBul1 = x + 88 
           else: 
            xbul1_change = -bulSpeed 
            xBul1 = x - 10 
          b1 = False 

      if y < loot_spawny+loot_height and y > loot_spawny or y + shipH < loot_spawny+loot_height and y + shipH > loot_spawny: 
       ycross = True 

       if x < loot_spawnx +loot_width and x > loot_spawnx or x + shipW > loot_spawnx and x + shipW < loot_spawnx + loot_width: 
        loot_spawnx = random.randrange(0,display_width) 
        loot_spawny = random.randrange(0,display_height) 
        asteroids += 10 
        xcross = True 
       elif x + shipW > loot_spawnx + loot_width and x < loot_spawnx: 
        loot_spawnx = random.randrange(0,display_width) 
        loot_spawny = random.randrange(0,display_height) 
        asteroids += 10 
        xcross = True 
       else: 
        xcross = False 
      else: 
       ycross = False 

      if x > loot_spawnx and x < loot_spawnx + loot_width or x + shipW > loot_spawnx and x + shipW < loot_spawnx + loot_width: 
       xcross = True 

       if y < loot_spawny+loot_height and y > loot_spawny or y + shipH < loot_spawny+loot_height and y + shipH > loot_spawny: 
        loot_spawnx = random.randrange(0,display_width) 
        loot_spawny = random.randrange(0,display_height) 
        asteroids += 10 
        ycross = True 
       elif y + shipH > loot_spawny + loot_height and y < loot_spawny: 
        loot_spawnx = random.randrange(0,display_width) 
        loot_spawny = random.randrange(0,display_height) 
        asteroids += 10 
        ycross = True 
       else: 
        ycross = False 
      else: 
       xcross = False 

      if y + shipH > loot_spawny + loot_height and y < loot_spawny: 
       ycross = True 
       if x + shipW > loot_spawnx + loot_width and x < loot_spawnx: 
        xcross = True 
        loot_spawnx = random.randrange(0,display_width) 
        loot_spawny = random.randrange(0,display_height) 
        asteroids += 10 
       else: 
        xcross = False 
      else: 
       ycorss = False 

      now = pygame.time.get_ticks() 
      if cooldown == True: 
       nowTime = pygame.time.get_ticks() 
       if nowTime - lastTime >= cooltime: 
        lastTime = nowTime 
        cooldown = False 
        boost = True 
        power = True 
        pygame.display.update() 



      gameDisplay.fill(green) 

      y += y_change 
      x += x_change 

      if x <= -5: 
       x += 5 
      elif x + shipW >= display_width: 
       x -= 5 
      elif y <= -5: 
       y += 5 
      elif y + shipH >= display_height: 
       y -= 5 

      #oldRect = shipImg.get_rect(center=(x + (shipW/ 2), y + (shipH/2))) 
      #gameDisplay.blit(shipImg, oldRect) 

      gameDisplay.blit(shipImg, (x, y)) 

    ##  if event.type == pygame.KEYDOWN: 
    ##   if event.key == pygame.K_UP: 
    ##    gameDisplay.blit(forwardthrust, (x + 19, y + shipH)) 
    ##    gameDisplay.blit(forwardthrust, (x + 42, y + shipH)) 

      #1 
      if fire1 == 1 or fire1 == 3: 
       pygame.draw.rect(gameDisplay, red, [lGun1, yBul1, 2, 10]) 
       pygame.draw.rect(gameDisplay, red, [rGun1, yBul1, 2, 10]) 
      elif fire1 == 2 or fire1 == 4: 
       pygame.draw.rect(gameDisplay, red, [xBul1, lGun1, 10, 2]) 
       pygame.draw.rect(gameDisplay, red, [xBul1, rGun1, 10, 2]) 


      #1 
      yBul1 += ybul1_change 
      xBul1 += xbul1_change 

      pygame.draw.rect(gameDisplay, blue, [loot_spawnx, loot_spawny, loot_width, loot_height]) 
      money(asteroids) 
      devMode(x, y, shipW, shipH, xcross, ycross, face) 
      pygame.display.update() 
      clock.tick(FPS) 

    game_loop() 
    pygame.quit() 
    quit() 

如果有人知道更好的方式,而不是追加到列表中,那麼我都是耳朵。

+0

請顯示代碼。請參閱[如何提出問題](http://stackoverflow.com/help/how-to-ask) – Pynchia

+0

在本例中,*爲什麼它會影響哪一個?* – jonrsharpe

+0

@Ruarri以及哪個部分導致問題? – garg10may

回答

1

這裏有無限的可能性,你只需要以某種方式識別你的戰利品。

我希望所有的船都不同,攜帶不同的戰利品。像大,小。

所以我們說

>>> ships = [5, 67, 3, 10] 
>>> loot = [100, 30, 300, 50] 

所以說我的船號爲67的模具。你甚至可以在這裏使用名字'a','b'。 我們將移除這艘船並將其掠奪。

首先讓我們在刪除它之前存儲它的索引。

>>> index = ships.index(67) 

現在讓我們刪除它。

>>> ships.remove(67) 

最後刪除戰利品。以前使用remove方法我們刪除了確切的元素。這裏使用del,我們給出需要刪除的索引。

>>> del loot[index] 

>>> ships 
[5, 3, 10] 
>>> loot 
[100, 300, 50] 

在這裏你也可以增加/減少戰利品,比如說船隻不會死亡,但只會減少一些戰利品。

但理想情況下,您應該製作{shipNo:loot}的字典,這會更容易,訪問時間更短。

+0

代碼在哪裏? nvm,看看上面是否有幫助。 – garg10may

+0

我仍然沒有100%的清單清單,但這似乎是有道理的,但我怎麼會產生多個敵人,而不必複製每個船的代碼塊? – Ruarri

+0

你是什麼意思,「我怎麼會在一個以上的敵人身上產卵」,請讓我更加清楚 – garg10may

相關問題