2017-09-14 62 views
2

我想創建一個模擬。 (不知道它會在哪裏頭)PyGame模擬座標錯誤

我創建了一個Human類和HumanRace類。 HumanRace類包含列表中的每個Human類對象。 目前我每個種族使用2個HumanRaces和10個人類。

人類用一個圓圈表示,它的顏色被分配給HumanRace。此外,還會有一圈代表這場比賽的出生地區,儘管這背後的數學有點馬虎。

人類在其種族的產卵區內產卵。這就是問題出現的地方。如果我只提供一場比賽,一切都可以,但是如果我也提供第二場比賽,那麼將會用不同的顏色重新繪製這些位置,即使我加入比賽的人類具有不同的位置。所以我想我的渲染定義是不正確的。 Main.py

import sys, pygame 
from HumanRace import * 
from random import randint 

BLACK = 0, 0, 0 
WHITE = 255, 255, 255 
GREEN = 124, 252, 0 
RED = 255, 0, 0 
PURPLE = 255, 23, 240 
BLUE = 0, 68, 255 
YELLOW = 255, 255, 0 

humanRaces = [] 

def generateHuman(race): 
    spawnpos = (race.getSpawnX(), race.getSpawnY()) 
    rsize = race.getSize() 
    rHP = randint(10, 100) 
    rATK = randint(20, 100) 
    rAGIL = randint(20, 50) 
    x = randint(spawnpos[0] - rsize, spawnpos[0] + rsize) 
    y = randint(spawnpos[1] - rsize, spawnpos[1] + rsize) 
    print(x, y, rHP) 
    return Human(x, y, rHP, rATK, rAGIL) 

def generateHumans(race, amount): 
    for i in range(0, amount): 
     newHuman = generateHuman(race) 
     race.addHuman(newHuman) 


barbaren = HumanRace("Barbar", 300, 320) 
barbaren.setColor(GREEN) 
barbaren.setSpawnColor(PURPLE) 
generateHumans(barbaren, 4) 

chinesen = HumanRace("Chinese", 100, 100) 
chinesen.setColor(YELLOW) 
chinesen.setSpawnColor(BLUE) 
generateHumans(chinesen, 4) 



humanRaces.append(barbaren) 
humanRaces.append(chinesen) 


pygame.init() 
screen = pygame.display.set_mode((640, 480)) 
pygame.display.set_caption("Fighting Era") 

clock = pygame.time.Clock() 


def renderHumanRaceSpawn(): 
    raceNumber = len(humanRaces) 
    for i in range(0, raceNumber): 
     currRace = humanRaces[i] 
     scolor = currRace.getSpawnColor() 
     spawnX = currRace.getSpawnX() 
     spawnY = currRace.getSpawnY() 
     radius = currRace.getSize() 
     pygame.draw.circle(screen, scolor, (spawnX, spawnY), radius * 2,  0) 

def renderHumanRace(): 
    for i in range(0, 2): 
     currRace = humanRaces[i] 
     color = currRace.getColor() 
     for x in range(0, currRace.getSize()): 
      currHuman = currRace.getAt(x) 
      posX = currHuman.getPosX() 
      posY = currHuman.getPosY() 
      pygame.draw.circle(screen, color, (posX, posY), 3, 0) 

while 1: 
    clock.tick(246) 

    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      sys.exit() 

    screen.fill(BLACK) 

    renderHumanRaceSpawn() 
    renderHumanRace() 
    pygame.display.flip() 

人力和HumanRace類

class Human: 
    def __init__(self, posX, posY, HP, ATK, AGIL): 
     self.posX = posX 
     self.posY = posY 
     self.HP = HP 
     self.ATK = ATK 
     self.AGIL = AGIL 

    def getIndex(self): 
     return self.index 

    def getPosX(self): 
     return self.posX 

    def getPosY(self): 
     return self.posY 

    def setPosX(self, posX): 
     self.posX = posX 

    def setPosY(self, posY): 
     self.posY = posY 

from Human import * 
class HumanRace: 

    size = 0 
    humans = [] 
    spawn = (0, 0) 
    color = 0, 0, 0 
    spawnColor = 1, 1, 1 

    def __init__(self, name, spawnX, spawnY): 
     self.name = name 
     self.spawnX = spawnX 
     self.spawnY = spawnY 

    def getName(self): 
     return self.name 

    def getSpawnX(self): 
     return self.spawnX 

    def getColor(self): 
     return self.color 

    def setColor(self, color): 
     self.color = color 

    def getSpawnColor(self): 
     return self.spawnColor 

    def setSpawnColor(self, color): 
     self.spawnColor = color 

    def getSpawnY(self): 
     return self.spawnY 

    def getSize(self): 
     return self.size 

    def addHuman(self, human): 
     global size 
     self.humans.append(human) 
     self.size += 1 

    def getAt(self, index): 
     return self.humans[index] 

    def removeAt(self, index): 
     global size 
     self.humans.delete(index) 
     self.size -= 1 

Picture of Window

回答

2

humans列表中HumanRace類的內部是一個類變量。 HumanRace的所有實例將共享相同的humans列表。您應該將其更改爲實例變量,方法是放入__init__函數。

事情是這樣的:

class HumanRace: 
    def __init__(self, name, spawnX, spawnY): 
     self.name = name 
     self.spawn = (spawnX, spawnY) 
     self.size = 0 # dont really need this, you can just len(humans) 
     self.humans = [] 
     self.color = 0, 0, 0 
     self.spawnColor = 1, 1, 1 
+0

謝謝主席先生。 :) –