11. Skier Let’s Learn Saengthong School, June – August 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus

Slides:



Advertisements
Similar presentations
Games in Python – the easy way
Advertisements

GAME:IT Junior Ping Pong Objectives: Review skills from previous lessons Create a 2-player game Create a scoring display system Using old and new skills,
Create a Simple Game in Scratch
Create a Simple Game in Scratch
Congratulations! You have just installed the Presentation Game Add-In.
Cosc 5/4730 Game Design. A short game design primer. A game or animation is built on an animation loop. – Instance variables of “objects” are updated.
Week 9 Writing Games with Pygame Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted,
Week 10 Writing Games with Pygame Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise.
GAME:IT Junior Bouncing Ball Objectives: Create Sprites Create Sounds Create Objects Create Room Program simple game.
2 What is pyGame? A set of Python modules to make it easier to write games. –home page: –documentation:
Guide to Programming with Python
Unit 9 pyGame Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work. Except where otherwise noted, this.
Week 10 Writing Games with Pygame, continued Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where.
Introduction to TouchDevelop
GAME:IT Bouncing Ball Objectives: Create Sprites Create Sounds Create Objects Create Room Program simple game.
Meet Pygame Noah Kantrowitz June 8, The Basics Cross-platform Based on SDL (don’t quote me on that) Handle input (keyboard, mouse) and output (graphics,
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 7 The Game Loop and Animation Starting Out with Games & Graphics.
GAME:IT Helicopter Objectives: Review skills in making directional sprites Create objects that shoot and destroy for points Create random enemies on the.
Game Maker Terminology
AD 305 Electronic Visualization I : School of Art and Design : University of Illinois at Chicago : Spring 2007 Intro to Action Script 9 "The games of a.
Shooters in GameMaker J Parker.
Guide to Programming with Python Chapter Twelve Sound, Animation, and Program Development: The Astrocrash Game.
Creating a Simple Game in Scratch Barb Ericson Georgia Tech June 2008.
AD 206 Intermediate CG : School of Art and Design : University of Illinois at Chicago : Spring 2009 Intro to Action Script 9 "The games of a people reveal.
Game Maker Galactic Mail Advanced Group: Complete Galactic Mail, then start developing an independent project.
Create a Halloween Computer Game in Scratch Stephanie Smullen and Dawn Ellis Barb Ericson October 2008.
1. Starting 1 Let’s Learn Saenthong School, January – February 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
Tank Game Part 3 of 6. Secondary Weapons and Pick ups Pick ups will appear randomly in the battle area and can be collected by driving into them. Each.
PyGame - Unit 1 PyGame Unit – – Introduction to PyGame.
GAME:IT Junior Paddle Ball Objectives: Review skills from Introduction Create a background Add simple object control (up and down) Add how to create a.
GAME:IT Paddle Ball Objectives: Review skills from Introduction Create a background Add simple object control (up and down) Add how to create a simple.
Tank Game Part 2 of 6. Firing Shells Coming up… Players Scores Large Explosions Small Explosions Damage Health Bars Parent Shell Destructible Walls Reappear.
Graphics in Python On entry: Run Python 2.78 from N: drive/computing and ICT VLE: Computing home page - check your feedback Success criteria: ●Understands.
Intro to Pygame Lecture 05. What is Pygame? It is a set of Python modules designed for writing games. It makes writing games possible for beginners. import.
Creating a Simple Game in Scratch Barb Ericson Georgia Tech May 2009.
AD 206 Intermediate CG : School of Art and Design : University of Illinois at Chicago : Spring 2009 Intro to Action Script 11 "The games of a people reveal.
3. Drawing Let’s Learn Saengthong School, June – August 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
9. Media (sound effects, music, video) Let’s Learn Saengthong School, June – August 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
Game Maker Tutorials Introduction Clickball IntroductionClickball Where is it? Shooting Where is it?Shooting.
5. Animation Let’s Learn Saengthong School, June – August 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
13. More Games Let’s Learn Saengthong School, June – August 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
13. Sprites. Outline 1.Game Things in Pygame (again) 2.The Pygame sprite Module 3.The Sprite Class 4.Groups of Sprites 5.Types of Collision Detection.
MOM! Phineas and Ferb are … Aims:
15. Media (sound effects, music, video)
Scratch for Interactivity
PYGAME.
Let’s Learn 2. Installing Pygame
8. Installing Pygame
Keyboard Input.
9. Drawing.
10. User Input.
11. Animation Let's Learn Python and Pygame

9. Drawing Let's Learn Python and Pygame
Let’s Learn 10. Invaders Saengthong School, June – August 2016
Intro to GML.
13. Sprites Let's Learn Python and Pygame
8. Starting Pygame Let's Learn Python and Pygame
10. User Input Let's Learn Python and Pygame
14. Pong.
16. Invaders.
Tank Game Part 2 of 6.
Let’s Learn 7. Sprites Saengthong School, June – August 2016
14. Pong Let's Learn Python and Pygame
11. Animation.
Creating a Simple Game in Scratch
CoE Software Lab II , Semester 2, Pong.
CoE Software Lab II 1. Pygame Intro , Semester 2,
CoE Software Lab II , Semester 2, Sprites.
Presentation transcript:

11. Skier Let’s Learn Saengthong School, June – August 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus

Outline 1.The Skier Game 2.skier.py Classes 3.The main Program 4.Support Functions used in main 5.The Obstacles Class 6.The AnimSprite Class 7.The PicsSprite Class 8.The Skier Class 9.The Button Class 2

1. The Skier Game 3 Two kinds of Obstacles – flags and trees The obstacles move up, so it looks like the skier is moving down. A Button sprite which can be "pressed" The player can move left and right horizontally. The image changes to show the direction. Hitting obstacles changes the score and displays an animated explosion

Help 'Screen' 4 The help screen is a picture which appears when the user presses the Help button. The game pauses while the help picture is visible. The help disappears, and the game resumes, when the user presses the Help button again. Typing 'p' causes the game to pause or resume.

2. invaders.py Classes 5 variables functions inherits Almost the same class as used in the invaders game; update() is new

 The Skier class inherits PicsSprite so it can change between different images  there is one skier object for the player  The Button class is used to make the button sprite  Many Obstacle sprites are created for trees and flags during the game  1 animation is made from AnimSprite  used for tree and flag explosions Objects made by Classes 6

def makeObstacles(): : # create trees/flags at random positions off the # bottom of the screen def checkCollisions(): : # check for a collision between the skier # and any obstacle def isGameOver(): : # has player won (score > 90) or lost (score < -90)? def centerImage(): : # draw an image in the center of the window Support Functions used in main 7

pygame.init() screen = pygame.display.set_mode([640, 640]) pygame.display.set_caption('Skier') scrWidth, scrHeight = screen.get_size() font = pygame.font.Font(None, 50) # load game sounds pygame.mixer.music.load('arpanauts.ogg') pygame.mixer.music.play(-1) pygame.mixer.music.set_volume(0.7) boom = pygame.mixer.Sound('boom.wav') # game vars isPaused = False showHelp = False gameOver = False finalMsg = "" score = 0 3. The main Program 8 Music and sound effect game state booleans for pausing, showing help and game playing

# create sprites and groups skier = Skier(scrWidth/2) yLandPos = 0 # y position of skier on the land explo = AnimSprite('exploSheet.png', 10) # create obstacles obstacles = pygame.sprite.Group() # group of obstacles makeObstacles() button = Button("Help", (scrWidth-80, 40)) instructions = pygame.image.load("instructions.png").convert_alpha() 9 Instructions as a picture

clock = pygame.time.Clock() running = True while running: clock.tick(30) # handle events for event in pygame.event.get(): if event.type == QUIT: running = False if event.type == MOUSEBUTTONDOWN: if button.isPressed( pygame.mouse.get_pos() ): # print('button pressed') showHelp = not showHelp # toggle isPaused = showHelp if event.type == KEYDOWN: if event.key == K_p: isPaused = not isPaused # toggle elif event.key == K_ESCAPE: running = False elif event.key == K_LEFT: # left arrow turns left skier.turn(-1) elif event.key == K_RIGHT: # right arrow turns right skier.turn(1) 10 game state changes for help and pausing

# update game if not gameOver and not isPaused: skier.move() # create new obstacles if skier is at end of old obstacles yLandPos += skier.steps[1] if yLandPos >= scrHeight: # skier at bottom of land? makeObstacles() yLandPos = 0 # reset so skier at top of land checkCollisions() # update obstacles and explosion based on skier's y "step" obstacles.update( skier.steps[1] ) explo.update(skier.steps[1]) gameOver = isGameOver() 11 game is updated only if game is not over and not paused

# redraw game screen.fill(WHITE) button.draw(screen) obstacles.draw(screen) screen.blit(skier.image, skier.rect) explo.draw(screen) screen.blit(font.render("Score: " +str(score), 1, BLACK), [10, 10]) if gameOver: centerImage(screen, font.render(finalMsg, True, RED)) elif isPaused: if showHelp: centerImage(screen, instructions) else: centerImage(screen, font.render('Paused...', True, BLACK)) pygame.display.update() pygame.quit() 12 game state affects what is drawn in 3 ways

def makeObstacles(): : # create trees/flags at random positions off the # bottom of the screen def checkCollisions(): : # check for a collision between the skier # and any obstacle def isGameOver(): : # has player won (score > 90) or lost (score < -90)? def centerImage(): : # draw an image in the center of the window 4. Support Functions used in main 13

def makeObstacles(): # create trees/flags at random positions off the # bottom of the screen locs = [] for i in range(10): # 10 obstacles row = random.randint(0, 9) col = random.randint(0, 9) loc = [(col * scrWidth/10) + 32, (row * scrWidth/10) scrHeight] # (x,y) center for an obstacle if not (loc in locs): # prevent 2 obstacles from # being in the same place locs.append(loc) type = random.choice(["tree", "flag"]) if type == "tree": obstacles.add( Obstacle("tree.png", loc) ) else: # a flag obstacles.add( Obstacle("flag.png", loc) ) The makeObstacle() Function 14

def checkCollisions(): global score hits = pygame.sprite.spritecollide(skier, obstacles, False) if hits: if not hits[0].isPassed: # has skier not passed obstacle? explo.setPosition( hits[0].rect.center ) explo.setVisible(True) boom.play() hits[0].isPassed = True if hits[0].fnm == "tree.png": # hit a tree score -= 10 skier.initSpeed() else: # hit a flag score += 10 hits[0].kill() # remove the flag The checkCollision() Function 15 Nothing deleted when there is a collision. Position and show the explosion, and play a sound.

class Obstacle(pygame.sprite.Sprite): def __init__(self, fnm, loc): super().__init__() self.fnm = fnm self.image = pygame.image.load(fnm) self.rect = self.image.get_rect() self.rect.center = loc self.isPassed = False def update(self, step): self.rect.y -= step if self.rect.y < -self.rect.height: # if above top self.kill() 5. The Obstacle Class 16

 This class is almost the same as the one in invaders.py except that an animation must move vertically.  This is required because the game is a top- scroller, and so everything (except the skier) must move up as the game progresses. 6. The AnimSprite Class 17

def update(self, step): # movement function needed since using # AnimSprite in a top-scroller if self.rect.y > 0: # if below top of window self.rect.y -= step # move up else: self.setVisible(False) 18 Compare to update() in the Obstacle class – reuse the object by making it invisible

class PicsSprite(pygame.sprite.Sprite): def __init__(self, fnms): super().__init__() self.images = [] for fnm in fnms: self.images.append( pygame.image.load(fnm + ".png").convert_alpha() ) self.image = self.images[0] self.rect = self.image.get_rect() def setPic(self, idx): center = self.rect.center self.image = self.images[idx] self.rect = self.image.get_rect() self.rect.center = center 7. The PicsSprite Class 19 A list of picture file names Switch to picture in position idx in the list

class Skier(PicsSprite): def __init__(self, x): super().__init__(["down", "right1", "right2", "left2", "left1"]) self.rect.center = [x, 100] self.initSpeed() def initSpeed(self): self.steps = [0, SKIER_STEP] # in x- and y- dirs self.setPic(0) 8. The Skier Class 20 A list of picture file names for the skier

def turn(self, dirChange): dir = self.steps[0] + dirChange if dir < -2: # restrict direction to -2 to 2 dir = -2 if dir > 2: dir = 2 self.steps = [dir, SKIER_STEP - abs(dir)*2] self.setPic(dir) def move(self): # move the skier right and left self.rect.centerx += self.steps[0] if self.rect.x < 0: # stay visible in window self.rect.x = 0 if self.rect.x > (scrWidth - self.rect.width): self.rect.x = scrWidth - self.rect.width 21 The y- 'step' is not used by the skier; it is used to update the obstacles in main Changes the y- 'step' based on the direction.

class Button(pygame.sprite.Sprite): def __init__(self, label, pos): super().__init__() self.image = pygame.image.load("button.png").convert_alpha() self.rect = self.image.get_rect() self.rect.center = pos font = pygame.font.Font(None, 24) self.labelIm = font.render(label, True, YELLOW) self.labelRect = self.labelIm.get_rect() self.labelRect.x = self.rect.x + \ (self.rect.width - self.labelIm.get_width())/2 self.labelRect.y = self.rect.y + \ (self.rect.height - self.labelIm.get_height())/2 9. The Button Class 22 Loads a button picture then writes a label on top of it.

def isPressed(self, mousePos): return self.rect.collidepoint(mousePos) def draw(self, screen): screen.blit(self.image, self.rect) screen.blit(self.labelIm, self.labelRect) 23 Mouse pressing is checked in the game loop. Here only check if the mouse is "inside" the button.

 A more fancy Button class is used in the "Menu Test" example in the Other Games folder.  The buttons change color when the mouse moves over them. 24