-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
128 lines (112 loc) · 2.81 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
from time import sleep
from random import randint
from sense_hat import SenseHat
sense = SenseHat()
sense.clear(0, 0, 0)
x = 4
#ratio 1 (hard) ++ (easier)
ratio=8
loops=0
score=0
asteroids=[]
rematch=True
O = (0,0,0)
R = (255,0,0)
G = (0,255,0)
big_V = [
O,O,O,O,O,O,O,O,
O,O,O,O,O,O,O,O,
O,O,O,O,O,O,G,O,
O,O,O,O,O,G,O,O,
O,O,O,O,G,O,O,O,
O,G,O,G,O,O,O,O,
O,O,G,O,O,O,O,O,
O,O,O,O,O,O,O,O
]
big_X = [
O,O,O,O,O,O,O,O,
O,R,O,O,O,O,R,O,
O,O,R,O,O,R,O,O,
O,O,O,R,R,O,O,O,
O,O,O,R,R,O,O,O,
O,O,R,O,O,R,O,O,
O,R,O,O,O,O,R,O,
O,O,O,O,O,O,O,O
]
def draw_ship():
sense.set_pixel(x, 7, 255, 255, 255)
def asteroids_fall():
global loops
global score
global ratio
for a in asteroids:
sense.set_pixel(a[0], a[1], 0, 0, 0)
# Adding new asteroid
if (loops % ratio == 0):
asteroids.insert(0,[randint(0,7),0])
for a in asteroids:
# Hitting an asteroid
if ((a[0] ==x ) & (a[1]==7)):
return False
# Brown
sense.set_pixel(a[0], a[1], 210, 105, 30)
# Asteroid out of screen
if a[1]==7:
asteroids.pop()
score += 1
if (ratio>1):
if(score % 5 == 0):
ratio -= 1
# Asteroid moving down
else:
a[1] += 1
loops += 1
return True
def move_left(event):
global x
if x > 0 and event.action=='pressed':
x -= 1
def move_right(event):
global x
if x < 7 and event.action=='pressed':
x += 1
sense.stick.direction_left = move_left
sense.stick.direction_right = move_right
def show_rematch():
middlepressed=False
option=1
sense.show_message("Play again?", text_colour=(0, 255, 0))
sense.set_pixels(big_V)
event = sense.stick.wait_for_event()
if(event.direction == "middle"):
middlepressed=True
while(middlepressed==False):
if((event.direction == "left") and (event.action == "pressed")) or ((event.direction == "right") and (event.action == "pressed")):
if (option == 1):
option = 0
sense.set_pixels(big_X)
else:
option = 1
sense.set_pixels(big_V)
if(event.direction == "middle"):
middlepressed=True
else:
event = sense.stick.wait_for_event()
print (event.action)
print(event.direction)
if (option == 1):
return True
else:
return False
while rematch==True:
score=0
ratio=8
asteroids = []
while asteroids_fall():
draw_ship()
sleep(0.5)
sense.clear(0, 0, 0)
sense.show_message("Game Over - Score: "+str(score), text_colour=(255, 99, 71))
rematch = show_rematch()
sense.show_message("Bye!", text_colour=(0,0,255))
sense.clear(0, 0, 0)