Pygame 吃豆子幽灵,随机改变方向
Pygame Pacman ghost, random change direction
我正在创建一个吃豆人游戏,到目前为止,除了幽灵之外一切正常,当幽灵撞到墙上时,会调用 class 吼叫声。然而,正如您所看到的 self.a
returns 一个 str,但我需要将它应用于我的幽灵精灵 Ghost1、Ghost2 等。所以它调用 Ghost1.a 并且幽灵相应地移动.
如有任何帮助,我们将不胜感激。
class Ghost_move(object):
def __init__(self,g_speed):
super(Ghost_move, self).__init__()
self.left=".rect.x-=g_speed"
self.right=".rect.x+=g_speed"
self.up=".rect.y-=g_speed"
self.down=".rect.y+=g_speed"
self.direction=self.left,self.right,self.up,self.down
self.a=random.choice(self.direction)
正如 abccd 已经指出的那样,将要执行的源代码放入字符串中是个坏主意。最接近您的解决方案是为 left
、right
、up
、down
定义函数。然后你可以将这些功能存储在指令中并执行随机选择的一个:
class Ghost_move(object):
def __init__(self,g_speed):
super(Ghost_move, self).__init__()
self.g_speed = g_speed
self.directions = self.left, self.right, self.up, self.down
self.a = random.choice(self.directions)
def left(self):
self.rect.x -= self.g_speed
def right(self):
self.rect.x += self.g_speed
def up(self):
self.rect.y -= self.g_speed
def down(self):
self.rect.y += self.g_speed
现在self.a
是一个可以调用的函数。例如 ghost1.a()
会在四个方向之一随机移动 ghost1
。但要小心,因为 a 只设置一次,因此 ghost1.a()
总是将这个幽灵朝同一个方向移动,而不是每次调用它时都选择随机方向。
另一种方法是使用向量:
class Ghost_move(object):
def __init__(self,g_speed):
super(Ghost_move, self).__init__()
self.left = (-g_speed, 0)
self.right = (g_speed, 0)
self.up = (0, -g_speed)
self.down = (0, g_speed)
self.directions = self.left, self.right, self.up, self.down
self.random_dir = random.choice(self.directions)
def a():
self.rect.x += self.random_dir[0]
self.rect.y += self.random_dir[1]
用法和之前一样,直接在ghost上调用a()
即可。
我正在创建一个吃豆人游戏,到目前为止,除了幽灵之外一切正常,当幽灵撞到墙上时,会调用 class 吼叫声。然而,正如您所看到的 self.a
returns 一个 str,但我需要将它应用于我的幽灵精灵 Ghost1、Ghost2 等。所以它调用 Ghost1.a 并且幽灵相应地移动.
如有任何帮助,我们将不胜感激。
class Ghost_move(object):
def __init__(self,g_speed):
super(Ghost_move, self).__init__()
self.left=".rect.x-=g_speed"
self.right=".rect.x+=g_speed"
self.up=".rect.y-=g_speed"
self.down=".rect.y+=g_speed"
self.direction=self.left,self.right,self.up,self.down
self.a=random.choice(self.direction)
正如 abccd 已经指出的那样,将要执行的源代码放入字符串中是个坏主意。最接近您的解决方案是为 left
、right
、up
、down
定义函数。然后你可以将这些功能存储在指令中并执行随机选择的一个:
class Ghost_move(object):
def __init__(self,g_speed):
super(Ghost_move, self).__init__()
self.g_speed = g_speed
self.directions = self.left, self.right, self.up, self.down
self.a = random.choice(self.directions)
def left(self):
self.rect.x -= self.g_speed
def right(self):
self.rect.x += self.g_speed
def up(self):
self.rect.y -= self.g_speed
def down(self):
self.rect.y += self.g_speed
现在self.a
是一个可以调用的函数。例如 ghost1.a()
会在四个方向之一随机移动 ghost1
。但要小心,因为 a 只设置一次,因此 ghost1.a()
总是将这个幽灵朝同一个方向移动,而不是每次调用它时都选择随机方向。
另一种方法是使用向量:
class Ghost_move(object):
def __init__(self,g_speed):
super(Ghost_move, self).__init__()
self.left = (-g_speed, 0)
self.right = (g_speed, 0)
self.up = (0, -g_speed)
self.down = (0, g_speed)
self.directions = self.left, self.right, self.up, self.down
self.random_dir = random.choice(self.directions)
def a():
self.rect.x += self.random_dir[0]
self.rect.y += self.random_dir[1]
用法和之前一样,直接在ghost上调用a()
即可。