Sprites
- One other example of mutability, sprites
- I also created a module to make doing some of common things with the graphics module easier - import graphics import sprite a_sprite = sprite.Sprite(0, 0, 'sprite.gif') while graphics.window_open(): graphics.clear() graphics.draw_sprite(a_sprite) graphics.wait()
- Similar to the turtle module, you create sprite objects
- The graphics module can draw a sprite
- Note, the location of the sprite is an attribute of the sprite
- You can change the attributes of a sprite using the dot notation - a_sprite.y += 1
- Sprites are mutable, which means that functions can modify them
- This makes it much easier to organize you code - def update_sprite(a_sprite): a_sprite.y += 1
- Also have useful methods for testing for and handling collision - if a_sprite.collides(b_sprite): a_sprite.speed_y = 0 a_sprite.uncollide(b_sprite)