Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import pygame
- class ball:
- def __init__(s):
- s.x = random.randint(0, ds[0])
- s.y = random.randint(0, ds[1])
- s.dx = random.uniform(0.01, 2) * [-1, 1][random.randint(0, 1)]
- s.dy = random.uniform(0.01, 2) * [-1, 1][random.randint(0, 1)]
- s.color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
- def update(s):
- pygame.draw.circle(display, s.color, (s.x, s.y), 25)
- s.x += s.dx
- s.y += s.dy
- if s.x + 25 > ds[0]:
- s.x = ds[0] - 25
- s.dx = -s.dx
- if s.x < 25:
- s.x = 25
- s.dx = -s.dx
- if s.y + 25 > ds[1]:
- s.y = ds[1] - 25
- s.dy = -s.dy
- if s.y < 25:
- s.y = 25
- s.dy = -s.dy
- pygame.init()
- display = pygame.display.set_mode((800, 600))
- ds = display.get_rect().size
- # ds = width and height
- BLACK = (0, 0, 0)
- WHITE = (255, 255, 255)
- balls = list([ball() for _ in range(50)])
- exit_demo = False
- while not exit_demo:
- events = pygame.event.get()
- for event in events:
- if event.type == pygame.KEYUP and event.key == pygame.K_ESCAPE:
- exit_demo = True
- display.fill(BLACK)
- for b in balls:
- b.update()
- pygame.display.update()
- pygame.time.Clock().tick(120)
- # this is outside of the main game loop. So after ESCAPE is pressed!
- if 1 == 0:
- print("true")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement