Skip to content

Instantly share code, notes, and snippets.

Last active Aug 5, 2021
What would you like to do?

Battle with memory.

Gamebuino tends to run out of memory to run CircuitPython.

CircuitPython works by loading scripts into memory. So keep in mind that comments also consume memory. And. Certainly OOP will consume more memory. Therefore, you need to be careful not to create classes that you do not need.

For example, how is this? I made a class only for players and apples. These actually work!

In contrast, stages are not implemented in classes.

import gamebuino_meta as gb
import math,random

class Player:
    def __init__(self):
        self.xx = 0
        self.yy = 0
        self.point = bytearray(0)
        self.point[0:] = b"\x00"
        self.point[1:] = b"\x00"

    def setVect(self,x=0,y=0):
        self.xx = x
        self.yy = y

    def move(self):
        plen = int( len(self.point) / 2 )
        for i in range( 1, plen ):
            idx = (plen)*2 - i*2
            self.point[idx  ] = self.point[idx-2]
            self.point[idx+1] = self.point[idx-1]
        self.point[0] += self.xx
        self.point[1] += self.yy

    def getLen(self):
        return int(len(self.point) / 2)

    def get(self,index):
        return self.point[index*2] , self.point[index*2+1]

    def add(self):
        plen = len(self.point)
        self.point[plen:] = bytes([self.point[plen-2]])
        self.point[plen+1:] = bytes([self.point[plen-1]])

class Apple:
    def __init__(self):
       self.size = 2
    def next(self):
        half = int( self.size / 2 )
        self.x = random.randint(half,gb.display.width() -self.size)
        self.y = random.randint(half,gb.display.height()-self.size)
    def get(self):
        return self.x, self.y

def drawStage():
    gb.display.setColor( gb.color.WHITE )
    gb.display.fillRect(0,0,gb.display.width(),  gb.display.height()  )
    gb.display.setColor( gb.color.BLACK )

p = Player()
a = Apple()

while True:

There is still a way.

The battle with memory is, in fact, quite tight. So we use the final weapon.

CircuitPython can convert scripts to binary using the "mpy-cross" compiler. This means that more memory can be used.

You need to use mpy-cross that matches the version of CircuitPython. The gamebuino CircuitPython version is 3.4.0. ...HM You may need to look at it. Verson 3.1.2 mpy-cross can be found here.

The binaries created by mpy-cross may need to be placed in the "lib" folder. To make "/lib/sample.mpy" work, use import sample in "/".

It was also work with the OOP version snake.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment