从'墙上的墙'开始,所有红色的“自我”都加上了下划线?
class Player(object):
def __init__(self):
self.rect = pygame.Rect(x,y,16,16)
def move(self, dx, dy):
if dx != 0:
self.move_dir(dx, 0)
if dy != 0:
self.move_dir(0, dy)
def move_dir(self, dx, dy):
self.rect.x = self.rect.x + dx
self.rect.y = self.rect.y + dy
#^it accepts all of this...
# but none of the following...
for wall in walls:
if self.rect.colliderect(wall.rect):
if dx > 0:
self.rect.right = wall.rect.left
if dx < 0:
self.rect.left = wall.rect.right
if dy > 0:
self.rect.bottom = wall.rect.top
if dy < 0:
self.rect.top = wall.rect.bottom
for spike in spikes:
if self.rect.colliderect(spike.rect):
self.rect.x = 16
self.rect.y = 16
if player.rect.colliderect(portal_rect):
self.rect.x = 528
self.rect.y = 144
if player.rect.colliderect(portal_rect1):
self.rect.x = 176
self.rect.y = 208
无关,但它也存在portal_rect
的问题答案 0 :(得分:2)
缩进很重要!
class Example(object):
def some_method(self):
# we are inside 'some_method' so 'self' is valid
self.a = 1
# we are no longer inside a method so 'self' is not valid
self.b = 2