14. Pong.

Slides:



Advertisements
Similar presentations
Games in Python – the easy way
Advertisements

Create a Simple Game in Scratch
Create a Simple Game in Scratch
Pages and boxes Building quick user interfaces. learning objectives o Build a quick UI with pages and boxes o understand how pages and boxes work o click.
Constructor and New Fields // Don't synch draw() with vertical retrace of monitor graphics.SynchronizeWithVerticalRetrace = false; IsFixedTimeStep = true;
Display of Objects on Screen. COUNTERS b A horizontal counter represents the horizontal position of the monitor’s electron beam. b A vertical counter.
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.
2 What is pyGame? A set of Python modules to make it easier to write games. –home page: –documentation:
Pygame Dick Steflik.
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 Maker Day 2 Making a Maze Game.
Creating a Simple Game in Scratch Barb Ericson Georgia Tech June 2008.
Video Games Writing Games with Pygame Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise.
Create a Halloween Computer Game in Scratch Stephanie Smullen and Dawn Ellis Barb Ericson October 2008.
Game Project 1 Homage to Pong. Project Rules: The primary project is Pong, the design and development of which will be discussed in detail here. If you.
PyGame - Unit 1 PyGame Unit – – Introduction to PyGame.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 22 Game Graphics.
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.
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.
11. Skier Let’s Learn Saengthong School, June – August 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
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.
Building a rocks and spaceship game with Pygame Zero
Sound and more Animations
Create a Halloween Computer Game in Scratch
MOM! Phineas and Ferb are … Aims:
15. Media (sound effects, music, video)
Scratch for Interactivity
Exploring Mathematical Relationships Module 5: Investigation 3
Catapult 2016.
PYGAME.
Let’s Learn 2. Installing Pygame
8. Installing Pygame
Game Maker Intro to Programming Game Maker Pop Quiz (Both Groups)
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
Scratch for Interactivity
Basic Graphics Drawing Shapes 1.
The One Where You Scratch
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
16. Invaders.
Adding a paddle (1) We now have a code, which will run forever and just have a ball bouncing around the screen until we shut down the program. To make.
Breakout in Greenfoot Barb Ericson
Let’s Learn 7. Sprites Saengthong School, June – August 2016
14. Pong Let's Learn Python and Pygame
Let’s begin our game!.
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.
Catch Game Cards Catch Game Cards Make a Card Go to the Top Fall Down
Presentation transcript:

14. Pong

Outline beachBounce.py Reused Pong in Action The main Code The BlockSprite Class The Paddle Class The BallSprite Class

1. beachBounce.py Reused The Pong game can reuse lots of code and sprites from beachBounce.py BlockSprite, BallSprite, and most of the main code

2. Pong in Action each game 'thing' == a sprite object 4 BlockSprites for the 4 walls 2 Paddle sprites (new) 1 BallSprite

The score text could be a sprite, but I've decided to code it as extra variables inside main.

pong.py Classes Picture inherits variables uses functions inherits Generated using pyNSouceGUI.exe from http://www.andypatterns.com/index.php/ products/pynsource/

Pong Design

3. The main Code # some colors BLACK = ( 0, 0, 0) WHITE = ( 255, 255, 255) RED = ( 255, 0, 0) GREEN = ( 0, 255, 0) BLUE = ( 0, 0, 255) WALL_SIZE = 10 STEP = 8 PADDLE_STEP = 10 LEFT = 0 RIGHT = 1 WINNING_SCORE = 5 game constants (better to have names than numbers in the program)

# code for the classes, see later class BlockSprite(pygame. sprite # code for the classes, see later class BlockSprite(pygame.sprite.Sprite): : class Paddle(BlockSprite): class BallSprite(pygame.sprite.Sprite): # ----------------------------------- # function(s) used by main def centerImage(screen, im): x = (scrWidth - im.get_width())/2 y = (scrHeight - im.get_height())/2 screen.blit(im, (x,y))

Usual initialization stuff; # main pygame.init() screen = pygame.display.set_mode([640,480]) screen.fill(WHITE) pygame.display.set_caption("Pong") scrWidth, scrHeight = screen.get_size() # create wall sprites top = BlockSprite(0, 0, scrWidth, WALL_SIZE) bottom = BlockSprite(0, scrHeight-WALL_SIZE, scrWidth, WALL_SIZE) left = BlockSprite(0, 0, WALL_SIZE, scrHeight) right = BlockSprite(scrWidth-WALL_SIZE, 0, WALL_SIZE, scrHeight) horizWalls = pygame.sprite.Group(top, bottom) vertWalls = pygame.sprite.Group(left, right) # create two paddles leftPaddle = Paddle(50, scrHeight/2) rightPaddle = Paddle(scrWidth-50, scrHeight/2) ball = BallSprite('smallBall.png') sprites = pygame.sprite.OrderedUpdates(top, bottom, left, right, leftPaddle, rightPaddle, ball) Usual initialization stuff; scrWidth, scrHeight are usually very useful Create the sprite objects and groups

# game variables leftStep = 0; rightStep = 0 # move step in pixels for paddles scoreLeft = 0; scoreRight = 0 winMsg = "" gameOver = False font = pygame.font.Font(None, 72)

clock = pygame. time. Clock() running = True while running: clock 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 == KEYDOWN: if event.key == K_q: # left paddle leftStep = -PADDLE_STEP # up elif event.key == K_s: leftStep = PADDLE_STEP # down if event.key == K_p: # right paddle rightStep = -PADDLE_STEP # up elif event.key == K_l: rightStep = PADDLE_STEP # down elif event.type == KEYUP: if event.key == K_q or event.key == K_s: # left paddle leftStep = 0 if event.key == K_p or event.key == K_l: # right paddle rightStep = 0

# update game if not gameOver: leftPaddle. move(leftStep) rightPaddle # update game if not gameOver: leftPaddle.move(leftStep) rightPaddle.move(rightStep) ball.update() if scoreLeft >= WINNING_SCORE: winMsg = "Left Wins!" gameOver = True elif scoreRight >= WINNING_SCORE: winMsg = "Right Wins!" # redraw screen.fill(WHITE) sprites.draw(screen); screen.blit( font.render(str(scoreLeft) + ":" + str(scoreRight), True, RED), [20, 20]) if gameOver: centerImage(screen, font.render(winMsg, True, RED)) pygame.display.update() pygame.quit()

Paddle Keys There are two paddles left paddle up and down uses 'q' and 's' right paddle up and down uses 'p' and 'l' q p s l

Repeating Key Action If the user holds down a key then the paddle keeps moving until the user releases the key. very common game behaviour Implemented by checking for KEYDOWN and KEYUP events for the key KEYDOWN sets the move step (e.g. to 10 or -10) KEYUP resets the step to 0

Finishing the Game ≠ Finishing the Program The code uses two booleans (True/False): running and gameOver running is used to end the game loop and program gameOver means that the user's game is finished, but not the game loop When gameOver is true, the game loop must continue in order to show things (e.g. the winner's message) gameOver is also used to skip the game update

4. The BlockSprite Class The same as before – it's used to represent a non-moving sprite that looks like a coloured rectangle. class BlockSprite(pygame.sprite.Sprite): def __init__(self, x, y, width, height, color=BLACK): super().__init__() self.image = pygame.Surface((width, height)) self.image.fill(color) self.rect = self.image.get_rect() self.rect.topleft = (x, y)

5. The Paddle Class This can be coded as a moving version of my BlockSprite class so Paddle inherits BlockSprite, which inherits Sprite see slide 6 The extra code in Paddle is a move() function that allows the sprite to move up or down but moving must stop when the paddle is at the top or bottom of the window

class Paddle(BlockSprite): def __init__(self, x, y): super() class Paddle(BlockSprite): def __init__(self, x, y): super().__init__(x, y-75, 10, 150, BLUE) # paddle width & height def move(self, step): if pygame.sprite.collide_rect(self, top) and (step < 0): # at top & going up step = 0 elif pygame.sprite.collide_rect(self, bottom) and (step > 0): # at bottom and going down self.rect.y += step 1 2

Collision Testing Usually colllision testing uses a sprite collision function and a direction test the direction is used to see if the thing is moving towards or away from the collision top 1 step is + step is - 2 bottom

6. The BallSprite Class The hardest part of this class is coding update() which moves the ball. Normally the move is xStep in the x-direction and yStep in the y-direction, but... what happens when the ball hits: the left paddle? the right paddle? the horizontal walls (i.e. the top and bottom sides)? the vertical walls (i.e. the left and right sides)?

class BallSprite(pygame. sprite class BallSprite(pygame.sprite.Sprite): def __init__(self, fnm): super().__init__() self.image = pygame.image.load(fnm).convert_alpha() self.rect = self.image.get_rect() self.rect.center = [scrWidth/2, scrHeight/2] # start position of the ball in center of window self.xStep, self.yStep = self.randomSteps() # step size and direction along each axis def randomSteps(self): # create a random +/- STEP pair x = STEP if random.random() > 0.5: x = -x y = STEP y = -y return [x,y]

def update(self): global scoreLeft, scoreRight if pygame. sprite def update(self): global scoreLeft, scoreRight if pygame.sprite.collide_rect(self, leftPaddle) and (self.xStep < 0): # hit left paddle and going left self.xStep = -self.xStep # change direction elif pygame.sprite.collide_rect(self,rightPaddle) and (self.xStep>0): # hit right paddle and going right if pygame.sprite.spritecollideany(self, horizWalls): # change y-step direction at top and bottom sides self.yStep = -self.yStep if pygame.sprite.spritecollideany(self, vertWalls): # ball has reached left or right sides if pygame.sprite.collide_rect(self, right): scoreLeft += 1 else: # left side scoreRight += 1 # reset the ball self.rect.center = (scrWidth/2, scrHeight/2) self.xStep, self.yStep = self.randomSteps() self.rect.x += self.xStep # move the ball horizontally self.rect.y += self.yStep # and vertically 1 2 3 4

Special Cases 1 2 left paddle right paddle vert wall horiz wall 3 4 xStep < 0 xStep > 0 bounce bounce left paddle right paddle vert wall horiz wall 3 4 xStep yStep or or