import time import random import network import espnow from machine import Pin from neopixel import NeoPixel # --- KONFIGURACJA SIECI --- wlan = network.WLAN(network.STA_IF) wlan.active(True) wlan.disconnect() wlan.config(channel=1) e = espnow.ESPNow() e.active(True) # --- MATRYCA 16x16 --- SCREEN_WIDTH = 16 SCREEN_HEIGHT = 16 PIN_NEO = 16 np = NeoPixel(Pin(PIN_NEO, Pin.OUT), SCREEN_WIDTH * SCREEN_HEIGHT) # --- FIZYKA GRY --- player_x = 7.0 player_y = 10.0 direction = "STOP" player_height = 2 player_draw_width = 2 SCROLL_THRESHOLD = 8 PLAYER_SPEED = 0.8 ay = 0.15 jump_height = 6.0 jump_speed = -((2 * ay * jump_height) ** 0.5) # --- KOLORY --- C_PLAYER = (50, 0, 50) C_PLATFORM = (0, 30, 0) C_BG = (0, 0, 0) vy = 0.0 # --- GENEROWANIE PLATFORM --- platforms = [] platforms.append([6, 14, 4]) for _ in range(6): w = random.randrange(3, 6) # ZAMIANA RANDINT NA RANDRANGE max_x = SCREEN_WIDTH - w if max_x <= 0: max_x = 1 x = random.randrange(0, max_x + 1) y = random.randrange(0, SCREEN_HEIGHT - 2) platforms.append([x, y, w]) # --- FUNKCJA RYSOWANIA --- def set_pixel(x, y, color): if x < 0: x = 0 if x >= SCREEN_WIDTH: x = SCREEN_WIDTH - 1 if y < 0: y = 0 if y >= SCREEN_HEIGHT: y = SCREEN_HEIGHT - 1 x = int(x) y = int(y) if y % 2 == 0: idx = y * SCREEN_WIDTH + x else: idx = y * SCREEN_WIDTH + (SCREEN_WIDTH - 1 - x) np[idx] = color def draw(): np.fill(C_BG) for p in platforms: px, py, pw = p if 0 <= py < SCREEN_HEIGHT: for i in range(pw): set_pixel(px + i, py, C_PLATFORM) px = int(player_x) py = int(player_y) px_wrapped = px % SCREEN_WIDTH set_pixel(px_wrapped, py, C_PLAYER) set_pixel((px_wrapped+1)%SCREEN_WIDTH, py, C_PLAYER) set_pixel(px_wrapped, py+1, C_PLAYER) set_pixel((px_wrapped+1)%SCREEN_WIDTH, py+1, C_PLAYER) np.write() print("GRA START!") # --- GŁÓWNA PĘTLA --- while True: # 1. ODCZYT WIFI while True: host, msg = e.recv() if msg: if msg == b'L': direction = "LEFT" elif msg == b'R': direction = "RIGHT" elif msg == b'S': direction = "STOP" else: break # 2. RUCH if direction == "LEFT": player_x -= PLAYER_SPEED elif direction == "RIGHT": player_x += PLAYER_SPEED if player_x < 0: player_x += SCREEN_WIDTH if player_x >= SCREEN_WIDTH: player_x -= SCREEN_WIDTH vy += ay player_y += vy # 3. KOLIZJE if vy > 0: player_bottom = player_y + player_height for p in platforms: plat_x, plat_y, plat_w = p if plat_y - 1 <= player_bottom <= plat_y + 1.5: p_left = player_x % SCREEN_WIDTH p_right = (player_x + player_draw_width) % SCREEN_WIDTH if (plat_x <= p_left < plat_x + plat_w) or (plat_x <= p_right < plat_x + plat_w): vy = jump_speed break # 4. PRZEWIJANIE (POPRAWIONE RANDINT -> RANDRANGE) if player_y < SCROLL_THRESHOLD: offset = SCROLL_THRESHOLD - player_y player_y = SCROLL_THRESHOLD for p in platforms: p[1] += offset if p[1] > SCREEN_HEIGHT: p[1] = 0 p[2] = random.randrange(3, 6) # POPRAWKA max_x = SCREEN_WIDTH - p[2] if max_x <= 0: max_x = 1 p[0] = random.randrange(0, max_x + 1) # POPRAWKA if player_y > SCREEN_HEIGHT - player_height: vy = jump_speed player_y = SCREEN_HEIGHT - player_height draw() time.sleep(0.02)