|
Author
|
||||
|
More in Development
|
||||
Welcome to the Starting Small series. The aim of this series is to take a programming language that you hopefully know a bit about (enough to feel comfortable using) or that you want to try out and show you how to use it to make games. The language that this tutorial focuses on is Python using Pygame.
Unfortunately, if you're reading this article because you want to start developing something spectacular in Pygame, I have to disappoint you. Though, hopefully, by the end of the article you will be able to use Pygame and understand the basics of it. We are going to make something step by step; that is, the "hello world" of game development: Pong. If you are still here and want to follow the tutorial it would be a good idea to get Python and Pygame now.
Python and Pygame are quite are both lightweight and easy to rapidly develop in. The only major drawback is that Python requires an interpreter to run; there are ways to compile projects into exe files, but I won't touch on that any further in this article.
The best way to learn something is to try it for yourself, so I encourage you as a reader to try out the code for yourself (not just copying and pasting it) and experimenting with it to see what you can do.
To begin with we will need to import the modules we are going to use. We also need to import everything from "pygame.locals" for use in event handling. Immediately after importing "random" and "pygame", we initialise the "pygame" engine, and give our random module a seed so that we get decent pseudo- random numbers.
import pygame, random from pygame.locals import * pygame.init() random.seed()
The next step is to create our game window. For the purpose of this tutorial we will use a 400 by 400 resolution; there's no need to make it massive. We create our window and keep a reference to it using the following, which handles the window creation for you:
screen = pygame.display.set_mode((400, 400))
The next step is to create a game loop for this new window we have to draw on.
run = True while run: #game logic
The next addition should be indented to be in line with the "#game logic" placeholder comment. This new code handles refreshing of the window's contents and filling it with a background colour. Once we're done drawing we then can "flip" the backbuffer to the screen
screen.fill((0,0,0)) pygame.display.flip()
If you run your game now, you will be unable to close it without closing python completely because we are within an infinite loop and we aren't checking for input. To stop this infinite loop we will check if the user clicks on the close button and stop the game loop. This code will actually allow our game to handle all user events, so that at a later stage we can check for other game-related input.
for event in pygame.event.get():
if event.type == QUIT:
run = False
Finally, right at the end of your code you need to add the line (outside of your loop) which will result in allowing you to run your game and exit it.
pygame.quit()
Your final code should look like this. I have inserted some comments for you to refer to in the next section.
import pygame, random
from pygame.locals import *
pygame.init()
random.seed()
screen = pygame.display.set_mode((400, 400))
#---Place 1
run = True
while run:
#game logic
#---Place 2
screen.fill((0,0,0))
#---Place 3
pygame.display.flip()
for event in pygame.event.get():
if event.type == QUIT:
run = False
pygame.quit()
|
Words from the readers
|
||||
|
No comments posted for this article yet. Have something to say? Make yourself heard below.
Have your say:
|
||||