-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
72 lines (59 loc) · 1.96 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import pygame
import random
import sys
pygame.init()
surface = pygame.display.set_mode((1000, 800))
color = (255, 255, 255)
surface.fill(color)
pygame.display.flip()
WIDTH = 1000
HEIGHT = 800
BACKGROUND = (255, 255, 255)
class Ball:
def __init__(self):
self.image = pygame.image.load(random.choice(("zombies-zombie-kenny.png", "other-alter-egos-me-kenny-2-cc.png")))
self.speed = [random.uniform(-4, 4), 2]
self.rect = self.image.get_rect()
self.alive = True
def update(self):
if self.rect.top < 0:
self.speed[1] = -self.speed[1]
self.speed[0] = random.uniform(-4, 4)
elif self.rect.left < 0 or self.rect.right > WIDTH:
self.speed[0] = -self.speed[0]
elif self.rect.bottom > HEIGHT:
self.alive = False
self.move()
def move(self):
self.rect = self.rect.move(self.speed)
def main():
clock = pygame.time.Clock()
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
ball1 = Ball()
ball2 = Ball()
ball3 = Ball()
ball4 = Ball()
balls = [ball1, ball2, ball3, ball4]
while True:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
for ball in balls:
if ball.rect.collidepoint(pygame.mouse.get_pos()):
ball.speed[0] = random.randrange(-4, 4)
ball.speed[1] = -2
break
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill(BACKGROUND)
for i, ball in enumerate(balls):
if ball.alive:
screen.blit(ball.image, ball.rect)
ball.update()
if not ball.alive:
del balls[i]
pygame.display.flip()
clock.tick(65)
if __name__ == "__main__":
main()