-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathbox.py
133 lines (102 loc) · 3.3 KB
/
box.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
133
import pygame
import random
from pygame import mixer
# Initialize Pygame
pygame.init()
# Initialize the Pygame mixer
pygame.mixer.init()
pygame.mixer.init()
mixer.music.load("gg.mp3")
volume = 0.5
mixer.music.set_volume(volume)
mixer.music.play(-1)
collision_sound = mixer.Sound("durm.mp3")
collision_sound.set_volume(volume)
WIDTH, HEIGHT = 900, 800
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("BoXeS")
clock = pygame.time.Clock()
run = True
FPS = 60
GREY = (128, 128, 128)
DARKGREY = (65, 65, 65)
GRADIENT_START = (30,30,30)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
COLOUR = GRADIENT_START
WIDTH_OF_LINE = 5
inc_or_dec_up_down = -1
inc_or_dec_left_right = 1
x_pos = random.randint(100, 500)
y_pos = random.randint(200, 400)
inc_value = HEIGHT/100
#x_pos = 0.2*WIDTH+2*WIDTH_OF_LINE
#y_pos = 0.8 * HEIGHT
counter = 10
color_inc_or_dec = [1,1,1]
def new_colour(colour):
global color_inc_or_dec
colour = list(colour)
index = random.randint(0, 2)
if(colour[index] >= 230) or (colour[index] <= 0):
color_inc_or_dec[index] *= -1
colour[index] = (colour[index] + counter*color_inc_or_dec[index])
return tuple(colour)
#function to adjust volume with up and down keys
def adjust_volume(increase):
global volume
if increase:
if volume < 1.0:
volume += 0.1
else:
if volume > 0.0:
volume -= 0.1
mixer.music.set_volume(volume)
collision_sound.set_volume(volume)
def draw_window():
global x_pos, y_pos, inc_or_dec_left_right, inc_or_dec_up_down, COLOUR
outer_box = pygame.Rect(0.2*WIDTH+WIDTH_OF_LINE, 0.1*HEIGHT+WIDTH_OF_LINE, 0.6*WIDTH, 0.8*HEIGHT)
inner_box = pygame.Rect(x_pos, y_pos, 0.1 * HEIGHT, 0.1*HEIGHT)
COLOUR = new_colour(COLOUR)
# print(COLOUR)
pygame.draw.rect(WIN, WHITE, outer_box, width=WIDTH_OF_LINE)
pygame.draw.rect(WIN, COLOUR, inner_box)
pygame.draw.rect(WIN, DARKGREY, inner_box, width=WIDTH_OF_LINE//2)
if outer_box.colliderect(inner_box):
if outer_box.top >= inner_box.top:
collision_sound.play()
# collision_direction: top
inc_or_dec_up_down *= -1
if outer_box.bottom <= inner_box.bottom:
collision_sound.play()
# collision_direction: bottom
inc_or_dec_up_down *= -1
if outer_box.right <= inner_box.right:
collision_sound.play()
# collision_direction: left
inc_or_dec_left_right *= -1
if outer_box.left >= inner_box.left:
collision_sound.play()
# collision_direction: right
inc_or_dec_left_right *= -1
x_pos += inc_value*inc_or_dec_left_right
y_pos += inc_value*inc_or_dec_up_down
pygame.display.update()
pygame.time.delay(60)
def main():
global run
WIN.fill(GREY)
# Main game loop
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
adjust_volume(True) # Increase volume
elif event.key == pygame.K_DOWN:
adjust_volume(False) # Decrease volume
draw_window()
pygame.quit()
if __name__ == "__main__":
main()