Download presentation
Presentation is loading. Please wait.
Published byGeraldine Bell Modified over 9 years ago
1
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 23 Game Graphics II
2
2 Class Inheritance In python, we can create new classes from existing classes using inheritance. The new class is called the child or subclass. The existing class is called the parent or superclass. The child class inherits everything (both properties and methods) from the parent class. The child class is specialized by adding new methods and/or properties, or modifying methods inherited from the parent class.
3
3 Inheritance Hierarchy among vehicles vehicle wheeled vehicleboat bicyclecar four-door two-door Every car is a wheeled vehicle.
4
4 Inheritance for Sprites The Sprite class is defined in the games module of livewires. It is referenced as: games.Sprite( ) Some properties of the Sprite class are: image, width, height, x, y, dx, dy, left, right, top, bottom, etc. Some methods of the Sprite class are: get_image( ), set_image(new_image), get_x( ), set_x(new_x) get_y( ), set_y(new_y), update( ), etc.
5
5 The Pizza Class The Pizza class inherits from the Sprite class: class Pizza(games.Sprite): def update(self ): if self.right > games.screen.width or self.left < 0: self.dx = -self.dx if self.bottom > games.screen.height or self.top < 0: self.dy = - self.dy The Pizza class will have all the properties and methods of the Sprite class. The new update( ) method will override the Sprite update method.
6
6 General form of Class inheritance In general, the form for creating a child class from a parent class is as follows: class childClassName(parentClassName): #Properties and method definitions go here.
7
7 The mouse class The games module provides a mouse class. Properties of the mouse class: games.mouse.xthe x position of the mouse games.mouse.ythe y position of the mouse games.mouse.is_visibleIf true, the cursor is visible. If false, the cursor is invisible Methods of the mouse class: games.mouse.get_x( ) Returns the x position of the mouse games.mouse.set_x(new_x) Sets the x position to new_x games.mouse.get_y( ) Returns the y position of the mouse games.mouse.set_y(new_y) Sets the y position to new_y games.mouse.is_pressed(button_number) Returns true if the button is pressed.
8
8 A Mouse-controlled Sprite Use the update function to move a sprite with the mouse: class Pan(games.Sprite): def update(self): self.x = games.mouse.x self.y = games.mouse.y... #add to main program: pan_image = games.load_image("pan.bmp") the_pan = Pan(image = pan_image, x = games.mouse.x, y = games.mouse.y) games.screen.add(the_pan)
9
9 Other useful mouse properties Make the cursor invisible with: games.mouse.is_visible = False Keep the cursor within the screen limits: games.screen.event_grab = True Notes: If using event_grab, need to end program with the Escape key After mainloop( ), reset games.mouse.is_visible = True Leave the cursor visible while debugging program.
10
10 Detecting Collisions To detect a collision, we will write a function, check_collide( ), in the Pan class to check for overlap with other sprites. We will then modify the update( ) function of the Pan class to call check_collide( ) def check_collide(self): for the_pizza in self.overlapping_sprites: pizza.handle_collide( ) def update(self): self.x = games.mouse.x self.y = games.mouse.y self.check_collide( )
11
11 Writing handle_collide Because we want the Pizza to do something when the pan hits it, we will add the function handle_collide( ) to the Pizza class: class Pizza(games.Sprite): #update function here... def handle_collide(self): #Move pizza to new random position in the screen self.x = random.randrange(games.screen.width) self.y = random.randrange(games.screen.height)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.