Toggle Navigation
Hatchery
Eggs
Snek
__init__.py
Users
Badges
Login
Register
MCH2022 badge?
go to mch2022.badge.team
__init__.py
raw
Content
import system, random, buttons import display as disp SCREEN_WIDTH = 120 SCREEN_HEIGHT = 60 JUMP_HEIGHT = 20 MOVEMENT_STEP = 1 class Snacc: def __init__(self, x, y): self.x = x self.y = y self.eaten = False class Game: def __init__(self): self.last_pressed = buttons.BTN_RIGHT self.yPos = 10 self.xPos = 10 self.gravity = 1 self.snacc = Snacc(30, 30) def add_snacc(self): self.snacc = Snacc(30, 30) def clear_screen(self): disp.drawFill(0x000000) def flush(self): disp.flush() def draw_arena(self): disp.drawRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, False, 0xFFFFFF) def draw_snek(self, x, y, fill): playerSize = 5 disp.drawRect(x, y, playerSize, playerSize, fill, 0xFFFFFF) def move_snek(self): if(self.last_pressed == buttons.BTN_UP): self.up(True) elif(self.last_pressed == buttons.BTN_DOWN): self.down(True) elif(self.last_pressed == buttons.BTN_LEFT): self.left(True) elif(self.last_pressed == buttons.BTN_RIGHT): self.right(True) def draw_snacc(self): if type(self.snacc) is Snacc: disp.drawRect(self.snacc.x, self.snacc.y, 2, 2, True, 0xFFFFFF) def check_snacc(self): if self.snacc.x == self.xPos and self.snacc.y == self.yPos : self.snacc = Snacc(random.randint(2, SCREEN_WIDTH - 2), random.randint(2, SCREEN_HEIGHT - 2)) def update_screen(self): self.clear_screen() self.check_snacc() self.draw_arena() self.move_snek() self.draw_snek(self.xPos, self.yPos, False) self.draw_snacc() self.flush() def loop(self): while True: self.update_screen() def up(self, pressed): if pressed: self.last_pressed = buttons.BTN_UP self.yPos -= MOVEMENT_STEP if self.yPos < 0: self.yPos = 0 def down(self, pressed): if pressed: self.last_pressed = buttons.BTN_DOWN self.yPos += MOVEMENT_STEP if self.yPos > SCREEN_HEIGHT: self.yPos = SCREEN_HEIGHT def left(self, pressed): if pressed: self.last_pressed = buttons.BTN_LEFT self.xPos -= MOVEMENT_STEP if self.xPos < 0: self.xPos = 0 def right(self, pressed): if pressed: self.last_pressed = buttons.BTN_RIGHT self.xPos += MOVEMENT_STEP if self.xPos > SCREEN_WIDTH: self.xPos = SCREEN_WIDTH def start(self): self.loop() game = Game() buttons.attach(buttons.BTN_UP, game.up) buttons.attach(buttons.BTN_DOWN, game.down) buttons.attach(buttons.BTN_LEFT, game.left) buttons.attach(buttons.BTN_RIGHT, game.right) buttons.attach(buttons.BTN_START, system.home) game.start()