eclipse 如何让一个类“吃掉”另一个类?

oxf4rvwz  于 9个月前  发布在  Eclipse
关注(0)|答案(1)|浏览(74)

此问题在此处已有答案

How do I detect collision in pygame?(5个答案)
How do you program collision in classes?(1个答案)
28天前关闭
我目前有3类。一类草,羊和狼。我想使它,所以每当狼和羊在相同的x和y位置的狼会吃羊。我得到这个错误虽然.

Traceback (most recent call last):
File "C:\Users\Owner\Downloads\wolfAndSheepSimulation\simulationMain.py", 
line 82, in <module>
main()
File "C:\Users\Owner\Downloads\wolfAndSheepSimulation\simulationMain.py", 
line 38, in main
w.update(grassList[w.x][w.y])
File "C:\Users\Owner\Downloads\wolfAndSheepSimulation\wolf.py", line 74, in 
update
self.wolfKill()
File "C:\Users\Owner\Downloads\wolfAndSheepSimulation\wolf.py", line 67, in 
wolfKill
for (self.x, self.y) in (s.Sheep.x, s.Sheep.y):
AttributeError: type object 'Sheep' has no attribute 'x'

字符串
下面是我的代码的功能杀...

def wolfKill(self):
    for (self.x, self.y) in (s.Sheep.x, s.Sheep.y): #if they are both on the same x and y position then...
        Wolf.energy += s.Sheep.energy #adds the current energy level of the sheep to the wolves energy level
        s.Sheep.energy -= 15 #subtracts 15 from the energy which in turn kills the sheep


这些是课程

import pygame as p
import random as r 
import variables as v
SQ_LENGTH = v.SQ_LENGTH
NUM_OF_SQUARES = v.NUM_OF_SQUARES

class Grass(object):
'''
class for creating grass
'''
MAX_GRASS_GRAZES = v.MAX_GRASS_GRAZES
GRASS_REFRESH_RATE = v.GRASS_REFRESH_RATE

def __init__(self, x, y):
    '''
    Parameters: x, y
    attributes: x, y, energy, color, turnsToRegrowth
    '''
    self.x = x
    self.y = y
    self.energy = Grass.MAX_GRASS_GRAZES
    self.turnsToRegrowth = Grass.GRASS_REFRESH_RATE
    self.colors = [(79,69,37), (99,99,41), (104,119,41), (102,140,36), (89,155,27), (60,201,18)]

def draw(self, screen):

    p.draw.rect(screen, self.colors[self.energy], p.Rect(self.x * SQ_LENGTH, self.y * SQ_LENGTH, SQ_LENGTH, SQ_LENGTH))

def update(self):         
    '''
    If regrowth counter has counted to 0,
    then the grass regrows one energy unit.
    Otherwise, increment regrowth timer
    '''
    if self.turnsToRegrowth <= 0 and self.energy < Grass.MAX_GRASS_GRAZES:
        self.energy +=1
        self.turnsToRegrowth = Grass.GRASS_REFRESH_RATE
    else:
        self.turnsToRegrowth -=1

def graze(self):
    self.energy -= 1   

def decompose(self):
    self.energy = Grass.MAX_GRASS_GRAZES


import random as r
import pygame as p
import variables as v

#global variables within the class
SQ_LENGTH = v.SQ_LENGTH
NUM_OF_SQUARES = v.NUM_OF_SQUARES
WIDTH = HEIGHT = SQ_LENGTH
RADIUS = SQ_LENGTH//2 #integer division

class Sheep(object):
'''
A class for creating sheep
'''

def __init__(self, x, y):
    '''
    Parameters: x, y
    Attributes: x, y, color
    '''
    self.x = x
    self.y = y
    self.color = (255, 255, 255)
    self.energy = 15

def draw(self, screen):
    p.draw.circle(screen, self.color, ((self.x * WIDTH + WIDTH//2,
                                        self.y * HEIGHT + HEIGHT//2)), RADIUS)

def move(self):
    '''
    move a sheep one square randomly in one of four directions
    make sure sheep doesn't go off the screen
    '''
    direction = r.randint(0, 3)

    if direction == 0 and self.y < NUM_OF_SQUARES - 1:
        self.y += 1
    elif direction == 1 and self.y > 1:
        self.y -= 1
    elif direction == 2 and self.x > 1:
        self.x -= 1
    elif direction == 3 and self.x < NUM_OF_SQUARES - 1:
        self.x += 1

    self.energy -= 1

def isAlive(self):
    if self.energy > 0:
        return True
    else:
        return False

def canGraze(self, grassSquare):
    if grassSquare.energy > 0:
        return True 
    else:
        return False

def graze(self, grassSquare):
    grassSquare.graze()
    self.energy +=1

def update(self, grassSquare):
    if self.isAlive():
        if self.canGraze(grassSquare):
            self.graze(grassSquare)
        else:
            self.move()
    else:
        grassSquare.decompose()
        self.color = (255, 255, 255)


import random as r
import pygame as p
import variables as v
import sheep as s

#global variables within the class
SQ_LENGTH = v.SQ_LENGTH
NUM_OF_SQUARES = v.NUM_OF_SQUARES
WIDTH = HEIGHT = SQ_LENGTH
RADIUS = SQ_LENGTH//2 #integer division

class Wolf(object):
'''
A class for creating wolves
'''

def __init__(self, x, y):
    '''
    Parameters: x, y
    Attributes: x, y, color
    '''
    self.x = x
    self.y = y
    self.color = (139, 150, 168)
    self.energy = 15

def draw(self, screen):
    p.draw.circle(screen, self.color, ((self.x * WIDTH + WIDTH//2,
                                        self.y * HEIGHT + HEIGHT//2)), RADIUS)

def move(self):
    '''    
    move a sheep one square randomly in one of four directions
    make sure sheep doesn't go off the screen
    '''
    direction = r.randint(0, 3)
    if direction == 0 and self.y < NUM_OF_SQUARES - 1:
        self.y += 1
    elif direction == 1 and self.y > 1:
        self.y -= 1
    elif direction == 2 and self.x > 1:
        self.x -= 1
    elif direction == 3 and self.x < NUM_OF_SQUARES - 1:
        self.x += 1

    self.energy -= 1

def isAlive(self):
    if self.energy > 0:
        return True
    else:
        return False

def canHunt(self, sheepSquare):
    if sheepSquare.energy > 0:
        return True 
    else:
        return False

def wolfKill(self):
    for (self.x, self.y) in (s.Sheep.x, s.Sheep.y): #if they are both on the same x and y position then...
        Wolf.energy += s.Sheep.energy #adds the current energy level of the sheep to the wolves energy level
        s.Sheep.energy -= 15 #subtracts 15 from the energy which in turn kills the sheep

def update(self, sheepSquare):
    if self.isAlive():
        if self.canHunt(sheepSquare):
            self.wolfKill()
        else:
            self.move()
    else:
        sheepSquare.decompose()
        self.color = (0, 0, 0)

t3irkdon

t3irkdon1#

你引用的是一个类属性。你可以通过回溯“AttributeError:type object 'Sheep' has no attribute 'x'“. python中的每个类都有它的元类,默认的元类是'type',所以“type object 'Sheep'”指的是Sheep类。如果你想访问另一个对象,你应该把它作为一个参数传递给一个方法,例如“def kill为了获得关于类属性的直觉,试着把它们想象成通常不会改变的属性,它们描述了羊的总体情况,但没有告诉你关于单个羊的情况(示例属性用于哪些)。为了解决您的主要问题,(当羊遇到狼时的表演),你可能想读一下观察者模式。我希望这对你有帮助,祝你好运!

相关问题