Games with Python

With game development, you often have quite a bit of logic, mathematics, physics, artificial intelligence, and other things, all of which come together for game creation

Many times people like to visualize the programs they are creating, as it can help people to learn programming logic quickly. Games are fantastic for this, as your are specifically programming everything you see.

First, you're going to need PyGame!

Installation may vary by operating system. Macs historically have had trouble with PyGame, so you may need to get an earlier version of Python and PyGame.

Nowadays,  is probably the best method for installing PyGame, since it comes with the latest versions of Python 2 and Python 3.

import pygame
pygame.init()  # This will initiate PyGame

Above, we've imported PyGame, which is obviously necessary to make use of the module! Then, we run pygame.init(), which is integral to every single PyGame application that you will ever write. This will initiate PyGame, and allow you to then make various commands with PyGame and our game.

gameDisplay = pygame.display.set_mode((800,600)) 

#we define our game's display,
# we want the resolution of our game to be 800 px wide and 600 px tall
pygame.display.set_caption('A bit Racey')

#The title of the window.

Next, we define our game's display, which is the main "display" for our game. You may also see this referred to as a "surface," as this is basically our canvas that we will draw things to, and the function literally returns a pygame.Surface object. We are saying right now that we want the resolution of our game to be 800 px wide and 600 px tall. Take note that this is a tuple as a function argument. If you do not make this a tuple with parenthesis, then 600 and 800 will be treated as separate parameters and the function will blow up. It's a big deal.

After that, we define our display's "caption." To me, it's more like a title, and is the title of the window. We've decided to call our game "A bit Racey." (tm!)

clock = pygame.time.Clock()

Simple enough, this is a our game clock. We use this to track time within the game, and this is mostly used for FPS, or "frames per second." While somewhat trivial seeming, FPS is very important, and can be tweaked as we will see later. For the most part, the average human eye can see ~30 FPS. It's important to note, however, that this is only a very general statement, since every human eye is slightly different, and the human eye does not process things in "frames." The better way to put it is that after about 30 FPS, people generally cannot tell the difference.

Take that 60 FPS YouTube. Anyway, we can increase FPS to literally speed up the game, or slow them down to slow down the game. This isn't ideal, especially when speeding up FPS, as the entire game loop is run per frame, and might be a massive waste of processing. More on this later though!

crashed = False
while not crashed:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            crashed = True
        print(event)

    pygame.display.update()
    clock.tick(60)

Okay, a bit more here. I don't like to separate loops and functions if I don't have to, since it can cause people to get indentation wrong. So, first, we've got a crashed = False statement, which is just a variable that we set initially. Then, we run our "game loop," which will run until we crash. Currently, the only way we're saying crashed = True is if the user exits out of the window, however.

You'll notice here that we have a for loop within this while loop. This is going to be present in most PyGame scripts, where events are constantly being logged. It is shown in the video, but not here, but you can still try it: Try adding "print event" above the if statement. You will see in your console everything you do within the PyGame window. Pretty neat!

After our if statement. you'll see that we run a pygame.display.update. It's important to note the difference between display.update and display.flip. Display.flip will update the entire surface. Basically the entire screen. Display.update can just update specific areas of the screen. That said, if you do not pass a parameter, then update will update the entire surface as well, bascially making flip() pointless for our interests. There might come times when you want to use flip for very specific tasks, however.

The last thing within this while loop is clock.tick(60). Basically, this is how many frames per second we are running. In this case, we are running 60 FPS.

pygame.quit() # pygame.quit(). This will end our pygame instance.
quit() # Then we can run a simple quit(), which will exit Python and the application.

Once we have broken our game loop, we want to run a pygame.quit(). This will end our pygame instance. Then we can run a simple quit(), which will exit Python and the application.

 

 

###############################

 

Displaying images with PyGame

import pygame
pygame.init()
display_width = 800
display_height = 600

gameDisplay = pygame.display.set_mode((display_width,display_height))

pygame.display.set_caption('A bit Racey')

black = (0,0,0)
white = (255,255,255)

clock = pygame.time.Clock()
crashed = False
carImg = pygame.image.load('racecar.png')

def car(x,y):
    gameDisplay.blit(carImg, (x,y))

x =  (display_width * 0.45)
y = (display_height * 0.8)

while not crashed:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            crashed = True

    gameDisplay.fill(white)
    car(x,y)

        
    pygame.display.update()
    clock.tick(60)

pygame.quit()
quit()

So, here we've gone ahead and embedded the new code in our existing script. First, we use:

display_width = 800
display_height = 600

gameDisplay = pygame.display.set_mode((display_width,display_height))

The functioning here is the same as before, but this time we're not hard coding the resolution. We want to use this method so that we can reference the variables. We're going to see how useful that can be already in this script, but even more later on.

Then, here:

black = (0,0,0)
white = (255,255,255)

We're defining our colors, using RGB formatting. If you want to know a bit more about RGB, check out the video.

Next up:

carImg = pygame.image.load('racecar.png')

Here, we load the racecar.png image to our carImg variable. racecar.png is an image that I have personally created. pull one from Google Images, or make your own. Chances are, you will do better than I did!

def car(x,y):
    gameDisplay.blit(carImg, (x,y))

Now, we define our car function, which really just places the car to the display. "Blit" basically just draws the image to the screen, but we still have yet to fully show it to the display. In graphics, generally, there is a lot done in the background, and only when every update is done is when the screen is visually updated.

x =  (display_width * 0.45)
y = (display_height * 0.8)

Here, we've defined the starting points for our car. See how we used out previously-defined variables? We're already seeing how this could be useful, especially if we EVER want to change our resolution. A lot of people start out writing programs, thinking that some of the variables will just never be changed. I've done it a few times. Always wrong and regretful.

Now, within our while loop, we see the addition of:

    gameDisplay.fill(white)
    car(x,y)

Here, we've filled our display with a color, white. What this does is cover everything in white. Paint the game white, so-to-speak. This will cover over any existing stuff. After that, we run our car function to draw the car to the screen. Always keep in mind the order here. If you drew the car first, then called the gameDisplay.fill function, you would have just covered your car and it would not show up.

Also, if you'd like the same image I used:

pygame racecar image

 

 

 

 

######################

import pygame

pygame.init()
gameDisplay = pygame.display.set_mode((800,600))
pygame.display.set_caption('A bit Racey')
clock = pygame.time.Clock()
crashed = False

while not crashed:

for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True

print(event)
pygame.display.update()
clock.tick(60)

pygame.quit()
quit()

 

################

 

import pygame

def car(x,y):
gameDisplay.blit(kidsImg, (x,y))

pygame.init()
display_width = 800
display_height = 600

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('A bit Racey')

black = (0,0,0)
white = (255,255,255)

clock = pygame.time.Clock()
crashed = False
kidsImg = pygame.image.load('racecar.png')

x = (display_width * 0.45)
y = (display_height * 0.8)

while not crashed:

for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True

print(event)
gameDisplay.fill(white)
car(x,y)

pygame.display.update()
clock.tick(60)

pygame.quit()
quit()

 

 

################

 

import pygame

def car(x,y):
gameDisplay.blit(kidsImg, (x,y))

pygame.init()
display_width = 800
display_height = 600

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('A bit Racey')

black = (0,0,0)
white = (255,255,255)

clock = pygame.time.Clock()
crashed = False
kidsImg = pygame.image.load('racecar.png')

x = (display_width * 0.45)
y = (display_height * 0.8)

x_change = 0
y_change =0
car_speed = 0

while not crashed:

for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
############################
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
elif event.key == pygame.K_RIGHT:
x_change = 5
elif event.key == pygame.K_DOWN:
y_change = 5
elif event.key == pygame.K_UP:
y_change = -5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
######################
##
x += x_change
y += y_change
gameDisplay.fill(white)
car(x,y)

pygame.display.update()
clock.tick(60)

pygame.quit()
quit()