我目前有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'
这是我的函数wolfKill的代码......
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)
答案 0 :(得分:0)
您指的是一个类属性。您可以通过追溯&#34;属性错误告诉它:输入对象&#39;绵羊&#39;没有属性&#39; x&#39;&#34;。 python中的每个类都有它的元类,默认的元类是&#39;类型&#39;所以&#34;类型对象&#39;羊&#39;&#34;&#39;指的是羊类。如果您想要访问另一个对象,则应将其作为参数传递给方法,例如&#34; def wolfkill(自我,绵羊)&#34;。为了获得关于类属性的直觉,尝试将它们视为通常不会改变的类,它们通常描述绵羊,但不会告诉你任何关于绵羊的个体(用于实例属性)。 为了解决你的主要问题(当羊遇到狼时),你可能想要了解观察者模式。我希望这有帮助,祝你好运!