为什么我的对象在python类中使用相同的变量

时间:2019-04-19 19:32:24

标签: python class variables

我正在为学校项目创建塔防游戏。我正在尝试使用路径后的类创建多个不同的对象。但是,当我运行我的代码时,当我希望它们在地图开始时生成时,它们会使对象彼此重叠或重叠。如何更改代码,以使每个对象遵循一条路线,而不是彼此重叠

class enemies(object):
    #enemysprite = pygame.image.load("enemy.png")enter code here
    def __init__(self,x,y,width,height):
        self.xpos = x
        self.ypos = y
        self.width = width
        self.heigh = height
        self.speed = 2
        self.left = False
        self.right = False
        self.up = False
        self.down = False
        self.hitbox = (self.xpos+20,self.ypos+20)
        self.hp = 100
        self.path = (self.xpos,self.ypos)
        self.counter = 0
        self.enemy = pygame.image.load("enemy.png")



    def routelist(self):
        self.counter = self.counter + 1
        self.route = [(0,423),(220,423),(220,180),(456,180),(456,536),(697,536),(697,180),(1198,180),(1198,581),(1354,594)]
        index = self.route[self.counter]
        self.x0,self.y0 = self.route[self.counter-1]
        self.x1,self.y1 = self.route[self.counter]

    def helpme(self):
            if self.x1-self.x0 == 0 and self.y0 > self.y1:
                self.up = True
            if self.x1-self.x0 ==0 and self.y0 < self.y1:
                self.down = True
            if self.y1-self.y0 ==0:
                self.right = True



    def emove(self):
        if self.right:
            if self.xpos <= self.x1:
                self.xpos = self.xpos + self.speed
                return self.xpos
            else:
                self.right = False
                Ec.routelist()
        if self.up:
            if self.ypos >= self.y1:
                self.ypos = self.ypos - self.speed
                return self.ypos
            else:
                self.up = False
                Ec.routelist()
        if self.down:
            if self.ypos <= self.y1:
                self.ypos = self.ypos + self.speed
                return self.ypos
            else:
                self.down = False
                Ec.routelist()


    def redraw(self):
        gameDisplay.blit(self.enemy,(self.xpos,self.ypos))

    def countdown(self):
        start_time = time.time()
        end_time = time.time()
        timer = start_time-end_time
class waves(object):
    def __init__(self):
        self.enemywave = [1,2,3,4,5,6,8,1]
        self.enemylist = []
        self.count = 0
        self.maxcount = 30
Wc = waves()

def wave():
    if Wc.enemywave != []:
        if  Wc.count ==  Wc.maxcount:
            NewEnemy =  Wc.enemywave.pop(0)
            Wc.enemylist.append(NewEnemy)
            Wc.count = 0
        else:
             Wc.count +=1
    Ec.redraw()
    for enemy in  Wc.enemylist:
        Ec.emove()

这会在我希望它从头开始的同时移动时输出并在彼此之上绘制对象

0 个答案:

没有答案