from machine import Pin, ADC, I2C, PWM import time import math import random import sys uselect = sys.modules.get('uselect') or __import__('uselect') import json import network import ntptime import sh1106 import framebuf # --- WiFi A ČAS PŘI STARTU --- WIFI_SSID = "ZS KM" WIFI_PASS = "KMilicovu674*" device_ip = "0.0.0.0" def init_wifi_and_time(): global device_ip wlan = network.WLAN(network.STA_IF) wlan.active(True) wlan.connect(WIFI_SSID, WIFI_PASS) start_wait = time.ticks_ms() while not wlan.isconnected(): if time.ticks_diff(time.ticks_ms(), start_wait) > 8000: break time.sleep(0.5) if wlan.isconnected(): try: device_ip = wlan.ifconfig()[0] except: pass try: ntptime.settime() except: pass wlan.disconnect() wlan.active(False) init_wifi_and_time() # --- HARDWAROVÁ INICIALIZACE --- i2c = I2C(0, sda=Pin(4), scl=Pin(5), freq=400000) WIDTH = 128 HEIGHT = 64 display = sh1106.SH1106_I2C(WIDTH, HEIGHT, i2c, addr=0x3c) vrx = ADC(Pin(26)) vry = ADC(Pin(27)) btn_ok = Pin(22, Pin.IN, Pin.PULL_UP) btn_menu = Pin(21, Pin.IN, Pin.PULL_UP) battery_adc = ADC(Pin(28)) BAT_FACTOR = (3.3 / 65535) * 2 * (3.80 / 3.87) # --- VIBRAČNÍ MOTOR (PWM na GP16) --- vibrator = None try: vibrator = PWM(Pin(16)) vibrator.freq(1000) vibrator.duty_u16(0) except: pass def vibrate(percent, duration_sec): global setting_vibration if not setting_vibration or not vibrator: return try: duty = int((percent / 100.0) * 65535) vibrator.duty_u16(duty) time.sleep(duration_sec) vibrator.duty_u16(0) except: pass # --- NASTAVENÍ A SKÓRE --- setting_contrast = 255 setting_vibration = True bg_buffer = bytearray(1024) custom_images = {} highscores = {"snake": 0, "pong": 0, "catch": 0, "spad": 0, "dino": 0, "sokoban": 0, "dogger": 0} def load_settings_and_scores(): global setting_contrast, setting_vibration, bg_buffer, custom_images, highscores try: with open("settings.json", "r") as f: data = json.load(f) setting_contrast = data.get("contrast", 255) setting_vibration = data.get("vibration", True) except: pass try: with open("scores.json", "r") as f: saved_scores = json.load(f) highscores.update(saved_scores) except: pass try: with open("background.json", "r") as f: data = json.load(f) hex_bg = data.get("bg", "") if hex_bg: bg_buffer[:] = bytes.fromhex(hex_bg) except: pass try: with open("images.json", "r") as f: saved_imgs = json.load(f) for name, hex_str in saved_imgs.items(): custom_images[name] = bytes.fromhex(hex_str) except: pass def save_settings(): try: with open("settings.json", "w") as f: json.dump({"contrast": setting_contrast, "vibration": setting_vibration}, f) except: pass def save_scores(): try: with open("scores.json", "w") as f: json.dump(highscores, f) except: pass def save_background(hex_data): try: with open("background.json", "w") as f: json.dump({"bg": hex_data}, f) except: pass def save_all_images(): try: data_to_save = {name: b.hex() for name, b in custom_images.items()} with open("images.json", "w") as f: json.dump(data_to_save, f) except: pass load_settings_and_scores() def set_contrast(val): global setting_contrast setting_contrast = max(1, min(255, val)) try: display.contrast(setting_contrast) except: display.write_cmd(0x81) display.write_cmd(setting_contrast) save_settings() set_contrast(setting_contrast) # --- STAVY A MENU --- STATE_MAIN = 0 STATE_MENU = 1 STATE_SNAKE = 2 STATE_PONG = 3 STATE_CATCH = 4 STATE_SPAD = 5 STATE_TETRIS = 6 STATE_DINO = 7 STATE_FLAPPY = 8 STATE_SOKOBAN = 9 STATE_DOOM = 10 STATE_DOGGER = 11 STATE_SHOOTER = 12 STATE_GALAGA = 13 STATE_GAME_2048 = 14 STATE_BREAKOUT = 15 STATE_SUDOKU = 16 STATE_STOPKY = 17 STATE_CASOVAC = 18 STATE_VISUALY = 19 STATE_IMAGES = 20 STATE_VIEW_IMAGE = 21 STATE_SETTINGS = 22 STATE_INFO = 23 current_state = STATE_MAIN menu_items = [ "Had", "Pong", "Catch", "Spad", "Tetris", "Dino", "Flappy", "Sokoban", "3D Doom", "Dogger", "SHOOTER", "Galaga", "2048", "Breakout", "Sudoku", "Stopky", "Casovac", "Visualy", "Obrazky", "Nastaveni", "Info" ] selected_index = 0 last_input_time = 0 selected_image_name = "" start_ticks = time.ticks_ms() # --- VYHLAZENÍ BATERIE --- bat_samples = [] smoothed_pct = -1 def read_battery(): return battery_adc.read_u16() * BAT_FACTOR def get_battery_percent(): global bat_samples, smoothed_pct v = read_battery() pct = int((v - 3.0) / 1.2 * 100) pct = max(0, min(100, pct)) bat_samples.append(pct) if len(bat_samples) > 10: bat_samples.pop(0) avg_pct = int(sum(bat_samples) / len(bat_samples)) if smoothed_pct == -1: smoothed_pct = avg_pct elif abs(avg_pct - smoothed_pct) >= 2: smoothed_pct = avg_pct return smoothed_pct def get_chip_temp(): try: sensor = ADC(4) reading = sensor.read_u16() * (3.3 / 65535) temperature = 27 - (reading - 0.706) / 0.001721 return temperature except: return 25.0 def get_menu_input(max_len): global selected_index, last_input_time now = time.ticks_ms() if time.ticks_diff(now, last_input_time) < 200: return x = vrx.read_u16() if x < 15000: selected_index = (selected_index - 1) % max_len last_input_time = now vibrate(100, 0.03) elif x > 50000: selected_index = (selected_index + 1) % max_len last_input_time = now vibrate(100, 0.03) spoll = uselect.poll() spoll.register(sys.stdin, uselect.POLLIN) def handle_serial(): global bg_buffer, custom_images if spoll.poll(0): line = sys.stdin.readline() if line: line = line.strip() if line.startswith("IMG:"): try: parts = line[4:].split(":", 1) img_name = parts[0] hex_data = parts[1] img_bytes = bytes.fromhex(hex_data) custom_images[img_name] = img_bytes save_all_images() print("OK:IMG_SAVED") except Exception as e: print(f"ERR:IMG:{e}") elif line.startswith("BG_SET:"): try: hex_data = line[7:].strip() bg_buffer[:] = bytes.fromhex(hex_data) save_background(hex_data) print("OK:BG_SET") except Exception as e: print(f"ERR:BG:{e}") elif line == "GET_INFO": runtime_sec = time.ticks_diff(time.ticks_ms(), start_ticks) // 1000 temp = get_chip_temp() bat = read_battery() print(f"INFO_RES:{device_ip},{temp:.1f},{bat:.2f},{runtime_sec}") def exit_game(game_name): global current_state, selected_index vibrate(30, 0.5) if game_name in menu_items: selected_index = menu_items.index(game_name) current_state = STATE_MENU # --- JEDNOTLIVÉ HRY A APLIKACE --- def run_snake(): global highscores snake = [[32, 32], [28, 32], [24, 32]] dx, dy = 4, 0 food = [random.randint(0, 31) * 4, random.randint(3, 15) * 4] score = 0 while True: handle_serial() if not btn_menu.value(): vibrate(100, 0.03) time.sleep(0.2) exit_game("Had") return x_val = vrx.read_u16() y_val = vry.read_u16() if x_val < 15000 and dy == 0: dx, dy = 0, -4 elif x_val > 50000 and dy == 0: dx, dy = 0, 4 elif y_val < 15000 and dx == 0: dx, dy = 4, 0 elif y_val > 50000 and dx == 0: dx, dy = -4, 0 new_head = [snake[0][0] + dx, snake[0][1] + dy] if (new_head[0] < 0 or new_head[0] >= 128 or new_head[1] < 10 or new_head[1] >= 64 or new_head in snake): 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("snake", 0): highscores["snake"] = score save_scores() display.text("Nove Highscore!", 12, 45, 1) else: display.text(f"Hi-Sc: {highscores.get('snake', 0)}", 28, 45, 1) display.show() time.sleep(2.5) exit_game("Had") return if abs(new_head[0] - food[0]) < 4 and abs(new_head[1] - food[1]) < 4: score += 1 vibrate(100, 0.03) food = [random.randint(0, 31) * 4, random.randint(3, 15) * 4] else: snake.pop() snake.insert(0, new_head) display.fill(0) display.text(f"Sk:{score} Hi:{highscores.get('snake', 0)}", 0, 0, 1) display.hline(0, 9, 128, 1) display.fill_rect(food[0], food[1], 3, 3, 1) for seg in snake: display.fill_rect(seg[0], seg[1], 3, 3, 1) display.show() time.sleep(0.18) def run_pong(): global highscores paddle_y = 24 ball_x, ball_y = 64.0, 32.0 ball_dx, ball_dy = 2.0, 2.0 score = 0 while True: handle_serial() if not btn_menu.value(): vibrate(100, 0.03) time.sleep(0.2) exit_game("Pong") return x_val = vrx.read_u16() if x_val < 15000: paddle_y = max(10, paddle_y - 4) elif x_val > 50000: paddle_y = min(50, paddle_y + 4) ball_x += ball_dx ball_y += ball_dy if ball_y <= 10: ball_y = 10 ball_dy = -ball_dy elif ball_y >= 62: ball_y = 62 ball_dy = -ball_dy if ball_x >= 124: ball_x = 124 ball_dx = -ball_dx if ball_x <= 6: if paddle_y <= ball_y <= paddle_y + 14: ball_dx = abs(ball_dx) ball_x = 6 score += 1 vibrate(100, 0.03) speed_inc = 1.05 ball_dx *= speed_inc ball_dy *= speed_inc else: display.fill(0) display.text("GAME OVER", 28, 16, 1) display.text(f"Skore: {score}", 32, 30, 1) if score > highscores.get("pong", 0): highscores["pong"] = score save_scores() display.text("Nove Highscore!", 12, 45, 1) else: display.text(f"Hi-Sc: {highscores.get('pong', 0)}", 28, 45, 1) display.show() time.sleep(2.5) exit_game("Pong") return display.fill(0) display.text(f"Pong Sc:{score} Hi:{highscores.get('pong', 0)}", 0, 0, 1) display.hline(0, 9, 128, 1) display.fill_rect(2, paddle_y, 3, 14, 1) display.fill_rect(int(ball_x), int(ball_y), 3, 3, 1) display.show() time.sleep(0.04) def run_catch(): global highscores player_y = 32 drop_x = 120.0 drop_y = random.randint(12, 56) score = 0 while True: handle_serial() if not btn_menu.value(): vibrate(100, 0.03) time.sleep(0.2) exit_game("Catch") return x_val = vrx.read_u16() if x_val < 15000: player_y = max(12, player_y - 4) elif x_val > 50000: player_y = min(56, player_y + 4) drop_speed = 3.0 + (score * 0.3) drop_x -= drop_speed if drop_x <= 0: if player_y <= drop_y <= player_y + 16: score += 1 vibrate(100, 0.03) else: display.fill(0) display.text("GAME OVER", 28, 16, 1) display.text(f"Skore: {score}", 32, 30, 1) if score > highscores.get("catch", 0): highscores["catch"] = score save_scores() display.text("Nove Highscore!", 12, 45, 1) else: display.text(f"Hi-Sc: {highscores.get('catch', 0)}", 28, 45, 1) display.show() time.sleep(2.5) exit_game("Catch") return drop_x = 120.0 drop_y = random.randint(12, 56) display.fill(0) display.text(f"Catch:{score} Hi:{highscores.get('catch', 0)}", 0, 0, 1) display.hline(0, 9, 128, 1) display.fill_rect(int(drop_x), drop_y, 3, 3, 1) display.fill_rect(4, player_y, 3, 16, 1) display.show() time.sleep(0.04) def run_spad(): global highscores px, py, vy, score = 64.0, 15.0, 0.0, 0 platforms = [ [random.randint(0, 88), 60, 40, False, 0.0], [random.randint(0, 88), 40, 40, True, 20.0], [random.randint(0, 88), 20, 40, False, 0.0] ] while True: handle_serial() if not btn_menu.value(): vibrate(100, 0.03) time.sleep(0.2) exit_game("Spad") return y_val = vry.read_u16() if y_val < 15000: px = min(WIDTH - 8, px + 3.5) elif y_val > 50000: px = max(0, px - 3.5) base_speed = 0.8 + (score * 0.05) vy += 0.4 py += vy for p in platforms: p[1] -= base_speed if p[3]: snail_abs_x = p[0] + p[4] if snail_abs_x < px: p[4] = min(p[2] - 8, p[4] + 0.4 + (score * 0.02)) elif snail_abs_x > px: p[4] = max(0, p[4] - (0.4 + (score * 0.02))) if p[1] < 10: p[1] = 63 p[0] = random.randint(0, WIDTH - 40) p[3] = random.choice([True, False]) p[4] = random.uniform(5, 25) score += 1 on_ground = False for p in platforms: plat_x1 = p[0] plat_x2 = p[0] + p[2] plat_y = p[1] player_bottom = py + 8 player_center_x = px + 4 if vy > 0 and (plat_y <= player_bottom <= plat_y + 4) and (plat_x1 - 2 <= player_center_x <= plat_x2 + 2): py = plat_y - 8 vy = 0 on_ground = True if p[3]: snail_abs_x = p[0] + p[4] if abs(player_center_x - (snail_abs_x + 4)) < 6: p[3] = False vy = -3.5 score += 5 vibrate(100, 0.04) if p[3]: snail_abs_x = p[0] + p[4] snail_y = p[1] - 5 if abs(player_center_x - (snail_abs_x + 4)) < 6 and (snail_y <= py + 8 and py <= snail_y + 5): if not (vy > 0 and py + 8 <= plat_y): display.fill(0) display.text("GAME OVER", 28, 16, 1) display.text(f"Skore: {score}", 32, 30, 1) if score > highscores.get("spad", 0): highscores["spad"] = score save_scores() display.text("Nove Highscore!", 12, 45, 1) else: display.text(f"Hi-Sc: {highscores.get('spad', 0)}", 28, 45, 1) display.show() time.sleep(2.5) exit_game("Spad") return if py < 10: py = 11 vy = 2.0 if py > HEIGHT: display.fill(0) display.text("GAME OVER", 28, 16, 1) display.text(f"Skore: {score}", 32, 30, 1) if score > highscores.get("spad", 0): highscores["spad"] = score save_scores() display.text("Nove Highscore!", 12, 45, 1) else: display.text(f"Hi-Sc: {highscores.get('spad', 0)}", 28, 45, 1) display.show() time.sleep(2.5) exit_game("Spad") return display.fill(0) display.text(f"Spad:{score} Hi:{highscores.get('spad', 0)}", 0, 0, 1) display.hline(0, 9, 128, 1) for p in platforms: display.hline(int(p[0]), int(p[1]), int(p[2]), 1) if p[3]: sx = int(p[0] + p[4]) sy = int(p[1] - 5) display.fill_rect(sx, sy, 7, 5, 1) display.pixel(sx + 1, sy - 1, 1) display.pixel(sx + 5, sy - 1, 1) display.pixel(sx + 3, sy + 2, 0) ipx, ipy = int(px), int(py) display.fill_rect(ipx + 1, ipy, 6, 8, 1) display.pixel(ipx + 2, ipy + 1, 0) display.pixel(ipx + 5, ipy + 1, 0) display.pixel(ipx, ipy + 3, 1) display.pixel(ipx + 7, ipy + 3, 1) display.show() time.sleep(0.03) def run_tetris(): global highscores grid = [[0 for _ in range(10)] for _ in range(16)] shapes = [ [[1,1,1,1]], [[1,1],[1,1]], [[0,1,0],[1,1,1]], [[1,0],[1,0],[1,1]], [[0,1,1],[1,1,0]], [[1,1,0],[0,1,1]], [[1,0,0],[1,1,1]] ] cur_shape = random.choice(shapes) next_shape = random.choice(shapes) cx, cy = 3, 0 score = 0 lines = 0 drop_delay = 14 drop_counter = 0 last_move_time = 0 last_rotate_time = 0 def check_collision(shape, px, py): for r in range(len(shape)): for c in range(len(shape[0])): if shape[r][c]: nx = px + c ny = py + r if nx < 0 or nx >= 10 or ny >= 16: return True if ny >= 0 and grid[ny][nx]: return True return False while True: handle_serial() if not btn_menu.value(): vibrate(100, 0.03) time.sleep(0.2) exit_game("Tetris") return now = time.ticks_ms() x_val = vry.read_u16() y_val = vrx.read_u16() if time.ticks_diff(now, last_move_time) > 120: if x_val < 15000: if not check_collision(cur_shape, cx + 1, cy): cx += 1 last_move_time = now elif x_val > 50000: if not check_collision(cur_shape, cx - 1, cy): cx -= 1 last_move_time = now if y_val < 15000: if time.ticks_diff(now, last_rotate_time) > 350: rotated = [list(row) for row in zip(*cur_shape[::-1])] if not check_collision(rotated, cx, cy): cur_shape = rotated vibrate(50, 0.01) last_rotate_time = now is_fast_drop = (y_val > 50000) effective_delay = 2 if is_fast_drop else drop_delay drop_counter += 1 if drop_counter >= effective_delay: drop_counter = 0 if not check_collision(cur_shape, cx, cy + 1): cy += 1 else: for r in range(len(cur_shape)): for c in range(len(cur_shape[0])): if cur_shape[r][c]: ny = cy + r nx = cx + c if 0 <= ny < 16 and 0 <= nx < 10: grid[ny][nx] = 1 full_rows = [i for i, row in enumerate(grid) if all(row)] for r_idx in full_rows: grid.pop(r_idx) grid.insert(0, [0 for _ in range(10)]) lines += 1 score += 100 if full_rows: vibrate(100, 0.03) cur_shape = next_shape next_shape = random.choice(shapes) cx, cy = 3, 0 if check_collision(cur_shape, cx, cy): display.fill(0) display.text("GAME OVER", 28, 20, 1) display.text(f"Skore:{score}", 28, 35, 1) display.show() time.sleep(2.5) exit_game("Tetris") return display.fill(0) display.text("SC:", 2, 2, 1) display.text(str(score), 2, 12, 1) display.text("LAY:", 2, 26, 1) display.text(str(lines), 2, 36, 1) display.rect(35, 9, 32, 50, 1) for r in range(16): for c in range(10): if grid[r][c]: display.fill_rect(36 + c * 3, 10 + r * 3, 2, 2, 1) for r in range(len(cur_shape)): for c in range(len(cur_shape[0])): if cur_shape[r][c]: px = 36 + (cx + c) * 3 py = 10 + (cy + r) * 3 if py >= 10: display.fill_rect(px, py, 2, 2, 1) display.text("NEXT", 75, 2, 1) display.rect(74, 12, 22, 22, 1) for r in range(len(next_shape)): for c in range(len(next_shape[0])): if next_shape[r][c]: display.fill_rect(78 + c * 3, 16 + r * 3, 2, 2, 1) display.show() time.sleep(0.03) def run_dino(): global highscores if "dino" not in highscores: highscores["dino"] = 0 dino_y = 44.0 dino_vy = 0.0 is_jumping = False score = 0 obstacles = [[128.0, random.choice([0, 1, 2])]] obs_speed = 3.5 anim_counter = 0 while True: handle_serial() if not btn_menu.value(): vibrate(100, 0.03) time.sleep(0.2) exit_game("Dino") return x_val = vrx.read_u16() if (not btn_ok.value() or x_val < 15000) and not is_jumping: vibrate(100, 0.03) dino_vy = -7.0 is_jumping = True dino_vy += 0.8 dino_y += dino_vy if dino_y >= 44: dino_y = 44.0 is_jumping = False dino_vy = 0.0 current_speed = obs_speed + (score * 0.05) for obs in obstacles: obs[0] -= current_speed if obstacles and obstacles[0][0] < -15: obstacles.pop(0) score += 1 vibrate(20, 0.01) if not obstacles or obstacles[-1][0] < 128.0 - random.randint(45, 95): obstacles.append([128.0, random.choice([0, 1, 2])]) anim_counter += 1 anim_frame = (anim_counter // 5) % 2 dino_x1, dino_x2 = 17, 27 dino_y1, dino_y2 = int(dino_y) + 1, int(dino_y) + 11 hit = False for obs in obstacles: ox = int(obs[0]) otype = obs[1] if otype == 0: obs_x1, obs_x2 = ox, ox + 6 obs_y1, obs_y2 = 44, 56 elif otype == 1: obs_x1, obs_x2 = ox, ox + 13 obs_y1, obs_y2 = 50, 56 elif otype == 2: obs_x1, obs_x2 = ox, ox + 9 obs_y1, obs_y2 = 45, 56 if (dino_x2 > obs_x1 and dino_x1 < obs_x2 and dino_y2 > obs_y1 and dino_y1 < obs_y2): hit = True break if hit: display.fill(0) display.text("GAME OVER", 28, 16, 1) display.text(f"Skore: {score}", 32, 30, 1) if score > highscores.get("dino", 0): highscores["dino"] = score save_scores() display.text("Nove Highscore!", 12, 45, 1) else: display.text(f"Hi-Sc: {highscores.get('dino', 0)}", 28, 45, 1) display.show() time.sleep(2.5) exit_game("Dino") return display.fill(0) display.text(f"Dino Sc:{score} Hi:{highscores.get('dino', 0)}", 0, 0, 1) display.hline(0, 9, 128, 1) display.hline(0, 56, 128, 1) dy = int(dino_y) display.fill_rect(20, dy + 2, 8, 8, 1) display.pixel(26, dy + 1, 1) display.pixel(27, dy + 1, 1) display.pixel(25, dy + 2, 0) display.pixel(18, dy + 4, 1) display.pixel(17, dy + 5, 1) if is_jumping: display.pixel(21, dy + 10, 1) display.pixel(25, dy + 10, 1) else: if anim_frame == 0: display.pixel(21, dy + 10, 1) display.pixel(21, dy + 11, 1) display.pixel(25, dy + 10, 1) else: display.pixel(20, dy + 10, 1) display.pixel(26, dy + 10, 1) display.pixel(26, dy + 11, 1) for obs in obstacles: ox = int(obs[0]) otype = obs[1] if otype == 0: display.fill_rect(ox + 2, 44, 2, 12, 1) display.pixel(ox, 47, 1) display.pixel(ox + 1, 47, 1) display.pixel(ox, 46, 1) display.pixel(ox + 4, 50, 1) display.pixel(ox + 5, 50, 1) display.pixel(ox + 5, 49, 1) elif otype == 1: for i in range(3): cx = ox + (i * 5) display.fill_rect(cx + 1, 50, 2, 6, 1) display.pixel(cx, 52, 1) display.pixel(cx + 3, 53, 1) elif otype == 2: display.fill_rect(ox + 1, 45, 3, 11, 1) display.fill_rect(ox + 6, 47, 3, 9, 1) display.pixel(ox, 48, 1) display.pixel(ox + 9, 50, 1) display.show() time.sleep(0.03) def run_flappy(): bird_y = 32.0 bird_vy = 0.0 pipe_x = 128.0 gap_size = 24 pipe_h = random.randint(15, 30) score = 0 last_jump_time = 0 while True: handle_serial() if not btn_menu.value(): vibrate(100, 0.03) time.sleep(0.2) exit_game("Flappy") return now = time.ticks_ms() if not btn_ok.value() or vrx.read_u16() < 15000: if time.ticks_diff(now, last_jump_time) > 150: vibrate(50, 0.02) bird_vy = -3.8 last_jump_time = now bird_vy += 0.4 bird_y += bird_vy pipe_x -= 3.0 if pipe_x < -14: pipe_x = 128.0 pipe_h = random.randint(15, 30) score += 1 vibrate(50, 0.01) by = int(bird_y) px = int(pipe_x) top_pipe_bottom = 10 + pipe_h bottom_pipe_top = top_pipe_bottom + gap_size if by <= 10 or by + 6 >= 63: display.fill(0) display.text("GAME OVER", 28, 20, 1) display.text(f"Skore: {score}", 28, 35, 1) display.show() time.sleep(2.0) exit_game("Flappy") return bird_x1, bird_x2 = 18, 26 bird_y1, bird_y2 = by + 1, by + 6 pipe_x1, pipe_x2 = px, px + 12 if bird_x2 > pipe_x1 and bird_x1 < pipe_x2: if bird_y1 <= top_pipe_bottom or bird_y2 >= bottom_pipe_top: display.fill(0) display.text("GAME OVER", 28, 20, 1) display.text(f"Skore: {score}", 28, 35, 1) display.show() time.sleep(2.0) exit_game("Flappy") return display.fill(0) display.text(f"Flappy:{score}", 0, 0, 1) display.hline(0, 9, 128, 1) display.fill_rect(px + 1, 10, 10, pipe_h - 2, 1) display.fill_rect(px, 10 + pipe_h - 2, 12, 2, 1) display.fill_rect(px, bottom_pipe_top, 12, 2, 1) display.fill_rect(px + 1, bottom_pipe_top + 2, 10, 64 - (bottom_pipe_top + 2), 1) display.fill_rect(18, by + 1, 6, 4, 1) display.pixel(24, by + 2, 1) display.pixel(25, by + 2, 1) display.pixel(22, by + 1, 0) display.pixel(19, by + 5, 1) display.show() time.sleep(0.03) def run_sokoban(): levels = [ { "map": [ "########", "#@ B . #", "########" ], "player": [1, 1] }, { "map": [ "########", "#@ B .#", "# # . #", "########" ], "player": [1, 1] }, { "map": [ "##########", "# # #", "#@ B B . #", "# B . #", "# B . . #", "##########" ], "player": [1, 2] }, { "map": [ "#########", "# . # #", "# . B @ #", "# # #", "#########" ], "player": [6, 2] }, { "map": [ "##########", "# # #", "# B . #", "# @# . #", "# B . #", "##########" ], "player": [2, 3] }, { "map": [ "##########", "# ### #", "# @ B .# #", "# # . #", "##########" ], "player": [3, 2] }, { "map": [ "###########", "# # #", "# B . B #", "# @# # #", "# . . #", "###########" ], "player": [2, 3] }, { "map": [ "###########", "# ##### #", "# # # #", "# B . B #", "# @# # #", "# . . #", "###########" ], "player": [3, 4] }, { "map": [ "############", "# ### #", "# # .# #", "# B B @ #", "# . . #", "############" ], "player": [9, 3] }, { "map": [ "############", "# ###### #", "# # # #", "# B . B# #", "# @# . # #", "# ###### #", "############" ], "player": [3, 4] } ] lvl_idx = 0 while lvl_idx < len(levels) and current_state == STATE_SOKOBAN: data = levels[lvl_idx] initial_map = data["map"] initial_px = data["player"][0] initial_py = data["player"][1] grid = [list(row) for row in initial_map] px, py = initial_px, initial_py grid[py][px] = " " history = [] last_input = 0 last_ok_press = time.ticks_ms() ok_was_pressed = False while True: handle_serial() if not btn_menu.value(): vibrate(100, 0.03) time.sleep(0.2) exit_game("Sokoban") return now = time.ticks_ms() b_ok = btn_ok.value() if b_ok == 0 and ok_was_pressed: if time.ticks_diff(now, last_ok_press) > 1000: vibrate(150, 0.1) grid = [list(row) for row in initial_map] px, py = initial_px, initial_py grid[py][px] = " " history = [] ok_was_pressed = False time.sleep(0.3) elif b_ok == 0 and not ok_was_pressed: last_ok_press = now ok_was_pressed = True elif b_ok == 1 and ok_was_pressed: if time.ticks_diff(now, last_ok_press) < 800: if history: prev_state = history.pop() grid = [list(row) for row in prev_state["grid"]] px, py = prev_state["px"], prev_state["py"] vibrate(50, 0.02) ok_was_pressed = False time.sleep(0.15) dx, dy = 0, 0 if time.ticks_diff(now, last_input) > 200: x_v, y_v = vrx.read_u16(), vry.read_u16() if y_v < 15000: dx = 1; last_input = now elif y_v > 50000: dx = -1; last_input = now elif x_v < 15000: dy = -1; last_input = now elif x_v > 50000: dy = 1; last_input = now if dx != 0 or dy != 0: nx, ny = px + dx, py + dy if 0 <= ny < len(grid) and 0 <= nx < len(grid[ny]) and grid[ny][nx] != "#": if grid[ny][nx] in ("B", "*"): nnx, nny = nx + dx, ny + dy if 0 <= nny < len(grid) and 0 <= nnx < len(grid[nny]) and grid[nny][nnx] in (" ", "."): grid_copy = [list(r) for r in grid] history.append({"grid": grid_copy, "px": px, "py": py}) grid[ny][nx] = " " if grid[ny][nx] == "B" else "." grid[nny][nnx] = "B" if grid[nny][nnx] == " " else "*" px, py = nx, ny vibrate(100, 0.03) elif grid[ny][nx] in (" ", "."): grid_copy = [list(r) for r in grid] history.append({"grid": grid_copy, "px": px, "py": py}) px, py = nx, ny vibrate(100, 0.03) if not any("B" in r for r in grid): lvl_idx += 1 vibrate(150, 0.2) display.fill(0) display.text("LEVEL SKONCEN!", 16, 25, 1) display.show() time.sleep(1.2) break display.fill(0) display.text(f"Sokoban L{lvl_idx+1}/10", 2, 0, 1) display.hline(0, 9, 128, 1) for r in range(len(grid)): for c in range(len(grid[r])): ch = grid[r][c] box_x = 20 + c * 7 box_y = 12 + r * 7 if ch == "#": display.fill_rect(box_x, box_y, 6, 6, 1) display.pixel(box_x + 3, box_y + 3, 0) elif ch == ".": display.pixel(box_x + 3, box_y + 3, 1) elif ch == "B": display.rect(box_x, box_y, 6, 6, 1) elif ch == "*": display.fill_rect(box_x, box_y, 6, 6, 1) display.pixel(box_x + 1, box_y + 1, 0) display.pixel(box_x + 4, box_y + 1, 0) display.pixel(box_x + 1, box_y + 4, 0) display.pixel(box_x + 4, box_y + 4, 0) player_x = 20 + px * 7 player_y = 12 + py * 7 display.fill_rect(player_x + 1, player_y, 4, 6, 1) display.pixel(player_x + 2, player_y + 1, 0) display.pixel(player_x + 3, player_y + 1, 0) display.show() time.sleep(0.03) exit_game("Sokoban") def run_galaga(): ship_x = 60 ship_y = 52 bullets = [] enemies = [] score = 0 spawn_timer = 0 for row in range(2): for col in range(6): enemies.append([20 + col * 16, 14 + row * 10, True]) while True: handle_serial() if not btn_menu.value(): vibrate(100, 0.03) time.sleep(0.2) exit_game("Galaga") return y_val = vry.read_u16() direction = 0 if y_val < 15000: ship_x = min(WIDTH - 12, ship_x + 3) direction = 1 elif y_val > 50000: ship_x = max(0, ship_x - 3) direction = -1 if not btn_ok.value(): vibrate(100, 0.03) time.sleep(0.12) bullets.append([ship_x + 5, ship_y - 2]) for b in bullets[:]: b[1] -= 5 if b[1] < 10: bullets.remove(b) spawn_timer += 1 if spawn_timer > 40: spawn_timer = 0 for e in enemies: e[1] += 3 for b in bullets[:]: for e in enemies: if e[2] and e[0] <= b[0] <= e[0] + 10 and e[1] <= b[1] <= e[1] + 6: e[2] = False if b in bullets: bullets.remove(b) score += 100 vibrate(100, 0.03) break if all(not e[2] for e in enemies): for row in range(2): for col in range(6): enemies.append([20 + col * 16, 14 + row * 10, True]) display.fill(0) display.text(f"Galaga Sc:{score}", 0, 0, 1) display.hline(0, 9, 128, 1) for e in enemies: if e[2]: display.fill_rect(e[0], e[1], 10, 6, 1) display.pixel(e[0] + 2, e[1] + 1, 0) display.pixel(e[0] + 7, e[1] + 1, 0) if e[1] >= ship_y - 6: display.fill(0) display.text("GAME OVER", 28, 25, 1) display.show() time.sleep(2) exit_game("Galaga") return for b in bullets: display.fill_rect(b[0], b[1], 2, 4, 1) if direction == 0: display.fill_rect(ship_x + 4, ship_y, 4, 8, 1) display.pixel(ship_x + 5, ship_y - 1, 1) display.hline(ship_x, ship_y + 4, 12, 1) elif direction == -1: display.fill_rect(ship_x + 3, ship_y + 1, 6, 4, 1) display.pixel(ship_x + 1, ship_y + 3, 1) display.line(ship_x, ship_y + 6, ship_x + 10, ship_y + 2, 1) elif direction == 1: display.fill_rect(ship_x + 3, ship_y + 1, 6, 4, 1) display.pixel(ship_x + 10, ship_y + 3, 1) display.line(ship_x, ship_y + 2, ship_x + 10, ship_y + 6, 1) display.show() time.sleep(0.03) def run_dogger(): global highscores if "dogger" not in highscores: highscores["dogger"] = 0 px = 60.0 py = 48.0 score = 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: handle_serial() if not btn_menu.value(): vibrate(100, 0.03) time.sleep(0.2) exit_game("Dogger") return # Čtení surové hodnoty z osy joysticku dle přesné kalibrace z nákresu val_y = vry.read_u16() min_val = 5000 max_val = 60000 val_y = max(min_val, min(max_val, val_y)) # Přesné mapování: střed joysticku je uprostřed, dorazy vedou až na samé okraje normalized = (max_val - val_y) / (max_val - min_val) px = normalized * (WIDTH - 8) # Postupné zrychlování překážek podle skóre speed_multiplier = 1.0 + (score * 0.04) for obs in obstacles: 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 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.text(f"Dogger:{score} Hi:{highscores.get('dogger', 0)}", 0, 0, 1) display.hline(0, 9, 128, 1) for obs in obstacles: display.fill_rect(int(obs[0]), int(obs[1]), int(obs[2]), 6, 1) ipx, ipy = int(px), int(py) 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() time.sleep(0.03) def run_doom_game(): px, py, pa = 2.0, 2.0, 0.0 ammo = 35 health = 100 room_lines = [ [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, 22.0, -2.0, 1.0, 22.0, -2.0], [1.0, 22.0, -2.0, 1.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, 22.0, 2.0, 1.0, 22.0, 2.0], [1.0, 22.0, 2.0, 1.0, 1.0, 2.0], [1.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, 22.0, 2.0], [1.0, 22.0, -2.0, 1.0, 22.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, 6.0, -2.0, 12.0, 6.0, -2.0], [4.0, 6.0, 2.0, 12.0, 6.0, 2.0], [8.0, 6.0, -2.0, 8.0, 18.0, -2.0], [8.0, 6.0, 2.0, 8.0, 18.0, 2.0], [12.0, 2.0, -2.0, 12.0, 12.0, -2.0], [12.0, 2.0, 2.0, 12.0, 12.0, 2.0], [12.0, 12.0, -2.0, 18.0, 12.0, -2.0], [12.0, 12.0, 2.0, 18.0, 12.0, 2.0], [16.0, 6.0, -2.0, 16.0, 20.0, -2.0], [16.0, 6.0, 2.0, 16.0, 20.0, 2.0], [2.0, 16.0, -2.0, 8.0, 16.0, -2.0], [2.0, 16.0, 2.0, 8.0, 16.0, 2.0], [18.0, 12.0, -2.0, 18.0, 22.0, -2.0], [18.0, 12.0, 2.0, 18.0, 22.0, 2.0], [8.0, 18.0, -2.0, 16.0, 18.0, -2.0], [8.0, 18.0, 2.0, 16.0, 18.0, 2.0], ] walls = [ [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], [4.0, 1.0, 4.0, 16.0], [4.0, 6.0, 12.0, 6.0], [8.0, 6.0, 8.0, 18.0], [12.0, 2.0, 12.0, 12.0], [12.0, 12.0, 18.0, 12.0], [16.0, 6.0, 16.0, 20.0], [2.0, 16.0, 8.0, 16.0], [18.0, 12.0, 18.0, 22.0], [8.0, 18.0, 16.0, 18.0] ] monsters = [ [6.0, 3.0, True, 0], [10.0, 10.0, True, 0], [3.0, 19.0, True, 0], [14.0, 15.0, True, 0], [20.0, 18.0, True, 0], [14.0, 4.0, True, 0] ] projectiles = [] anim_timer = 0 hit_flash = 0 def check_collision(x, y, r=0.25): for w in walls: x1, y1, x2, y2 = w line_len_sq = (x2 - x1)**2 + (y2 - y1)**2 if line_len_sq == 0: continue t = max(0, min(1, ((x - x1) * (x2 - x1) + (y - y1) * (y2 - y1)) / line_len_sq)) proj_x = x1 + t * (x2 - x1) proj_y = y1 + t * (y2 - y1) if (x - proj_x)**2 + (y - proj_y)**2 < r*r: return True return False while True: handle_serial() if not btn_menu.value(): vibrate(100, 0.03) time.sleep(0.2) exit_game("3D Doom") return turn_val = vry.read_u16() move_val = vrx.read_u16() turn_speed = 0.08 move_speed = 0.06 if turn_val < 15000: pa -= turn_speed elif turn_val > 50000: pa += turn_speed move_step = 0.0 if move_val < 15000: move_step = move_speed elif move_val > 50000: move_step = -move_speed if move_step != 0: nx = px - math.sin(pa) * move_step ny = py + math.cos(pa) * move_step if not check_collision(nx, ny): px, py = nx, ny anim_timer += 1 for m in monsters: if not m[2]: continue dx = px - m[0] dy = py - m[1] dist = math.sqrt(dx*dx + dy*dy) if 0.8 < dist < 7.0: step_x = (dx / dist) * 0.015 step_y = (dy / dist) * 0.015 if not check_collision(m[0] + step_x, m[1] + step_y, 0.2): m[0] += step_x m[1] += step_y m[3] += 1 if dist < 5.5 and m[3] > 70: m[3] = 0 p_vx = (dx / dist) * 0.1 p_vy = (dy / dist) * 0.1 projectiles.append([m[0], m[1], p_vx, p_vy]) for p in projectiles[:]: p[0] += p[2] p[1] += p[3] if check_collision(p[0], p[1], 0.1): projectiles.remove(p) elif abs(p[0] - px) < 0.4 and abs(p[1] - py) < 0.4: health -= 15 hit_flash = 5 vibrate(200, 0.1) projectiles.remove(p) if health <= 0: display.fill(0) display.text("GAME OVER", 30, 25, 1) display.show() time.sleep(2.5) exit_game("3D Doom") return elif not (1.0 < p[0] < 22.0 and 1.0 < p[1] < 22.0): projectiles.remove(p) if not btn_ok.value(): vibrate(100, 0.03) time.sleep(0.15) if ammo > 0: ammo -= 1 cos_a, sin_a = math.cos(-pa), math.sin(-pa) for m in monsters: if m[2]: mx, my = m[0] - px, m[1] - py trz = mx * sin_a + my * cos_a trx = mx * cos_a - my * sin_a if trz > 0.1: screen_x = int(64 + (trx / trz) * 90) if abs(screen_x - 64) < 18 and trz < 6.0: m[2] = False vibrate(150, 0.08) break if all(not m[2] for m in monsters): display.fill(0) display.text("VYHRA!", 44, 25, 1) display.show() time.sleep(2.0) exit_game("3D Doom") return display.fill(0) if hit_flash > 0: hit_flash -= 1 cos_a = math.cos(-pa) sin_a = math.sin(-pa) for line in room_lines: x1, y1, z1, x2, y2, z2 = line mx1, my1 = x1 - px, y1 - py trz1 = mx1 * sin_a + my1 * cos_a trx1 = mx1 * cos_a - my1 * sin_a mx2, my2 = x2 - px, y2 - py trz2 = mx2 * sin_a + my2 * cos_a trx2 = mx2 * cos_a - my2 * sin_a if trz1 < 0.1 and trz2 < 0.1: continue if trz1 < 0.1: t = (0.1 - trz1) / (trz2 - trz1) trz1 = 0.1 trx1 = trx1 + t * (trx2 - trx1) z1 = z1 + t * (z2 - z1) elif trz2 < 0.1: t = (0.1 - trz2) / (trz1 - trz2) trz2 = 0.1 trx2 = trx2 + t * (trx1 - trx2) z2 = z2 + t * (z1 - z2) sx1 = int(64 + (trx1 / trz1) * 90) sy1 = int(25 - (z1 / trz1) * 90) sx2 = int(64 + (trx2 / trz2) * 90) sy2 = int(25 - (z2 / trz2) * 90) display.line(sx1, sy1, sx2, sy2, 1) for p in projectiles: mx, my = p[0] - px, p[1] - py trz = mx * sin_a + my * cos_a if trz > 0.1: trx = mx * cos_a - my * sin_a sx = int(64 + (trx / trz) * 90) display.fill_rect(sx - 1, 24, 3, 3, 1) for m in monsters: if not m[2]: continue mx, my = m[0] - px, m[1] - py trz = mx * sin_a + my * cos_a if trz > 0.1: trx = mx * cos_a - my * sin_a screen_x = int(64 + (trx / trz) * 90) size = max(4, min(48, int(34 / trz))) top = max(0, 25 - size // 2) bottom = min(50, 25 + size // 2) left = screen_x - size // 2 right = screen_x + size // 2 if -20 < screen_x < 148 and size > 4: display.fill_rect(left + size//6, top + size//4, size * 2 // 3, size * 3 // 4, 1) display.fill_rect(left + size//4, top, size // 2, size // 3, 1) display.pixel(left + size//5, top - 1, 1) display.pixel(left + size*4//5, top - 1, 1) if size > 10: display.pixel(screen_x - 2, top + size//6, 0) display.pixel(screen_x + 2, top + size//6, 0) display.hline(0, 51, 128, 1) display.pixel(64, 25, 1) display.pixel(63, 25, 1) display.pixel(65, 25, 1) display.pixel(64, 24, 1) display.pixel(64, 26, 1) display.text(f"H:{health}", 2, 55, 1) display.text(f"A:{ammo}", 92, 55, 1) display.show() time.sleep(0.02) def run_shooter(): pa = 0.0 pitch = 0.0 score = 0 base_verts = [[-0.8,-0.8,-0.8],[0.8,-0.8,-0.8],[0.8,0.8,-0.8],[-0.8,0.8,-0.8],[-0.8,-0.8,0.8],[0.8,-0.8,0.8],[0.8,0.8,0.8],[-0.8,0.8,0.8]] cube_edges = [(0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(6,7),(7,4),(0,4),(1,5),(2,6),(3,7)] cubes = [] def spawn_cube(): return { "x": random.uniform(-6, 6), "y": random.uniform(-3, 3), "z": random.uniform(3, 14) } for _ in range(6): cubes.append(spawn_cube()) last_btn_ok = 1 while True: handle_serial() if not btn_menu.value(): vibrate(100, 0.03) time.sleep(0.2) exit_game("SHOOTER") return x_val = vrx.read_u16() y_val = vry.read_u16() diff_y = y_val - 32768 if abs(diff_y) > 3000: speed_factor = diff_y / 32768.0 pa -= speed_factor * 0.1 diff_x = x_val - 32768 if abs(diff_x) > 3000: pitch_factor = diff_x / 32768.0 pitch += pitch_factor * 0.08 pitch = max(-1.0, min(1.0, pitch)) b_ok = btn_ok.value() shot_fired = (last_btn_ok == 1 and b_ok == 0) last_btn_ok = b_ok display.fill(0) cos_a, sin_a = math.cos(pa), math.sin(pa) cos_p, sin_p = math.cos(pitch), math.sin(pitch) cubes.sort(key=lambda c: c["z"]) hit_index = -1 for idx, cb in enumerate(cubes): cx, cy, cz = cb["x"], cb["y"], cb["z"] proj = [] vis = True sum_screen_x, sum_screen_y = 0, 0 for v in base_verts: x, y, z = v[0] + cx, v[1] + cy, v[2] + cz rx = x * cos_a - z * sin_a rz = x * sin_a + z * cos_a ry = y ry_p = ry * cos_p - rz * sin_p rz_p = ry * sin_p + rz * cos_p if rz_p > 0.2: sx = int(64 + (rx / rz_p) * 70) sy = int(32 + (ry_p / rz_p) * 70) proj.append((sx, sy, True)) sum_screen_x += sx sum_screen_y += sy else: proj.append((0, 0, False)) vis = False if vis: avg_x = sum_screen_x / 8.0 avg_y = sum_screen_y / 8.0 if shot_fired and hit_index == -1 and abs(avg_x - 64) < 18 and abs(avg_y - 32) < 18: hit_index = idx for e in cube_edges: p1, p2 = proj[e[0]], proj[e[1]] if p1[2] and p2[2]: display.line(p1[0], p1[1], p2[0], p2[1], 1) if hit_index != -1: vibrate(100, 0.05) score += 1 cubes.pop(hit_index) cubes.append(spawn_cube()) display.pixel(64, 32, 1) display.pixel(63, 32, 1) display.pixel(65, 32, 1) display.pixel(64, 31, 1) display.pixel(64, 33, 1) display.text(f"Skore:{score}", 2, 2, 1) display.show() time.sleep(0.03) def run_2048(): board = [[0 for _ in range(8)] for _ in range(8)] def add_tile(): empty = [(r, c) for r in range(8) for c in range(8) if board[r][c] == 0] if empty: r, c = random.choice(empty) board[r][c] = 2 if random.random() < 0.9 else 4 add_tile(); add_tile() while True: handle_serial() if not btn_menu.value(): vibrate(100, 0.03) time.sleep(0.2) exit_game("2048") return x_v, y_v = vrx.read_u16(), vry.read_u16() moved = False time.sleep(0.2) def slide_row(row): nonlocal moved new_row = [i for i in row if i != 0] res = [] i = 0 while i < len(new_row): if i + 1 < len(new_row) and new_row[i] == new_row[i+1]: res.append(new_row[i] * 2) i += 2 moved = True else: res.append(new_row[i]) i += 1 res += [0] * (8 - len(res)) if res != row: moved = True return res if y_v < 15000: for r in range(8): rev = board[r][::-1] rev = slide_row(rev) board[r] = rev[::-1] elif y_v > 50000: for r in range(8): board[r] = slide_row(board[r]) elif x_v < 15000: for c in range(8): col = [board[r][c] for r in range(8)] col = slide_row(col) for r in range(8): board[r][c] = col[r] elif x_v > 50000: for c in range(8): col = [board[r][c] for r in range(8)][::-1] col = slide_row(col)[::-1] for r in range(8): board[r][c] = col[r] if moved: add_tile() vibrate(100, 0.03) display.fill(0) display.text("2048 (8x8)", 32, 0, 1) for r in range(8): for c in range(8): val = board[r][c] txt = str(val) if val > 0 else "." display.text(txt, c * 15, 10 + r * 7, 1) display.show() time.sleep(0.05) def run_breakout(): px = 50 bx, by = 64, 30 bdx, bdy = 2, -2 score = 0 level = 1 def init_bricks(): return [[c, r] for r in range(3) for c in range(6)] bricks = init_bricks() while True: handle_serial() if not btn_menu.value(): vibrate(100, 0.03) time.sleep(0.2) exit_game("Breakout") return y_v = vry.read_u16() if y_v < 15000: px = min(WIDTH - 24, px + 4) elif y_v > 50000: px = max(0, px - 4) bx += bdx by += bdy if bx <= 0 or bx >= 124: bdx = -bdx if by <= 10: bdy = -bdy if by >= 58 and px <= bx <= px + 24: bdy = -bdy score += 1 vibrate(100, 0.03) for b in bricks[:]: bx_c = 10 + b[0] * 18 by_c = 14 + b[1] * 8 if bx_c <= bx <= bx_c + 16 and by_c <= by <= by_c + 6: bricks.remove(b) bdy = -bdy score += 10 vibrate(100, 0.03) if not bricks: level += 1 bricks = init_bricks() bx, by = 64, 30 bdx = 2 if bdx > 0 else -2 bdy = -2 display.fill(0) display.text(f"DALSI LEVEL {level}!", 12, 28, 1) display.show() time.sleep(1.5) if by > HEIGHT: display.fill(0) display.text("GAME OVER", 28, 25, 1) display.show() time.sleep(2) exit_game("Breakout") return display.fill(0) display.text(f"L:{level} Sc:{score}", 2, 0, 1) display.hline(0, 9, 128, 1) display.fill_rect(px, 60, 24, 3, 1) display.fill_rect(int(bx), int(by), 3, 3, 1) for b in bricks: display.fill_rect(10 + b[0] * 18, 14 + b[1] * 8, 16, 6, 1) display.show() time.sleep(0.03) def run_sudoku(): grid = [ [5, 3, 0, 0, 7, 0, 0, 0, 0], [6, 0, 0, 1, 9, 5, 0, 0, 0], [0, 9, 8, 0, 0, 0, 0, 6, 0], [8, 0, 0, 0, 6, 0, 0, 0, 3], [4, 0, 0, 8, 0, 3, 0, 0, 1], [7, 0, 0, 0, 2, 0, 0, 0, 6], [0, 6, 0, 0, 0, 0, 2, 8, 0], [0, 0, 0, 4, 1, 9, 0, 0, 5], [0, 0, 0, 0, 8, 0, 0, 7, 9] ] cx, cy = 0, 0 editing = False while True: handle_serial() if not btn_menu.value(): vibrate(100, 0.03) time.sleep(0.2) exit_game("Sudoku") return x_v, y_v = vrx.read_u16(), vry.read_u16() time.sleep(0.18) if not editing: if y_v > 50000: cx = (cx - 1) % 9; vibrate(100, 0.03) elif y_v < 15000: cx = (cx + 1) % 9; vibrate(100, 0.03) elif x_v < 15000: cy = (cy - 1) % 9; vibrate(100, 0.03) elif x_v > 50000: cy = (cy + 1) % 9; vibrate(100, 0.03) if not btn_ok.value(): editing = True vibrate(100, 0.03) time.sleep(0.2) else: if x_v < 15000: grid[cy][cx] = (grid[cy][cx] + 1) % 10 vibrate(100, 0.03) elif x_v > 50000: grid[cy][cx] = (grid[cy][cx] - 1) % 10 vibrate(100, 0.03) if not btn_ok.value(): editing = False vibrate(100, 0.03) time.sleep(0.2) display.fill(0) start_x = 28 start_y = 1 cell_w = 8 cell_h = 7 for r in range(9): for c in range(9): val = str(grid[r][c]) if grid[r][c] > 0 else "" px_x = start_x + c * cell_w + 1 px_y = start_y + r * cell_h + 1 if val: display.text(val, px_x, px_y, 1) if r == cy and c == cx: if editing: display.rect(start_x + c * cell_w, start_y + r * cell_h, cell_w, cell_h, 1) else: display.pixel(px_x - 1, px_y, 1) for i in range(10): display.vline(start_x + i * cell_w, start_y, 9 * cell_h, 1) display.hline(start_x, start_y + i * cell_h, 9 * cell_w, 1) display.show() time.sleep(0.03) # --- STOPKY --- def run_stopky(): start_time = 0 elapsed = 0 running = False while True: handle_serial() if not btn_menu.value(): vibrate(100, 0.03) time.sleep(0.2) exit_game("Stopky") return if not btn_ok.value(): vibrate(100, 0.03) if not running: running = True start_time = time.ticks_ms() - elapsed else: running = False time.sleep(0.2) x_v, y_v = vrx.read_u16(), vry.read_u16() if x_v < 15000 and not running: elapsed = 0 vibrate(100, 0.03) time.sleep(0.2) if running: elapsed = time.ticks_diff(time.ticks_ms(), start_time) total_sec = elapsed // 1000 mins = total_sec // 60 secs = total_sec % 60 ms = (elapsed % 1000) // 100 display.fill(0) display.text("STOPKY", 40, 2, 1) display.hline(0, 11, 128, 1) time_str = f"{mins:02d}:{secs:02d}.{ms}" display.text(time_str, 24, 28, 1) status_txt = "Spusteno" if running else "Zastaveno" display.text(status_txt, 20, 50, 1) display.show() time.sleep(0.03) # --- ČASOVAČ --- def run_casovac(): seconds = 10 running = False start_tick = 0 remaining = 0 while True: handle_serial() if not btn_menu.value(): vibrate(100, 0.03) time.sleep(0.2) exit_game("Casovac") return x_v, y_v = vrx.read_u16(), vry.read_u16() if not running: if y_v < 15000: seconds = max(1, seconds - 5) vibrate(100, 0.03) time.sleep(0.15) elif y_v > 50000: seconds += 5 vibrate(100, 0.03) time.sleep(0.15) if not btn_ok.value(): running = True remaining = seconds start_tick = time.ticks_ms() vibrate(100, 0.03) time.sleep(0.2) else: elapsed = time.ticks_diff(time.ticks_ms(), start_tick) // 1000 remaining = seconds - elapsed if remaining <= 0: remaining = 0 running = False vibrate(30, 0.5) time.sleep(0.5) if not btn_ok.value(): running = False vibrate(100, 0.03) time.sleep(0.2) display.fill(0) display.text("CASOVAC", 36, 2, 1) display.hline(0, 11, 128, 1) if running: display.text("Zbyva:", 32, 20, 1) display.text(f" {remaining} sekund ", 16, 35, 1) else: display.text("Nastav (s):", 24, 18, 1) display.text(f" < {seconds} s > ", 16, 34, 1) display.text("OK=Start M=Zpet", 8, 52, 1) display.show() time.sleep(0.05) # --- VIZUÁLY --- def run_visualy(): mode = 0 stars = [[random.randint(0, 127), random.randint(0, 63), random.choice([1, 2])] for _ in range(20)] strip_pos = 0 circle_r = 0 # 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: handle_serial() if not btn_menu.value(): vibrate(100, 0.03) time.sleep(0.2) exit_game("Visualy") return # Plynulé přepínání vizuálů pomocí osy vry na joysticku now = time.ticks_ms() if time.ticks_diff(now, last_nav_time) > 300: y_val = vry.read_u16() if y_val < 15000: 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) if mode == 0: for s in stars: s[0] += s[2] if s[0] > 127: s[0] = 0 s[1] = random.randint(0, 63) display.pixel(s[0], s[1], 1) elif mode == 1: strip_pos = (strip_pos + 2) % 32 for i in range(-32, 160, 16): x_start = i + strip_pos display.line(x_start, 0, x_start + 32, 63, 1) display.line(x_start + 4, 0, x_start + 36, 63, 1) elif mode == 2: circle_r += 2 if circle_r > 70: circle_r = 0 for r in range(0, circle_r, 8): display.rect(64 - r, 32 - (r//2), r * 2, r, 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() time.sleep(0.04) def run_images_menu(): global current_state, selected_image_name img_keys = list(custom_images.keys()) if not img_keys: display.fill(0) display.text("ZADNE OBRAZKY", 12, 20, 1) display.text("M = Zpet", 28, 52, 1) display.show() time.sleep(1.0) while True: handle_serial() if not btn_menu.value(): vibrate(100, 0.03) time.sleep(0.2) exit_game("Obrazky") return if custom_images: break time.sleep(0.1) img_keys = list(custom_images.keys()) local_sel, last_nav = 0, 0 while current_state == STATE_IMAGES: handle_serial() if not btn_menu.value(): vibrate(100, 0.03) time.sleep(0.2) exit_game("Obrazky") return now = time.ticks_ms() if time.ticks_diff(now, last_nav) > 200: x_v = vrx.read_u16() if x_v < 15000: local_sel = (local_sel - 1) % len(img_keys); last_nav = now; vibrate(100, 0.03) elif x_v > 50000: local_sel = (local_sel + 1) % len(img_keys); last_nav = now; vibrate(100, 0.03) y_v = vry.read_u16() if y_v < 15000: vibrate(100, 0.03) time.sleep(0.2) selected_image_name = img_keys[local_sel] current_state = STATE_VIEW_IMAGE return if not btn_ok.value(): vibrate(100, 0.03) time.sleep(0.2) selected_image_name = img_keys[local_sel] current_state = STATE_VIEW_IMAGE return display.fill(0) display.text("SEZNAM OBRAZKU", 8, 2, 1) display.hline(0, 11, 128, 1) start_i = max(0, min(local_sel - 1, len(img_keys) - 3)) for i in range(3): idx = start_i + i if idx < len(img_keys): pfx = ">" if idx == local_sel else " " display.text(f"{pfx} {img_keys[idx]}", 4, 18 + (i * 14), 1) display.show() time.sleep(0.05) # --- PROHLÍŽENÍ OBRAZKŮ --- def run_view_image(): global current_state, selected_image_name, bg_buffer, custom_images img_keys = list(custom_images.keys()) if not img_keys or selected_image_name not in img_keys: current_state = STATE_IMAGES return img_idx = img_keys.index(selected_image_name) last_nav = 0 while current_state == STATE_VIEW_IMAGE: handle_serial() img_data = custom_images.get(img_keys[img_idx]) display.fill(0) if img_data: for i in range(len(img_data)): display.buffer[i] = img_data[i] display.show() while True: handle_serial() if not btn_menu.value(): vibrate(100, 0.03) time.sleep(0.2) current_state = STATE_IMAGES return now = time.ticks_ms() if time.ticks_diff(now, last_nav) > 250: y_v = vry.read_u16() if y_v < 15000: img_idx = (img_idx + 1) % len(img_keys) selected_image_name = img_keys[img_idx] last_nav = now vibrate(100, 0.03) break elif y_v > 50000: img_idx = (img_idx - 1) % len(img_keys) selected_image_name = img_keys[img_idx] last_nav = now vibrate(100, 0.03) break if not btn_ok.value(): vibrate(100, 0.03) time.sleep(0.2) opt_sel = 0 while True: handle_serial() if not btn_menu.value(): vibrate(100, 0.03) time.sleep(0.2) break x_v = vrx.read_u16() if x_v < 15000: opt_sel = (opt_sel - 1) % 3; time.sleep(0.15); vibrate(100, 0.03) elif x_v > 50000: opt_sel = (opt_sel + 1) % 3; time.sleep(0.15); vibrate(100, 0.03) if not btn_ok.value(): vibrate(100, 0.03) time.sleep(0.2) if opt_sel == 0 and img_data: bg_buffer[:] = img_data save_background(img_data.hex()) display.fill(0) display.text("OK: Nastaveno!", 16, 25, 1) display.show() time.sleep(1.0) elif opt_sel == 1: if selected_image_name in custom_images: del custom_images[selected_image_name] save_all_images() display.fill(0) display.text("Smazano!", 32, 25, 1) display.show() time.sleep(1.0) current_state = STATE_IMAGES return display.fill(0) display.text("AKCE S OBRAZKEM", 10, 2, 1) display.hline(0, 11, 128, 1) p0 = ">" if opt_sel == 0 else " " p1 = ">" if opt_sel == 1 else " " p2 = ">" if opt_sel == 2 else " " display.text(f"{p0} Nastavit pozadi", 4, 18, 1) display.text(f"{p1} Smazat obrazek", 4, 30, 1) display.text(f"{p2} Zpet", 4, 42, 1) display.show() time.sleep(0.05) break time.sleep(0.03) # --- HLAVNÍ SMYČKA --- while True: handle_serial() try: t = time.localtime(time.time() + 3600) curr_hour, curr_min = t[3], t[4] except: curr_hour, curr_min = 12, 0 if current_state == STATE_MAIN: display.fill(0) for i in range(len(bg_buffer)): display.buffer[i] = bg_buffer[i] time_str = f"{curr_hour:02d}:{curr_min:02d}" bat_pct = get_battery_percent() display.fill_rect(0, 0, 128, 10, 0) display.text(f"{time_str} Bat:{bat_pct}%", 2, 1, 1) display.hline(0, 10, 128, 1) display.show() time.sleep(0.1) if not btn_menu.value(): vibrate(100, 0.03) time.sleep(0.2) current_state = STATE_MENU elif current_state == STATE_MENU: get_menu_input(len(menu_items)) select_action = False if not btn_ok.value(): select_action = True else: y_val = vry.read_u16() if y_val < 15000: select_action = True if select_action: vibrate(100, 0.03) time.sleep(0.2) if selected_index == 0: current_state = STATE_SNAKE elif selected_index == 1: current_state = STATE_PONG elif selected_index == 2: current_state = STATE_CATCH elif selected_index == 3: current_state = STATE_SPAD elif selected_index == 4: current_state = STATE_TETRIS elif selected_index == 5: current_state = STATE_DINO elif selected_index == 6: current_state = STATE_FLAPPY elif selected_index == 7: current_state = STATE_SOKOBAN elif selected_index == 8: current_state = STATE_DOOM elif selected_index == 9: current_state = STATE_DOGGER elif selected_index == 10: current_state = STATE_SHOOTER elif selected_index == 11: current_state = STATE_GALAGA elif selected_index == 12: current_state = STATE_GAME_2048 elif selected_index == 13: current_state = STATE_BREAKOUT elif selected_index == 14: current_state = STATE_SUDOKU elif selected_index == 15: current_state = STATE_STOPKY elif selected_index == 16: current_state = STATE_CASOVAC elif selected_index == 17: current_state = STATE_VISUALY elif selected_index == 18: current_state = STATE_IMAGES elif selected_index == 19: current_state = STATE_SETTINGS elif selected_index == 20: current_state = STATE_INFO if not btn_menu.value(): vibrate(100, 0.03) time.sleep(0.2) current_state = STATE_MAIN display.fill(0) display.text("VYBER AKCI", 24, 0, 1) display.hline(0, 10, 128, 1) start_idx = max(0, min(selected_index - 1, len(menu_items) - 3)) for i in range(3): idx = start_idx + i if idx < len(menu_items): prefix = ">" if idx == selected_index else " " display.text(f"{prefix} {menu_items[idx]}", 4, 16 + (i * 15), 1) display.show() time.sleep(0.05) elif current_state == STATE_SNAKE: run_snake() elif current_state == STATE_PONG: run_pong() elif current_state == STATE_CATCH: run_catch() elif current_state == STATE_SPAD: run_spad() elif current_state == STATE_TETRIS: run_tetris() elif current_state == STATE_DINO: run_dino() elif current_state == STATE_FLAPPY: run_flappy() elif current_state == STATE_SOKOBAN: run_sokoban() elif current_state == STATE_DOOM: run_doom_game() elif current_state == STATE_DOGGER: run_dogger() elif current_state == STATE_SHOOTER: run_shooter() elif current_state == STATE_GALAGA: run_galaga() elif current_state == STATE_GAME_2048: run_2048() elif current_state == STATE_BREAKOUT: run_breakout() elif current_state == STATE_SUDOKU: run_sudoku() elif current_state == STATE_STOPKY: run_stopky() elif current_state == STATE_CASOVAC: run_casovac() elif current_state == STATE_VISUALY: run_visualy() elif current_state == STATE_IMAGES: run_images_menu() elif current_state == STATE_VIEW_IMAGE: run_view_image() elif current_state == STATE_SETTINGS: setting_index = 0 while current_state == STATE_SETTINGS: handle_serial() x_v, y_v = vrx.read_u16(), vry.read_u16() if x_v < 15000: setting_index = (setting_index - 1) % 2 vibrate(100, 0.03) time.sleep(0.2) elif x_v > 50000: setting_index = (setting_index + 1) % 2 vibrate(100, 0.03) time.sleep(0.2) if y_v < 15000: if setting_index == 0: set_contrast(setting_contrast + 15) else: setting_vibration = not setting_vibration save_settings() vibrate(100, 0.03) time.sleep(0.15) elif y_v > 50000: if setting_index == 0: set_contrast(setting_contrast - 15) else: setting_vibration = not setting_vibration save_settings() vibrate(100, 0.03) time.sleep(0.15) if not btn_menu.value(): vibrate(100, 0.03) time.sleep(0.2) current_state = STATE_MAIN display.fill(0) display.text("NASTAVENI", 28, 2, 1) display.hline(0, 11, 128, 1) p0 = ">" if setting_index == 0 else " " p1 = ">" if setting_index == 1 else " " vib_text = "Zapnuto" if setting_vibration else "Vypnuto" display.text(f"{p0} Jas: {setting_contrast}", 4, 20, 1) display.text(f"{p1} Vibrace: {vib_text}", 4, 32, 1) display.text("<- -> Zmenit M=Zpet", 2, 52, 1) display.show() time.sleep(0.05) elif current_state == STATE_INFO: while current_state == STATE_INFO: handle_serial() if not btn_menu.value() or not btn_ok.value(): vibrate(100, 0.03) time.sleep(0.2) current_state = STATE_MAIN total_sec = time.ticks_diff(time.ticks_ms(), start_ticks) // 1000 hrs = total_sec // 3600 mins = (total_sec % 3600) // 60 secs = total_sec % 60 if hrs > 0: run_str = f"{hrs:02d}:{mins:02d}:{secs:02d}" else: run_str = f"{mins:02d}:{secs:02d}" display.fill(0) display.text("INFO O ZARIZENI", 12, 2, 1) display.hline(0, 11, 128, 1) display.text(f"IP:{device_ip}", 2, 16, 1) display.text(f"Teplota:{get_chip_temp():.1f}C", 2, 28, 1) display.text(f"Baterie:{read_battery():.2f}V", 2, 40, 1) display.text(f"Beh: {run_str}", 2, 52, 1) display.show() time.sleep(0.1)