Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在pygame中,有没有一种方法可以平滑地在随机方向上移动对象?_Python_Random_Pygame - Fatal编程技术网

Python 在pygame中,有没有一种方法可以平滑地在随机方向上移动对象?

Python 在pygame中,有没有一种方法可以平滑地在随机方向上移动对象?,python,random,pygame,Python,Random,Pygame,我试图让水滴在随机方向上移动几帧,而不是只移动一次,这样看起来不那么急促,更平滑,但一直无法做到这一点。在选择另一个随机方向并执行相同操作之前,是否有任何方法使每个对象在同一方向上移动几次记号 我的代码(大多数与此无关): 导入pygame 随机输入 将numpy作为np导入 宽度=1800 高度=1000 蓝色=(15,15180) 红色=(150,0,0) 类Blob: 定义初始值(自身、颜色、x_边界、y_边界、大小): 颜色 self.size=大小 self.x_边界=x_边界 sel

我试图让水滴在随机方向上移动几帧,而不是只移动一次,这样看起来不那么急促,更平滑,但一直无法做到这一点。在选择另一个随机方向并执行相同操作之前,是否有任何方法使每个对象在同一方向上移动几次记号

我的代码(大多数与此无关):

导入pygame
随机输入
将numpy作为np导入
宽度=1800
高度=1000
蓝色=(15,15180)
红色=(150,0,0)
类Blob:
定义初始值(自身、颜色、x_边界、y_边界、大小):
颜色
self.size=大小
self.x_边界=x_边界
self.y_边界=y_边界
self.x=random.randrange(0,self.x_边界)
self.y=random.randrange(0,self.y_边界)
def移动(自我):
self.x+=random.randrange(-6,7)
self.y+=random.randrange(-6,7)
def限制(自我):
如果self.x<0:
self.x=0
elif self.x>self.x_边界:
self.x=self.x_边界
如果self.y<0:
self.y=0
elif self.y>self.y_边界:
self.y=self.y\u边界
定义添加(自身、其他块):
如果其他blob.size>self.size:
其他blob.size+=int(self.size*0.5)
self.size=0
类FastBlob(Blob):
定义初始值(自身、颜色、x_边界、y_边界、大小):
超级()
def移动(自我):
self.x+=random.randrange(-20,21)
self.y+=random.randrange(-20,21)
pygame.init()
game_display=pygame.display.set_模式((宽度、高度))
pygame.display.set_标题('Blob world')
clock=pygame.time.clock()
def接触(b1、b2):
返回np.linalg.norm(np.array([b1.x,b1.y])-np.array([b2.x,b2.y])<(b1.size+b2.size)
def句柄_冲突(blob_列表):
蓝色、红色、慢速红色=斑点列表
对于蓝色、红色、慢速红色的首字母:
对于first_blob_id,first_blob.copy()中的first_blob.items():
对于其他蓝色、红色、慢速红色的斑点:
对于other_blob_id,other_blob.copy().items()中的other_blob:
如果第一个\u blob==其他\u blob:
通过
其他:
如果正在触摸(第一个污点,其他污点):
第一个污点+其他污点
返回蓝调,红色,慢红色
def draw_环境(blob_列表):
游戏显示填充((210210))
处理碰撞(blob\U列表)
对于blob_列表中的blob_dict:
对于blob_dict中的blob_id:
blob=blob_dict[blob_id]
pygame.draw.circle(游戏显示,blob.COLOR,[blob.x,blob.y],blob.size)
blob.move()
blob.limits()
pygame.display.update()
def main():
blue_blob=dict(枚举([FastBlob(blue,WIDTH,HEIGHT,random.randrange(10,15)),用于范围(20)中的i)
red_blob=dict(枚举([FastBlob(red,WIDTH,HEIGHT,random.randrange(5,10)),用于范围(30)中的i)
slow_red_blobs=dict(枚举范围(5)中i的[Blob(红色,宽度,高度,random.randrange(20,30)))
尽管如此:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
退出
绘制环境([蓝色、红色、慢速、红色])
时钟滴答作响(7)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
main()

在这里,我在游戏中遇到了类似的问题,敌人必须随机改变方向,因此玩家无法预测

def goblin_move():                                                  #Goblin auto (random) movement

    if goblin.x < 0:
        goblin.go_right()
    elif goblin.x > 500:
        goblin.go_left()
    else:
        if goblin.x > (rnd_1 * win_width) and goblin.move_flag == -1:
            goblin.go_left()
            goblin.move_flag = -1

        else:
            goblin.go_right()
            goblin.move_flag = 1

    if goblin.x > (rnd_2 * win_width):
        goblin.move_flag = -1


def set_random(rnd_1, rnd_2):                                       #Random function generator

    rnd_1 = round(random.uniform(0, 0.45), 2)
    rnd_2 = round(random.uniform(0.65, 0.95), 2)

    return rnd_1, rnd_2

希望你会发现它有用。

在这里,我在游戏中遇到了类似的问题,当敌人随机改变方向时,玩家无法预测

def goblin_move():                                                  #Goblin auto (random) movement

    if goblin.x < 0:
        goblin.go_right()
    elif goblin.x > 500:
        goblin.go_left()
    else:
        if goblin.x > (rnd_1 * win_width) and goblin.move_flag == -1:
            goblin.go_left()
            goblin.move_flag = -1

        else:
            goblin.go_right()
            goblin.move_flag = 1

    if goblin.x > (rnd_2 * win_width):
        goblin.move_flag = -1


def set_random(rnd_1, rnd_2):                                       #Random function generator

    rnd_1 = round(random.uniform(0, 0.45), 2)
    rnd_2 = round(random.uniform(0.65, 0.95), 2)

    return rnd_1, rnd_2
希望您会发现它有用。

用于进行计算。将blob的坐标存储到向量2,并定义最大距离(
self.maxdist
)、速度(
self.speed
)、随机距离(
self.dist
)和随机方向(
self.dir
)。随机方向是长度为1()和随机角度()的向量:

class Blob:
定义初始值(自身、颜色、x_边界、y_边界、大小):
颜色
self.size=大小
self.x_边界=x_边界
self.y_边界=y_边界
self.x=random.randrange(0,self.x_边界)
self.y=random.randrange(0,self.y_边界)
self.pos=pygame.math.Vector2(self.x,self.y)
self.maxdist=7
自身速度=1
self.dist=random.randrange(self.maxdist)
self.dir=pygame.math.Vector2(1,0).rotate(random.randrange(360))
当水滴移动时,按速度缩放方向并将其添加到位置(
self.pos+=self.dir*self.speed
)。减小距离(
self.dist-=self.speed
)并将
self.x
self.y
更新为四舍五入的()位置。如果
self.dist
低于0,则会创建一个新的随机方向和距离:

class Blob:
# [...]
def移动(自我):
self.pos+=self.dir*self.speed
自距离-=自速度
self.x,self.y=圆形(self.pos.x),圆形(self.pos.y)
如果self.dist self.x_边界:
self.pos.x=self.x_边界
如果自身位置y<0:
自身位置y=0
elif self.pos.y>self.y_边界:
self.pos.y=self.y\u边界
self.x,self.y=圆形(self.pos.x),圆形(self.pos.y)
FastBlob
不需要自己的
move
方法。只需定义自己的
self.maxdist
self.speed

类FastBlob(Blob):
定义初始值(自身、颜色、x_边界、y_边界、大小):
超级()
self.maxdist=35
自身速度=5
用于进行计算。将blob的坐标存储到a
Vector2