Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 22 Game Graphics.

Similar presentations


Presentation on theme: "1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 22 Game Graphics."— Presentation transcript:

1 1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 22 Game Graphics

2 2 Packages Packages have libraries of Python code that we can use with our programs. We will use two packages to create simple games: Pygames: The pygames package has many useful classes to make simple games. Livewires: The livewires package was created by educators to make the pygames package easier to use.

3 3 Creating a Graphics Screen We can use the livewires package to create a graphics screen with a background. Example: from livewires import games#import the games package #Create the graphics screen games.init(screen_width = 640, screen_height = 480, fps = 50) #Add a background image to the screen #First load in an image, then bind it to the background wall_image = games.load_image("wall.jpg", transparent = False) games.screen.background = wall_image #Start the main event loop games.screen.mainloop( )

4 4 Adding a sprite Sprites are images that move around the graphics screen. To add a sprite, first load in the image, then create a sprite with the image. Finally, add the sprite to the screen. Example: pizza_image = games.load_image("pizza.bmp") the_pizza = games.Sprite(image = pizza_image, x = 320, y = 240) games.screen.add(the_pizza) Note: The coordinate system for the graphics window has (0, 0) at the top left corner. The y coordinate increases as you move downward in the window.

5 5 Moving a Sprite A sprite can be moved by setting the horizontal or vertical displacement variables (dx and dy) to a non-zero value. With each iteration of the mainloop( ), the sprite's position by the amount given by dx and dy. Example: the_pizza = games.Sprite(image = pizza_image, x = 320, y = 240, dx = 1, dy = 1) This will move the sprite down and to the right. Note: We can also express the initial position as a function of the screen width and height: x = games.screen.width/2, y = games.screen.height/2 The above values will position the sprite in the middle of the screen.

6 6 Class Inheritance In python, we can create new classes from existing classes using inheritance. The new class inherits everything from the existing class. We can add new methods and properties, or modify methods from the existing class. The new class is called the child or subclass. The existing class is called the parent or superclass.

7 7 Creating a Pizza class To generate a more complex motion for our sprite, we will create a new class that will inherit from the Sprite class. The new class will define an update function that will move the pizza in a more complex pattern. class Pizza(games.Sprite): """A bouncing pizza""" 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


Download ppt "1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 22 Game Graphics."

Similar presentations


Ads by Google