11. Animation.

Slides:



Advertisements
Similar presentations
Games in Python – the easy way
Advertisements

Microsoft® Small Basic
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.
Announcements At the end of your project, you’ll be given an opportunity to make a standalone (.exe) version of your project. For this to work, you’ll.
First some catch-up. Everyone who uses slide presentation software should regularly check their show in show mode. This is the end product. You need to.
2 What is pyGame? A set of Python modules to make it easier to write games. –home page: –documentation:
Pygame Dick Steflik.
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.
Game Design Creating a game called PING Phase 3: Steps for building basic game.
DATA STRUCTURE AND ALGORITHM PROJECT Fall2011 Group Members: Asma Rafi BS-3 Fatima Saleem BS-3.
HTML, HTML5 Canvas, JavaScript PART 3 LOOKING MORE CLOSELY AT FULL BLOWN GAME DESIGN A PATTERNED BACKGROUND (CREATED WTIH LOOPS) WITH A MOVING OBJECT CHALLENGES.
Introduction to TouchDevelop
1.What is it? 2.How do you draw a planometric ? 3.Exercises.
KeyListener and Keyboard Events Just as we can implement listeners to handle mouse events, we can do the same for keyboard events (keypresses) –to implement.
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,
Processing Lecture.2 Mouse and Keyboard
By Melissa Dalis Professor Susan Rodger Duke University June 2011 Multiplication Table.
EGR 115 Introduction to Computing for Engineers 3D animation in MATLAB Monday 17 Nov 2014 EGR 115 Introduction to Computing for Engineers.
Photoshop Image Slicing. Reasons to Image Slide To create links out of sliced images To optimise different areas. (flat areas of colour, such as logos,
First you need to load ‘legal’ size paper in your printer. Then select the handout you want and double click it. This should open the file in Power Point.
PyGame - Unit 1 PyGame Unit – – Introduction to PyGame.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 22 Game Graphics.
ICT/COMPUTING RULES Only use software allowed by the teacher
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.
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
Section06: Sequences Chapter 4 and 5. General Description "Normal" variables x = 19 – The name "x" is associated with a single value Sequence variables:
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
MOM! Phineas and Ferb are … Aims:
Pixels, Colors and Shapes
A look ahead: At the end of your project, you’ll be given an opportunity to make a standalone (.exe) version of your project. For this to work, you’ll.
15. Media (sound effects, music, video)
Catapult 2016.
Animations.
PYGAME.
WITH PYGAME ON THE RASPBERRY PI
Let’s Learn 2. Installing Pygame
8. Installing Pygame
Or just artificialworlds.net/blog
Keyboard Input.
9. Drawing.
10. User Input.
11. Animation Let's Learn Python and Pygame

9. Drawing Let's Learn Python and Pygame
Catapult 2014 Session 10.
Let’s Learn 10. Invaders Saengthong School, June – August 2016
13. Sprites Let's Learn Python and Pygame
Sensors and Logic Switches
8. Starting Pygame Let's Learn Python and Pygame
10. User Input Let's Learn Python and Pygame
14. Pong.
16. Invaders.
Introduction to TouchDevelop
Let’s Learn 7. Sprites Saengthong School, June – August 2016
Collision Detection Platforms.
14. Pong Let's Learn Python and Pygame
Let’s begin our game!.
Ball meets Paddle.
Plotting Points Guided Notes
L L Line CSE 420 Computer Games Lecture #6 Game Foundations.
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. Animation

Outline Basic Game Loop Again Animation Loop for a Ball Ball Animation 1 Ball Animation 2 Multiple Balls Falling Bouncing Ball

1. Basic Game Loop Again handle events update game redraw game pygame.init() screen = pygame.display.set_mode((640, 480)) screen.fill((255,255,255)) pygame.display.set_caption("Game") clock = pygame.time.Clock() running = True while running: # game loop clock.tick(30) # handle events for event in pygame.event.get(): if event.type == QUIT: running = False # update game # redraw game pygame.display.update() pygame.quit() handle events update game redraw game

2. Animation Loop for a Ball # setup Pygame window as on last slide # load ball as image # initialize ball's (x,y) position # initialize ball's step (xStep, yStep) done in each loop clock = pygame.time.Clock() running = True while running: # game loop clock.tick(30) # handle events for event in pygame.event.get(): if event.type == QUIT: running = False # update game x += xStep; y += yStep # redraw game # draw image at (x,y) pygame.display.update() pygame.quit() (x, y) xStep yStep

3. Ball Animation 1 The ball starts at a random position along the top, and then drops downwards forever. the ball's position is printed in the command prompt window

Code: animBall-1.py pygame.init() screen = pygame.display.set_mode((400, 400)) pygame.display.set_caption("Ball Animation") # load ball as image ballIm = pygame.image.load('smallBall.png').convert_alpha() # store dimensions for later scrWidth, scrHeight = screen.get_size() imWidth, imHeight = ballIm.get_size() # initialize ball's position x = random.randrange(0, scrWidth-1 - imWidth) y = 0 screen.blit(ballIm, [x,y]) # initialize ball's step done in each loop xStep = 0; yStep = 8 clock = pygame.time.Clock() :

running = True while running: clock running = True while running: clock.tick(30) # handle events for event in pygame.event.get(): if event.type == QUIT: running = False # update game state # Move ball down y += yStep print("Pos: ", x, y) # redraw screen.fill(BLACK) screen.blit(ballIm, [x,y]) pygame.display.update() pygame.quit()

Setting the Ball's Start Position x = random.randrange(0, scrWidth-1 - imWidth) y = 0 screen.blit(ballIm, [x,y]) imWidth scrWidth (0, 0) ( scrWidth-1, 0) imWidth (x, y) range of x for the ball

Where's the Ball? The problem is that the ball keeps moving downwards forever, even after it has dropped below the bottom of the screen (y == 480).

4. Ball Animation 2 When the ball drops off the bottom of the screen, it's (x,y) position is reset to be at a random place along the top. then starts at the top again

Code: animBall-2.py screen = pygame.display.set_mode((400, 400)) pygame.display.set_caption("Ball Animation") # load ball as image ballIm = pygame.image.load('smallBall.png').convert_alpha() # store dimensions for later scrWidth, scrHeight = screen.get_size() imWidth, imHeight = ballIm.get_size() # initialize ball's position pos = randomPos() # pos is the list [x, y] # screen.blit(ballIm, pos) # not needed usually # initialize ball's step done in each loop xStep = 0; yStep = 8 clock = pygame.time.Clock() :

running = True while running: clock running = True while running: clock.tick(30) # handle events for event in pygame.event.get(): if event.type == QUIT: running = False # update game state # if ball has exited bottom if pos[1] > scrHeight-1: pos = randomPos() # Move ball down pos[1] += yStep # print("Pos: ", pos) # redraw screen.fill(BLACK) screen.blit(ballIm, pos) pygame.display.update() pygame.quit()

randomPos() def randomPos(): # create a random position # x position somewhere along top x = random.randrange(0, scrWidth-1 - imWidth) y = 0 return [x,y]

When has the ball left the screen? if pos[1] > scrHeight-1: pos = randomPos() (0, 0) scrHeight (0, scrHeight-1) (x, y)

5. Multiple Balls Falling Have 30 balls falling, and reappearing back at the top in random new positions. Only one image is needed, but a list of 30 (x,y) positions. then starts at the top again

Code: animBalls.py pygame.init() screen = pygame.display.set_mode((400, 400)) pygame.display.set_caption("Balls Animation") # load ball as image ballIm = pygame.image.load('smallBall.png').convert_alpha() # store dimensions for later scrWidth, scrHeight = screen.get_size() imWidth, imHeight = ballIm.get_size() # initialize many random positions posList = [] for i in range(30): posList.append(randomPos()) # initialize ball's step done in each loop xStep = 0; yStep = 8 clock = pygame.time.Clock() : The list is a list of [x,y] values, one for each ball: [ [x0,y0], [x1,y1], …. ]

The position of the ith ball (x, y) is stored in posList[i]. running = True while running: clock.tick(30) # handle events for event in pygame.event.get(): if event.type == QUIT: running = False # update game state for i in range(len(posList)): # if ball has exited bottom if posList[i][1] > scrHeight-1: posList[i] = randomPos() # Move ball down posList[i][1] += yStep # redraw screen.fill(BLACK) # draw a ball at each position screen.blit(ballIm, posList[i]) pygame.display.update() pygame.quit() The position of the ith ball (x, y) is stored in posList[i]. posList[i][0] == x posList[i][1] == y

Revised randomPos() scrWidth - 1- imWidth position a ball anywhere def randomPos(): # create a random position # x position somewhere along top x = random.randrange(0, scrWidth -1 - imWidth) # y position above the top y = random.randrange(-scrHeight, 0) return [x,y] scrWidth - 1- imWidth position a ball anywhere in this area -scrHeight (0,0)

6. Bouncing Ball Have a single ball bounce off the 'walls' of the screen.

Bouncing Ball Coords

Code: beachBounce.py pygame.init() screen = pygame.display.set_mode([640,480]) screen.fill(WHITE) pygame.display.set_caption("Bouncing Beachball") ballIm = pygame.image.load('ball.png').convert_alpha() # store dimensions for later scrWidth, scrHeight = screen.get_size() imWidth, imHeight = ballIm.get_size() # start position of the ball x = 50; y = 50 # step size and direction along each axis xStep = 10; yStep = 10 clock = pygame.time.Clock() :

running = True while running: clock running = True while running: clock.tick(30) # handle events for event in pygame.event.get(): if event.type == QUIT: running = False # update game state # change x-step direction at left and right sides if (x <= 0) or (x >= scrWidth - 1 - imWidth): xStep = -xStep # change y-step direction at top and bottom sides if (y <= 0) or (y >= scrHeight -1 - imHeight): yStep = -yStep x += xStep # move the ball horizontally y += yStep # and vertically # redraw screen.fill(WHITE) screen.blit(ballIm, [x, y]) pygame.display.update() pygame.quit()

x- Direction Change The x- direction depends on if xStep is positive or negative. the direction is changed by swapping +/- xStep = -xStep It should change when the ball tries to leaves the screen on the left side or on the right side. (0, y) (scrWidth-1, y) (x, y) (x, y) x >= scrWidth-1 - imWidth x <= 0 X X

y- Direction Change The y- direction depends on if yStep is positive or negative. the direction is changed by swapping +/- yStep = -yStep It should change when the ball tries to leaves the screen on the top or bottom. (x,0) y <= 0 (x, y) X y >= scrHeight-1 - imHeight (x, y) (scrHeight-1,0) X