5. Animation 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

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.
2 What is pyGame? A set of Python modules to make it easier to write games. –home page: –documentation:
Pygame Dick Steflik.
DATA STRUCTURE AND ALGORITHM PROJECT Fall2011 Group Members: Asma Rafi BS-3 Fatima Saleem BS-3.
by Chris Brown under Prof. Susan Rodger Duke University June 2012
Introduction to TouchDevelop
VIDEO GAME PROGRAMMING Video Game Programming Junior – DigiPutt INSTRUCTOR TEACHER’S ASSISTANT.
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,
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.
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.
5. Loops 1 Let’s Learn Saenthong School, January – February 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
1. Starting 1 Let’s Learn Saenthong School, January – February 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
Let’s Learn 3. Modules Saenthong School, January – February 2016
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.
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.
5. Loops 1 Make a program do things over-and-over-and over again. Counting loops; Conditional loops. Games: guessing, ghost, hangman Computer.
Building a rocks and spaceship game with Pygame Zero
Let’s Learn 12. Packaging a Game Saengthong School, June – August 2016
Sound and more Animations
MOM! Phineas and Ferb are … Aims:
Pixels, Colors and Shapes
15. Media (sound effects, music, video)
Catapult 2016.
Animations.
PYGAME.
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
5. Loops Let's Learn Python and Pygame
Let’s Learn 10. Invaders Saengthong School, June – August 2016
What are variables? Using input()
Let's Learn Python and Pygame
13. Sprites Let's Learn Python and Pygame
Let’s Learn 6. Nested Loops Saenthong School, January – February 2016
8. Starting Pygame Let's Learn Python and Pygame
10. User Input Let's Learn Python and Pygame
14. Pong.
16. Invaders.
BSc in Digital Media, PSUIC
Introduction to TouchDevelop
7. Functions Let's Learn Python and Pygame
Let’s Learn 7. Sprites Saengthong School, June – August 2016
14. Pong Let's Learn Python and Pygame
Multiplication Grids.
Let’s begin our game!.
11. Animation.
What are variables? Using input()
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:

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

Outline 1.Basic Game Loop Again 2.Animation Loop for a Ball 3.Ball Animation 1 4.Ball Animation 2 5.Multiple Balls Falling 6.Bouncing Ball 2

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() 1. Basic Game Loop Again 3 handle events update game redraw game

# 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() 2. Animation Loop for a Ball 4 (x, y) xStep yStep

 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 3. Ball Animation 1 5

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() : Code: animBall-1.py 6

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() 7

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

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

 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. 4. Ball Animation 2 10 then starts at the top again

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() : Code: animBall-2.py 11

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() 12

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

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

 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. 5. Multiple Balls Falling 15 then starts at the top again

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() : Code: animBalls.py 16 The list is a list of [x,y] values, one for each ball: [ [x0,y0], [x1,y1], …. ]

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) for i in range(len(posList)): # draw a ball at each position screen.blit(ballIm, posList[i]) pygame.display.update() pygame.quit() 17 The position of the ith ball (x, y) is stored in posList[i]. posList[i][0] == x posList[i][1] == y

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] Revised randomPos() 18 position a ball anywhere in this area -scrHeight (0,0) scrWidth - 1- imWidth

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

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() : Code: beachBounce.py 20

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 = scrWidth imWidth): xStep = -xStep # change y-step direction at top and bottom sides if (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() 21

 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. x- Direction Change 22 (scrWidth-1, y) (0, y) (x, y) X X x <= 0 x >= scrWidth-1 - imWidth

 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. y- Direction Change 23 (x,0) (scrHeight-1,0) (x, y) X y >= scrHeight-1 - imHeight X y <= 0