SPIKE Prime Memory Game Tutorial with Python ***For SPIKE App version 2 (Legacy)***
Skills:
AI Pair Programming70%
Key Takeaways
Codes a memory game using Python with the LEGO Education SPIKE Prime
Full Transcript
g'day and welcome to another episode of python for lego robotics today we are going to make this awesome memory game using lego spike prime and python lists [Music] there's something about the game master that makes it one of the least popular projects in class maybe because the face looks a little bit creepy with these gigantic lips and these hypnotic eyes it's almost as if it's trying to tell you to do something to code the game master i'm going to teach you to use python lists and to do that we're going to have to do some warming up so if you've done coding with another programming language before then you'll probably know a list as an array and python lists are pretty much the same they are a collection of objects and these can be integers floats or strings or even other lists i'm going to create a list of my favorite foods here so here i'm going to go favorite foods and a list is denoted by these square brackets so if you press open square bracket then you can start typing the items inside your list so these are my favorite foods spag bowl pizza and and mash okay so here are my favorite foods and make sure that you separate everything with a comma and because my favorite foods are strings which means another word for words i also need to put quotation marks around them the most important thing about using lists is that you can access the items inside a list simply by using square brackets and putting the right index number inside in a python list the first item uses the index of zero the second item uses an index of one the third item uses an index of two and so forth if you wanted to go the other way and get the last item of a list you can use negative indexes so to get the last item of a list you just use negative 1 the second last item is negative 2 and so forth all right so here we're going to demonstrate how to retrieve an item in my list here instead of showing the image happy i'm going to get the line matrix to actually write the first item in my list so to do that we go favorite foods open square bracket zero close square brackets and then when i press play it should get the first item in my list which is spag bolt okay right so it is working so uh you should do the same thing with your robot you can put down your favorite favorite foods into a list and then see if you can retrieve the right item from that list the other cool thing about lists is that you can add things to the end of it by using something called the append method so here i'm going to look again at my code and i'm going to add an item at the end of my list so favorite foods dot append and then if i put in my brackets uh burgers then uh this list will now have four items in the list and i can also make it write it out by typing out negative one which if you remember is the last item in the list okay so if i hit pre if i press play burgers right so that's looking really really good so far working with this is super easy right but before we jump into the code let's play a game in my brand new segment [Music] to play songs in code all you have to do is look at the clue that i've written in python over here this clue represents a popular song i wonder what this song can be write your guess and explain why in the comments section below the comment that identifies the song title the artist and best explains the code will get pinned all right let's go back to the code now that we're familiar with lists it's time to go into our code so let's clear out all of our exercise and you can rename your project to python game master and like always you can find all this code in my github link in the comments in the description below so now we are going to create our color list so here we go color list equals open and close square bracket so what this does is it creates an empty list okay so normally we would fill this up with items but we don't want to fill it up just yet we're going to leave it blank because we want our game master to actually scan the blocks and add the colors in dynamically all right after that we're going to set our mouth motor kills motor a and our scanner is the color sensor and motor b next we're going to define a function now to define a function we use the def keyword eat blocks this is the function where the game master is actually going to retrieve those game blocks and gobble them up so here we go mouth dot run for seconds 2.1 comma negative 50. so the first argument over here is the number of seconds that we're running for and negative 50 is the speed at which we're running okay next we're going to scan the colors we'll actually define the code to scan the colors so here def scan colors this is where we after you've eaten your blocks after the game master has gobbled up the blocks it's going to spit out the blocks one by one to start scanning and adding those colors into the into the array or into the list so for i in range five this means i'm going to do this for five times hub.status light dot on we're going to turn it on to a special color uh the color is going to be scanner.get underscore color all right so if you've seen one of my old color sensor tutorials you'll understand how this works but basically the status light is going to light up with a string and that string is either something like red yellow violet blue green or whatever and you can get that color by going using the color sensors get color method okay the same time we're going to append that color to our list so color list dot append scanner dot get underscore color make sure you close all your brackets and make sure that you have these indentations correct as well mouth dot run for degrees 95 degrees at a speed of 50. so this is where we have scanned our first block and now we're moving to the second block so we get our mouth motor to spit out the next block and then we're going to go wait for half a second and then we're going to also turn off the light just so that we got a clear separation of the colors okay all right so that's looking good so far we also need a third function a third function to replay the colors that we have scanned and this is what the lists are good for because lists are great for us to keep a record of the colors uh that we have scanned and now we're going to use it to um to replay those colors so define replay colors for i in range 5 again hub.status light dot on and what are we going to do we're looping through um up to five again so i is iterating through zero one two three and four and what else is uh starting at zero and ends in four that's right it's the colorless right because out the indexes of our color list start at zero and end in four because there are five colors so now all you have to do is go hub.statuslight.on and then we'll just read out whatever is on this iteration of the color list just like that color list i because i starts off at zero so we will it will start off turning the light on the zero width item in the list and then one and then two and then three and then four next we'll wait for another half second and then we can turn the light off at the end okay that's looking good so now we're going we've got all three of our functions but having these functions defined is not going to actually make our robot move it's because we just defined the function we haven't actually called the function or executed the function so here we're going to actually execute the function now and for this we are going to use a loop while true during this loop we are going to check if we have pressed the left button or the right button pressing the left button will scan the blocks pressing the right button will replay the blocks okay so here if hub.left button dot is pressed color list equals so we are blanking out the color list and then we go eat blocks and then we're going to scan each color by using our scan colors and then we're going to wait for a second before we replay the colors so straight after we eat the blocks we're going to go replay our colors otherwise if we press the right hand side button then we're just going to replay the colors so here lf hub.right button. is pressed and the length of color list is equal to five okay so why am i doing this and operator i want to make sure that when i press the right button i also check if my color list is being populated because remember when we start off we have no no list of colors so we are just putting this check here to make sure that we only want to replay the colors if there is actually something in that list and then if there is we just go replay colors okay super easy and this code makes good use of lists as well and now we're going to test it out to see if there are any errors running no red light for me all right that's good so let's have a look at my code i'm my robot okay so here uh is my robot first of all i'm going to uh yep i'm going to press the right button nothing's going to happen because there's nothing in the list i put in my colored blocks and then i press the left button whoops whoops let's do that again because i couldn't didn't eat the blocks properly whoops okay here we go let's do that again here we go now that's proper all right so it's green blue purple red yellow okay good so now and then it replaced the colors as well and then now if i press the right hand button it's going to also replay the list again okay so how you'd play this game is you'd probably uh scan it uh scan one person's uh string of blocks and then play the um play the colors and then see if the uh other person can rebuild the same sequence using their color blocks i want to thank the sponsor of today's video more educational ever since creator academy started making lego education videos we have been greatly supported by the experts at more educational they are an authorized partner of lego education with over 20 years experience working with lego education products so if you're in australia and you want to buy genuine lego education products like the ones shown in this video then make sure you check out the more educational website that's it from me today i'll see you next time bye bye you
Original Description
***For SPIKE App version 2 (Legacy)***
Use the LEGO Education SPIKE Prime to code the Game Master model from LEGO SPIKE Prime.
BRAND NEW SEGMENT: SONGS IN CODE! Check out the clue inside the video, and play along in the comments section!
00:48 - Python Lists introduction
04:21 - Mr. Code's Songs in Code
05:00 - Coding Game Master functions
10:00 - Coding Game Master events
To buy SPIKE Prime, visit https://www.mooreed.com.au/
Project files:
https://github.com/CreatorAcademyAu/PythonGameMaster
Big thanks to musician Anthony Tambrin for creating the Songs In Code jingle: https://linktr.ee/anthonytambrin
Vocals:
- Aaron Tambrin
- Hiroaki Xu
- Michelle Lim
- Mikaela Phaon
- Oliver Kusumo
For more in-depth science, building and coding on this subject, visit www.creatoracademy.com.au
LEGO® is a trademark of the LEGO Group of companies which does not sponsor, authorise, or endorse this video.
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Creator Academy Australia · Creator Academy Australia · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
LEGO Wedo 2.0 Kangaroo and Joey Demonstration
Creator Academy Australia
LEGO Wedo 2.0 Koala Demonstration
Creator Academy Australia
LEGO WeDo 2.0 Eagle vs Rabbit Demonstration
Creator Academy Australia
Animatronic Australia: The Kangaroo
Creator Academy Australia
How Semiconductors Work: 3D Animation
Creator Academy Australia
LEGO Wedo 2.0 Koala
Creator Academy Australia
First impressions and unboxing First Lego League: Into Orbit 2018
Creator Academy Australia
FLL Into Orbit Team Ranking Gears talks about Artificial Gravity
Creator Academy Australia
FLL Into Orbit team Ev3lution discusses isolation in space
Creator Academy Australia
PI3-BOT (Pie Bot) Prototype
Creator Academy Australia
Dynamic display stand for LEGO Bugatti Chiron (preview)
Creator Academy Australia
Creative Kids TV announcement featuring Creator Academy
Creator Academy Australia
LEGO WeDo 2.0 Guided Lesson: Milo
Creator Academy Australia
LEGO SPIKE PRIME: Turtle Prime Demonstration
Creator Academy Australia
LEGO SPIKE PRIME "Wheelchair Prime"
Creator Academy Australia
10 Reasons why EV3 is SUPERIOR to SPIKE Prime
Creator Academy Australia
Your first launch after installing Python: Python for Kids Webinar Highlight
Creator Academy Australia
What is "Sharpening the Saw": Python for Kids Webinar Highlight
Creator Academy Australia
Why We Use Comments: Python for Kids Webinar Highlight
Creator Academy Australia
Build a Scratch Dinosaur Park (Full Lesson)
Creator Academy Australia
Scratch 3.0: Cat and Mouse Game Stream (Krunal)
Creator Academy Australia
Python Tutorial: Make a Toilet Paper Calculator (Full Lesson)
Creator Academy Australia
Scratch 3.0 Tutorial: Making a chatbot that plays 'heads or tails' (Full Lesson)
Creator Academy Australia
Getting Started with MicroPython on SPIKE PRIME (old)
Creator Academy Australia
Easter Egg Game Tutorial in Scratch (Full Lesson)
Creator Academy Australia
Accessing Weather API with Python (Highlights)
Creator Academy Australia
Accessing Translate API with Python (Highlights)
Creator Academy Australia
Scissors, Paper, Rock with Scratch 3.0 (Highlights)
Creator Academy Australia
Making a really scary game with Scratch 3.0 (Highlights)
Creator Academy Australia
Making a Lemmings game with Scratch 3.0 (Highlights)
Creator Academy Australia
Space Navigation Game in Scratch (Highlights)
Creator Academy Australia
Scratch Supercat Game (Full Lesson)
Creator Academy Australia
Introduction to Robots with LEGO Wedo 2.0 (Full Lesson)
Creator Academy Australia
Getting Started With LEGO Spike Prime (Full Lesson)
Creator Academy Australia
Scratch Walk Cycle Animation Tutorial (Full Lesson)
Creator Academy Australia
Building a Scissor Lift with LEGO Simple Machines (Highlights)
Creator Academy Australia
EV3 datalogging RPM challenge (highlights)
Creator Academy Australia
Learning about buses with LEGO (Full Lesson)
Creator Academy Australia
Make "Flappy Bird" in Scratch 3.0 (Highlights)
Creator Academy Australia
"Memory Arrows" game with Scratch 3.0 Lists (Highlights)
Creator Academy Australia
Learning about Gear Ratios with LEGO Simple Machines (Highlights)
Creator Academy Australia
Scratch 3.0 Bouncing Ball with Stretch, Speed and Acceleration (Full Lesson)
Creator Academy Australia
LEGO SPIKE Prime Order Tracking Robot (Highlights)
Creator Academy Australia
Scratch 3.0 Race Guesser (Full Lesson)
Creator Academy Australia
How Banks Work - a quick explanation for kids
Creator Academy Australia
SPIKE Prime Safe + importance of passwords (Highlights)
Creator Academy Australia
Combination Safe Coding Guide with LEGO SPIKE Prime
Creator Academy Australia
LEGO SPIKE Prime Parcel Scanning Robot
Creator Academy Australia
Building "Addy" the Addition Chatbot
Creator Academy Australia
SPIKE 2.x PID Balancer with Scratch for SPIKE Prime / 51515: Gyro Girl
Creator Academy Australia
What is Procedural Programming
Creator Academy Australia
Zombie game in SCRATCH (plus horrifying zombie facts!)
Creator Academy Australia
Swarming Bugs Scratch Game
Creator Academy Australia
LEGO Mindstorms/SPIKE Prime Scanner and Printer
Creator Academy Australia
Fishing Game with SCRATCH (For Fathers Day!)
Creator Academy Australia
Measuring Paper Plane Speed with SPIKE Prime Line Graphs
Creator Academy Australia
Comparing Mindstorms Robot Inventor and SPIKE Prime
Creator Academy Australia
Code a 'Knock Knock Joke' game in Scratch 3.0
Creator Academy Australia
Creator Academy Australia Live Stream
Creator Academy Australia
Discovering LEGO Mindstorms/SPIKE Gyro Pitch Deadzone
Creator Academy Australia
More on: AI Pair Programming
View skill →Related Reads
📰
📰
📰
📰
My first weekend challenge
Dev.to · Dina
Nomad AI Review 2026: Can This AI-Powered Travel Platform Help You Build an Online Business?
Medium · AI
The Only 4 Claude Prompts I Use Every Single Day
Medium · Programming
The Most Valuable Way to Use AI in Your Business Is Also the Most Boring
Medium · AI
Chapters (4)
0:48
Python Lists introduction
4:21
Mr. Code's Songs in Code
5:00
Coding Game Master functions
10:00
Coding Game Master events
🎓
Tutor Explanation
DeepCamp AI