import max7219
import time
from machine import Pin, SPI
import random
# Вы играете за корабль (одна красная точка на дисплее). Нужно уворачиваться от стен, идущих сверху. Цель игры - выжить как можно больше
up_btn = Pin(21, Pin.IN, Pin.PULL_DOWN)
down_btn = Pin(22, Pin.IN, Pin.PULL_DOWN)
left_btn = Pin(23, Pin.IN, Pin.PULL_DOWN)
right_btn = Pin(19, Pin.IN, Pin.PULL_DOWN)
bullet_btn = Pin(16, Pin.IN, Pin.PULL_DOWN)
ship = [3,3]
spi = SPI(1, baudrate=10000000, polarity=1, phase=0, sck=Pin(4), mosi=Pin(2))
ss = Pin(5, Pin.OUT)
display = max7219.Matrix8x8(spi, ss, 1)
display.fill(0)
display.show()
class wall_generator:
def __init__(self, display):
self.walls = []
self.__display = display
def generate(self) -> list:
pass_count = random.randint(1, 3)
pass_places = []
for i in range(pass_count):
pass_places.append(random.randint(0, 7))
wall = []
for i in range(0, 8):
if i not in pass_places:
wall.append([i, 8])
self.walls.append(wall)
self.draw()
def draw(self):
for w in self.walls:
for p in w:
self.__display.pixel(p[0], p[1] + 1, 0)
self.__display.pixel(p[0], p[1], 1)
self.__display.show()
def move(self):
old_walls = []
for i in range(len(self.walls)):
if self.walls[i][0][1] == 0:
for p in self.walls[i]:
self.__display.pixel(p[0], p[1], 0)
old_walls.append(self.walls[i])
continue
for j in range(len(self.walls[i])):
self.walls[i][j][1] -= 1
for elem in old_walls:
self.walls.remove(elem)
self.draw()
class Bullet:
def __init__(self, display, x, y):
self.time = time.ticks_ms()
self.__display = display
self.x = x
self.y = y
self.draw()
def move(self):
self.y += 1
self.draw()
def draw(self):
self.__display.pixel(self.x, self.y - 1, 0)
self.__display.pixel(self.x, self.y, 1)
self.__display.show()
game = True
t_wall_gen = 0
t_wall_move = 0
t_ammo_refresh = 0
t_fire_reload = 0.1
gen_level = 2000
move_level = 500
t_difficult = time.ticks_ms()
t_button = 0.15
w_gen = wall_generator(display)
ammo = 0
bullets = []
display_score = 0
while game:
new_t = time.ticks_ms()
if (new_t - t_wall_gen) > gen_level:
t_wall_gen = time.ticks_ms()
w_gen.generate()
if (new_t - t_wall_move) > move_level:
t_wall_move = time.ticks_ms()
w_gen.move()
if (new_t - t_difficult) > 5000:
t_difficult = time.ticks_ms()
gen_level -= 30
move_level -= 20
if (new_t - t_ammo_refresh) > 20000:
t_ammo_refresh = time.ticks_ms()
ammo += 1
print('Ammo refresh!')
for bullet in bullets:
if new_t - bullet.time > 300:
bullet.time = new_t
bullet.move()
if new_t - display_score > 10000:
display_score = new_t
print(f'Score: {display_score // 1000}')
if bullet_btn.value():
if ammo > 0:
bullet = Bullet(display, ship[0], ship[1] + 1)
bullets.append(bullet)
ammo -= 1
time.sleep(t_fire_reload)
if up_btn.value():
display.pixel(ship[0], ship[1], 0)
if ship[1] < 7:
ship[1] += 1
time.sleep(t_button)
if down_btn.value():
display.pixel(ship[0], ship[1], 0)
if ship[1] > 0:
ship[1] -= 1
time.sleep(t_button)
if right_btn.value():
display.pixel(ship[0], ship[1], 0)
if ship[0] < 7:
ship[0] += 1
time.sleep(t_button)
if left_btn.value():
display.pixel(ship[0], ship[1], 0)
if ship[0] > 0:
ship[0] -= 1
time.sleep(t_button)
old_bullets = []
old_points = []
for bullet in bullets:
if bullet.y > 8:
old_bullets.append(bullet)
continue
for i in range(len(w_gen.walls)):
for j in range(len(w_gen.walls[i])):
if (w_gen.walls[i][j][0], w_gen.walls[i][j][1]) == (bullet.x, bullet.y):
old_bullets.append(bullet)
old_points.append((i, w_gen.walls[i][j]))
for bullet in old_bullets:
bullets.remove(bullet)
display.pixel(bullet.x, bullet.y, 0)
t_button -= 0.005
for point in old_points:
w_gen.walls[point[0]].remove(point[1])
for wall in w_gen.walls:
for p in wall:
if p == ship:
print(f'Game over. Score: {new_t // 1000}')
game = False
if game:
display.pixel(ship[0], ship[1], 1)
display.show()