loading...

Creating the DVD Screensaver in Pygame!

jcowie profile image JCowie ・3 min read

Intro

We are all aware of the DVD Screensaver, if you are not then see this GIF below:
gif
Sorry about the GIF, it's the best I could find!

So, as a quick tutorial to Pygame, how about we make it(in pygame!)

Pygame

Checklist

  • Check out the official website here
  • Make sure that you understand the underlying basics
  • Continue reading this article Now, let's begin!

Creating the basis

First, we have to import Pygame and initialize all the imported pygame modules with pygame.init().

Alt Text

After that, we have to declare the width and height variables that represent the size of the screen.

Alt Text

Then, we're setting backgroundColor to a tuple of 3 elements that represent the RGB numbers.

Alt Text

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

Alt Text

Lastly, in the infinite loop, we're filling the screen to the backgroundColor and updating the display Surface to the screen. In other words pygame.display.flip() "refreshes" the screen with changes you made to the grahpics.
Alt Text

So far, you should have this:

import pygame

pygame.init()
width, height = 800,  600
backgroundColor = 0,  0,  0

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

while True:
    screen.fill(backgroundColor)
    pygame.display.flip()

Got it? Great! Then let's move on.

Adding the DVD

Right, after the line that has the setmode method, import an image of your DVD. Feel free to use the same image I used, in the repository linked at the bottom.

Alt Text

Then, create a rectangle from the Surface, or from the image you just loaded with get_rect()

Alt Text

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.

Alt Text

Now, we get a DVD logo!

Alt Text
(If you use dark mode on DEV(like me) because you don't want to burn your retinas then obviously you won't see the black background)

Here is the code so far:

import pygame

pygame.init()
width, height = 800,  600
backgroundColor = 0,  0,  0

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

dvdLogo = pygame.image.load("dvd-logo-white.png")
dvdLogoRect = dvdLogo.get_rect()

while True:
    screen.fill(backgroundColor)
    pygame.display.flip()

        screen.blit(dvdLogo, dvdLogoRect)
        dvdLogoRect = dvdLogoRect.move(dvdLogoSpeed)

Now all we have to do is get the DVD to move!

Making the DVD logo move

To move the DVD logo, simply use move() by a speed:

Alt Text

Make sure you also declare and initialize dvdLogoSpeed at the top of the file. dvdLogoSpeed[0] represents the speed in the x direction.

Alt Text

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

So now the code should look like this:

import pygame, time

pygame.init()
width, height = 800, 600
dvdLogoSpeed = [1, 1]
backgroundColor = 0, 0, 0

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

dvdLogo = pygame.image.load("dvd-logo-white.png")
dvdLogoRect = dvdLogo.get_rect()

while True:
    screen.fill(backgroundColor)

    screen.blit(dvdLogo, dvdLogoRect)
    dvdLogoRect = dvdLogoRect.move(dvdLogoSpeed)

    pygame.display.flip()
    time.sleep(10 / 1000)

But, you will notice that the logo goes off the screen, so to fix that we need to implement a check for bouncing. So, 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.

Finished

So, run that and hopefully, you will be presented with the DVD logo moving across the screen and bouncing off any edges it hits.

The github repo can be found below

Let me know if you have any issues and if you have any tutorial ideas, let me know!

Have a Great Day!

Posted on Feb 3 by:

jcowie profile

JCowie

@jcowie

16 Year Old Programmer/Web Developer/Aspiring Cyber Security Expert and the Lord of Linux.

Discussion

markdown guide