Aktualizovat main.py

This commit is contained in:
2026-11-06 15:14:45 +00:00
parent 774d3f2a3e
commit 5f1b4ae03c

215
main.py
View File

@@ -8,6 +8,7 @@ import json
import network import network
import ntptime import ntptime
import sh1106 import sh1106
import framebuf
# --- WiFi A ČAS PŘI STARTU --- # --- WiFi A ČAS PŘI STARTU ---
WIFI_SSID = "ZS KM" WIFI_SSID = "ZS KM"
@@ -81,7 +82,7 @@ setting_contrast = 255
setting_vibration = True setting_vibration = True
bg_buffer = bytearray(1024) bg_buffer = bytearray(1024)
custom_images = {} custom_images = {}
highscores = {"snake": 0, "pong": 0, "catch": 0, "spad": 0, "dino": 0, "sokoban": 0} highscores = {"snake": 0, "pong": 0, "catch": 0, "spad": 0, "dino": 0, "sokoban": 0, "dogger": 0}
def load_settings_and_scores(): def load_settings_and_scores():
global setting_contrast, setting_vibration, bg_buffer, custom_images, highscores global setting_contrast, setting_vibration, bg_buffer, custom_images, highscores
@@ -172,7 +173,7 @@ STATE_DINO = 7
STATE_FLAPPY = 8 STATE_FLAPPY = 8
STATE_SOKOBAN = 9 STATE_SOKOBAN = 9
STATE_DOOM = 10 STATE_DOOM = 10
STATE_TUNEL_2D = 11 STATE_DOGGER = 11
STATE_SHOOTER = 12 STATE_SHOOTER = 12
STATE_GALAGA = 13 STATE_GALAGA = 13
STATE_GAME_2048 = 14 STATE_GAME_2048 = 14
@@ -189,7 +190,7 @@ current_state = STATE_MAIN
menu_items = [ menu_items = [
"Had", "Pong", "Catch", "Spad", "Tetris", "Had", "Pong", "Catch", "Spad", "Tetris",
"Dino", "Flappy", "Sokoban", "3D Doom", "2D Tunel", "Dino", "Flappy", "Sokoban", "3D Doom", "Dogger",
"SHOOTER", "Galaga", "2048", "Breakout", "Sudoku", "SHOOTER", "Galaga", "2048", "Breakout", "Sudoku",
"Stopky", "Casovac", "Visualy", "Obrazky", "Nastaveni", "Info" "Stopky", "Casovac", "Visualy", "Obrazky", "Nastaveni", "Info"
] ]
@@ -314,7 +315,6 @@ def run_snake():
new_head = [snake[0][0] + dx, snake[0][1] + dy] new_head = [snake[0][0] + dx, snake[0][1] + dy]
# Kontrola narazu do steny nebo do vlastniho tela
if (new_head[0] < 0 or new_head[0] >= 128 or if (new_head[0] < 0 or new_head[0] >= 128 or
new_head[1] < 10 or new_head[1] >= 64 or new_head[1] < 10 or new_head[1] >= 64 or
new_head in snake): new_head in snake):
@@ -1272,70 +1272,101 @@ def run_galaga():
display.show() display.show()
time.sleep(0.03) time.sleep(0.03)
def run_2d_tunnel(): def run_dogger():
px, py = 64, 32 global highscores
if "dogger" not in highscores:
highscores["dogger"] = 0
px = 60.0
py = 48.0
score = 0 score = 0
blocks = []
spawn_timer = 0 # Seznam překážek: [x, y, velikost/šířka, rychlost padání]
obstacles = []
for _ in range(4):
obstacles.append([random.randint(0, 116), random.randint(-40, 0), random.randint(8, 16), random.uniform(1.5, 3.0)])
while True: while True:
handle_serial() handle_serial()
if not btn_menu.value(): if not btn_menu.value():
vibrate(100, 0.03) vibrate(100, 0.03)
time.sleep(0.2) time.sleep(0.2)
exit_game("2D Tunel") exit_game("Dogger")
return return
val_x = vrx.read_u16() # Čtení surové hodnoty z osy joysticku dle přesné kalibrace z nákresu
val_y = vry.read_u16() val_y = vry.read_u16()
px = int((val_x / 65535) * 122) min_val = 5000
py = int((val_y / 65535) * 44) + 2 max_val = 60000
spawn_timer += 1 val_y = max(min_val, min(max_val, val_y))
if spawn_timer > 12:
spawn_timer = 0
bx = random.randint(0, 122)
bs = random.randint(1, 3)
blocks.append([bx, 0, bs])
for b in blocks[:]: # Přesné mapování: střed joysticku je uprostřed, dorazy vedou až na samé okraje
b[1] += b[2] normalized = (max_val - val_y) / (max_val - min_val)
px = normalized * (WIDTH - 8)
if abs(px - b[0]) < 5 and abs(py - b[1]) < 5: # Postupné zrychlování překážek podle skóre
vibrate(200, 0.15) speed_multiplier = 1.0 + (score * 0.04)
display.fill(0)
display.text("GAME OVER", 28, 22, 1)
display.text(f"Skore: {score}", 28, 36, 1)
display.show()
time.sleep(2.0)
exit_game("2D Tunel")
return
if b[1] > 51: for obs in obstacles:
blocks.remove(b) obs[1] += obs[3] * speed_multiplier
if obs[1] > HEIGHT:
obs[1] = random.randint(-30, -5)
obs[0] = random.randint(0, WIDTH - int(obs[2]))
obs[2] = random.randint(8, 16)
obs[3] = random.uniform(1.5, 3.0)
score += 1 score += 1
vibrate(15, 0.01)
hit = False
player_rect = [int(px), int(py), 8, 8]
for obs in obstacles:
ox, oy, ow, _ = int(obs[0]), int(obs[1]), int(obs[2]), obs[3]
if (player_rect[0] < ox + ow and player_rect[0] + player_rect[2] > ox and
player_rect[1] < oy + 6 and player_rect[1] + player_rect[3] > oy):
hit = True
break
if hit:
vibrate(200, 0.2)
display.fill(0)
display.text("GAME OVER", 28, 16, 1)
display.text(f"Skore: {score}", 28, 30, 1)
if score > highscores.get("dogger", 0):
highscores["dogger"] = score
save_scores()
display.text("Nove Highscore!", 12, 45, 1)
else:
display.text(f"Hi-Sc: {highscores.get('dogger', 0)}", 28, 45, 1)
display.show()
time.sleep(2.5)
exit_game("Dogger")
return
display.fill(0) display.fill(0)
display.fill_rect(px, py, 6, 6, 1) display.text(f"Dogger:{score} Hi:{highscores.get('dogger', 0)}", 0, 0, 1)
display.hline(0, 9, 128, 1)
for b in blocks: for obs in obstacles:
display.fill_rect(int(b[0]), int(b[1]), 4, 4, 1) display.fill_rect(int(obs[0]), int(obs[1]), int(obs[2]), 6, 1)
display.hline(0, 52, 128, 1) ipx, ipy = int(px), int(py)
display.text(f"Skore: {score}", 2, 55, 1) display.fill_rect(ipx + 1, ipy, 6, 8, 1)
display.pixel(ipx + 2, ipy + 1, 0)
display.pixel(ipx + 5, ipy + 1, 0)
display.show() display.show()
time.sleep(0.025) time.sleep(0.03)
def run_doom_game(): def run_doom_game():
px, py, pa = 2.0, 2.0, 0.0 px, py, pa = 2.0, 2.0, 0.0
ammo = 35 ammo = 35
health = 100 health = 100
# Rozsáhlá síť chodeb a místností (bludiště)
room_lines = [ room_lines = [
# Vnější obvod
[1.0, 1.0, -2.0, 22.0, 1.0, -2.0], [1.0, 1.0, -2.0, 22.0, 1.0, -2.0],
[22.0, 1.0, -2.0, 22.0, 22.0, -2.0], [22.0, 1.0, -2.0, 22.0, 22.0, -2.0],
[22.0, 22.0, -2.0, 1.0, 22.0, -2.0], [22.0, 22.0, -2.0, 1.0, 22.0, -2.0],
@@ -1351,7 +1382,6 @@ def run_doom_game():
[22.0, 22.0, -2.0, 22.0, 22.0, 2.0], [22.0, 22.0, -2.0, 22.0, 22.0, 2.0],
[1.0, 22.0, -2.0, 1.0, 22.0, 2.0], [1.0, 22.0, -2.0, 1.0, 22.0, 2.0],
# Hustá síť vnitřních chodeb a stěn
[4.0, 1.0, -2.0, 4.0, 16.0, -2.0], [4.0, 1.0, -2.0, 4.0, 16.0, -2.0],
[4.0, 1.0, 2.0, 4.0, 16.0, 2.0], [4.0, 1.0, 2.0, 4.0, 16.0, 2.0],
@@ -1380,7 +1410,6 @@ def run_doom_game():
[8.0, 18.0, 2.0, 16.0, 18.0, 2.0], [8.0, 18.0, 2.0, 16.0, 18.0, 2.0],
] ]
# Seznam pevných kolizních úseček [x1, y1, x2, y2] pro detekci nárazů do stěn
walls = [ walls = [
[1.0, 1.0, 22.0, 1.0], [22.0, 1.0, 22.0, 22.0], [1.0, 1.0, 22.0, 1.0], [22.0, 1.0, 22.0, 22.0],
[22.0, 22.0, 1.0, 22.0], [1.0, 22.0, 1.0, 1.0], [22.0, 22.0, 1.0, 22.0], [1.0, 22.0, 1.0, 1.0],
@@ -1404,11 +1433,9 @@ def run_doom_game():
anim_timer = 0 anim_timer = 0
hit_flash = 0 hit_flash = 0
# Pomocná funkce pro kontrolu kolize úsečky pohybu se stěnami
def check_collision(x, y, r=0.25): def check_collision(x, y, r=0.25):
for w in walls: for w in walls:
x1, y1, x2, y2 = w x1, y1, x2, y2 = w
# Vzdálenost bodu od úsečky
line_len_sq = (x2 - x1)**2 + (y2 - y1)**2 line_len_sq = (x2 - x1)**2 + (y2 - y1)**2
if line_len_sq == 0: if line_len_sq == 0:
continue continue
@@ -1427,7 +1454,6 @@ def run_doom_game():
exit_game("3D Doom") exit_game("3D Doom")
return return
# Prohozené osy X a Y joysticku
turn_val = vry.read_u16() turn_val = vry.read_u16()
move_val = vrx.read_u16() move_val = vrx.read_u16()
@@ -1448,11 +1474,9 @@ def run_doom_game():
if move_step != 0: if move_step != 0:
nx = px - math.sin(pa) * move_step nx = px - math.sin(pa) * move_step
ny = py + math.cos(pa) * move_step ny = py + math.cos(pa) * move_step
# Kontrola kolize se zdi s poloměrem hráče
if not check_collision(nx, ny): if not check_collision(nx, ny):
px, py = nx, ny px, py = nx, ny
# Pohyb a střelba monster
anim_timer += 1 anim_timer += 1
for m in monsters: for m in monsters:
if not m[2]: if not m[2]:
@@ -1465,7 +1489,6 @@ def run_doom_game():
if 0.8 < dist < 7.0: if 0.8 < dist < 7.0:
step_x = (dx / dist) * 0.015 step_x = (dx / dist) * 0.015
step_y = (dy / dist) * 0.015 step_y = (dy / dist) * 0.015
# Monstra také respektují zdi
if not check_collision(m[0] + step_x, m[1] + step_y, 0.2): if not check_collision(m[0] + step_x, m[1] + step_y, 0.2):
m[0] += step_x m[0] += step_x
m[1] += step_y m[1] += step_y
@@ -1477,7 +1500,6 @@ def run_doom_game():
p_vy = (dy / dist) * 0.1 p_vy = (dy / dist) * 0.1
projectiles.append([m[0], m[1], p_vx, p_vy]) projectiles.append([m[0], m[1], p_vx, p_vy])
# Projektily
for p in projectiles[:]: for p in projectiles[:]:
p[0] += p[2] p[0] += p[2]
p[1] += p[3] p[1] += p[3]
@@ -1498,7 +1520,6 @@ def run_doom_game():
elif not (1.0 < p[0] < 22.0 and 1.0 < p[1] < 22.0): elif not (1.0 < p[0] < 22.0 and 1.0 < p[1] < 22.0):
projectiles.remove(p) projectiles.remove(p)
# Střelba hráče
if not btn_ok.value(): if not btn_ok.value():
vibrate(100, 0.03) vibrate(100, 0.03)
time.sleep(0.15) time.sleep(0.15)
@@ -1532,7 +1553,6 @@ def run_doom_game():
cos_a = math.cos(-pa) cos_a = math.cos(-pa)
sin_a = math.sin(-pa) sin_a = math.sin(-pa)
# --- VYKRESLOVÁNÍ 3D MAPY S OŘEZÁVÁNÍM ---
for line in room_lines: for line in room_lines:
x1, y1, z1, x2, y2, z2 = line x1, y1, z1, x2, y2, z2 = line
@@ -1565,7 +1585,6 @@ def run_doom_game():
display.line(sx1, sy1, sx2, sy2, 1) display.line(sx1, sy1, sx2, sy2, 1)
# --- PROJEKTILY ---
for p in projectiles: for p in projectiles:
mx, my = p[0] - px, p[1] - py mx, my = p[0] - px, p[1] - py
trz = mx * sin_a + my * cos_a trz = mx * sin_a + my * cos_a
@@ -1574,7 +1593,6 @@ def run_doom_game():
sx = int(64 + (trx / trz) * 90) sx = int(64 + (trx / trz) * 90)
display.fill_rect(sx - 1, 24, 3, 3, 1) display.fill_rect(sx - 1, 24, 3, 3, 1)
# --- MONSTRA ---
for m in monsters: for m in monsters:
if not m[2]: if not m[2]:
continue continue
@@ -1598,7 +1616,6 @@ def run_doom_game():
display.pixel(screen_x - 2, top + size//6, 0) display.pixel(screen_x - 2, top + size//6, 0)
display.pixel(screen_x + 2, top + size//6, 0) display.pixel(screen_x + 2, top + size//6, 0)
# --- HUD ---
display.hline(0, 51, 128, 1) display.hline(0, 51, 128, 1)
display.pixel(64, 25, 1) display.pixel(64, 25, 1)
display.pixel(63, 25, 1) display.pixel(63, 25, 1)
@@ -2053,10 +2070,40 @@ def run_casovac():
# --- VIZUÁLY --- # --- VIZUÁLY ---
def run_visualy(): def run_visualy():
mode = 0 mode = 0
stars = [[random.randint(0, 127), random.randint(0, 63), random.choice([1, 2])] for _ in range(25)] stars = [[random.randint(0, 127), random.randint(0, 63), random.choice([1, 2])] for _ in range(20)]
strip_pos = 0 strip_pos = 0
circle_r = 0 circle_r = 0
last_btn_ok = 1
# DVD logo screensaver (velikost 52x21 pixelů)[cite: 7]
dvd_x = 38.0
dvd_y = 21.0
dvd_vx = 1.2
dvd_vy = 1.0
dvd_w = 52
dvd_h = 21
# Vytvoření bufferu a FrameBufferu pro DVD logo (inverzní barvy: černé pozadí, bílé logo)
dvd_buf = bytearray((dvd_w * dvd_h + 7) // 8)
dvd_fb = framebuf.FrameBuffer(dvd_buf, dvd_w, dvd_h, framebuf.MONO_HLSB)
# Vykreslení DVD loga do bufferu (inverzně: pozadí 0, logo 1)
dvd_fb.fill(0)
# Horní nápis DVD (věrný tvar podle pixel-artové předlohy)
dvd_fb.fill_rect(2, 1, 12, 9, 1)
dvd_fb.fill_rect(5, 3, 6, 5, 0)
dvd_fb.fill_rect(17, 1, 4, 7, 1)
dvd_fb.fill_rect(21, 6, 5, 2, 1)
dvd_fb.fill_rect(26, 1, 4, 7, 1)
dvd_fb.fill_rect(32, 1, 12, 9, 1)
dvd_fb.fill_rect(35, 3, 6, 5, 0)
# Spodní disk
dvd_fb.fill_rect(0, 13, 52, 8, 1)
dvd_fb.fill_rect(21, 15, 10, 4, 0)
last_nav_time = 0
while current_state == STATE_VISUALY: while current_state == STATE_VISUALY:
handle_serial() handle_serial()
@@ -2066,12 +2113,18 @@ def run_visualy():
exit_game("Visualy") exit_game("Visualy")
return return
b_ok = btn_ok.value() # Plynulé přepínání vizuálů pomocí osy vry na joysticku
if last_btn_ok == 1 and b_ok == 0: now = time.ticks_ms()
mode = (mode + 1) % 3 if time.ticks_diff(now, last_nav_time) > 300:
vibrate(100, 0.03) y_val = vry.read_u16()
time.sleep(0.2) if y_val < 15000:
last_btn_ok = b_ok mode = (mode - 1) % 4
vibrate(50, 0.02)
last_nav_time = now
elif y_val > 50000:
mode = (mode + 1) % 4
vibrate(50, 0.02)
last_nav_time = now
display.fill(0) display.fill(0)
@@ -2082,7 +2135,6 @@ def run_visualy():
s[0] = 0 s[0] = 0
s[1] = random.randint(0, 63) s[1] = random.randint(0, 63)
display.pixel(s[0], s[1], 1) display.pixel(s[0], s[1], 1)
display.text("Hvezdy (OK=dalsi)", 2, 2, 1)
elif mode == 1: elif mode == 1:
strip_pos = (strip_pos + 2) % 32 strip_pos = (strip_pos + 2) % 32
@@ -2090,7 +2142,6 @@ def run_visualy():
x_start = i + strip_pos x_start = i + strip_pos
display.line(x_start, 0, x_start + 32, 63, 1) display.line(x_start, 0, x_start + 32, 63, 1)
display.line(x_start + 4, 0, x_start + 36, 63, 1) display.line(x_start + 4, 0, x_start + 36, 63, 1)
display.text("Pasky (OK=dalsi)", 2, 2, 1)
elif mode == 2: elif mode == 2:
circle_r += 2 circle_r += 2
@@ -2098,7 +2149,39 @@ def run_visualy():
circle_r = 0 circle_r = 0
for r in range(0, circle_r, 8): for r in range(0, circle_r, 8):
display.rect(64 - r, 32 - (r//2), r * 2, r, 1) display.rect(64 - r, 32 - (r//2), r * 2, r, 1)
display.text("Kruhy (OK=dalsi)", 2, 2, 1)
elif mode == 3:
dvd_x += dvd_vx
dvd_y += dvd_vy
hit_corner = False
if dvd_x <= 0:
dvd_x = 0
dvd_vx = -dvd_vx
if dvd_y <= 0 or dvd_y + dvd_h >= HEIGHT:
hit_corner = True
elif dvd_x + dvd_w >= WIDTH:
dvd_x = WIDTH - dvd_w
dvd_vx = -dvd_vx
if dvd_y <= 0 or dvd_y + dvd_h >= HEIGHT:
hit_corner = True
if dvd_y <= 0:
dvd_y = 0
dvd_vy = -dvd_vy
if dvd_x <= 0 or dvd_x + dvd_w >= WIDTH:
hit_corner = True
elif dvd_y + dvd_h >= HEIGHT:
dvd_y = HEIGHT - dvd_h
dvd_vy = -dvd_vy
if dvd_x <= 0 or dvd_x + dvd_w >= WIDTH:
hit_corner = True
if hit_corner:
vibrate(150, 0.1)
# Vykreslení DVD loga (inverzní pro OLED)
display.blit(dvd_fb, int(dvd_x), int(dvd_y))
display.show() display.show()
time.sleep(0.04) time.sleep(0.04)
@@ -2310,7 +2393,7 @@ while True:
elif selected_index == 6: current_state = STATE_FLAPPY elif selected_index == 6: current_state = STATE_FLAPPY
elif selected_index == 7: current_state = STATE_SOKOBAN elif selected_index == 7: current_state = STATE_SOKOBAN
elif selected_index == 8: current_state = STATE_DOOM elif selected_index == 8: current_state = STATE_DOOM
elif selected_index == 9: current_state = STATE_TUNEL_2D elif selected_index == 9: current_state = STATE_DOGGER
elif selected_index == 10: current_state = STATE_SHOOTER elif selected_index == 10: current_state = STATE_SHOOTER
elif selected_index == 11: current_state = STATE_GALAGA elif selected_index == 11: current_state = STATE_GALAGA
elif selected_index == 12: current_state = STATE_GAME_2048 elif selected_index == 12: current_state = STATE_GAME_2048
@@ -2351,7 +2434,7 @@ while True:
elif current_state == STATE_FLAPPY: run_flappy() elif current_state == STATE_FLAPPY: run_flappy()
elif current_state == STATE_SOKOBAN: run_sokoban() elif current_state == STATE_SOKOBAN: run_sokoban()
elif current_state == STATE_DOOM: run_doom_game() elif current_state == STATE_DOOM: run_doom_game()
elif current_state == STATE_TUNEL_2D: run_2d_tunnel() elif current_state == STATE_DOGGER: run_dogger()
elif current_state == STATE_SHOOTER: run_shooter() elif current_state == STATE_SHOOTER: run_shooter()
elif current_state == STATE_GALAGA: run_galaga() elif current_state == STATE_GALAGA: run_galaga()
elif current_state == STATE_GAME_2048: run_2048() elif current_state == STATE_GAME_2048: run_2048()