0
我的代码应该产生多个追逐我的玩家的敌人。相反,敌人只是呆在一个地方。只有当我尝试向spawn坐标的敌方产卵函数输入一个值时才会出现这种情况。继承人代码:不知道如何产生多个敌人
class spawn(object):
def __init__(self,place1,place2):
self.place1=place1
self.place2=place2
def AIPrototype(self):#The important parts to this error star here
global x,y,x1,y1
pygame.draw.rect(screen,THECOLORS['blue'],(self.place1,self.place2,50,50))
x1=self.place1
y1=self.place2#end here
if x1<x:
xspeed1=1
slopex1=x-x1
if x1>x:
xspeed1=-1
slopex1=x1-x
if y1<y:
yspeed1=1
slopey1=y-y1
if y1>y:
yspeed1=-1
slopey1=y1-y
#
hypo1=((slopex1**2)+(slopey1**2))**0.5
speedmark1=hypo1/3
speedy1=slopey1/speedmark1
speedx1=slopex1/speedmark1
movex1=speedx1
movey1=speedy1
if x1<=640 and x1>=0:
if x1>x:
x1+=xspeed1*movex1
if x1<x:
xspeed1=0
if y1<=480 and x1>=0:
if y1>y:
y1+=yspeed1*movey1
if y1<y:
yspeed1=0
if x1<=640 and x1>=0:
if x1<x:
x1+=xspeed1*movex1
if x1>x:
xspeed1=0
if y1<=480 and x1>=0:
if y1<y:
y1+=yspeed1*movey1
if y1>y:
yspeed1=0
#
if x1>640:
x1=640
if x1<0:
x1=0
if y1>480:
y1=480
if y1<0:
y1=0
self.place1=x1#start
self.place2=y1#end
这是产生敌人的作用。这个函数在这里被称为:
clock = pygame.time.Clock()
keepGoing = True
try:
while keepGoing:
clock.tick(60)
screen.fill(THECOLORS['red'])
pygame.draw.rect(screen,THECOLORS['green'],(x,y,50,50))
char()
spawn1=spawn(200,200)#start
spawn1.AIPrototype()#end
pygame.display.flip()
我不知道我的编码错误在哪里,所以如果有人能帮我一把,那会很好。
请提供一个[最小,完整且可验证的示例](https://stackoverflow.com/help/mcve),否则可能很难甚至无法帮助您。 – skrx
对不起,有这么多的代码,但我认为我削减了所有不重要的部分。这些都是我认为我的错误可能存在的部分。如果我再去掉,我也可以删除错误。 – Qartx
问题是您的代码示例不完整且可验证。你应该提供一个例子,我们可以复制,粘贴和运行。另一个问题是缺少文件和评论,解释了一切应该如何工作。它也看起来像你一次问两个问题:如何产生多个敌人,以及如何移动它们。移动敌人的代码似乎被破坏了。我建议使用矢量来处理敌人的移动,然后你可以将整个移动代码减少到几行。我可以向你展示一个例子(你知道pygame.Sprites是如何工作的吗?)。 – skrx