PyGame模拟协调Bug

时间:2017-09-14 21:11:47

标签: python 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()

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

Picture of Window

1 个答案:

答案 0 :(得分: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
相关问题