🚀 A Starter Guide to Pygame 📀
eankeen (1493)

🚀 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!

We're going to be building a simple screen saver. The final result will look like the following

Starting out

When creating a new project, be sure to select "PyGame" when creating a project.

Now we're ready to start writing some code!

Painting the Screen Red 🎨

First, we're importing Pygame and initializing all the imported pygame modules with pygame.init().

Second, we're declaring width and 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.

Fourth, screen is a display Surface, or an initialized window or screen for display with the set_mode() method.

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

width, height = 800,  600
backgroundColor = 255,  0,  0

screen = pygame.display.set_mode((width, height))

while True:

So, we get a red background!

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 get_rect()

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 dvdLogoRect rectangle.

screen.blit(dvdLogo, dvdLogoRect)

Now, we get a DVD!

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[0] represents the speed in the x direction.

dvdLogoSpeed = [1,  1]

Finally, I added a time.sleep(10 / 1000) so the logo moves slower.

So my code ended up looking like

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[0] = -dvdLogoSpeed[0]
if dvdLogoRect.top < 0  or dvdLogoRect.bottom > height:
	dvdLogoSpeed[1] = -dvdLogoSpeed[1]

The .left and .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 .right, .top, and .bottom.

Here is the final result

Now let me know when the logo hits the corner!

You are viewing a single comment. View All
octopyBot (264)

I used this to make it a bit more random which direction the HULU goes:

import pygame, time
import random

hlsxcor = random.randint(1, 4)
hlsycor = random.randint(1, 4)

bgColor = 0, 0, 0
huluspeed = [hlsxcor, hlsycor]
width, height = 800, 600
screen = pygame.display.set_mode((width, height))

hulu = pygame.image.load("hulu_logo.png")
hulurect = hulu.get_rect()
while True:
  screen.blit(hulu, hulurect)
  hulurect = hulurect.move(huluspeed)
  if hulurect.left < 0 or hulurect.right > width:
    huluspeed[0] = -huluspeed[0]
  if hulurect.top < 0 or hulurect.bottom > height:
    huluspeed[1] = -huluspeed[1]
  time.sleep(10 / 1000)
NidhiSinha (0)

@octopyBot I am new to this coding thingy. I was wondering how do we import images. I mean there needs to be a source file from where the program can pick up the images. Can you please help me with this

octopyBot (264)

@NidhiSinha sure. download the image you want to your computer's image files, then click the three buttons on top of your files like so:

then click add file and choose your image. then refer to the image by it's file name, + png or jpg.

NidhiSinha (0)

Hey, That I had figured already, but I was actually getting this problem of ALSA, and I am not sure why I was getting that. it said cannot find directory. So I needed help with that @octopyBot . If you have enough experience with pygame. Can you please tutor me on Pygame. I need some extensive help on designing something which needs images to be presented systematically for n number of time followed by a response

octopyBot (264)

@NidhiSinha no. next time, either be more specific or go to the ask board. I am experienced in pygame, but I'm not going to tutor you.

octopyBot (264)

@NidhiSinha well there are worries. next time, if you want pygame help, then there's the ask board. don't go asking random people to tutor you.

NidhiSinha (0)

Well. I have a question for you: what happened in your life that made you so cold. Like really! I mean, you were very cold while also replying to my request to tutor you. I gave you the benefit of the doubt and politely said no worries, and then there goes this comment of yours. If you think you can comment like a rude person, and no one is going to reply back then dude you are highly mistaken. Also, read my above comment, where I said I am new to this pygame and so I didn't know where to ask questions related to this if I had one. So as a nice person, you should have politely directed to me where I was supposed to be directed. But no, you have to be a loser here. I guess you like when people thrash you back, and yes, you were right about the point that there are worries because we have people like you in this world who don't know how to be kind and considerate and only know how to be selfish. I hope to not meet another one like you on this platform @octopyBot

octopyBot (264)

@NidhiSinha try to look at it from my point of view. you ask a question, not on the ask board. I assume you're new, and give you the benefit of the doubt, answering it. you respond saying it got you an error and asking me to tutor you?! now I'm mad. you tell me I didn't give you the correct answer and tell me you wanted to `know about an error you mentioned nothing about in the first question. I can't tutor anyone on repl just cause they ask me to. so you were completely disrespectful and immature in your rude comment back to me. I wasn't being cold, you thought I was being cold and lashed back for no reason. I expect an apology, and I don't want to see you hating on my comments again.