运行 通过给定圆周内的点,如果有东西就把它移除

Run through points within a given circumference, and if something is there remove it

我正在开发一个相当简单的游戏,但我正在努力实现一个功能,当玩家拥有武器并选择攻击时,我想知道是否有任何玩家在武器的攻击距离内(在该距离的周长),如果我发现任何其他玩家,那么我想消灭他们。

我在“game.py -> processPlayerInput”下工作的space,我目前正在尝试解决“手榴弹”问题,但它并不优雅,并且对于我更大的“枪支”射程来说,实现起来并不容易。 Game.py如下:

from treasure import Treasure
from player import Player
from randomNum import Random
import sys
from randomNum import Random
rand= Random()
if len(sys.argv) > 1:
  rand.setSeed(int(sys.argv[1])) 
from weapon import Weapon

class Game:
    # the constructor (initialize all game variables)
    def __init__(self, w, h, numPlayers):  
        self.gameBoardWidth = w;
        self.gameBoardHeight = h;
        self.listOfPlayers = []
        self.listOfTreasures = []
        self.listOfWeapons = []
        
        for player in range(numPlayers):
          player = Player(rand.randrange(w), rand.randrange(h), str(player + 1))
          self.listOfPlayers.append(player)   
        
        t1 = Treasure("silver","S", 20, rand.randrange(w), rand.randrange(h))
        self.listOfTreasures.append(t1)
        t2 = Treasure("gold","G", 25, rand.randrange(w), rand.randrange(h))
        self.listOfTreasures.append(t2)
        t3 = Treasure("platinum", "P", 50, rand.randrange(w), rand.randrange(h))
        self.listOfTreasures.append(t3)
        t4 = Treasure("diamond", "D", 40, rand.randrange(w), rand.randrange(h))
        self.listOfTreasures.append(t4)
        t5 = Treasure("emerald", "E", 35, rand.randrange(w), rand.randrange(h))
        self.listOfTreasures.append(t5)
        w1 = Weapon("gun", "/", 7, rand.randrange(w), rand.randrange(h))
        self.listOfWeapons.append(w1)
        w2 = Weapon("grenade", "o", 4, rand.randrange(w), rand.randrange(h))
        self.listOfWeapons.append(w2)
    
    def play(self):
        self.printInstructions()
      
        self.drawUpdatedGameBoard()
    
        # MAIN GAME LOOP to ask players what they want to do
        currentPlayerNum = 0
        while (len(self.listOfTreasures) >= 1) and (len(self.listOfPlayers) > 1):
            
            # get the player object for the player whose turn it is
            currentPlayer = self.listOfPlayers[currentPlayerNum];

            # ask the player what they would like to do
            choice = input("Player " + currentPlayer.gameBoardSymbol + ", do you want to (m)ove or (r)est? ")
            self.processPlayerInput(currentPlayer, choice)
            
            # show the updated player information and game board
            self.printUpdatedPlayerInformation();
            self.drawUpdatedGameBoard()
            
            # update whose turn it is
            currentPlayerNum += 1
            if currentPlayerNum >= len(self.listOfPlayers):
                currentPlayerNum = 0
        
        var = ""
        var_score = 0
        for i in self.listOfPlayers:
          if i.getPoints() > var_score:
            var = i.gameBoardSymbol
            var_score = i.getPoints()
        print("Player " + var + " wins!")
    
    def processPlayerInput(self, plyr, action) :
        if action == "m":  # move
            direction = input("Which direction (r, l, u, or d)? ")
            distance = int(input("How Far? "))
            
            if plyr.energy >= distance / 2:
              plyr.move(direction,distance)
              plyr.energy -= distance / 2
            
            else:
              plyr.move(direction, (plyr.energy * 2))
              plyr.energy = 0.0
            
            # check to see if player moved to the location of another game item
            for treasure in self.listOfTreasures:
                if plyr.x == treasure.x and plyr.y == treasure.y:
                    plyr.collectTreasure(treasure)
                    print("You collected",treasure.name,"worth",treasure.pointValue,"points!")
                    self.listOfTreasures.remove(treasure)  # remove the treasure from the list of available treasures
                    break
                  
            for weapon in self.listOfWeapons:
              if plyr.x == weapon.x and plyr.y == weapon.y:
                plyr.collectWeapon(weapon)
                print("You aquired the" + str(weapon.name) + "!")
                self.listOfWeapons.remove(weapon)
                break            
                  
            for player in self.listOfPlayers:
              if plyr.x == player.x and plyr.y == player.y and plyr.gameBoardSymbol != player.gameBoardSymbol:
                self.listOfPlayers.remove(player)
                print("You eliminated player",player.gameBoardSymbol,"from the game!")
                break            
                
        elif action == "r":
            plyr.energy += 4
            
        elif action == "a": #the player wants to use a weapon to attack other players
          #if the player has a gun, use that, otherwise use a grenade, if player has neither, do nothing. check if players are within the weapon striking distance circumference in any direction and eliminate them
          if plyr.hasWeapon("gun"):
            for player in self.listOfPlayers:
              for weaponRange in range(-7,7):
                if (player.x == plyr.x and player.y == plyr.y + weaponRange) or (player.y == plyr.y and player.x == plyr.x + weaponRange):
                  self.listOfPlayers.remove(player)
              if (player.x == (plyr.x) or player.x == (plyr.x+1) or player.x == (plyr.x-1)) and (player.y == (plyr.y + 7) or player.y == (plyr.y - 7)):
                self.listOfPlayers.remove(player)
              elif (player.y == (plyr.y) or player.y == (plyr.y+1) or player.y == (plyr.y-1)) and (player.x == (plyr.x + 7) or player.x == (plyr.x - 7)):
                self.listOfPlayers.remove(player)
          elif plyr.hasWeapon("grenade"):
            for player in self.listOfPlayers:
              for weaponRange in range(-3,3):
                if (player.x == plyr.x and player.y == plyr.y + weaponRange) or (player.y == plyr.y and player.x == plyr.x + weaponRange):
                  self.listOfPlayers.remove(player)
              if (player.x == (plyr.x) or player.x == (plyr.x+1) or player.x == (plyr.x-1)) and (player.y == (plyr.y + 4) or player.y == (plyr.y - 4)):
                self.listOfPlayers.remove(player)
              elif (player.y == (plyr.y) or player.y == (plyr.y+1) or player.y == (plyr.y-1)) and (player.x == (plyr.x + 4) or player.x == (plyr.x - 4)):
                self.listOfPlayers.remove(player)
        else :
            print("Sorry, that is not a valid choice")
    
    
    def printUpdatedPlayerInformation(self):
        for p in self.listOfPlayers:
            print("Player " + p.gameBoardSymbol + " has " + str(p.getPoints()) + " points and has " + str(p.energy) + " energy")
      
    
    def drawUpdatedGameBoard(self) :     
        # loop through each game board space and either print the gameboard symbol
        # for what is located there or print a dot to represent nothing is there
        for y in range(0,self.gameBoardHeight):
            for x in range(0,self.gameBoardWidth):
                symbolToPrint = "."
                for treasure in self.listOfTreasures:
                  if treasure.x == x and treasure.y == y:
                    symbolToPrint = treasure.gameBoardSymbol
                for player in self.listOfPlayers:
                  if player.x == x and player.y == y:
                    symbolToPrint = player.gameBoardSymbol
                for weapon in self.listOfWeapons:
                  if weapon.x == x and weapon.y == y:
                    symbolToPrint = weapon.gameBoardSymbol
                print(symbolToPrint,end="")
            print() # go to next row
        print()
  
    def printInstructions(self) :
        print("Players move around the game board collecting treasures worth points")
        print("The game ends when all treasures have been collected or only 1 player is left")
        print("Here are the point values of all of the treasures:")
        for treasure in self.listOfTreasures :
            print( "   " + treasure.name + "(" + treasure.gameBoardSymbol + ") " + str(treasure.pointValue) )
        print()

如果您想要另一个 类,请告诉我,我也可以附在此处。

看来你只需要比较距离(这里是平方欧氏距离):

for player in self.listOfPlayers:
    if (player.x-plyr.x)**2+(player.y-plyr.y)**2 <= grenaderadius**2:
        self.listOfPlayers.remove(player)

另请注意,在迭代期间从列表中删除玩家可能不是一个好主意(可能导致错误)。