SPIKE Prime Memory Game Tutorial with Python ***For SPIKE App version 2 (Legacy)***

Creator Academy Australia · Beginner ·🛠️ AI Tools & Apps ·4y ago

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 LEGO Wedo 2.0 Kangaroo and Joey Demonstration
LEGO Wedo 2.0 Kangaroo and Joey Demonstration
Creator Academy Australia
2 LEGO Wedo 2.0 Koala Demonstration
LEGO Wedo 2.0 Koala Demonstration
Creator Academy Australia
3 LEGO WeDo 2.0 Eagle vs Rabbit Demonstration
LEGO WeDo 2.0 Eagle vs Rabbit Demonstration
Creator Academy Australia
4 Animatronic Australia: The Kangaroo
Animatronic Australia: The Kangaroo
Creator Academy Australia
5 How Semiconductors Work: 3D Animation
How Semiconductors Work: 3D Animation
Creator Academy Australia
6 LEGO Wedo 2.0 Koala
LEGO Wedo 2.0 Koala
Creator Academy Australia
7 First impressions and unboxing First Lego League: Into Orbit 2018
First impressions and unboxing First Lego League: Into Orbit 2018
Creator Academy Australia
8 FLL Into Orbit Team Ranking Gears talks about Artificial Gravity
FLL Into Orbit Team Ranking Gears talks about Artificial Gravity
Creator Academy Australia
9 FLL Into Orbit team Ev3lution discusses isolation in space
FLL Into Orbit team Ev3lution discusses isolation in space
Creator Academy Australia
10 PI3-BOT (Pie Bot) Prototype
PI3-BOT (Pie Bot) Prototype
Creator Academy Australia
11 Dynamic display stand for LEGO Bugatti Chiron (preview)
Dynamic display stand for LEGO Bugatti Chiron (preview)
Creator Academy Australia
12 Creative Kids TV announcement featuring Creator Academy
Creative Kids TV announcement featuring Creator Academy
Creator Academy Australia
13 LEGO WeDo 2.0 Guided Lesson: Milo
LEGO WeDo 2.0 Guided Lesson: Milo
Creator Academy Australia
14 LEGO SPIKE PRIME: Turtle Prime Demonstration
LEGO SPIKE PRIME: Turtle Prime Demonstration
Creator Academy Australia
15 LEGO SPIKE PRIME "Wheelchair Prime"
LEGO SPIKE PRIME "Wheelchair Prime"
Creator Academy Australia
16 10 Reasons why EV3 is SUPERIOR to SPIKE Prime
10 Reasons why EV3 is SUPERIOR to SPIKE Prime
Creator Academy Australia
17 Your first launch after installing Python: Python for Kids Webinar Highlight
Your first launch after installing Python: Python for Kids Webinar Highlight
Creator Academy Australia
18 What is "Sharpening the Saw": Python for Kids Webinar Highlight
What is "Sharpening the Saw": Python for Kids Webinar Highlight
Creator Academy Australia
19 Why We Use Comments: Python for Kids Webinar Highlight
Why We Use Comments: Python for Kids Webinar Highlight
Creator Academy Australia
20 Build a Scratch Dinosaur Park (Full Lesson)
Build a Scratch Dinosaur Park (Full Lesson)
Creator Academy Australia
21 Scratch 3.0: Cat and Mouse Game Stream (Krunal)
Scratch 3.0: Cat and Mouse Game Stream (Krunal)
Creator Academy Australia
22 Python Tutorial: Make a Toilet Paper Calculator (Full Lesson)
Python Tutorial: Make a Toilet Paper Calculator (Full Lesson)
Creator Academy Australia
23 Scratch 3.0 Tutorial: Making a chatbot that plays 'heads or tails' (Full Lesson)
Scratch 3.0 Tutorial: Making a chatbot that plays 'heads or tails' (Full Lesson)
Creator Academy Australia
24 Getting Started with MicroPython on SPIKE PRIME (old)
Getting Started with MicroPython on SPIKE PRIME (old)
Creator Academy Australia
25 Easter Egg Game Tutorial in Scratch (Full Lesson)
Easter Egg Game Tutorial in Scratch (Full Lesson)
Creator Academy Australia
26 Accessing Weather API with Python (Highlights)
Accessing Weather API with Python (Highlights)
Creator Academy Australia
27 Accessing Translate API with Python (Highlights)
Accessing Translate API with Python (Highlights)
Creator Academy Australia
28 Scissors, Paper, Rock with Scratch 3.0 (Highlights)
Scissors, Paper, Rock with Scratch 3.0 (Highlights)
Creator Academy Australia
29 Making a really scary game with Scratch 3.0 (Highlights)
Making a really scary game with Scratch 3.0 (Highlights)
Creator Academy Australia
30 Making a Lemmings game with Scratch 3.0 (Highlights)
Making a Lemmings game with Scratch 3.0 (Highlights)
Creator Academy Australia
31 Space Navigation Game in Scratch (Highlights)
Space Navigation Game in Scratch (Highlights)
Creator Academy Australia
32 Scratch Supercat Game (Full Lesson)
Scratch Supercat Game (Full Lesson)
Creator Academy Australia
33 Introduction to Robots with LEGO Wedo 2.0 (Full Lesson)
Introduction to Robots with LEGO Wedo 2.0 (Full Lesson)
Creator Academy Australia
34 Getting Started With LEGO Spike Prime (Full Lesson)
Getting Started With LEGO Spike Prime (Full Lesson)
Creator Academy Australia
35 Scratch Walk Cycle Animation Tutorial (Full Lesson)
Scratch Walk Cycle Animation Tutorial (Full Lesson)
Creator Academy Australia
36 Building a Scissor Lift with LEGO Simple Machines (Highlights)
Building a Scissor Lift with LEGO Simple Machines (Highlights)
Creator Academy Australia
37 EV3 datalogging RPM challenge (highlights)
EV3 datalogging RPM challenge (highlights)
Creator Academy Australia
38 Learning about buses with LEGO (Full Lesson)
Learning about buses with LEGO (Full Lesson)
Creator Academy Australia
39 Make "Flappy Bird" in Scratch 3.0 (Highlights)
Make "Flappy Bird" in Scratch 3.0 (Highlights)
Creator Academy Australia
40 "Memory Arrows" game with Scratch 3.0 Lists (Highlights)
"Memory Arrows" game with Scratch 3.0 Lists (Highlights)
Creator Academy Australia
41 Learning about Gear Ratios with LEGO Simple Machines (Highlights)
Learning about Gear Ratios with LEGO Simple Machines (Highlights)
Creator Academy Australia
42 Scratch 3.0 Bouncing Ball with Stretch, Speed and Acceleration (Full Lesson)
Scratch 3.0 Bouncing Ball with Stretch, Speed and Acceleration (Full Lesson)
Creator Academy Australia
43 LEGO SPIKE Prime Order Tracking Robot (Highlights)
LEGO SPIKE Prime Order Tracking Robot (Highlights)
Creator Academy Australia
44 Scratch 3.0 Race Guesser (Full Lesson)
Scratch 3.0 Race Guesser (Full Lesson)
Creator Academy Australia
45 How Banks Work - a quick explanation for kids
How Banks Work - a quick explanation for kids
Creator Academy Australia
46 SPIKE Prime Safe + importance of passwords (Highlights)
SPIKE Prime Safe + importance of passwords (Highlights)
Creator Academy Australia
47 Combination Safe Coding Guide with LEGO SPIKE Prime
Combination Safe Coding Guide with LEGO SPIKE Prime
Creator Academy Australia
48 LEGO SPIKE Prime Parcel Scanning Robot
LEGO SPIKE Prime Parcel Scanning Robot
Creator Academy Australia
49 Building "Addy" the Addition Chatbot
Building "Addy" the Addition Chatbot
Creator Academy Australia
50 SPIKE 2.x PID Balancer with Scratch for SPIKE Prime / 51515: Gyro Girl
SPIKE 2.x PID Balancer with Scratch for SPIKE Prime / 51515: Gyro Girl
Creator Academy Australia
51 What is Procedural Programming
What is Procedural Programming
Creator Academy Australia
52 Zombie game in SCRATCH (plus horrifying zombie facts!)
Zombie game in SCRATCH (plus horrifying zombie facts!)
Creator Academy Australia
53 Swarming Bugs Scratch Game
Swarming Bugs Scratch Game
Creator Academy Australia
54 LEGO Mindstorms/SPIKE Prime Scanner and Printer
LEGO Mindstorms/SPIKE Prime Scanner and Printer
Creator Academy Australia
55 Fishing Game with SCRATCH (For Fathers Day!)
Fishing Game with SCRATCH (For Fathers Day!)
Creator Academy Australia
56 Measuring Paper Plane Speed with SPIKE Prime Line Graphs
Measuring Paper Plane Speed with SPIKE Prime Line Graphs
Creator Academy Australia
57 Comparing Mindstorms Robot Inventor and SPIKE Prime
Comparing Mindstorms Robot Inventor and SPIKE Prime
Creator Academy Australia
58 Code a 'Knock Knock Joke' game in Scratch 3.0
Code a 'Knock Knock Joke' game in Scratch 3.0
Creator Academy Australia
59 Creator Academy Australia Live Stream
Creator Academy Australia Live Stream
Creator Academy Australia
60 Discovering LEGO Mindstorms/SPIKE Gyro Pitch Deadzone
Discovering LEGO Mindstorms/SPIKE Gyro Pitch Deadzone
Creator Academy Australia

Related Reads

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
Up next
How to Clean Up Your Backlog with AI & MCP | Businessmap Quick Tips
Businessmap
Watch →