A long time ago, in a tutorial far away... Ok, so maybe it hasn't been that long since our last foray into the world of programming, but it sure seems like it sometimes. On a more serious note, however, a while back we did go over some very basic things involving Python and a library known as Pygame. Today my friends, we will be continuing on this path, and adding to what we build that fateful December day.
In our last examination of Pygame, all we really did was set you up with Python and displayed something on the screen. It was simple enough, but it doesn't really show us why Pygame has the word "game" in its name. Today, we will be adding something to our program that every game needs: movement. By the end of the whole process, we will not only have the awesome-rific SOTC logo, but we will be able to move it around like a glorious logo spaceship....or whatever vehicle you want to picture it as.
Silly vehicles aside, the first thing we need to do is add in another import, which allows us to utilize some pygame "global variables" that will help us with keyboard commands. It is a very simple, but different, import statement:
import pygame
from pygame.locals import *
pygame.init()
As you can see, I have added a from import, which allows us to import specific parts of files, rather than the whole thing. The locals are typically not imported with a straight pygame import, so we add it here. I have given you the whole "import and init" block as a reference to where to place the new import.
Another new addition we need before the game loop is a few variables, ones that are pretty key to movement. We need variables for the x and y position, along with a speed variable so we can speed up or slow down the movement as we see fit. In python, this is as simple as:
lx = 100
ly = 100
speed = 3
Pretty simple, but it does get slightly more complex. Besides just setting the position dynamically, we are going to have to test the position of our entity against the bounds of our window. For the top and left bound, that is easy, we just test the position against zero. However, it is only this easy because the origin for any surface if the top-left corner. So, to test the bottom and left, we have to take into account the size of our window, AND the size of said entity. Pretty standard concept, but to do this programatically, we are going to set up two variables, making it easy when we actually go about doing the bounds testing:
rightBound = screen.get_width() - sotcLogo.get_width()
bottomBound = screen.get_height() - sotcLogo.get_height()
Thankfully, the surface object we use for both the window and our entity have the built in methods to do everything we need. Basically we take the width or height of our window and subtract the width or height of our entity. What this does is set up the bounds for that entity, because we don't want any part of the entity to roam off-screen, we have to take into account its size. It all has to do with the origin for positioning being in the top-left corner.
These two bits of code, the variables we will be using, should go below the declarations of everything else, but outside the game loop, as so:
sotcLogo = pygame.image.load("sotc_logo.gif")
sotcLogo.convert()
#Logo Position Varibales
lx = 100
ly = 100
speed = 3
#Right and Bottom Bounds for Game Entities
rightBound = screen.get_width() - sotcLogo.get_width()
bottomBound = screen.get_height() - sotcLogo.get_height()
#Clock and Loop Variables
framerate = pygame.time.Clock()
GameGo = True
#The Main Loop
while GameGo:
So, now that we have our variables all handled, we can finally get down to some more exciting stuff. The first thing we are going to add to the game-loop are the key-press events. This bit of code can go right after the clock tick, which I will keep in there for reference:
framerate.tick(60)
#Keyborad Keypress Events, Movement
if pygame.key.get_pressed()[K_UP]:
ly = ly - speed
if pygame.key.get_pressed()[K_DOWN]:
ly = ly + speed
if pygame.key.get_pressed()[K_LEFT]:
lx = lx - speed
if pygame.key.get_pressed()[K_RIGHT]:
lx = lx + speed
Ok, what we have here is actually pretty simple as well. We are using a method called get_pressed() to get the key that is getting pressed. This could be any key, but we are testing this value against a value that is setup by pygame. That from pygame.locals import * line that we added first is importing these global key values, among other things. This is where we use them, and pygame actually does a really good job of providing a bunch of keycodes for us.
It is actually pretty amazing how easy a we can figure out what this is doing. If "this" key is pressed, add or subtract from x or y accordingly. Pygame makes key capturing really easy. But, as we stated above, simply changing x or y based on keystrokes is only half the story. We have to now test the position to make sure it is within the boundaries we want. To do this, we add a couple more if statements right below the key capturing:
if lx > rightBound:
lx = rightBound
if lx < 0:
lx = 0
if ly > bottomBound:
ly = bottomBound
if ly < 0:
ly = 0
Here we are using the "bounds" variables we set up earlier. The great thing about doing it this way, is that if we change the image of our entity or the size of the window, we never have to change the "bounds" variables. Dynamic code is always better, right?
Let's go ahead and run it now.......WAIT! There is one issue. We did all this, but we have one more change to make. The actual blit call for our entity has to be modified, the position has to be set to our x and y variables:
Now that we changed that, we should have an amazing moving SOTC logo:
And there it is, one step closer to something more game like. I hope you enjoyed the tutorial, and just remember, when you need coding help all you have to is Switch On The Code.
Add Comment
[language] [/language]
Examples:
[javascript] [/javascript]
[actionscript] [/actionscript]
[csharp] [/csharp]
See here for supported languages.
Javascript must be enabled to submit anonymous comments - or you can login.