First you have to think about how everything is going to happen. You want to interact with an NPC? How will this happen? I am going to assume you have some basic programming knowledge, and for this tutorial, I expect you to know a little bit about python, but it really doesn’t matter if you know nothing about pyGame, although it would help.
So first things first. Your going to need PyGame and I will be using Python 3.3 throughout this tutorial so some methods of doing things may not work for previous versions. You may also need GIMP or Photoshop. I suggest GIMP because it is free and can be used on OSX, Windows or Linux but Photoshop is still good. So once everything is set up, I guess we’re ready to start programming! Oh wait, the aim of this tutorial… Well the aim is quite simple. We are going to make a very very simple platformer that will feature 32×32 entities and blocks. It will have a nice little background and stuff like that so yeah… Should be good for a beginner reference program.
First thing is first. We need to define some variables that are going to help us run the game, load the screen and keep the game running. For the screen, we need to define a couple if things. We need to set the screen height and width. This will be the screen resolution of the game. Another thing to not is that all the blocks and entities that we are going to use will be 32×32 so therefore it will be important to ensure that all blocks will fit within this screen resolution perfectly. So we will use a screen resolution of around 640×640 (because 640 mod 32 = 0, and that is what we need). Create a new file and name this main.py and we can now get creating!
import pygame, sys, os, datetime, time, math from pygame.locals import * #Setting the initial Screen Settings And Variables Needed For The Game window_size = window_width, window_height = 640, 640 half_width, half_height = window_width/2, window_height/2 block_size = block_width, block_height = 32, 32 max_fps = 60
Ok, so the above snippet is pretty simple. We set the initial variables such as screen_width and screen_height and we also imported everything we needed such as pygame and all the locals from pygame. But wait, max_fps? What is that? Well that is going to be the maximum framerate for our game. It basically determines how fast our game loop (we’ll get into this later) will iterate (repeat itself). You may know the “Frames Per Second” from various other games like LOL, Minecraft and all that and this is what we are going to have in our game.
Next thing is next, we need to create the screen. Now we are going to create a function called “main” and this will be our main program screen and where our game is going to be run. We then need to create another function called “main_game_loop” and this is where our main game loop is going to be run as well as the main game features. We will, of course, have a starting screen and stuff like so that is why there is a need for the multiple functions. So lets write these functions
def main(): global screen, clock screen = pygame.display.set_mode(window_size) clock = pygame.time.Clock() main_game() def main_game(): #Main Game Loop while True: #Allows display of FPS clock.tick(max_fps) fps_current = float(clock.get_fps()) pygame.display.set_caption("Platformer By ReallyGoodPie %s" % (str(fps_current))) pygame.display.update()
So in the above snippet, we created two classes, as we discussed. They look bare now, but don’t worry they will be packed with stuff at a later date. We created the main function, with its sole intention to get everything ready for the game. The two variables that were declared first were screen and clock. The screen, as you may have guessed, is the screen for the game. The clock, well that’s the clock of the game. It determines how much FPS the game will run at. We then called the main_game function through the main function. The purpose of this function is to run the main game, as we discussed earlier. We now have our main loop or “main game loop” and this will run constantly (because of the “While True”) or until an exit function is called that will stop it. But this will update everything about the game. The screen, character, background, foreground, NPC’s and everything will be updated through this loop.
We then have the contents of the main loop. First we have the “clock.tick(max_fps)” and all this does is run the internal game clock and tells it to ticks at *whatever the max_fps was set to. We then set the title to tell us not only what the game is, but also how much FPS the game is running at (We will go more in depth about this at a later date). We then run the update function, and for now, all this does is update whatever is on the screen, which is currently nothing at all. So I guess that raps up todays lesson. Be sure to stay tuned for more content and more tutorials on game development and the Platformer game.
import pygame, sys, os, datetime, time, math from pygame.locals import * #Setting the initial Screen Settings And Variables Needed For The Game window_size = window_width, window_height = 640, 640 half_width, half_height = window_width/2, window_height/2 block_size = block_width, block_height = 32, 32 max_fps = 60 def main(): global screen, clock screen = pygame.display.set_mode(window_size) clock = pygame.time.Clock() main_game() def main_game(): #Main Game Loop while True: #Allows display of FPS clock.tick(max_fps) fps_current = float(clock.get_fps()) pygame.display.set_caption("Platformer By ReallyGoodPie %s" % (str(fps_current))) pygame.display.update()
Download Source Code For Part 1 * Login or register to download this item.
Note that this code won’t run as of yet but this is how I intended it to be. For semi-working code, please visit next weeks tutorial here: PyGame: Making A Platformer Part 2