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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
import pygame
from pygame import mixer
import random
pygame.init()
SCREEN_WIDTH = 1280
SCREEN_HEIGHT = 720
dimensions = (SCREEN_WIDTH, SCREEN_HEIGHT)
screen = pygame.display.set_mode(dimensions)
class DVD:
dvd_logo = pygame.image.load("DVD.png")
SCALE = 25 # change this to affect number of DVDs scaled by factor of 1/SCALE (smaller for less, bigger for more)
height = dvd_logo.get_height() // SCALE
width = dvd_logo.get_width() // SCALE
COLOURS = [(0, 0, 255), (255, 0, 255), (255, 0, 0), (255, 128, 0), (255, 255, 255), (255, 255, 0), (0, 255, 0)]
hitSound = mixer.Sound('hit.wav')
def __init__(self, x, y):
self.position = pygame.Vector2(x, y)
self.velocity = pygame.Vector2(0.2, 0.1)
#random directions
# if random.randint(0,1) == 1:
# self.velocity.x *= -1
# if random.randint(0,1) == 1:
# self.velocity.y *= -1
self.dvd_logo = pygame.transform.scale(self.dvd_logo, (self.width, self.height))
self.colour = random.choice(self.COLOURS)
self.randomizeColour()
def randomizeColour(self):
self.COLOURS.remove(self.colour)
new_colour = random.choice(self.COLOURS)
self.dvd_logo.fill((0, 0, 0), special_flags=pygame.BLEND_MULT)
self.dvd_logo.fill(new_colour, special_flags=pygame.BLEND_ADD)
self.COLOURS.append(self.colour)
self.colour = new_colour
# pass # comment everything above for images with diff colours
self.hitSound.play() # Too lazy to make another function, so here it is
def check_edges(self):
if (self.position.y < 0 and self.velocity.y < 0) or (
self.position.y + self.height > SCREEN_HEIGHT and self.velocity.y > 0):
self.velocity.y *= -1
self.randomizeColour()
if (self.position.x < 0 and self.velocity.x < 0) or (
self.position.x + self.width > SCREEN_WIDTH and self.velocity.x > 0):
self.velocity.x *= -1
self.randomizeColour()
def check_DVDs(self, DVDs, delta):
for dvd in DVDs:
if dvd.position == self.position:
continue
if ((dvd.position.x <= self.position.x <= dvd.position.x + dvd.width or
dvd.position.x <= self.position.x + self.width <= dvd.position.x + dvd.width) and
(dvd.position.y <= self.position.y <= dvd.position.y + dvd.height or
dvd.position.y <= self.position.y + self.height <= dvd.position.y + dvd.height)):
self.velocity *= -1
self.position += self.velocity * delta # to avoid overlapping (not tested thoroughly)
self.randomizeColour()
# old code
# if (dvd.position.x < self.position.x < dvd.position.x + dvd.width or dvd.position.x < self.position.x + self.width < dvd.position.x + dvd.width):
# if ((dvd.position.y < self.position.y < dvd.position.y + dvd.height and self.velocity.y < 0) or
# dvd.position.y < self.position.y + self.height < dvd.position.y + dvd.height and self.velocity.y > 0):
# self.velocity.y *= -1
# self.randomizeColour()
#
# if (dvd.position.y < self.position.y < dvd.position.y + dvd.height or dvd.position.y < self.position.y + self.height < dvd.position.y + dvd.height):
# if ((dvd.position.x + dvd.width > self.position.x > dvd.position.x and self.velocity.x < 0) or
# dvd.position.x < self.position.x + self.width < dvd.position.x + dvd.width and self.velocity.x > 0):
# self.velocity.x *= -1
# self.randomizeColour()
def update(self, delta, DVDs):
self.position += self.velocity * delta
self.check_DVDs(DVDs, delta)
self.check_edges()
def draw(self, screen):
screen.blit(self.dvd_logo, self.position)
clock = pygame.time.Clock()
# dvds = [DVD(0, 0), DVD(50, SCREEN_HEIGHT - DVD.height)] #weird i need to fix this
# dvds = [DVD(0, 0), DVD(0, SCREEN_HEIGHT - DVD.height)] #weird i need to fix this no.2
# dvds = [DVD(0, 0), DVD(0, SCREEN_HEIGHT - DVD.height), DVD(SCREEN_WIDTH - DVD.width, 0), DVD(SCREEN_WIDTH - DVD.width, SCREEN_HEIGHT - DVD.height)] #4 corners
# dvds = [DVD(1, 0), DVD(501, 0), DVD(0, 550), DVD(500, 500)] # testing collision with other dvds
def randomizeDVDS(num):
out = []
for i in range(num):
out.append(DVD(random.randint(0, SCREEN_WIDTH), random.randint(0, SCREEN_HEIGHT)))
return out
# dvds = randomizeDVDS(100) # lazy function has overlapping things so I can make it laggier to run or make something better
def gridDVDs():
out = []
dvdCount = 0
for i in range(SCREEN_WIDTH // (DVD.width * 2 + DVD.width // DVD.SCALE)):
for j in range(SCREEN_HEIGHT // (DVD.height * 2 + DVD.height // DVD.SCALE)):
out.append(
DVD(i * (DVD.width * 2 + DVD.width // DVD.SCALE), j * (DVD.height * 2 + DVD.height // DVD.SCALE)))
dvdCount += 1
print("The number of DVDs is", dvdCount) # also number of brain cells lost
return out
dvds = gridDVDs()
# dvds = [DVD(0,0)] # one dvd to enjoy in all its glory
is_running = True
while is_running:
screen.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
is_running = False
delta = clock.get_time()
for dvd in dvds:
dvd.update(delta, dvds)
dvd.draw(screen)
pygame.display.update()
clock.tick(60)
pygame.quit()
# TODO two dvds - done, use class - done, not same colour twice - done, sound effects - done but annoying,
# reach corner counter but too lazy, bounce off each other - done, random velocity but too lazy
|