Download presentation
Presentation is loading. Please wait.
Published byFrancine Lloyd Modified over 8 years ago
1
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 25 Game Graphics--Text and Animation
2
2 The games.Text class The games module provides a games.Text class that can be used to add text of a given value to the screen. The Text object is a type of Sprite. Example: from livewires import games, color myScore = games.Text(value = 0, size = 60, color = color.black, x = 550, y = 30) games.screen.add(myScore) Notes: The size of the text is given in pixels. To use color.black, you must import the color module from livewires.
3
3 Problem: Because games.Text inherits from the Sprite class, if we use it in our Pizza Pan game, we will get an error if we move the pan over the text, because we have not provided a handle_collide function. Solution: Create a new class that inherits from games.Text and provide a handle_collide function for it. Example: class Score(games.Text): def handle_collide(self): x = 0 #Line of code that does nothing relevant Note: Must change games.Text to Score in the main program. myScore = Score(value = 0, size = 60,...) The Problem with Text and Collisions
4
4 Changing the Text Value To change the value of the text, use the dot notation: myScore.value = myScore.value + 10 In our Pizza game, we can do this whenever the Pan collides with a pizza by changing the value of myScore.value in the handle_collision function: class Pizza(games.Sprite): def handle_collision(self): myScore.value = myScore.value + 10 self.destroy( )
5
5 Adding a Message The games.Message class can be used to add a message: Example: won_message = games.Message(value = "You won!", size = 100 color = color.red, x= games.screen.width/2, y = games.screen.height/2, lifetime = 250, after_death=games.screen.quit) games.screen.add(won_message) Notes: lifetime is the number of frames for the display. 250 frames is 5 seconds for a screen being displayed at 50 frames per second. after_death gives the name of the function (without parentheses) that is called after the message disappears.
6
6 Handling Collisions (Again) As with Text, we need to Handle collisions for our message. We can do this by creating a new class: class WinMessage(games.Message): def handle_collide(self): x = 0 #Essentially do nothing Make sure to change the creation of the message to the WinMessage class: won_message = WinMessage(value = "You Won!",...) games.screen.add(won_message)
7
7 Displaying the Message at the End of the Game We would like the message to appear at the end of the game. In our demo games, the user wins when they have caught all 4 pizzas, and thus have 40 points. We will change the handle_collide function in the Pizza class to display the message when the score is 40 or greater. In Pizza class: def handle_collide(self): myScore.value = myScore.value + 10 self.destroy( ) if myScore >= 40: self.displayMessage( )
8
8 The displayMessage( ) function In the Pizza class: def displayMessage(self): won_message = WinMessage(value = "You Won!", size = 100 color = color.red, x= games.screen.width/2, y = games.screen.height/2, lifetime = 250, after_death=games.screen.quit) games.screen.add(won_message)
9
9 Creating an Animation The Animation class in the games module of livewires allows you to create an animation from a set of individual frames. This class takes a list of images and displays them one after the other. Step 1. Create a list of files: explosion_files = ["explosion1.bmp", "explosion2.bmp", "explosion3.bmp", "explosion4.bmp", "explosion5.bmp", "explosion6.bmp", "explosion7.bmp", "explosion8.bmp", "explosion9.bmp"]
10
10 Animating the Frames Step 2: Create the Animation object. explosion = games.Animation(images = explosion_files, x = games.screen.width/2, y = games.screen.height/2, n_repeats = 0, repeat_interval = 5) games.screen.add(explosion) Notes: n_repeats is the number of times the animation repeats. If it is set to zero, the animation will loop forever. repeat_interval is the delay between frames. Higher numbers result in slower animation.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.