2017-09-14 3 views
2

Je souhaite créer une simulation. (Je ne sais pas où il va se diriger)PyGame Simulation Coordinate Bug

J'ai créé une classe Human et HumanRace. La classe HumanRace contient tous les objets de classe Human dans une liste. Actuellement j'utilise 2 HumanRaces et 10 Humains par course.

Les humains sont représentés par un cercle dont la couleur est affectée à HumanRace. Il y aura aussi un cercle représentant la zone de réapparition de cette course, bien que les maths derrière cela soient un peu bâclés.

Les humains fraient dans la zone de réapparition de leur race. C'est là qu'intervient le problème. Si je ne rends qu'une seule course, tout ira bien, mais si je rends la seconde aussi, il faudra redessiner ces positions avec une couleur différente, même si les Humains ajoutés à la course ont des positions différentes. Donc je suppose que ma définition de rendu est incorrecte. 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() 

humaine et HumanRace classe

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

Répondre

2

Votre liste humans dans la classe HumanRace est une variable de classe. Toutes les instances de HumanRace partageront la même liste humans. Vous devez le remplacer par une variable d'instance en insérant la fonction __init__.

Quelque chose comme ceci:

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

Merci monsieur. :) –