Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.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_Pygame - Fatal编程技术网

Python 如何在pygame中为蚂蚁模拟获得多个精灵的路线?

Python 如何在pygame中为蚂蚁模拟获得多个精灵的路线?,python,pygame,Python,Pygame,我正在尝试获取500个“蚂蚁”的路径,但使用update()函数,我不确定是否可以绘制,甚至找不到更好的方法。我正试图找到一种从更新中获取信息的方法,但我不确定如何才能做到这一点。这是一个ant模拟,下面是代码: import random import json #Ant position adding into json file removed from code (all 350, 350) pygame.init() clock = pygame.time.Clock() screen

我正在尝试获取500个“蚂蚁”的路径,但使用update()函数,我不确定是否可以绘制,甚至找不到更好的方法。我正试图找到一种从更新中获取信息的方法,但我不确定如何才能做到这一点。这是一个ant模拟,下面是代码:

import random
import json
#Ant position adding into json file removed from code (all 350, 350)
pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode((750, 750))
screen.fill((255, 255, 255))
background = pygame.image.load("background.png")
running = True
food_found = False
#foodfind() for each ant check if it collides with the food group
class Ant(pygame.sprite.Sprite):
    def __init__(self, picture_path, pos_x, pos_y):
        super().__init__()
        self.image = pygame.image.load(picture_path)
        self.rect = self.image.get_rect()
        self.rect.center = [pos_x, pos_y]
    def update(self):
        directs = random.randint(1, 4)
        #move ants randomly
        if 750 - self.rect.x > 5:
            if directs == 1:
                self.rect.x -= 5
                x = -5
        if 750 - self.rect.x < 745:
            if directs == 2:
                self.rect.x += 5
                x = 5
        if 750 - self.rect.y > 5:
            if directs == 3:
                self.rect.y -= 5
                y = -5
        if 750 - self.rect.y < 745:
            if directs == 4:
                self.rect.y += 5
                y = 5
        with open("positions.json", 'r') as f:
            positions = json.load(f)
        positions[i]["x"] += x
        positions[i]["y"] += y
        with open("positions.json", 'w') as f:
            json.dump(positions, f)
    def go_home(self):
        if food_found != False:
            pass
        #not done yet
pos_x, pos_y = random.randint(0, 750), random.randint(0, 750)
class Food(pygame.sprite.Sprite):
    #food for "ants"
    def __init__(self, picture_path):
        super().__init__()
        self.image = pygame.image.load(picture_path)
        self.rect = self.image.get_rect(center=(pos_x, pos_y))
        self.hitbox = self.rect.inflate(100, 100)
        x = random.randint(pos_x-25, pos_x+25)
        y = random.randint(pos_y - 25, pos_y + 25)
        self.rect.x = x
        self.rect.y = y
    def update(self):
        self.hitbox.center = self.rect.center
ant_gr = pygame.sprite.Group()
food_gr = pygame.sprite.Group()
# i add 500 ants and 1000 foods (not included)
#main loop not included
随机导入
导入json
#Ant位置添加到json文件从代码中删除(全部350350)
pygame.init()
clock=pygame.time.clock()
screen=pygame.display.set_模式((750750))
屏幕填充((255、255、255))
background=pygame.image.load(“background.png”)
运行=真
找到食物=错误
#foodfind()为每个蚂蚁检查它是否与食物组冲突
蚂蚁类(pygame.sprite.sprite):
定义初始化(自、图片路径、位置x、位置y):
super()。\uuuu init\uuuuu()
self.image=pygame.image.load(图片路径)
self.rect=self.image.get_rect()
self.rect.center=[pos_x,pos_y]
def更新(自我):
directs=random.randint(1,4)
#随机移动蚂蚁
如果750-自校正x>5:
如果=1:
自校正x-=5
x=-5
如果750-自校正x<745:
如果=2:
自校正x+=5
x=5
如果750-自校正y>5:
如果=3:
自校正y-=5
y=-5
如果750-自校正y<745:
如果=4:
自校正y+=5
y=5
将open(“positions.json”,“r”)作为f:
positions=json.load(f)
位置[i][“x”]+=x
位置[i][“y”]+=y
将open(“positions.json”,“w”)作为f:
json.dump(位置,f)
def回家(自我):
如果你找到食物!=错误:
通过
#还没做完
pos_x,pos_y=random.randint(0750),random.randint(0750)
班级食物(pygame.雪碧.雪碧):
#蚂蚁的食物
定义初始化(自身、图片路径):
super()。\uuuu init\uuuuu()
self.image=pygame.image.load(图片路径)
self.rect=self.image.get_rect(中心=(位置x,位置y))
self.hitbox=自校正充气(100100)
x=random.randint(位置x-25,位置x+25)
y=random.randint(位置y-25,位置y+25)
self.rect.x=x
自校正y=y
def更新(自我):
self.hitbox.center=self.rect.center
ant_gr=pygame.sprite.Group()
food\u gr=pygame.sprite.Group()
#我加了500只蚂蚁和1000种食物(不包括在内)
#不包括主回路

位置[i][“x”]+=x
行中,不清楚
i
来自何处。但在JSON中,似乎需要一个列表列表,其中外部列表是ant列表,内部列表是ant在每个时间步中所处的所有位置的列表。这有意义吗?似乎最好的方法是在for循环中实际更新它们,但是你可以尝试线程化并稍微拆分进程,但是我不知道这样的pygame是否可行,可能首先对所有蚂蚁运行
update()
,然后运行其他循环,从所有蚂蚁那里获得
rect.center
。通过这种方式,您可以只打开一次JSON文件,使用所有新值创建新的
位置
,然后立即编写。为了使代码更具可读性,您可以将所有类和所有函数放在其他代码之前-它指的是导入之后的
pygame.init()
。在中查看更多信息