yeon
yeon I am a Korean web programmer with bboy dancing as a hobby. :)

Beginning Game Programming for Teens with Python

Beginning Game Programming for Teens with Python

Beginning Game Programming for Teens with Python


Step 1: Hello Bunny


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 1 - Import library
import pygame
from pygame.locals import *

# 2 - Initialize the game
pygame.init()
width, height = 640, 480
screen=pygame.display.set_mode((width, height))

# 3 - Load images
player = pygame.image.load("resources/images/dude.png")

# 4 - keep looping through
while 1:
    # 5 - clear the screen before drawing it again
    screen.fill(0)
    # 6 - draw the screen elements
    screen.blit(player, (100,100))
    # 7 - update the screen
    pygame.display.flip()
    # 8 - loop through the events
    for event in pygame.event.get():
        # check if the event is the X button
        if event.type==pygame.QUIT:
            # if it is quit the game
            pygame.quit()
            exit(0)



Adding the Game Resources


You are almost ready to create your game. But what’s a game without some great graphics and sound effects? I’ve put together all the graphics and sound effects you’ll need for your game into a ZIP archive. You can download it here.
Once you’ve downloaded the file, create a folder for your game on your hard disk and extract the resources folder into that folder so that your game folder has a subfolder named resources, with the various resources grouped in additional folders inside it, like this:


  • Import the PyGame library. This allows you to use functions from the library in your program.
  • Initialize PyGame and set up the display window.
  • Load the image that you will use for the bunny.
  • Keep looping over the following indented code.



Step 2: Add Scenery


Let’s start by adding a background to the game scene. This can be done with a couple more screen.blit() calls.


At the end of section #3, after loading the player image, add the following code:


1
2
grass = pygame.image.load("resources/images/grass.png")
castle = pygame.image.load("resources/images/castle.png")


This loads the images and puts them into specific variables. Now they have to be drawn on screen. But if you check the grass image, you will notice that it won’t cover the entire screen area, which is 640 x 480. This means you have to tile the grass over the screen area to cover it completely.


Add the following code to game.py at the beginning of section #6 (before the player is drawn on screen):


1
2
3
4
5
6
7
    for x in range(width/grass.get_width()+1):
        for y in range(height/grass.get_height()+1):
            screen.blit(grass,(x*100,y*100))
    screen.blit(castle,(0,30))
    screen.blit(castle,(0,135))
    screen.blit(castle,(0,240))
    screen.blit(castle,(0,345 ))


As you can see, the for statement loops through x first. Then, within that for loop, it loops through y and draws the grass at the x and y values generated by the for loops. The next couple of lines just draw the castles on the screen.


If you run the program now, you should get something like this:



참고: Beginning Game Programming for Teens with Python

comments powered by Disqus