【问题标题】:How to make one class "eat" another?如何让一个班级“吃”另一个班级?
【发布时间】:2018-07-24 13:53:54
【问题描述】:

我目前有 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)

【问题讨论】:

  • 我们不知道您的课程是什么样的,所以我们帮不上什么忙。
  • 从错误消息中,您显然使用的是 Sheep 类而不是您的 Sheep 类的实例...除此之外还有许多其他问题...我认为您不明白for 做了什么,例如...
  • 我添加了课程
  • 您必须再次编码wolfkill 方法,因为您使用for 循环的方式存在问题。要让狼吃草,您可以将所有草对象添加到Group,当狼与组中的任何对象发生碰撞时,将其移除。代码看起来像:pygame.sprite.spritecollide(wolf, all_grass_sprites, True)True 参数将告诉 pygame 删除任何与狼碰撞的对象。

标签: python eclipse pygame simulation pydev


【解决方案1】:

您指的是类属性。您可以通过回溯“AttributeError:类型对象'Sheep'没有属性'x'”来判断它。 python中的每个类都有它的元类,默认的元类是'type',所以“type object 'Sheep'”指的是Sheep类。如果您想访问另一个对象,则应将其作为参数传递给方法,例如“def wolfkill(自我,羊)”。要获得有关类属性的直觉,请尝试将它们视为通常不会改变的属性,它们概括地描述了绵羊,但没有告诉您有关单个绵羊的任何信息(使用哪些实例属性)。 为了解决您的主要问题(当羊遇到狼时采取行动),您可能需要阅读观察者模式。我希望这会有所帮助,祝你好运!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-25
    • 1970-01-01
    • 2017-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多