我想做一些进化模拟的工作。第一部分是在 pygame 上制作独立移动的方块。这似乎工作,除了一个轻微的障碍!当使用 for 语句第 43 行时,它似乎并未将其应用于仅生成第一个方块的所有方块。所以一格按预期移动,其余的只是坐在那里无所事事..请帮助:D
import pygame, sys
from pygame.locals import *
import time
import random
pygame.init()
# set up the window
DISPLAYSURF = pygame.display.set_mode((500, 500), 0, 32)
pygame.display.set_caption('Drawing')
# set up the colors
BLACK = ( 0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = ( 0, 255, 0)
BLUE = ( 0, 0, 255)
# draw on the surface object
DISPLAYSURF.fill(BLACK)
robot_list=[]
counter =0
while counter < 26:
x=random.randrange(1,500)
x2 = x +6
y= random.randrange(1,500)
y2 = y +6
robot_file=pygame.draw.polygon(DISPLAYSURF, WHITE, ((x, y), (x2, y), (x2, y2), (x, y2)))
robot_list.append(robot_file)
counter +=1
# run the game loop
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
for r in robot_list:
time.sleep(0.1)
pygame.draw.polygon(DISPLAYSURF, BLACK, ((x, y), (x2, y), (x2, y2), (x, y2)))
rand2=random.randrange(1,6)
rand1=random.randrange(-6,6)
x += rand1
x2+= rand1
y+=rand2
y2+=rand2
pygame.draw.polygon(DISPLAYSURF, WHITE, ((x, y), (x2, y), (x2, y2), (x, y2)))
pygame.display.update()
pygame.display.update()