🚀 A Starter Guide to Pygame 📀
Pygame is an open-source library for making graphical applications with Python. Learn more about it on the official website.
This tutorial is intended to help you setup a very basic Pygame interface. It's for Python beginners or people who want to quickly bootstrap a Pygame project. Find more about Repl.it's GFX public beta announcement here. So let's get started!
Painting the Screen Red 🎨
First, we're importing Pygame and initializing all the imported pygame modules with
Second, we're declaring
height variables that represent the size of the screen.
Third, we're setting
backgroundColor to a tuple of 3 elements that represent the RGB numbers.
screen is a display Surface, or an initialized window or screen for display with the
Lastly, in the infinite loop, we're filling the screen to the
backGroundColor and updatiang the display Surface to the screen. In other words
pygame.display.flip() "refreshes" the screen with changes you made to the grahpics.
import pygame pygame.init() width, height = 800, 600 backgroundColor = 255, 0, 0 screen = pygame.display.set_mode((width, height)) while True: screen.fill(backgroundColor) pygame.display.flip()
Adding the DVD
Right before you while loop, import an image of your DVD. Feel free to use the same image I used, in the repl below.
dvdLogo = pygame.image.load("dvd-logo-white.png")
Then, create a rectangle from the Surface, or from the image you just loaded with
dvdLogoRect = dvdLogo.get_rect()
Now, inside of the
while loop (after filling the background color), "map" the imported image to the
dvdLogoRect rectangle using the
blit() method. That way, the image stays inside of the invisible
Moving the DVD Logo
To move the DVD logo, simply use
move() by a speed:
dvdLogoRect = dvdLogoRect.move(dvdLogoSpeed)
Make sure you also declare and initialie dvdLogoSpeed at the top of the file.
dvdLogoSpeed represents the speed in the
dvdLogoSpeed = [1, 1]
Finally, I added a
time.sleep(10 / 1000) so the logo moves slower.
The DVD logo will move off the screen because there is no bouncing.
To implement a check for bouncing, add the following:
if dvdLogoRect.left < 0 or dvdLogoRect.right > width: dvdLogoSpeed = -dvdLogoSpeed if dvdLogoRect.top < 0 or dvdLogoRect.bottom > height: dvdLogoSpeed = -dvdLogoSpeed
.right properties don't seem to be documented, but it's implied that
.left measures the distance from the left part of the
dvdLogoRect Rect (rectangle) to the left part of the
screen. And so on and so fourth for
Now let me know when the logo hits the corner!
no avaiable video device what
Traceback (most recent call last):
File "main.py", line 11, in <module>
screen = pygame.display.set_mode((width, height))
pygame.error: No available video device
Hmm. I'll have to check this error out. Do you get the error when forking the project?
It gives me the error message couldn't open dvd-logo-white.png when I run the program I don't know why.
The example works when I open it in your repl, but when I fork to my own it doens't work :-(
I wanted to test it because I find pygame and love2d kind of slow responding/ laggy when I move a picture around (with keyboard input)
Some of the error code:
Display is not capable of DPMS
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM default
^CTraceback (most recent call last):
File "main.py", line 25, in <module>
time.sleep(10 / 1000)
XIO: fatal IO error 11 (Resource temporarily unavailable) on X server ":0"
after 123 requests (123 known processed) with 12 events remaining.
exit status 1
if dvdLogoRect.left < 0 or dvdLogoRect.right > width:
dvdLogoSpeed = -dvdLogoSpeed
if dvdLogoRect.top < 0 or dvdLogoRect.bottom > height:
dvdLogoSpeed = -dvdLogoSpeed
can anyone explain this part for me
Whenever I try to use another image, it gives an error that "couldn't open [imagename].png". I imported the image into the project directly from my computer. Is there another thing I have to do?