因此,以下代码是用立方体制作实际的蛇(这段代码也是唯一带有术语self.pos
的部分):
class cube (object):
rows = 20
w = 500
def __init__(self, start, dirnx=1, dirny=0, colour=(255, 0, 0)):
self.pos = start
self.dirnx = 1
self.dirny = 0
self.colour = colour
def move(self, dirnx, dirny):
self.dirnx = dirnx
self.dirny = dirny
self.pos = (self.pos[0] + self.dirnx, self.pos[1] + self.dirny)
def draw(self, surface, eyes=False):
dis = self.w // self.rows
i = self.pos[0]
j = self.pos[1]
pygame.draw.rect(surface, self.colour, (i * dis + 1, j * dis + 1, dis - 2, dis - 2))
if eyes:
centre = dis//2
radius = 3
circleMiddle = (i * dis + centre - radius, j * dis + 8)
circleMiddle2 = (i * dis + dis - radius * 2, j * dis + 8)
pygame.draw.circle(surface, (0, 0, 0), circleMiddle, radius)
pygame.draw.circle(surface, (0, 0, 0,), circleMiddle2, radius)
当我运行整个程序(包含更多代码)时,出现此错误:
File "/Users/shreevathsagorur/PycharmProjects/FirstStepGameDevelopment/Snake.py", line 30, in draw
i = self.pos[0]
TypeError: 'NoneType' object is not subscriptable
我非常感谢您的帮助,因为我对这些东西还很陌生。 谢谢。