🚀 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!
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) pygame.init() 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.fill(bgColor) screen.blit(hulu, hulurect) hulurect = hulurect.move(huluspeed) if hulurect.left < 0 or hulurect.right > width: huluspeed = -huluspeed if hulurect.top < 0 or hulurect.bottom > height: huluspeed = -huluspeed pygame.display.flip() time.sleep(10 / 1000)
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
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
@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.