繁体   English   中英

随机运动pygame

[英]Random movement pygame

我正在尝试制作一个简单的生活模拟器,我需要“细胞”在屏幕上几乎随机移动(有一些规则)。 但问题是,一段时间后,它们往往会聚集在屏幕的左上角。 我试图改变很多东西,比如完全跳过规则并让它们完全随机地移动,但它们仍然聚集在一起。 我的代码中是否有一些明显的问题? 或者我应该以完全不同的方式进行运动?

剪掉不相关部分的代码:

import sys, random, pygame
from pygame.locals import *
pygame.init()

WIDTH = 640 #game window width
HEIGHT = 480 #game window height
FPS = 60 #game's speeds
Pixsize = 2
screen = pygame.display.set_mode((WIDTH, HEIGHT)) #set the game window


class cell:
    def __init__(self):
        self.x = random.randrange(10, WIDTH-10) #x position
        self.y = random.randrange(10, HEIGHT-10) #y position
        self.speed = random.randrange(2,5) #cell speed
        self.move = [None, None] #realtive x and y coordinates to move to
        self.direction = None #movement direction

    def draw(self):
        pygame.draw.rect(screen, (255,255,255), (self.x,self.y,Pixsize,Pixsize),0) #draw the cell


    def wander(self):
        directions = {"S":((-1,2),(1,self.speed)),"SW":((-self.speed,-1),(1,self.speed)),"W":((-self.speed,-1),(-1,2)),"NW":((-self.speed,-1),(-self.speed,-1)),"N":((-1,2),(-self.speed,-1)),"NE":((1,self.speed),(-self.speed,-1)),"E":((1,self.speed),(-1,2)),"SE":((1,self.speed),(1,self.speed))} #((min x, max x)(min y, max y))
        directionsName = ("S","SW","W","NW","N","NE","E","SE") #possible directions
        if random.randrange(0,5) == 2: #move about once every 5 frames
            if self.direction == None: #if no direction is set, set a random one
                self.direction = random.choice(directionsName)
            else:
                a = directionsName.index(self.direction) #get the index of direction in directions list
                b = random.randrange(a-1,a+2) #set the direction to be the same, or one next to the current direction
                if b > len(directionsName)-1: #if direction index is outside the list, move back to the start
                    b = 0
                self.direction = directionsName[b]
            self.move[0] = random.randrange(directions[self.direction][0][0],directions[self.direction][0][1]) #change relative x to a random number between min x and max x
            self.move[1] = random.randrange(directions[self.direction][1][0],directions[self.direction][1][1]) #change relative y to a random number between min y and max y
        if self.x < 5 or self.x > WIDTH - 5 or self.y < 5 or self.y > HEIGHT - 5: #if cell is near the border of the screen, change direction
            if self.x < 5:
                self.direction = "E"
            elif self.x > WIDTH - 5:
                self.direction = "W"
            elif self.y < 5:
                self.direction = "S"
            elif self.y > HEIGHT - 5:
                self.direction = "N"
            self.move[0] = random.randrange(directions[self.direction][0][0],directions[self.direction][0][1]) #change relative x to a random number between min x and max x
            self.move[1] = random.randrange(directions[self.direction][1][0],directions[self.direction][1][1]) #change relative x to a random number between min x and max x
        if self.move[0] != None: #add the relative coordinates to the cells coordinates
            self.x += self.move[0]
            self.y += self.move[1]


cells = []
for i in range(200): #generate n cells
    Cell = cell()
    cells.append(Cell)


def mainloop():
    while True:
        for event in pygame.event.get():
            if event.type== QUIT: #if pressing the X, quit the progra
                pygame.quit() #stop pygame
                sys.exit() #stop the program
        screen.fill((0,0,0)) #clear the screen;
        for i in cells: #update all cells
            i.wander()
            i.draw()
        pygame.display.update() #update display
        pygame.time.Clock().tick(FPS) #limit FPS

mainloop()

你的细胞聚集到左上角的原因是random.randrange()

randrange()部分 根据 python range()函数处理范围。 查看range()函数, range( A, B )返回列表A -> B-1

>>> for i in range( 1,10 ): print( i, end = ", " )
... 
1, 2, 3, 4, 5, 6, 7, 8, 9,

因此,当您的代码执行random.randrange(directions[self.direction][0][0],directions[self.direction][0][1]) ,它不包括范围中的最后一项,这看起来是“SE”。 因此,它们更有可能朝其他方向前进,经过数千次迭代,将它们移至左上角。

许多随机数生成器倾向于支持较小的数字。 您可能需要用其他一些微小的随机数来抵消随机值,或者查看更复杂的生成器。

我迅速环顾四周,找不到任何“真正”的随机生成器。

尝试向其他随机数添加一个小数,然后看看开始发生。

只是为了澄清我的意思:

#Instead of:

self.move[0] = random.randrange(directions[self.direction][0][0],directions[self.direction][0][1])
self.move[1] = random.randrange(directions[self.direction][1][0],directions[self.direction][1][1])

#Try something like:

smallOffset = random.random() #Random floating-point number between 0 and 1 ("Tiny number")

self.move[0] = random.randrange(directions[self.direction][0][0],directions[self.direction][0][1]) + smallOffset
self.move[1] = random.randrange(directions[self.direction][1][0],directions[self.direction][1][1]) + smallOffset

您可能需要调整smallOffset以限制它可以得到的大小(因为它可以是一个接近 1 的值,这不再是真的小了)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM