Python Skier Program

Skier is a very simple skiing game, inspired by a game called SkiFree. (You can find out all about SkiFree here: en.wikipedia.org/wiki/SkiFree.)

You ski down a hill, trying to avoid trees and pick up flags. Picking up a flag earns 10 points. Crashing into a tree makes you lose 100 points.

When you run the program, it should look something like this:

Pygame Skiers

Skier uses something called Pygame to help with the graphics. Pygame is a Python module. (We'll learn more about modules in chapter 15.) If you ran the book's installer, Pygame is installed. If not, you can download it from www.pygame.org. We'll learn all about Pygame in chapter 16.

There are some graphics files you'll need to go along with the program:

skier_down.png skier_right1.png skier_crash.png skier_right2.png skier_tree.png skier_left1.png skier_flag.png skier_left2.png

You can find these in the \examples\skier folder (if you ran the installer) or on the book's web site. Just put them in the same folder or directory where you save the program. That's pretty important. If they're not in the same directory as the program, Python won't find them, and the program won't work.

The code for Skier is in listing 10.1. The listing is a bit long, about 115 lines of code (plus some blank lines to make it easier to read), but I encourage you to take the time to type it in. The listing has some notes to give a bit of explanation of what the code does.

Like EasyGui, Pygame programs sometimes have trouble running properly in IDLE, so you might want to use SPE for entering and running this program.

Listing 10.1 Skier

import pygame, sys, random

skier images = ["skier down.png", "skier right1.png",

"skier right2.png", "skier left2.png",

"skier_left1.png"]

class SkierClass(pygame.sprite.Sprite):

def init (self):

pygame.sprite.Sprite.__init__(self)

self.image = pygame.image.load("skier down.png")

Creates skier

self.rect = self.image.get rect()

self.rect.center = [320, 100]

self.angle = 0

def turn(self, direction):

self.angle = self.angle + direction if self.angle < -2: self.angle = -2 if self.angle > 2: self.angle = 2 center = self.rect.center self.image = pygame.image.load(skier_images[self.angle]) self.rect = self.image.get_rect() self.rect.center = center speed = [self.angle, 6 - abs(self.angle) * 2] return speed

Turns skier def move(self, speed):

self.rect.centerx = self.rect.centerx + speed[0] if self.rect.centerx < 20: self.rect.centerx = 20 if self.rect.centerx > 620: self.rect.centerx = 620

class ObstacleClass(pygame.sprite.Sprite):

def _init_(self, image_file, location, type):

pygame.sprite.Sprite.__init__(self)

self.image_file = image_file self.image = pygame.image.load(image_file)

self.location = location self.rect = self.image.get_rect()

self.rect.center = location self.type = type self.passed = False

Moves skier left and right def scroll(self, t_ptr):

self.rect.centery = self.location[1] - t_ptr end)

Creates trees and flags

Makes scenery scroll up def create_map(start, end):

obstacles = pygame.sprite.Group() gates = pygame.sprite.Group() locations = [] for i in range(10):

row = random.randint(start, col = random.randint(0, 9) location = [col * 64 + 20, if not (location in locations): locations.append(location) type = random.choice(["tree", "flag"]) if type == "tree": img = "skier_tree.png" elif type == "flag": img = "skier_flag.png" obstacle = ObstacleClass(img, location, type) obstacles.add(obstacle) return obstacles def animate():

pygame.display.update(obstacles.draw(screen)) screen.blit(skier.image, skier.rect) screen.blit(score_text, [10, 10]) pygame.display.flip()

Creates one window of random trees and flags

Redraws screen when things move def updateObstacleGroup(map0, mapl): obstacles = pygame.sprite.Group() for ob in map0: obstacles.add(ob) for ob in mapl: obstacles.add(ob) return obstacles

Changes to next screen of scenery

Gets everything ready pygame.init()

screen = pygame.display.set_mode([64 0,64 0]) clock = pygame.time.Clock() skier = SkierClass() speed = [0, 6] map_position = 0 points = 0

map0 = create_map(2 0, 29) mapl = create_map(10, 19) activeMap = 0

obstacles = update0bstacleGroup(map0, mapl) font = pygame.font.Font(None, 50)

Starts main loop

Updates graphics while True:

clock.tick(30)

<)__ 30 times per second for event in pygame.event.get():

if event.type == pygame.QUIT: sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT:

speed = skier.turn(-l) elif event.key == pygame.K_RIGHT: speed = skier.turn(l) skier.move(speed) <- Moves skier map_position += speed[1] <- Scrolls scenery if map_position >=640 and activeMap == 0: activeMap = 1 map0 = create_map(2 0, 29)

obstacles = update0bstacleGroup(map0, mapl) if map_position >=1280 and activeMap == 1: activeMap = 0 for ob in map0:

ob.location[1] = ob.location[1] - 1280 map_position = map_position - 1280 map1 = create_map(10, 19)

obstacles = update0bstacleGroup(map0, map1)

Checks for keypresses or window close

Changes from one window of scenery to the next for obstacle in obstacles:

obstacle.scroll(map_position)

hit =

pygame.sprite.spritecollide(skier, obstacles, False)

if hit

if

hit[0].type == "tree" and not hit[0].passed:

skier.image = pygame.image.load("skier_crash.png") animate()

pygame.time.delay(1000)

Checks for hitting trees and getting flags

skier.image = pygame.image.load("skier down.png")

skier.angle = 0

speed = [0, 6]

hit[0].passed = True

elif hit[0].type == "flag" and not hit[0].passed:

points += 10

obstacles.remove(hit[0])

score

text = font.render("Score: " +str(points), 1, (0, 0, 0))

— Displays score

animate()

The code for listing 10.1 is in the \examples\skier folder, so if you get stuck or just don't want to type it all in, you can use that file. But believe it or not, you'll learn more by typing it in than by just opening and looking at the listing.

In later chapters, we'll learn about all the keywords and techniques that are used in Skier. For now, just type it in and give it a try.

11000111001110000110110100011011010111001100011001100110190

100 Bowling Tips

100 Bowling Tips

Playing bowling with your friends can help you decide if it is indeed the hobby that you want to invest your time on today. Aside from that, it can help you get a better feel of the sport. More importantly, when you play with your friends, it would become a more fun activity, which you can look forward to each week.

Get My Free Ebook


Responses

  • Cirilla
    Where to save skier_crash.png?
    8 years ago
  • JENSEN
    Why will skier not load on python?
    8 years ago
  • Aatos
    How to get to the examplesskier folder?
    7 years ago
  • laura
    How to make a skier game in pygame?
    7 years ago
  • GABRIEL
    Why won't python open skier down.png?
    6 years ago

Post a comment