-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp_invaders.py
151 lines (130 loc) · 4.18 KB
/
app_invaders.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
from sense_hat import SenseHat
from time import sleep
from random import randrange
from copy import copy
sense = SenseHat()
is_running = True
background = [
[[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0,]],
[[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0,]],
[[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0,]],
[[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0,]],
[[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0,]],
[[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0,]],
[[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0,]],
[[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0,]],
]
spaceship_position = 3
spaceship_color = [255,0,255]
max_block_count = 1
rock_color = [0, 255, 0]
delay = 1.2
counter = 0
def clear_background():
global background
sense.clear()
for y in range(0,len(background)):
for x in range(0,len(background[y])):
background[y][x][0] = 0
background[y][x][1] = 0
background[y][x][2] = 0
def draw_background():
global background
sense.clear()
for y in range(0,len(background)):
for x in range(0,len(background[y])):
sense.set_pixel(x, y, background[y][x][0], background[y][x][1], background[y][x][2])
def draw_spaceship():
global spaceship_position
global spaceship_color
sense.set_pixel(spaceship_position, 7, spaceship_color[0], spaceship_color[1], spaceship_color[2])
def clear_spaceship():
global spaceship_position
sense.set_pixel(spaceship_position, 7, 0, 0, 0)
def clear_first_row():
global background
print("clear_first_row")
for x in range(0,len(background[0])):
background[0][x] = [0,0,0];
def generate_new_row():
global background
global max_block_count
global rock_color
print("generate_new_row [0]")
for i in range(0, max_block_count):
x = randrange(8)
background[0][x] = copy(rock_color);
print("x=" + str(x) + ":" + str(rock_color))
def move_background_down():
global background
print("move_background_down")
for y in range(len(background)-1):
i = len(background) - y - 2
print(" " + str(i) + " -> " + str(i+1))
background[i + 1] = copy(background[i])
clear_first_row()
generate_new_row()
draw_background()
def ship_has_collision():
if background[7][spaceship_position][0] != 0 or background[7][spaceship_position][1] != 0 or background[7][spaceship_position][2] != 0:
return True
return False
def left_event(event):
global spaceship_position
if event.action=='pressed':
if spaceship_position<7:
clear_spaceship()
spaceship_position = spaceship_position + 1
draw_spaceship()
def right_event(event):
global spaceship_position
if event.action=='pressed':
if spaceship_position>0:
clear_spaceship()
spaceship_position = spaceship_position - 1
draw_spaceship()
def middle_event(event):
if event.action=='pressed':
print("fire !")
def adjust_game_parameters():
global delay
global max_block_count
global counter
if counter>24:
delay = delay - 0.3
max_block_count = max_block_count + 1
counter = 0
def run_main():
global is_running
global spaceship_position
global delay
global counter
global max_block_count
clear_background();
sense.clear()
sense.set_rotation(180)
sense.stick.direction_left = left_event
sense.stick.direction_right = right_event
sense.stick.direction_middle = middle_event
draw_background()
draw_spaceship()
is_running = True
score = 0
delay = 1.2
spaceship_position = 3
counter = 0
max_block_count = 1
while is_running:
print("main cycle")
sleep(delay)
move_background_down()
draw_spaceship()
if ship_has_collision():
is_running = False
score = score + 1
counter = counter + 1
adjust_game_parameters()
print("score: " + str(score))
sense.show_message("Score: " + str(score), text_colour=(0, 0, 255))
sense.clear()
return