-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
114 lines (100 loc) · 4.08 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
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
import pygame, random
from pygame.sprite import Sprite #? Use for later?
from crash import crash
from pause import pause
from functions import end
from constants import *
#? class Block(Sprite):
#? def __init__(self, y):
#? Sprite.__init__(self)
# Main Game loop
def main():
dodged = 0
carX = (dispWD - carWD) / 2
carY = dispHT - 5 - carHT
carSpeed = 3
keys = {'left':[False, False], 'right':[False, False], 'up': [False, False], 'down': [False, False]}
rectX = random.randrange(0, dispWD - 100)
rectY = -500
rectSpeed = 6
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
end()
elif event.type == pygame.KEYDOWN:
#? Maybe find some way to create a switch statement?
if event.key == pygame.K_LEFT:
keys['left'][0] = True
elif event.key == pygame.K_a:
keys['left'][1] = True
elif event.key == pygame.K_RIGHT:
keys['right'][0] = True
elif event.key == pygame.K_d:
keys['right'][1] = True
elif event.key == pygame.K_UP:
keys['up'][0] = True
elif event.key == pygame.K_w:
keys['up'][1] = True
elif event.key == pygame.K_DOWN:
keys['down'][0] = True
elif event.key == pygame.K_s:
keys['down'][1] = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
keys['left'][0] = False
elif event.key == pygame.K_a:
keys['left'][1] = False
elif event.key == pygame.K_RIGHT:
keys['right'][0] = False
elif event.key == pygame.K_d:
keys['right'][1] = False
elif event.key == pygame.K_UP:
keys['up'][0] = False
elif event.key == pygame.K_w:
keys['up'][1] = False
elif event.key == pygame.K_DOWN:
keys['down'][0] = False
elif event.key == pygame.K_s:
keys['down'][1] = False
elif event.key == pygame.K_ESCAPE:
# Paused
btnClicked = pause()
if btnClicked == crashButton.restart:
main()
return
elif btnClicked == crashButton.home:
return
keys = {'left':[False, False], 'right':[False, False], 'up': [False, False], 'down': [False, False]}
if (keys['left'][0] or keys['left'][1]) and carX > 0:
carX -= int(carSpeed)
if (keys['right'][0] or keys['right'][1]) and carX < dispWD - carWD:
carX += int(carSpeed)
if (keys['up'][0] or keys['up'][1]) and carY > 0:
carY -= int(carSpeed)
if (keys['down'][0] or keys['down'][1]) and carY < dispHT - carHT:
carY += int(carSpeed)
rectY += rectSpeed
display.fill(white)
display.blit(car, (carX, carY))
rect = (rectX, rectY, 100, 100)
pygame.draw.rect(display, shelf['rectColor'], rect)
display.blit(lobsterS.render('Dodged: ' + str(dodged), True, green), (3, 3))
record = lobsterS.render('Record: ' + str(shelf['record']), True, blue)
display.blit(record, record.get_rect(topright=(dispWD - 3, 3)))
pygame.display.update()
clock.tick(60)
# When user crashes
if car.get_rect(topleft=(carX, carY)).colliderect(rect):
btnClicked = crash(dodged)
if btnClicked == crashButton.restart:
main()
return
elif btnClicked == crashButton.home:
return
# When user dodges obstacle
if rectY > dispHT:
dodged += 1
rectX = random.randrange(0, dispWD - 100)
rectY = -100
rectSpeed += 1
carSpeed += 0.4