Reputation: 31
I want to create a simulation. (not sure where it will head)
I created a Human class and HumanRace class. The HumanRace class contains every Human class object within an list. Currently I'm using 2 HumanRaces and 10 Humans per race.
Humans get represented with a circle, the color of it is assigned to the HumanRace. Also there will be a circle representing the spawn area of this race, although the math behind this is a bit sloppy.
Humans spawn within the spawn area of their race. That's where the problem comes in. If I render only 1 race everything is okay, but if I do render the second too, it will redraw those positions with a different color, even tho my the Humans added to the race have different positions. So I guess my render definition is incorrect. 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()
Human and HumanRace Class
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
Upvotes: 2
Views: 48
Reputation: 5830
Your humans
list inside the HumanRace
class is a class variable. All instances of HumanRace
will share the same humans
list. You should change it to an instance variable by putting inside the __init__
function.
Something like this:
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
Upvotes: 2