Things used in this project

Hardware components:
Microbitheart
micro:bit BBC micro:bit board
×1
Software apps and online services:
MicroPython - BBC micro:bit

Schematics

HEX file
Copy this file on the micro:bit and you are ready to go.
microgameoflife_W0cbdwF9m2.hex

Code

Untitled fileMicroPython
from microbit import *

def draw_cursor(universe, mode, x, y):
    if mode == "CONFIG":
        if cell_state(universe, x, y) == 0:
            display.set_pixel(x, y, 4)
        else:
            display.set_pixel(x, y, 6)
        
def draw_universe( universe ):
    for y in range(0, 5):
        for x in range(0, 5):
            display.set_pixel(x, y, universe[x + y * 5])
            
def evolve( universe ):
    next_universe = []
    for y in range(0, 5):
        for x in range(0, 5):
            cell_neighbours = count_neighbours(universe, x, y)
            cell_is_alive = cell_state(universe, x, y) == 1
            if cell_is_alive and cell_neighbours < 2:
                next_universe.append(0)
            elif cell_is_alive and (cell_neighbours == 2 or cell_neighbours == 3):
                next_universe.append(9)
            elif cell_is_alive and cell_neighbours > 3:
                next_universe.append(0)
            elif not cell_is_alive and cell_neighbours == 3:
                next_universe.append(9)
            else:
                next_universe.append(0)
    return next_universe
    
def cell_state(universe, x, y):
    state = 1
    if universe[x + 5 * y] == 0:
        state = 0
    return state
    
def count_neighbours(universe, x, y):
    neighbours = -cell_state(universe, x, y)
    for dy in [-1, 0, 1]:
        for dx in [-1, 0, 1]:
            neighbours += cell_state(universe, (x + dx) % 5, (y + dy) % 5)
    return neighbours


current_universe = [ 0, 0 ,0 ,0 ,0,
                     0, 0, 9, 0, 0, 
                     0, 0, 9, 0, 0,
                     0, 0, 9, 0, 0,
                     0, 0, 0, 0, 0,]

cursor_x = 2
cursor_y = 2
              
mode = "CONFIG"
display.scroll(mode)

while True:
    
    if mode == "RUN":
        current_universe = evolve( current_universe )

        if button_b.is_pressed():
            mode = "CONFIG"
            display.scroll(mode)
            
            
    if mode == "CONFIG":
        
        accelerometer_xyz = accelerometer.get_values()
        if accelerometer_xyz[0] < -200:
            cursor_x = (cursor_x - 1) % 5
        if accelerometer_xyz[0] > 200:
            cursor_x = (cursor_x + 1) % 5
        if accelerometer_xyz[1] < -200:
            cursor_y = (cursor_y - 1) % 5
        if accelerometer_xyz[1] > 200:
            cursor_y = (cursor_y + 1) % 5
        
        if button_a.is_pressed():
            if cell_state(current_universe, cursor_x, cursor_y) == 0:
                current_universe[cursor_x + 5 * cursor_y] = 9
            else:  
                current_universe[cursor_x + 5 * cursor_y] = 0
                
        if button_b.is_pressed():
            mode = "RUN"
            display.scroll(mode)
           
           
    draw_universe( current_universe )
    draw_cursor(current_universe, mode, cursor_x, cursor_y) 
    sleep(1000)

Credits

Replications

Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Similar projects you might like

BitBotPathFollow
Intermediate
  • 41
  • 0

Use micro Python on micro:bit to program your bit:bot robot to retrace a path.

micro:bit controlled Halloween Cloud
Intermediate
  • 516
  • 12

Full instructions

A dark scary cloud that flashes white lightening and scary rainbows, controlled by a micro:bit and neopixels

Weather on micro:bit
Intermediate
  • 595
  • 6

Full instructions

Ask Amazon Alexa current weather, then display the result on the BBC micro:bit's LEDs.

Custom BLE services with micro:bit
Intermediate
  • 110
  • 1

Protip

Build your own Bluetooth low energy services and bundle them as blocks that beginners can use!

Micro;Bit Top Revolution Counter
Intermediate
  • 165
  • 1

Full instructions

A Micro;Bit attached to a self made top, that will count the number of times the top spins in a circle.

Programming using C/C++ (Offline IDE Setup) | BBC Microbit
Intermediate
  • 158
  • 0

This tutorial is around BBC Microbit and programming the microbit using mbed C/C++ IDE .

ProjectsCommunitiesTopicsContestsLiveAppsBetaFree StoreBlogAdd projectSign up / Login
Feedback