Lets Learn Python and Pygame Aj Andrew Davison













![When has the ball left the screen? if pos[1] > scr. Height-1: pos = When has the ball left the screen? if pos[1] > scr. Height-1: pos =](https://slidetodoc.com/presentation_image_h2/d915e4c841a73703d9db2ac6355ecdaf/image-14.jpg)






![Code: beach. Bounce. py pygame. init() screen = pygame. display. set_mode([640, 480]) screen. fill(WHITE) Code: beach. Bounce. py pygame. init() screen = pygame. display. set_mode([640, 480]) screen. fill(WHITE)](https://slidetodoc.com/presentation_image_h2/d915e4c841a73703d9db2ac6355ecdaf/image-21.jpg)



- Slides: 24
Let's Learn Python and Pygame Aj. Andrew Davison, Co. E, PSU Hat Yai Campus E-mail: ad@fivedots. coe. psu. ac. th 11. Animation
Outline 1. 2. 3. 4. 5. 6. Basic Game Loop Again Animation Loop for a Ball Animation 1 Ball Animation 2 Multiple Balls Falling Bouncing Ball 2
1. Basic Game Loop Again pygame. init() screen = pygame. display. set_mode((640, 480)) screen. fill((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 handle events update game # update game redraw game # redraw game pygame. display. update() pygame. quit() 3
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 (x. Step, y. Step) 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 += x. Step; y += y. Step # redraw game # draw image at (x, y) pygame. display. update() pygame. quit() (x, y) x. Step y. Step 4
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 5
Code: anim. Ball-1. py pygame. init() screen = pygame. display. set_mode((400, 400)) pygame. display. set_caption("Ball Animation") # load ball as image ball. Im = pygame. image. load('small. Ball. png'). convert_alpha() # store dimensions for later scr. Width, scr. Height = screen. get_size() im. Width, im. Height = ball. Im. get_size() # initialize ball's position x = random. randrange(0, scr. Width-1 - im. Width) y = 0 screen. blit(ball. Im, [x, y]) # initialize ball's step done in each loop x. Step = 0; y. Step = 8 clock = pygame. time. Clock() : 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 += y. Step print("Pos: ", x, y) # redraw screen. fill(BLACK) screen. blit(ball. Im, [x, y]) pygame. display. update() pygame. quit() 7
Setting the Ball's Start Position x = random. randrange(0, scr. Width-1 - im. Width) y = 0 screen. blit(ball. Im, [x, y]) scr. Width im. Width ( scr. Width-1, 0) (0, 0) im. Width (x, y) range of x for the ball 8
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). 9
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 10
Code: anim. Ball-2. py screen = pygame. display. set_mode((400, 400)) pygame. display. set_caption("Ball Animation") # load ball as image ball. Im = pygame. image. load('small. Ball. png'). convert_alpha() # store dimensions for later scr. Width, scr. Height = screen. get_size() im. Width, im. Height = ball. Im. get_size() # initialize ball's position pos = random. Pos() # pos is the list [x, y] # screen. blit(ball. Im, pos) # not needed usually # initialize ball's step done in each loop x. Step = 0; y. Step = 8 clock = pygame. time. Clock() : 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] > scr. Height-1: pos = random. Pos() # Move ball down pos[1] += y. Step # print("Pos: ", pos) # redraw screen. fill(BLACK) screen. blit(ball. Im, pos) pygame. display. update() pygame. quit() 12
random. Pos() def random. Pos(): # create a random position # x position somewhere along top x = random. randrange(0, scr. Width-1 - im. Width) y = 0 return [x, y] 13
When has the ball left the screen? if pos[1] > scr. Height-1: pos = random. Pos() (0, 0) scr. Height (0, scr. Height-1) (x, y) 14
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 15
Code: anim. Balls. py pygame. init() screen = pygame. display. set_mode((400, 400)) pygame. display. set_caption("Balls Animation") # load ball as image ball. Im = pygame. image. load('small. Ball. png'). convert_alpha() # store dimensions for later scr. Width, scr. Height = screen. get_size() im. Width, im. Height = ball. Im. get_size() # initialize many random positions pos. List = [] for i in range(30): pos. List. append(random. Pos()) The list is a list of [x, y] values, one for each ball: [ [x 0, y 0], [x 1, y 1], …. ] # initialize ball's step done in each loop x. Step = 0; y. Step = 8 clock = pygame. time. Clock() : 16
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(pos. List)): # if ball has exited bottom if pos. List[i][1] > scr. Height-1: pos. List[i] = random. Pos() # Move ball down pos. List[i][1] += y. Step # redraw screen. fill(BLACK) for i in range(len(pos. List)): # draw a ball at each position screen. blit(ball. Im, pos. List[i]) pygame. display. update() pygame. quit() The position of the ith ball (x, y) is stored in pos. List[i][0] == x pos. List[i][1] == y 17
Revised random. Pos() def random. Pos(): # create a random position # x position somewhere along top x = random. randrange(0, scr. Width -1 - im. Width) # y position above the top y = random. randrange(-scr. Height, 0) return [x, y] scr. Width -scr. Height - 1 - im. Width position a ball anywhere in this area (0, 0) 18
6. Bouncing Ball §Have a single ball bounce off the 'walls' of the screen. 19
Bouncing Ball Coords 20
Code: beach. Bounce. py pygame. init() screen = pygame. display. set_mode([640, 480]) screen. fill(WHITE) pygame. display. set_caption("Bouncing Beachball") ball. Im = pygame. image. load('ball. png'). convert_alpha() # store dimensions for later scr. Width, scr. Height = screen. get_size() im. Width, im. Height = ball. Im. get_size() # start position of the ball x = 50; y = 50 # step size and direction along each axis x. Step = 10; y. Step = 10 clock = pygame. time. Clock() : 21
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 >= scr. Width - 1 - im. Width): x. Step = -x. Step # change y-step direction at top and bottom sides if (y <= 0) or (y >= scr. Height -1 - im. Height): y. Step = -y. Step x += x. Step y += y. Step # move the ball horizontally # and vertically # redraw screen. fill(WHITE) screen. blit(ball. Im, [x, y]) pygame. display. update() pygame. quit() 22
x- Direction Change §The x- direction depends on if x. Step is positive or negative. § the direction is changed by swapping +/§ x. Step = -x. Step §It should change when the ball tries to leaves the screen on the left side or on the right side. (0, y) (x, y) x <= 0 X (scr. Width-1, y) (x, y) x >= scr. Width-1 im. Width X 23
y- Direction Change §The y- direction depends on if y. Step is positive or negative. § the direction is changed by swapping +/§ y. Step = -y. Step §It should change when the ball tries to leaves the screen on the top or bottom. (x, 0) (scr. Height-1, 0) (x, y) y <= 0 (x, y) y >= scr. Height-1 im. Height X X 24