cs498gd:more_images_animation_sprites
Table of Contents
More on Images, Animation and Sprites
Decoupling From Frame Rate
- The animation framework frame rate currently directly affects sprite speed, which is fine for animations, but not so fine for games.
- Example: MarioSprite.java at 120 frames per second.
- In this example, the sprite's position is incremented (x = x + xincr;) every time the game loop (run() method) is repeated, so the faster the game loop runs (higher frame rate), the faster the sprite moves back and forth.
- Want to be able to set a sprite's speed that will be independent of the animation frame rate.
- One way to accomplish this is to specify a sprite's speed in pixels per second, a quantity that we can actually visualize.
- From the pixels-per-second speed we can derive the sprite's speed in pixels per frame by dividing the pixels-per-second speed by the game frame rate (frames per second).
pixels frames pixels second pixels ------ / ------ = ------ * ------ = ------ second second second frames frame The sprite's speed in pixels per frame will then be inversely proportional to the game frame rate, i.e. the higher the frame rate, the lower the sprite speed in pixels per frame.
- Examples:
-
- In this example, sprite horizontal speed and rotation speed are made independent of the animation frame rate.
-
- Snowflake descent rate is independent of frame rate.
-
- A gravity-based interactive animation with sprite speed made independent of frame rate
-
Lab Activity: Menus
- Download and work on snow_slow_sprite.zip.
- Within that zip file, SpriteDemoPanel.java contains the beginnings of a simple on-screen, single-key-press menu that will allow control over the intensity of snowfall.
- Add code to SpriteDemoPanel.java to:
- Start light-to-heavy snowfall depending on menu-choices 1), 2) or 3)
- Just vary the number of snowflake sprites that are drawn.
- Exit to the menu when the Escape key is pressed
- Exit the program when Quit is chosen from the menu
- Sample solution: Modified SpriteDemoPanel.java
Lab Activity: Collisions
Download the "Lunar Lander" program.
A simple landing platform has been added to the program (See AnimatedSpriteDemoPanel.java).
Make the following changes to the program:
- Add collision detection to detect successful “landing” of the spaceship onto the landing platform.
- As an added twist, the landing must happen at a low enough vertical speed (e.g., >= -5) to be a successful landing.
- If a successful landing occurs, display “LANDED” somewhere on the screen and halt or pause the animation.
- If the lander hits the ground (bottom of screen), display “CRASHED” somewhere on the screen and halt or pause the animation.
cs498gd/more_images_animation_sprites.txt · Last modified: 2011/11/18 15:08 by jchung