Toggle Navigation
Hatchery
Eggs
Snake
__init__.py
Login
Register
__init__.py
Content
# Completely taken from https://badge.team/projects/supreme_snake # Kudos to Jan Fecht, I just ported it to Hacktivity badge import urandom # random food gen import utime import buttons import badge import ugfx import system import samd game_width = 128 game_height = 64 num_start_tiles = 4 sleep_time_ms = 100 snake_col = ugfx.BLACK food_col = ugfx.BLACK bg_col = ugfx.WHITE font = 'Roboto_BlackItalic24' physical_width = 128 physical_height = 64 tile_width = physical_width // game_width tile_height = physical_height // game_height class Direction: UP = (0,-1) LEFT = (-1,0) RIGHT = (1,0) DOWN = (0,1) clockwise = [UP, RIGHT, DOWN, LEFT] def __init__(self, direction=LEFT): self.direction = direction def turn_clockwise(self): self.direction = self.clockwise[(self.clockwise.index(self.direction) + 1) % 4] def turn_counterclockwise(self): self.direction = self.clockwise[(self.clockwise.index(self.direction) - 1) % 4] class Game: def reset(self): self.pos = [(game_width // 2 + i, game_height // 2) for i in range(num_start_tiles)] self.tile_directions = [Direction.LEFT] * len(self.pos) self.direction = Direction(Direction.LEFT) self.grow = False #def is_valid_pos(p): # x,y = p # return 0 <= x < game_width and 0 <= y <= game_width #assert(all[is_valid_pos(p) for p in self.pos]) self.gen_new_food() def __init__(self): badge.init() ugfx.input_init() self.reset() def __enter__(self): return self def __exit__ (self, t, value, tb): return def gen_new_food(self): while True: self.food_pos = (urandom.randrange(0, game_width), urandom.randrange(0, game_height)) if self.food_pos not in self.pos: break def step(self): """ returns True if not Dead """ if buttons.value(buttons.BTN_UP): self.direction.turn_counterclockwise() elif buttons.value(buttons.BTN_RIGHT): self.direction.turn_clockwise() else: pass self.tile_directions.pop() self.tile_directions.insert(0,self.direction.direction) headx, heady = self.pos[0] if ((headx + self.direction.direction[0]) % game_width, (heady + self.direction.direction[1]) % game_height) in self.pos: return False last = self.pos[-1] for i in range(len(self.pos)): x, y = self.pos[i] deltax, deltay = self.tile_directions[i] self.pos[i] = ((x + deltax) % game_width, (y + deltay) % game_height) if self.grow: self.pos.append(last) self.tile_directions.append(self.tile_directions[-1]) self.grow = False if self.food_pos in self.pos: self.gen_new_food() self.grow = True return True def draw(self): ugfx.clear() ugfx.area(0, 0, physical_width, physical_height, bg_col) for (x,y) in self.pos: #ugfx.area(x*tile_width, y*tile_width, (x+1)*tile_width, (y+1)*tile_height, snake_col) ugfx.area(x*tile_width, y*tile_width, 5, 5, snake_col) x,y = self.food_pos #ugfx.area(x*tile_width, y*tile_width, (x+1)*tile_width, (y+1)*tile_height, food_col) ugfx.area(x*tile_width, y*tile_width, 5, 5, food_col) ugfx.flush() def run(self): while True: self.draw() utime.sleep_ms(sleep_time_ms) alive = self.step() if not alive: for i in range(0, 3): samd.backlight(0) utime.sleep_ms(500) samd.backlight(255) utime.sleep_ms(500) msg = 'DED' ugfx.box(0, 0, physical_width, physical_height, ugfx.BLACK) width = ugfx.get_string_width(msg, font) ugfx.string(int((128 - width)/2), 20, msg, font, ugfx.BLACK) ugfx.string(0, 0, "Length: {}".format(len(self.pos)), 'Roboto_Regular12', ugfx.BLACK) ugfx.flush() utime.sleep_ms(1000) while True: if buttons.value(buttons.BTN_UP) or buttons.value(buttons.BTN_RIGHT): break elif buttons.value(buttons.BTN_B): system.home() utime.sleep_ms(100) break def main(): with Game() as g: while True: g.reset() g.run() main()