Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@m1cr0lab
Last active April 28, 2019 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m1cr0lab/8031233d7b050a45e675e199b82fdaa8 to your computer and use it in GitHub Desktop.
Save m1cr0lab/8031233d7b050a45e675e199b82fdaa8 to your computer and use it in GitHub Desktop.
Snake Game with Object Oriented Programming for Gamebuino META
# --------------------------------------------------------------------------
# Snake Game on Gamebuino META with CircuitPython
# --------------------------------------------------------------------------
from gamebuino_meta import begin, waitForUpdate, display, color, buttons
from random import randint
from gc import collect, mem_free
# --------------------------------------------------------------------------
# Graphic display definition
# --------------------------------------------------------------------------
class Screen:
width = 80
height = 64
class Grid:
size = 2
rows = (Screen.width - 4) // size
cols = (Screen.height - 4) // size
ox = (Screen.width - cols * size) // 2
oy = (Screen.height - rows * size) // 2
# --------------------------------------------------------------------------
# Point definition
# --------------------------------------------------------------------------
class Point:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def add(self, p):
self.x += p.x
self.y += p.y
# --------------------------------------------------------------------------
# Snake definition
# --------------------------------------------------------------------------
class Snake:
length = 4
color = color.LIGHTBLUE
def __init__(self, x, y):
self.pos = Point(x, y)
self.vel = Point()
self.tail = []
def move(self):
self.pos.add(self.vel)
def draw(self):
display.setColor(Snake.color)
display.fillRect(Grid.ox + self.pos.x * Grid.size, Grid.oy + self.pos.y * Grid.size, Grid.size, Grid.size)
# --------------------------------------------------------------------------
# Game definition
# --------------------------------------------------------------------------
class Game:
startMode = 0
playMode = 1
bgColor = color.DARKGRAY
wallColor = color.GRAY
appleColor = color.LIGHTGREEN
def __init__(self):
self.snake = Snake(Grid.cols // 2, Grid.rows // 2)
self.apple = Point()
self.mode = Game.startMode
self.score = 0
def tick(self):
self.update()
self.draw()
def update(self):
if self.mode == Game.startMode:
# --------------------------------------------------------------
# I can't call this method, due to error below...
# --------------------------------------------------------------
# self.spawnApple()
# --------------------------------------------------------------
self.mode = Game.playMode
self.snake.move()
# -------------------------------------------------------------------------
# when I uncomment this method, CircuitPython crashes with following error:
# -------------------------------------------------------------------------
# Traceback (most recent call last):
# File "code.py", line 125, in <module>
# MemoryError: memory allocation failed, allocating 10240 bytes
# -------------------------------------------------------------------------
# def spawnApple(self):
# self.apple.x = randint(0, Grid.cols - 1)
# self.apple.y = randint(0, Grid.rows - 1)
def draw(self):
display.clear(Game.bgColor)
self.drawWalls()
self.snake.draw()
def drawWalls(self):
display.setColor(Game.wallColor)
display.drawRect(0, 0, Screen.width, Screen.height)
# --------------------------------------------------------------------------
# Initialization
# --------------------------------------------------------------------------
# no longer needed!
# begin()
collect()
game = Game()
# --------------------------------------------------------------------------
# Main loop
# --------------------------------------------------------------------------
while True:
waitForUpdate()
game.tick()
print(mem_free())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment