element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
microbit
  • Learn
  • Learning Center
  • STEM Academy
  • microbit
  • More
  • Cancel
microbit
micro:bit Blog [Project] micro:bit game - CATch the MOUSE!
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join microbit to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: straytech
  • Date Created: 10 Dec 2018 5:38 PM Date Created
  • Views 1083 views
  • Likes 3 likes
  • Comments 0 comments
  • micro:bit education giveaway
  • micro:bit project
  • the great micro:bit education giveaway
Related
Recommended

[Project] micro:bit game - CATch the MOUSE!

straytech
straytech
10 Dec 2018

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.

Attachments:
imageCATchtheMOUSE.pdf
  • Sign in to reply
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube