This code creates a nice screensaver made with pygame.
Here is a video showing it, followed by the code.
import pygame, random, math def radians(degrees): return degrees*math.pi/180 class Particle: def __init__(self, xy, radius, speed, angle, colour, surface): self.x = xy[0] self.y = xy[1] self.speed = speed self.angle = angle self.radius = 3 self.surface = surface self.colour = colour self.rect = pygame.draw.circle(surface,(255,255,0), (int(round(self.x,0)), int(round(self.y,0))), self.radius) def move(self): """ Update speed and position based on speed, angle """ # for constant change in position values. self.x += math.sin(self.angle) * self.speed self.y -= math.cos(self.angle) * self.speed # pygame.rect likes int arguments for x and y self.rect.x = int(round(self.x)) self.rect.y = int(round(self.y)) def draw(self): """ Draw the particle on screen""" pygame.draw.circle(self.surface,self.colour,self.rect.center,self.radius) def bounce(self): """ Tests whether a particle has hit the boundary of the environment """ if self.x > self.surface.get_width() - self.radius: # right self.x = 2*(self.surface.get_width() - self.radius) - self.x self.angle = - self.angle elif self.x < self.radius: # left self.x = 2*self.radius - self.x self.angle = - self.angle if self.y > self.surface.get_height() - self.radius: # bottom self.y = 2*(self.surface.get_height() - self.radius) - self.y self.angle = math.pi - self.angle elif self.y < self.radius: # top self.y = 2*self.radius - self.y self.angle = math.pi - self.angle def rnd(): r = random.randrange(0,255) g = random.randrange(0,255) b = random.randrange(0,255) return r,g,b def main(): xmax = 640 #width of window ymax = 480 #height of window white = (0, 0, 0) black = (200,0,100) grey = (255,255,128) pygame.init() screen = pygame.display.set_mode((0,0)) clock = pygame.time.Clock() particles = [] for i in range(1000): if i % 2: colour = rnd() else: colour = rnd() # for readability x = random.randint(0, xmax) y = random.randint(0, ymax) speed = random.randint(0,20)*0.1 angle = random.randint(0,360) radius = 3 particles.append( Particle((x, y), radius, speed, angle, colour, screen) ) done = False while not done: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True break elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: done = True break if done: break screen.fill(white) for p in particles: p.move() p.bounce() p.draw() clock.tick(40) pygame.display.flip() pygame.quit() if __name__ == "__main__": main()
Flames
A sort of flame
import pygame,random from pygame.locals import * xmax = 1000 #width of window ymax = 600 #height of window class Particle(): def __init__(self, startx, starty, col, pause): self.x = startx self.y = starty self.col = col self.sx = startx self.sy = starty self.pause = pause self.radius = random.randrange(2,6) def move(self): if self.pause==0: if self.y < 0: self.x=self.sx self.y=self.sy else: self.y-=1 self.x+=random.randint(-2, 2) else: self.pause-=1 A = 100 black = (255,255,255) gray = (255,255,0) B = 380 def main(): pygame.init() screen = pygame.display.set_mode((xmax,ymax)) white = (255, 255, 255) black = (255,0,0) grey = (255,255,0) clock=pygame.time.Clock() particles = [] for part in range(1, A): if part % 2 > 0: col = black else: col = gray particles.append(Particle(515, B, col, round(B*part/A))) exitflag = False while not exitflag: for event in pygame.event.get(): if event.type == QUIT: exitflag = True elif event.type == KEYDOWN: if event.key == K_ESCAPE: exitflag = True #screen.fill(black) for p in particles: p.move() pygame.draw.circle(screen, p.col, (p.x, p.y), p.radius) if random.random() > 0.5: if p.radius > 0: p.radius -= .1 else: if p.radius < 6: p.radius += .1 pygame.display.flip() clock.tick(50) pygame.quit() if __name__ == "__main__": main()
Subscribe to the newsletter for updates
Tkinter templates
My youtube channel
Twitter: @pythonprogrammi - python_pygame