Despite the micro:bit Education Giveaway Challenge being over, one of my teams decided to release their game to the element14 community for everyone to try and enjoy!

This game requires no modification or additional circuit design, and can be played by multiple players with only a micro:bit club/classroom kit.

The game can serve as a class introduction to the micro:bit, or used as a tutorial to cover accelerometer, display, radio and defining custom functions.

 

The attached .pdf file explains how the game is loaded and played. You will need to modify one line of code for each participant, as documented in the pdf and source code comments. Please let us know if the program is useful for your classes or if you see any major errors in design or implementation. enjoy!

 

The code is here:

#
#CATch the MOUSE the multiplayer micro:bit game
#Game Design by Victoria Bernhart and Gabriel Di Francesco
#MicroPython code written by Victoria Bernhart
#©2018 Victoria Bernhart and Gabriel Di Francesco
#See bottom of file for MIT license information
#
#playerNum = 1 /Change this component in line 23 of the code for each hex downloaded to a different microbit!
from microbit import *
import radio
topBorder = [[0,0],[1,0],[2,0],[3,0],[4,0]]
leftBorder = [[0,0],[0,1],[0,2],[0,3],[0,4]]
bottomBorder = [[0,4],[1,4],[2,4],[3,4],[4,4]]
rightBorder = [[4,0],[4,1],[4,2],[4,3],[4,4]]
tO = 0
lO= 0
bO= 0
rO= 0
digitalX = 20
digitalY = 20
playerDX = 1
playerDY = 1
playerNum = 1   #<--- Change this line for each player playing. PlayerNum 1 is the cat. PlayerNum 2,3,4, etc. are mice.
cat = Image("09090:" "90909:" "99999:" "99999:" "00900:")
mouse = Image("99099:" "90909:" "99099:" "99099:" "00900:")
#Turn On/Off Borders Functions
def turnTop(lO,rO,p):
    r = 5
    pl=0
    if(lO or rO):
        r=4
        if(lO):
            pl=1
    for i in range(r):
            display.set_pixel(topBorder[i+pl][0],topBorder[i+pl][1],p)
def turnLeft(bO,tO,p):
    r=5
    pl=0
    if(bO or tO):
        r=4
        if(tO):
            pl=1
    for i in range(r):
                display.set_pixel(leftBorder[i+pl][0],leftBorder[i+pl][1],p)
def turnBottom(lO,rO,p):
    r = 5
    pl = 0
    if(lO or rO):
        r=4
        if(lO):
            pl=1
    for i in range(r):
                for j in range(1):
                    display.set_pixel(bottomBorder[i+pl][0],bottomBorder[i+pl][1],p)
def turnRight(bO,tO,p):
    r = 5
    pl=0
    if(bO or tO):
        r=4
        if(tO):
            pl=1
    for i in range(r):
                for j in range(1):
                    display.set_pixel(rightBorder[i+pl][0],rightBorder[i+pl][1],p)
def playerDeath(): #If player dies a skull is displayed 
    while True:
        display.clear()
        display.show(Image.SKULL)
        sleep(2000)
def checkPlayers():#This function checks other players and displays a mouse to the cat and a cat to the mouse if they're in the same 'boxed' zone. Mice cannot see each other
    message = str(playerDX) + " " + str(playerDY) + " " + str(playerNum)
    radio.send(message)
    receiveD = radio.receive();
    if receiveD != None:
            (dx, dy, pn) = receiveD.split();
            dx = int(dx)
            dy = int(dy)
            i = 0
            j=4
            if(playerNum==1 or int(pn)==1):
                while(i <= digitalX and j <= digitalX): #The line below checks the incomming variables in relations to this player's cordinates to determine if the other player is in the same zone and needs to be displayed
                    if(((dx >= i and playerDX >= i) and (dx <= i+4 and playerDX <= i+4)) and ((dy >= j-4 and playerDY >= j-4) and (dy <= j and playerDY <= j))):
                        display.set_pixel(dx%5,dy%5,9)
                        sleep(10)
                        display.set_pixel(dx%5,dy%5,0)
                    i+=5
                    if(i >= digitalX):
                        i=0
                        j+=5
            if(int(pn)==1): #For mice players only. If the received variables are from the cat. It checks to see if the cat is ontop of the player. If so the mouse dies.
                if(dx==playerDX and dy== playerDY):
                    playerDeath()
while True: #GAME START : Displays the 'title' screen of the game. When the player first plugs in the device this shows
    display.scroll("CATch the MOUSE",delay=75)
    display.show(cat)
    sleep(500)
    display.clear()
    display.show(mouse)
    sleep(500)
    display.clear()
    display.show('A')
    sleep(500)
    display.clear()
    display.show(Image.ARROW_W)
    sleep(500)
    display.clear()
    
    if(button_a.was_pressed()):
        break
radio.on();
#After the player breaks from the title screen they get shown either a cat or mouse icon depending on what player they are
if(playerNum==1):
    display.show(cat)
else: 
    playerDX=7 #Mouse players get set to the room next to the cat. This way they aren't too far away or too close and gives them a fair chance to run.
    playerDY=2
    display.show(mouse)
sleep(3000)
display.clear()

while True: #MAIN GAME LOOP
    display.set_pixel(playerDX%5,playerDY%5,8) #Display Player
    #Set borders
    if playerDY <= 4 and tO == 0:
        tO= 1
        turnTop(lO,rO,8)
    if playerDX <= 4 and lO == 0:
        lO = 1
        turnLeft(bO,tO,8)
    if playerDY >= digitalY-5 and bO==0:
        bO=1
        turnBottom(lO,rO,8)
    if playerDX >= digitalX-5 and rO==0:
        rO=1
        turnRight(bO,tO,8)
    #Player movement
    if accelerometer.was_gesture('down'):
        display.set_pixel(playerDX%5,playerDY%5,0)
        playerDY+=1
        sleep(100)
    if accelerometer.was_gesture('right'):
        display.set_pixel(playerDX%5,playerDY%5,0)
        playerDX+=1
        sleep(100)
    if accelerometer.was_gesture('left'):
        display.set_pixel(playerDX%5,playerDY%5,0)
        playerDX-=1
        sleep(100)
    if accelerometer.was_gesture('up'):
        display.set_pixel(playerDX%5,playerDY%5,0)
        playerDY-=1
        sleep(100)
    #Turn off borders
    if playerDY >= 5 and tO == 1:
        tO=0
        turnTop(lO,rO,0)
    if playerDX >= 5 and lO== 1:
        lO = 0
        turnLeft(bO,tO,0)
    if playerDY < digitalY-5 and bO == 1:
        bO=0
        turnBottom(lO,rO,0)
    if playerDX < digitalX-5 and rO==1:
        rO=0
        turnRight(bO,tO,0)
    #Check for wall collision
    if(playerDY < 1 and tO==1):
        playerDY+=1
    if(playerDX < 1 and lO==1):
        playerDX+=1
    if(playerDY >= digitalY-1 and bO==1):
        playerDY-=1
    if(playerDX >= digitalX-1 and rO==1):
        playerDX-=1
    #Check other player positions if player isn't moving
    if not accelerometer.was_gesture('down') and not accelerometer.was_gesture('right') and not accelerometer.was_gesture('left') and not accelerometer.was_gesture('up'):
        checkPlayers()
        
#©2018 Victoria Bernhart and Gabriel Di Francesco

#Permission is hereby granted, free of charge, to any person obtaining a copy of this 
#software and associated documentation files (the "Software"), to deal in the Software without restriction, including
#without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
#the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

#The above copyright notice and this permission notice shall be included in all copies or 
#substantial portions of the Software.

#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
#TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
#THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
#CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 
#IN THE SOFTWARE. 
#TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
#THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
#CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 
#IN THE SOFTWARE.