-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClassesandGraphics.py
executable file
·132 lines (97 loc) · 3.27 KB
/
ClassesandGraphics.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
"""
ClassesandGraphics.py
Alex Yu
November 16th 2017
Version 1.0
The program creates a bunch of moving ellipses and rectangles!
"""
import pygame
import random
# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
size = (700, 500)
class Rectangle(object):
def __init__(self):
self.x = random.randrange(700)
self.y = random.randrange(500)
self.change_x = random.randrange(-3, 3)
self.change_y = random.randrange(-3, 3)
# making sure that change(s) cannot be zero
while self.change_x == 0 or self.change_y == 0:
self.change_x = random.randrange(-3, 3)
self.change_y = random.randrange(-3, 3)
self.width = random.randrange(20, 70)
self.height = random.randrange(20, 70)
self.colour = [random.randrange(255), random.randrange(255), random.randrange(255)]
def move(self):
self.x += self.change_x
self.y += self.change_y
# if self.x > 700:
# self.x = 700
# self.change_x *= -1
# print("hit")
def draw(self, screen):
# self.width = random.randrange(20, 70)
# self.height = random.randrange(20, 70)
pygame.draw.rect(screen, self.colour, [self.x, self.y, self.width, self.height], 0)
def collision(self):
if self.x > size[0] - self.width:
self.x = size[0] - self.width
self.change_x = - self.change_x
elif self.x < 0:
self.x = 0
self.change_x = - self.change_x
elif self.y > size[1] - self.height:
self.y = size[1] - self.height
self.change_y = - self.change_y
elif self.y < 0:
self.y = 0
self.change_y = - self.change_y
class Ellipse(Rectangle):
def __init__ (self):
super(Ellipse, self).__init__()
def draw(self, screen):
pygame.draw.ellipse(screen, self.colour, [self.x, self.y, self.width, self.height], 0)
pygame.init()
# Set the width and height of the screen [width, height]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("My Game")
# Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
my_list = []
for i in range(200):
my_object = Rectangle()
my_ellipse = Ellipse()
my_list.append(my_object)
my_list.append(my_ellipse)
# -------- Main Program Loop -----------
while not done:
# --- Main event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
# --- Game logic should go here
# --- Screen-clearing code goes here
# Here, we clear the screen to white. Don't put other drawing commands
# above this, or they will be erased with this command.
# If you want a background image, replace this clear with blit'ing the
# background image.
screen.fill(BLACK)
# --- Drawing code should go here
# my_object.draw(screen)
# my_object.move()
for j in my_list:
j.draw(screen)
j.collision()
j.move()
# --- Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# --- Limit to 60 frames per second
clock.tick(60)
# Close the window and quit.
pygame.quit()