def run_game(): badge.eink_init() ugfx.init() ugfx.clear(ugfx.WHITE) ugfx.flush() badge.init() ugfx.clear(ugfx.WHITE) ugfx.input_init() ugfx.input_attach(ugfx.JOY_UP, lambda pressed: up(pressed, this_game)) ugfx.input_attach(ugfx.JOY_DOWN, lambda pressed: down(pressed, this_game)) ugfx.input_attach(ugfx.JOY_LEFT, lambda pressed: left(pressed, this_game)) ugfx.input_attach(ugfx.JOY_RIGHT, lambda pressed: right(pressed, this_game)) ugfx.input_attach(ugfx.BTN_SELECT, lambda pressed: exit_game(pressed, this_game)) ugfx.string(50, 50, "Snake Game", "PermanentMarker22", ugfx.BLACK) ugfx.string(50, 72, "press SELECT to exit", "Roboto_Regular18", ugfx.BLACK) ugfx.flush() time.sleep(5) ugfx.clear(ugfx.WHITE) ugfx.flush() snake = Snake(True, Renderer()) this_game = Game(snake, Border(), [Food.create_random_food(snake)]) ugfx.flush() print("Start Log") while True: if (this_game.game_state == "FAIL"): FailGame(this_game) Step(this_game, 5)
import ugfx import badge import dialogs import json import wifi import gc import deepsleep import network import urequest as request from time import * badge.eink_init() ugfx.init() wifi.init() ugfx.clear(ugfx.WHITE); ugfx.string(10,10,"Waiting for wifi...","Roboto_Regular12", 0) ugfx.flush() # Wait for WiFi connection while not wifi.sta_if.isconnected(): time.sleep(0.1) pass ugfx.clear(ugfx.BLACK) ugfx.flush() ugfx.clear(ugfx.WHITE) ugfx.flush() input_string = badge.nvs_get_str("Hashtag", "hashtag", "") hashtag = dialogs.prompt_text("What hashtag should we display?", input_string)
def game_of_life(): badge.eink_init() ugfx.init() ugfx.input_init() ugfx.input_attach(ugfx.JOY_RIGHT, reboot) ugfx.input_attach(ugfx.JOY_LEFT, reboot) ugfx.input_attach(ugfx.JOY_UP, reboot) ugfx.input_attach(ugfx.JOY_DOWN, reboot) ugfx.input_attach(ugfx.JOY_RIGHT, reboot) ugfx.input_attach(ugfx.BTN_A, reboot) ugfx.input_attach(ugfx.BTN_B, reboot) ugfx.input_attach(ugfx.BTN_START, reboot) ugfx.input_attach(ugfx.BTN_SELECT, reboot) ugfx.clear(ugfx.WHITE) ugfx.flush() width = 37 height = 16 cell_width = 8 cell_height = 8 grid = [[False] * height for _ in range(width)] def seed(): for x in range(0, width - 1): for y in range(0, height - 1): if urandom.getrandbits(30) % 2 == 1: grid[x][y] = True else: grid[x][y] = False def display(): for x in range(0, width): for y in range(0, height): if grid[x][y]: ugfx.area(x * cell_width, y * cell_height, cell_width - 1, cell_height - 1, ugfx.BLACK) else: ugfx.area(x * cell_width, y * cell_height, cell_width - 1, cell_height - 1, ugfx.WHITE) badge.eink_busy_wait() ugfx.flush() def alive_neighbours(x, y, wrap): if wrap: range_func = lambda dim, size: range(dim - 1, dim + 2) else: range_func = lambda dim, size: range(max(0, dim - 1), min(size, dim + 2)) n = 0 for nx in range_func(x, width): for ny in range_func(y, height): if grid[nx % width][ny % height]: if nx != x or ny != y: n += 1 return n def step(): changed = False new_grid = [[False] * height for _ in range(width)] for x in range(width): for y in range(height): neighbours = alive_neighbours(x, y, True) if neighbours < 2: # Starvation new_grid[x][y] = False changed = True elif neighbours > 3: # Overpopulation new_grid[x][y] = False changed = True elif not grid[x][y] and neighbours == 3: # Birth new_grid[x][y] = True changed = True else: # Survival new_grid[x][y] = grid[x][y] pass grid = new_grid return changed seed() generation = 0 while True: generation += 1 display() if not step() or generation > 50: seed() generation = 0
def game_of_life(): badge.eink_init() ugfx.init() ugfx.clear(ugfx.WHITE) ugfx.flush() width = 37 height = 16 cell_width = 8 cell_height = 8 grid = [[0 for x in range(height)] for y in range(width)] def seed(): for x in range(0, width - 1): for y in range(0, height - 1): if urandom.getrandbits(30) % 2 == 1: grid[x][y] = 1 else: grid[x][y] = 0 def display(): for x in range(0, width): for y in range(0, height): if grid[x][y] == 1: ugfx.area(x * cell_width, y * cell_height, cell_width - 1, cell_height - 1, ugfx.BLACK) else: ugfx.area(x * cell_width, y * cell_height, cell_width - 1, cell_height - 1, ugfx.WHITE) ugfx.flush() def step(): changed = 0 for x in range(1, width): for y in range(1, height): n = 0 # 1. tl if x > 0 and y > 0 and grid[x - 1][y - 1] == 1: n += 1 # 2. t if y > 0 and grid[x][y - 1] == 1: n += 1 # 3. tr if x < width - 1 and y > 0 and grid[x + 1][y - 1] == 1: n += 1 # 4. l if x > 0 and grid[x - 1][y] == 1: n += 1 # 5. r if x < width - 1 and grid[x + 1][y] == 1: n += 1 # 6. bl if x > 0 and y < height - 1 and grid[x - 1][y + 1] == 1: n += 1 # 7. b if y < height - 1 and grid[x][y - 1] == 1: n += 1 # 8. br if x < width - 1 and y < height - 1 and grid[x + 1][y + 1] == 1: n += 1 if grid[x][y] == 1: changed += 1 if n < 2: grid[x][y] = 0 elif n > 3: grid[x][y] = 0 else: grid[x][y] = 1 elif n == 3: grid[x][y] = 1 changed += 1 if changed > 0: return True else: return False seed() g = 0 while True: g += 1 display() if step() is False or g > 50: seed() g = 0