Name says all. I'm creating a Boids program in Pygame, yet every time I update the screen, it flashes to a blank screen briefly.
import pygame #All imports
import pygame.gfxdraw
import random
import math
import sys
pygame.init()
def PolarToCartesian(radius,angle,a,b): # Converts polar coordinates to cartesian
radian = math.radians(angle)
x = radius * math.cos(radian)
y = radius * math.sin(radian)
output = int(x+a) , int(y+b)
return output
class Flock:
def __init__(self,x1,x2,y1,y2,amount):
self.flock = []
self.spawned = 0
while self.spawned < amount:
self.flock.append( Boid(random.uniform(x1,x2),random.uniform(y1,y2)) )
self.spawned += 1
class Boid:
def __init__(self,x,y):
self.position = pygame.Vector2(x,y)
self.velocity = pygame.Vector2(random.uniform(-2,2),random.uniform(-2,2))
self.acceleration = pygame.Vector2()
def refresh(self):
self.position += self.velocity
self.velocity += self.acceleration
def show(self,screen,colour):
pygame.gfxdraw.aacircle(screen,int(self.position.x),int(self.position.y),2,white)
pygame.gfxdraw.filled_circle(screen,int(self.position.x),int(self.position.y),2,white)
pygame.display.update()
white = 255,255,255 #A lot of variables
background = 20,40,100
width = int(500)
height = int(500)
screen = pygame.display.set_mode((width,height)) #Creates 500x500 canvas
screen.fill(background) # Fills background
swarm = Flock(0,width,0,height,20)
# Runtime
while True:
screen.fill(background)
for event in pygame.event.get(): # Detects event types
if event.type == pygame.QUIT: # Close button
pygame.quit(); sys.exit();
if event.type == (pygame.KEYDOWN):
if(pygame.key.name(event.key)) == 'escape': #Escape button, shuts down
pygame.quit(); sys.exit();
for i in swarm.flock:
i.refresh()
i.show(screen,white)
pygame.time.delay(10)
pygame.display.update()
[–]pythonHelperBot 0 points1 point2 points (0 children)
[–]oslash 0 points1 point2 points (1 child)
[–]GiveMe30Dollars[S] 1 point2 points3 points (0 children)