Well i want more people to see how amazing python. To do this, i will create a few simple steps to creating your first python game.
To warn you, you need to know how understand common knowledge. Such as how to read, type and follow directions. Yep thats it, python/pygame is that user friendly that i think ants have developed a few python applications for linux.

Install:
Because i don’t run windows or Mac, i can’t show you how to install python or pygame. I am sure theres tutorials on the net to help you, but i don’t want to copy and paste anything which i have’t tested

Now if you run linux and/or a debian based OS run this:

Code:
sudo apt-get install python
sudo apt-get install python-pygame

If you run a different distro with out a apt-get repository, then check your local distro repository. example:(https://admin.fedoraproject.org/pkgdb/) on how to install. Or just check out pygame.org and download source.

Creating a window:
Creating a “hello world” was never that fun for me. So instead i am going to show you how to create a window. This way you are at least some (insert fake percent here) done with visual appearance for your game.
open up a blank doc and add this:

Code:
#! /user/bin/python
#above only valid for linux users 

import sys, pygame
pygame.init()

#Screen properties
size = width, hight = 500,500
screen = pygame.display.set_mode(size)

Save and run.
For linux users use terminal and type:

Code:
chmod +x filename.py
or
python filename.py

For windows users just double click your .py file

As you can see hopefully the window shows up and then closes its self.
This is cause everything you typed went through but there is no loop to make the window stay open for as long as you want  it. So we will need to add this at the end:

Code:
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:sys.exit()

What this is, is it calls for a loop to stay open and allows you to close your window after your done.
with out the Event.type == pygame.QUIT:sys.exit()
your program wont ever close. It doesn’t know to  close. So keep that in there.
One thing i would like to make clear, TABS are important in python. thats how python gets away with out using {,[,( for calling if’s,for,etc.
I think everything you learn so far is quite self explanatory. But if you have questions don’t be afraid to ask.
For the full source of Creating a window: http://pastebin.com/cVBxXSHM

Creating a moving Image:
Now this part is great if you want to add a little animation. This is not a tutorial if you want keyboard movement
but its important you read this tutorial because without it your character movement that we will go over will not look the way you would wish. sorry to make you suffer like that but i believe its import you understand this moving image tutorial.
Anyway enough, lets start coding.
So we will be using the same information from above, if you didn’t follow along here is the source(http://pastebin.com/cVBxXSHM), but i prefer you read the intro.
above the while 1: statement add this:

Code:
#image loading
set_loading_name_here = pygame.image.load(“image.png”)
x = 0
y = 0

Where it says set_loading_name_here that will be the referance to the file you are loading
So lets say i have a picture of a fox, id want that part to say:
fox = pygame.image.load(“image.png”)
or what have you.
pygame.image.load is just the standard way to load an image.
Don’t worry about the x and y just yet.
if you save and load your python file now, you will notice it does not show an image, thats cause we need to call it in our while 1: loop.
So to do this we need to add this under our QUIT event:

Code:
screen.blit(set_loading_name_here,(coordx,coordy))
pygame.display.flip()

Now where it says coordx,coordy thats where you need to set the coords you want the image to show up.
for the purpose of this tutorial part please just use this:

Code:
screen.blit(set_loading_name_here,(x,y))
pygame.display.flip()

Now with out the pygame.display.flip() your image wont show up. this this is like what allows the image to display.
Now we can talk about the x and y you added earlier. These are the coords in which the image will show up.
so we set it to 0,0 meaning the image is going to be on x 0 and y 0.
So successfully we have created a window and an image.
But the point of this was to add animation. Animation is extremely easy.
Lets say we want to add our image to go across the “x” axis.
Just add x = x+1 under the screen.blit
if we want to also add a “y” movement, just add y = y+1
Save it and see how the image moves. Mess with it add some other variables.
This is were knowing simple alg2 can make your animations interesting
maybe try a x = x+1^5 have fun with it.
Now what did you notice? did our image just draw a long line of similar colors?
Well thats cause each time the image moves, it is read drawing its self. We never set it up to update and clear after each draw. Luckily this is simple to fix
Under your screen properties add:

Code:
black = 0,0,0

This sets the background to black. 0,0,0 is black in the rgb color scale. To give you a better understanding black is the absence of light. Meaning that this creates a transparency to our game.
You also have to add one more thing to get this working.
under “pygame.display.flip()” add:

Code:
screen.fill(black)

Save your file and test it out. Clean animation!
If you got lost or are a little confused here is a source of what we have done so far.
http://pastebin.com/aF8wNWTs

I hope you enjoyed what you have learned. Please wait for Part 2 soon.
Part 2 will cover:
– using A,S,D,W for moving an image
– Adding Sound
– Adding a background

until next time
Happy trails

Advertisements