Upcoming SlideShare
×

# PyGame Basics by Example: Backend AI, Frame Rates, 2D Horizontal, Vertical, & Diagonal Sprite Movement, Timed Movement, Clocked Movement

1,114

Published on

1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total Views
1,114
On Slideshare
0
From Embeds
0
Number of Embeds
40
Actions
Shares
0
0
0
Likes
1
Embeds 0
No embeds

No notes for slide

### Transcript of "PyGame Basics by Example: Backend AI, Frame Rates, 2D Horizontal, Vertical, & Diagonal Sprite Movement, Timed Movement, Clocked Movement"

1. 1. Python & Perl PyGame Basics By Example Backend AI, Frame Rates, 2D Horizontal, Vertical, & Diagonal Sprite Movement, Timed Movement, Clocked Movement Vladimir Kulyukinwww.youtube.com/vkedco www.vkedco.blogspot.com
2. 2. Outline ● Review: Backend AI – Two Player Games – Min/Max Games – Min/Max for Tic-Tac-Toe ● 2D Sprite Movement – Horizontal 2D Movement – Vertical 2D Movement – Diagonal 2D Movement – Timed 2D Movement – Clocked 2D Movementwww.youtube.com/vkedco www.vkedco.blogspot.com
4. 4. Board State Representation ttt_board_state = [ [X, O, X], [X, O, None], [None, O, None] ]www.youtube.com/vkedco www.vkedco.blogspot.com
5. 5. Board States as Strings ttt_board_state = [ [X, O, X], [X, O, None], [None, O, None] ] def ttt_board_state_to_str(ttt_board_state): rslt_str = for row in ttt_board_state: for cell in row: if cell == X: rslt_str += x elif cell == O: rslt_str += o else: rslt_str += ? return rslt_str ttt_board_stgate_to_str(ttt_board_state) → xoxxo??o?www.youtube.com/vkedco www.vkedco.blogspot.com
6. 6. Mouse Click Capture def capture_mouse_click(ttt_board): global player mouse_click_x, mouse_click_y = pygame.mouse.get_pos() col, row = mouse_click_to_ttt_board_pos(mouse_click_x, mouse_click_y) if is_pos_available(col, row): draw_ttt_player(ttt_board, player, col, row) switch_ttt_player()www.youtube.com/vkedco www.vkedco.blogspot.com
7. 7. Min/Max Games ● Min/Max games are played by two players: MAX & MIN ● MAX & MIN are just names that can be replaced with WHITE & BLACK in the chess context ● MAX typically moves first and then turns are taken until the game is overwww.youtube.com/vkedco www.vkedco.blogspot.com
8. 8. Formal Definition of Min/Max Games ● A Min/Max game is defined by the following components: – Initial state – some formal representation of the initial state of the game where MAX must move – A finite set of operators available to both players (e.g., legal chess moves) – A game end test to determine if the current state is terminal – A state utility function that evaluates the current state for both players (e.g., utility function may return 1, 0, and -1, where 1 is a win for MAX, 0 is a draw, and -1 is a win for MIN)www.youtube.com/vkedco www.vkedco.blogspot.com
9. 9. Generic Min/Max Algorithm ● Given a game state, a player, and a utility function – Use the operators available to the player to generate the set of next states – For each of the generated states: ● if a state is terminal, use the utility function to compute its utility for the player ● if a state is not terminal, switch the player and recursively compute its utility – Sort the states by utility – If the player is MAX, choose a maximum utility state – if the player is MIN, choose a minimum utility statewww.youtube.com/vkedco www.vkedco.blogspot.com
10. 10. Example Mk,m is k-th move from state m MAX chooses state 4 from state 1, MAX 1 u=2 because it gives a maximum utility of 2 u=n, is utility value for a specificstate M1,1 M1,3 MIN chooses state 9 M1,2 from state 4, because it gives a minimum utility of 2 MIN 2 u=1 3 u=0 4 u=2 M2,1 M2,2 M3,1 M3,2 M4,1 M4,2 5 6 7 8 9 10 u=1 u=3 u=0 u=5 u=2 u=10http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
11. 11. 2D Sprite Movementwww.youtube.com/vkedco www.vkedco.blogspot.com
12. 12. Physics of Movement ● In the real world, the physics of movement is quite involved: objects move in many different ways, some of which are quite difficult to simulate ● In computer games, cars may turn 180 degrees in an instant and balls may jump up to the ceiling ● The good news is, as human-computer interaction research shows, that only a small percentage of details must be simulated to create a convincing illusion of realitywww.youtube.com/vkedco www.vkedco.blogspot.com
13. 13. Frame Rates ● Movement is simulated by sequences of image frames ● When the time between consecutive frames is short enough, most human viewers accept the sequence as realistic movement ● The number of images per unit of time is referred to as a frame rate ● Different frame rates may be required to convince different human viewers that movement is realwww.youtube.com/vkedco www.vkedco.blogspot.com
14. 14. Frame Rates ● The frame rate of a typical movie is 24 frames/second ● The more frames per second the better, but most viewers will be unable to detect change after the frame rate exceeds 100 frames per second ● Different display devices support different frame rates: for example, if an LCD monitor has a refresh rate of 70 hertz - it refreshes its display 70 times every secondwww.youtube.com/vkedco www.vkedco.blogspot.com
15. 15. Horizontal & Vertical Movement ● The most straight forward type of movement is moving an object along a straight line by a fixed amount each frame ● Horizontal movement means adding to an objects x coordinate a fixed amount each frame ● Vertical movement means adding to an objects y coordinate a fixed amount in each frame ● Moving 2D images are called spriteswww.youtube.com/vkedco www.vkedco.blogspot.com
16. 16. Horizontal Movement ● To move a sprite horizontally: – Initialize the sprites x coordinate – Initialize x_delta (number pixels the sprite moves per unit of time) – Move the sprite x_delta pixels right/left every unit of timewww.youtube.com/vkedco www.vkedco.blogspot.com
17. 17. Vertical Movement ● To move a sprite vertically: – Initialize the sprites y coordinate – Initialize y_delta (number pixels the sprite moves per unit of time) – Move the sprite y_delta pixels up/down every unit of timewww.youtube.com/vkedco www.vkedco.blogspot.com
18. 18. Exercise Write a PyGame script that moves a sprite horizontally with time.sleep().www.youtube.com/vkedco www.vkedco.blogspot.com
19. 19. Horizontal 2D Sprite Movement with TIME.SLEEP x=0 x_delta = 10 ## move sprite_img x_delta every sleep_time seconds sleep_time = 0.5 my_event = pygame.event.Event(KEYDOWN, key=K_SPACE, mod=0, unicode=u ) while True: pygame.event.post(my_event) ## checking for the QUICK event is omitted screen.blit(background_img, (0, 0)) screen.blit(sprite_img, (x, 80)) x += x_delta if x > 200.0: x -= 210.0 pygame.display.update() time.sleep(sleep_time) source code is herewww.youtube.com/vkedco www.vkedco.blogspot.com
20. 20. Exercise If a sprite moves at a speed of 15 pixels per second, how long will it take it to cross a screen of 255 pixels?www.youtube.com/vkedco www.vkedco.blogspot.com
21. 21. Exercise Write a PyGame script that moves a sprite horizontally with pygame.time.Clock().www.youtube.com/vkedco www.vkedco.blogspot.com
22. 22. Horizontal Clocked 2D Movement clock = pygame.time.Clock() x=0 speed = 15 ## pix/sec my_event = pygame.event.Event(KEYDOWN, key=K_SPACE, mod=0, unicode=u ) while True: pygame.event.post(my_event) for event in pygame.event.get(): if event.type == QUIT: ## quit the game screen.blit(background_img, (0, 0)) screen.blit(sprite_img, (x, 100)) time_passed = clock.tick() / 1000.0 distance_delta = speed * time_passed x += distance_delta if x > 225.0: x -= 225.0 pygame.display.update() source code is herewww.youtube.com/vkedco www.vkedco.blogspot.com
23. 23. Diagonal Movement ● To move a sprite diagonally: – Initialize the sprites x and y coordinates – Define x_delta and y_delta – Move the sprite x_delta and y_delta pixels simultaneouslywww.youtube.com/vkedco www.vkedco.blogspot.com
24. 24. Exercise Write a PyGame program that moves a sprite diagonally from the northwest corner to the southeast corner of the window. When the sprite reaches the southeast corner of the window, the it returned back to the northwest corner.www.youtube.com/vkedco www.vkedco.blogspot.com
25. 25. Diagonal Movement: Initialization screen_w = screen.get_width() screen_h = screen.get_height() sprite_w = sprite_img.get_width() sprite_h = sprite_img.get_height() sprite_x = 0 sprite_y = 0 speed_x = 100 ## pix/sec speed_y = 100 ## pix/secwww.youtube.com/vkedco www.vkedco.blogspot.com
26. 26. Diagonal Movement ## allow at most 30 frames per second time_passed = clock.tick(30) / 1000.0 ## change sprites x & y simultaneously sprite_x += speed_x * time_passed sprite_y += speed_y * time_passed ## return the sprite to the northwest corner if sprite_x > screen_w: sprite_x = 0 if sprite_y > screen_h: sprite_y = 0 source code is herewww.youtube.com/vkedco www.vkedco.blogspot.com
27. 27. Reading & References ● www.python.org ● http://en.wikipedia.org/wiki/Duck_typing ● http://www.pythonware.comwww.youtube.com/vkedco www.vkedco.blogspot.com