Skip to main content
 首页 » 编程设计

python之PyGame模拟坐标Bug

2024年10月01日7shangdawei

我想创建一个模拟。 (不确定它会去哪里)

我创建了一个 Human 类和 HumanRace 类。 HumanRace 类包含列表中的每个 Human 类对象。 目前我在每个种族中使用 2 个 HumanRaces 和 10 个人类。

人类用圆圈表示,圆圈的颜色分配给 HumanRace。还有一个圆圈代表这个种族的生成区域,尽管这背后的数学有点草率。

人类在其种族的出生区内出生。这就是问题所在。如果我只渲染一场比赛,一切都很好,但如果我也渲染第二场比赛,它会用不同的颜色重新绘制这些位置,即使我添加到比赛中的人类有不同的位置。所以我想我的渲染定义不正确。 主.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() 

人类和人类种族

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

请您参考如下方法:

HumanRace 类中的humans 列表是一个类变量。 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