How to use an SSD1306 and Potentiometer on the Pico to make a Video Game!
HTML-код
- Опубликовано: 14 июл 2024
- My previous video showed you how to do three quick projects to get you started on your Pico microprocessor journey. Today, I show you how, by adding an OLED display and a potentiometer, you can write a fun little Space Invaders clone.
0:00 Introduction
0:30 Initial Breadboard Setup
1:55 Controlling an SSD1306 OLED Display
5:36 Additional Hardware for Breadboard
6:56 Pico Invaders Demo
7:36 Uploading Standalone Programs to your Pi
8:50 Code Walkthru
16:47 Customization through Variable Manipulation
17:30 Outro
ssd1306.py:
github.com/micropython/microp...
Pico Invaders:
github.com/printnplay/Pico-Mi...
Download the Thonny IDE:
thonny.org/
Download the MicroPython firmware for your Raspberry Pi Pico:
micropython.org/resources/fir...
----
Interested in Print N Play Merch? Check it out here!
www.redbubble.com/people/Prin...
Pick up your board here!
www.raspberrypi.org/products/...
-- Follow me on the Intrawebs! --
Twitter - @jcprintnplay
Instagram - @jcprintnplay
Facebook - / jcprintnplay
Thingiverse - www.thingiverse.com/jcprintnp...
Reddit - / printnplayyt
-- Like what I'm doing here? Want early access to content, models, and more? Check me out on Patreon !
www.patreon.com/jcprintnplay
#RaspberryPi #Pico #MicroPython - Наука
Great tutorial!
Holy Cow! I'm literally watching your latest video right now! Thanks Ben!
The end result and your effort to write this game were awesome!
Thankyou. An easy to follow video for my 9yo grandsons.
I changed difficulty
I have a project in mind for some SSD1306 displays. This really explains what's possible with it with the pico. Great video, really thorough and concise.
thank you for cutting out the fluff most youtubers have, i had lot of questions about this and you were very concise and strait to the piont.
Glad you found it helpful! Let me know if i can answer any questions for you!
Thank you for pico tutorials, I was looking for one and found at last!💯💕
Glad you enjoyed them!
Damn! You're really good with pico. Amazing.
Excellent work. Well done!
Very impressive and very well done! Thanks!
Awesome project. Thanks for this
Thanks for posting this - I just got a pimoroni pico system handheld coming in soon and I’ve never learned how to code games - so far, I was able to follow your instructions and explanations on what does what, for a complete noobie, very important. I hope I can code a game similar to this one and even maybe add color to it and aliens shooting as well - good video and I subbed :)
Excellent work James, very well explained and exciting (especialy your Enthusiam) to view the video. I have been using pico for about 1yr on instrumentation but thus is awesome. I will make this for my grand children.
Thank you so much for your time and kind words! I am so glad you enjoyed it! I have some more games programed and ready to go, just need to find time to video them. Glad to hear you will get some enjoyment from it!
Great project James. Keep up the great work.
Thanks JT! Good to see you around!
Awesome job James
Great work, made my day.
Thank you so much for this video
Great explanations. Thanks!
Amazing project . Thank you so much
Very nice. Thumbs up and subscribed.
Nice project!
This is so cool! Thanks
6:40 counting the pins, if the pico can be placed left most then the breadboard has numbers to help.
May not need to solder the pins on, just pin the pico board with the pin from the lead(s) to the breadboard.
top left 3.3V is 5th from corner and bottom right third from the corner is ground. (pico is now attached)
Take the official layout jpg and replace numbering 40-21 with 1-20 (copy from left side) to make it easier !
use jpg as background for desktop or load it in your web browser as 1 of the homepage(s)
This gives me hope for my very simple needs! I think even I can do it.
Really man. Great tutorial. I just subbed. Ciao
well done!!
WOW, great work
Thank you!
This guy is a genius.
Fascinating.
That's awesome 🤩
Very nice!
Excellent
So fun!
Totally Awesome thanks
Nice. Thanks :)
Great tutorial. No nonsense and bla bla, direct to the point. Love it!
Question: how did you create the logo?
I created a 128x64 black and white bitmap, and found a font that I liked for the text. Then I drew the alien out pixel by pixel. Then I used the pillow library for python to import the image and generated a bitarray from it.
@@PrintNPlay Please provide a code with example if possible.
@@kamilkarchut8362 Sure! I'm working on a tutorial but, for the short form. You need the full install of Python on your computer, plus the Python Image Library (PIL) installed. From there, you can use this function. You pass it your image name, as well as its x and y size.
def ConvertImage(ImageName, x, y):
im = Image.open(ImageName)
resized_im = im.resize((x, y), Image.BILINEAR)
monochrome_im = resized_im.convert('1')
monochrome_im.tobytes()
Dude, your intro is perfect. "I'm James and welcome back to Print 'N Play". I am so tired of all the over produced 40 second intro spews. Scot Adams mentioned on his podcast that youtube should auto-start 40 seconds in. Great video as always.
fabbo, will try this one :)
Awesome
Regarding improvements: I think this code could be re-factored into separate definitions rather than a long script. That said, the code was ready to read. Great video👍
Couldn't agree more. While the code is functional, there are definitely segments that could be their own subroutines. I will have a new game next week that does this a bit better. I'm afraid I am a bit rusty on the coding side
I found your invaders game code from someone else's github then was wondering why this PnP seems familiar. Later I remember that I learnt the morse code interpreter from your video and code, also I've added oled output along with it, looks cool to get output displayed in the oled. Thank you for all the Pico projects, they really help! PnP subscribed :)
Glad you enjoyed the examples, and adding an OLED to the Morse code one is a fantastic enhancement! And glad you found your way back here.
@@PrintNPlay Hey I was just trying to add the potentiometer to the circuit as you've shown in the video but the ship's not moving as expected, it's just shaking randomly. Can you please tell me which potentiometer did you used? I had a 10k lying around and I was trying with that.
I'm pretty sure it was also a 10K pot. Does the movement seem to be related to you turning the pot, or is it random even when you aren't turning it?
@@PrintNPlay It's random. And also I was getting OSError: [Errno 5] EIO. So I thought maybe the signal pin from the pot was giving some noise so I removed it. Now strangely the random motion is still there even after removing the pot and it's wire. FYI, I have the morse interpreter circuit alongwith it in a single board, do you think that might be an issue?
So, since the potentiometer is read by checking the voltage coming in on an ADC pin, if there is nothing connected to that pin it may be left in a floating state. Static in the air, random em interference, etc, causes the voltage to read random values. And that's what causes the jerking on the ship.
Is it possible you had the potentiometer connected to the wrong pin? I don't have access to my source code at the moment, so I can't remember if any of the pins from the Morse code and space invaders game overlap in bad ways.
This is screaming for asteroids.
Must be harder to code due to the gravitational simulation and the vectored asteroids exploding in multiple parts, all in that tiny display
Great job, you beat me to it, I was going to do the same (just waiting on some screens to arrive to go with my Pico). Will the speed of your aliens increase as the number of them reduces? In particular when you are down to a few or just one, just like the real Space Invaders? I'm also wondering what performance is like when using Micro Python vs C/C++?
Hey! You can always do a better job of it than me! The alien speeds are constant, although they increase as the levels change. It was more of an experiment to show it at a code level that's good for beginners, so I kept the bells and whistles to a minimum.
Performance in C/C++ will undoubtedly be better, since Python adds an additional layer to make it all work. I am waiting for some more hardware to make a C based one. Stay tuned!
Oh, and if you code your own Space Invaders clone, feel free to send me a link! Love to see how other people tackle the task
👏🕹
hey,
very cool project!
But how did you convert images to the bytebuffers?
An answer would be very helpful
I'm working on a tutorial but, for the short form. You need the full install of Python on your computer, plus the Python Image Library (PIL) installed. From there, you can use this function. You pass it your image name, as well as its x and y size.
def ConvertImage(ImageName, x, y):
im = Image.open(ImageName)
resized_im = im.resize((x, y), Image.BILINEAR)
monochrome_im = resized_im.convert('1')
monochrome_im.tobytes()
Where did you find a frambuf library with blit in it, I can't seem to find one all mine are missing that command.
Great stuff...
Is it possible to modify your code to work with WaveShare RGB LED matix 64x64 displays???
Next, to do it on E-Paper: obviously a complete mis-use of the technology. (And check the datasheet to see that it won't brick your display).
6:36 - my first Pi analog (direct) input!
I am looking at epaper modules now... Hehehe
Great tutorial, how about and asteroids type game but with a wii nunchuck for control?
Hrmm... I will look at how to interface the nunchuck with the Pico. No promises ,but something fun to try!
animation sequences could be kept in an array, this way you don't have to have if-else
That's a great idea
Thank you! This is the Project i was looking for my kid! But, how do i add buttons for left , right and the fire?? Thanks for helping out!
Be glad to help you with the modifications! Are you on twitter by any chance? Might be a good candidate for a direct message! Let me know. If not, I am planning to modify this for buttons shortly, so it's compatible with the other games I have developed for the Pico
Hi! As long as I am not into Twitter i thought it will be the better way to place a PullRequest or an issue(issue is the wrong word for that!) on your Github repository. So everybody else can follow it and search for FAQ, solved problems and so on. What do you think?
@@andreaslubeck7716 sorry about the delay. Code changes are done, will be pushing to git soon!
@@PrintNPlay ...great news! Thank you very much for the effort!
Как вы подготавливаете изображение чтобы получить из него массив данных? И почему некоторые массивы 16-ричные, а некоторые нет?
great ... next is flappy bird 😊
Haha! I mean... maybe
Great video, but do you know how to use buttons instead of a potentiometer?
Sure do! A special version of the game was made for the Alien 3D UFO box. Here's a link to the newsletter. If you scroll through it you'll find a wiring diagram, as well as a link to my source code for the build using buttons.
www.alien3d.us/july-2021-ufo/
how did you convert images to byte array? I'm trying but only meaningless lines and pixels come out
I'm working on a tutorial but, for the short form. You need the full install of Python on your computer, plus the Python Image Library (PIL) installed. From there, you can use this function. You pass it your image name, as well as its x and y size.
def ConvertImage(ImageName, x, y):
im = Image.open(ImageName)
resized_im = im.resize((x, y), Image.BILINEAR)
monochrome_im = resized_im.convert('1')
monochrome_im.tobytes()
Pls make an arcadestick with 0.96 inch screen that showing what buttons are pressed on screen.
Check this one out, it's pretty close. You could easily modify it to have the display the button info!
ruclips.net/video/__QZQEOG6tA/видео.html
YOUR 999 like is mine . very good game , and perfect explanation congratulations .
the game works fine . Pity we have to sacrify one raspberry pico for only one game. I would like to put more things inside this program like animations , and images , controlled by buttons ... is that possible?? I have tried but no results .... the game seems to be a whole thing ...... can not control the start of the game ......
good explanation any way .
Print 'N Play, excellent video!
I am trying to adapt your program to my ESP8266. I have modified Pot=ADC(0) and speaker = PWM(Pin(5)) as those are the only ports that would make it work on the ESP8266.
However, I keep getting errors :(
Any help will be greatly appreciated.
Keep up the good work!!!!
I haven't done of coding on the esp8266, but I can try to help! What are the errors you are getting?
Can you tell me how the aliens get faster? The "difficulty" variable is obviously related but I cannot see how this value changes the actual speed of the aliens :-)
So, I originally posted that I never implemented it, but I guess my ctrl+f abilities are a little lack luster. This is the line that increases the speed, by removing one from the loop that controls when the aliens move.
if loopCount > 16 - difficulty:
@@PrintNPlay Hi, yes thanks for that. I actually managed to work it out soon after i posted that request 🤦👍😊
got your code working on an 1106 OLED
Awesome! Is that the 1.3" version?
@@PrintNPlay yes 1.3 bought by accident but it works with the proper libraries from Github
Glad to hear it! I had built a stream deck using one of them a while ago. I love how clear they are
Hey I'm using the sh1106 lib too, I used this code but it's not taking input from the potentiometer maybe the "pot" or its function "read_u16" is not defined in this library, but idk how to make it work or make a function in the library myself so that it takes input , could you help?
@@ashutoshm.5920 I had some issues with the potentiometer also. Ended up being wired incorrectly!
Hi...love your video about video on the Pico but I have faithfully copied and pasted your code for ssd1306 and Pico pirates but when I try to run it in Thonny I just get the following errors:
Traceback (most recent call last):
File "", line 20, in
File "ssd1306.py", line 110, in __init__
File "ssd1306.py", line 36, in __init__
File "ssd1306.py", line 71, in init_display
File "ssd1306.py", line 115, in write_cmd
Any ideas what is happening and what I can do to get the program to run?? Tearing my hair out...the errors messages are meaningless to me !!
So, the error seems to be coming from the init subroutine for the display, which usually indicates the screen is incorrectly connected, or the screen is not the right type.
Let me know if the wiring is for sure correct and we can go from there!
@@PrintNPlay YESSSSSS...its ALIVE...After finally being able to prove or disprove all my stock of OLED's I finally found a way to prove them using the I2C_Scanner sketch for Arduino. That proved that all my stock of OLED's was "stuffed" since the scanner did not return any address for each OLED tried. Luckily I had suspected them for a while and had ordered a bunch of new ones . I then checked those when they arrived today and proved that they worked by checking their addresses (0x3C). I then wired up your "Pico Invaders" code but powered the OLED up by a separate and known power source (both 3.3v and 5v) to start with, ran the code and Hey...IT WORKED. I then proved the OLED would work from the Pico pins at physical Pins 36(+) and 38(-)and sketch in a couple of ways but it is all working nicely now...MANY, MANY thanks to you P'N'P !!! Hope this helps other viewers who try and fail at first but stick with it.
Could you make a version of this game in the C programming language ?
My C skills are a little rusty, but I am getting back into it... Few more projects to finish ahead of time, but I'll see what I can do!
@@PrintNPlay thanks a s lot!
The best learning lesson is how much of a realtime system it gets when you dont have a multitasking Linux system laying down; a tiny pico processor like this can run a realtime game with input and responses even if it's coded in python; I bet with C coding you can make a full blown game like 1942 or shooter like that run in the pico
Sir I am facing an error could u please help me out
The printed error is given below
File "", line 20, in
File "/lib/ssd1306.py", line 110, in __init__
File "/lib/ssd1306.py", line 36, in __init__
File "/lib/ssd1306.py", line 71, in init_display
File "/lib/ssd1306.py", line 115, in write_cmd
OSError: 5
BTW nice and concise explanation...thank you so much sir!
Hey there, first thing to check is the wiring on the screen. Errors are coming from the screen library, which is often caused by incorrect wiring. Hope that helps!
@@PrintNPlay Thanks for ur relpy sir , I have checked wiring multiple times it seems correct but still I am facing this error anyways I will try to sort out...Thanku so much sir!
I'm trying to do space invaders with buttons. I copied and pasted everything from github, but I keep getting this error:
Traceback (most recent call last):
File "", line 28, in
File "ssd1306.py", line 118, in __init__
File "ssd1306.py", line 37, in __init__
File "ssd1306.py", line 74, in init_display
File "ssd1306.py", line 123, in write_cmd
OSError: [Errno 110] ETIMEDOUT
I suspect it is a wiring issue. Is there a schematic for the game with buttons I could use?
Parts of the code that make it fail on newer python versions. I2C requires the sda,scl pins to be set. The lowres mode will also fail. Add and replace the I2C init lines below to your game code:
# Init I2C using I2C0 defaults, SCL=Pin(GP9), SDA=Pin(GP8), freq=400000
i2c = I2C(0, scl=Pin(9), sda=Pin(8), freq=400000)
oled = SSD1306_I2C(WIDTH, HEIGHT, i2c, 0x3C)
lowres = False
No fault of James, but python and its libraries are feckin pain when they decide to update or 'make things worse than before', it screws all your projects up with it.
@@arnolduk123 You are an amazing human.
@@DanielKeating-tr6hs I'm an hologram actually :)
@arnolduk123 please state the nature of the medical emergency. And thanks for the assist! I appreciate you
@@PrintNPlay ha ha..no problem matey!
Next game Giana sister?
This'll sound boring but how about tetris? Old school I'm afraid! 🤣
That's actually one of three games I already have partially coded! Just need better collision detection when moving the piece from side to side. Oh, and to use a joystick instead of potentiometer.
It will be out soon!
@@PrintNPlay that sounds good. I'm new to coding so some of the simpler projects are great to help me grasp the concepts involved which I still find quite daunting!
i can't help but think that this should be integrated into the arduino sphere
Oh, the game could 100 percent be run on an Arduino.
Are you a Linux user?
I usually work in both Windows and Linux day to day
@@PrintNPlay The reason I ask is that you seem to me like someone who knows a thing or two and I want some advice please, here's the thing, I want to know how to install a Linux distro that is more specific to the hardware I am using, I have Linux Mint 20 installed and it bugs me that the automatic updates install firmware and software for hardware I ain't got and software I ain't using.
Is there any Linux distro where one can specify hardware at the time of install and get an installation tailored to that hardware?
I don't have a good answer for you off the top of my head but I am looking into it and asking some friends. Will try my best to get you an answer
@@PrintNPlay thank you
How do you expect me to connect all the wires when you only show the diagram for only a few seconds .it's IMPOSSIBLE!
It certainly is a cause for pause!
@@PrintNPlay hahaha loved the answer
Great video, but this is too much for my 🧠...