Manual for Programming with Python.


98 views
Uploaded on:
Description
Manual for Programming with Python Part Eleven Representation: The Pizza Alarm Amusement Destinations Make a design window Make and control sprites Showcase content in an illustrations window Test for impacts between sprites Handle mouse data Control a PC adversary The Pizza Alarm Diversion
Transcripts
Slide 1

Manual for Programming with Python Chapter Eleven Graphics: The Pizza Panic Game

Slide 2

Objectives Create a design window Create and control sprites Display content in a representation window Test for impacts between sprites Handle mouse data Control a PC rival Guide to Programming with Python

Slide 3

The Pizza Panic Game Figure 11.1: Sample keep running of the Pizza Panic diversion The player must find the falling pizzas. Manual for Programming with Python

Slide 4

The Pizza Panic Game (proceeded with) Figure 11.2: Sample keep running of the Pizza Panic diversion Once the player misses a pizza, the amusement is over. pizza_panic.py Guide to Programming with Python

Slide 5

The pygame and livewires Packages Package: Set of modules pygame module Provides access to extensive variety of interactive media classes Made particularly to write recreations livewires module Built on top of pygame , less demanding to utilize Used for all illustrations amusements in reading material Guide to Programming with Python

Slide 6

Creating a Graphics Window Graphics window Must make as initial phase in design program All pictures and content showed on it Guide to Programming with Python

Slide 7

The New Graphics Window Program Figure 11.3: Sample keep running of the New Graphics Window program A first representation window. Not much, but rather it’s a begin. Manual for Programming with Python

Slide 8

The New Graphics Window Program (proceeded) from livewires import amusements games.init(screen_width = 640, screen_height = 480, fps = 50) games.screen.mainloop() (screen_width is truly window width screen_height is truly window tallness) Guide to Programming with Python

Slide 9

Importing the recreations Module from livewires import diversions from articulation gives you a chance to import particular module from a bundle livewires bundle made up of a few critical modules, including amusements diversions contains classes and capacities for diversion programming new_graphics_window.py Guide to Programming with Python

Slide 10

recreations Module Objects Table 11.1: Useful diversions module articles Guide to Programming with Python

Slide 11

diversions Module Classes Table 11.2: Useful diversions module classes Guide to Programming with Python

Slide 12

Initializing a Graphics Window games.init(screen_width = 640, screen_height = 480, fps = 50) recreations init() makes another design window screen_width for screen (window) width screen_height for screen (window) stature Screen measurements measured in pixels Pixel: single point on illustrations screen fps (outlines every second) for number of times screen upgraded every second Here, window width 640, tallness 480, overhauled 50 times each second Guide to Programming with Python

Slide 13

Starting the Main Loop games.screen.mainloop() screen speaks to the representation screen screen.mainloop() Updates illustrations screen fps times each second Keeps the design window open Guide to Programming with Python

Slide 14

Useful screen Properties Can get and set these properties with game.screen.<property> Table 11.3: Useful screen properties Guide to Programming with Python

Slide 15

Useful screen Methods Table 11.4: Useful screen techniques Guide to Programming with Python

Slide 16

Setting a Background Image Background picture Generally need one Example: Brick divider picture in Pizza Panic diversion Guide to Programming with Python

Slide 17

The Background Image Program Figure 11.4: Sample keep running of the Background Image system; foundation property of screen used to set foundation. Manual for Programming with Python

Slide 18

The Background Image Program (proceeded) from livewires import amusements games.init(screen_width = 640, screen_height = 480, fps = 50) wall_image = games.load_image("wall.jpg", straightforward = False) games.screen.background = wall_image games.screen.mainloop() Guide to Programming with Python

Slide 19

Loading an Image wall_image = games.load_image("wall.jpg", straightforward = False) load_image() capacity Loads and returns picture item Works with JPG, BMP, GIF, PNG, PCX, and TGA documents Takes two contentions String for record name of the picture True or False for straightforward (utilize False for foundation picture ) Here, wall_image set to picture put away in wall.jpg Guide to Programming with Python

Slide 20

Setting the Background games.screen.background = wall_image foundation property Represents foundation of illustrations screen Can be allocated a picture object Here, screen foundation is set to wall_image background_image.py Guide to Programming with Python

Slide 21

Understanding the Graphics Coordinate System Graphics screen made up of lines and sections of pixels Specify point on screen with directions: x and y x speaks to segment y speaks to column Upper-furthest left pixel is (0,0) Can put design questions anyplace on screen utilizing this direction framework Guide to Programming with Python

Slide 22

Understanding the Graphics Coordinate System (proceeded with) Figure 11.5: Graphics coordinate framework Specify focuses on representation screen with x-and y-direction sets. Manual for Programming with Python

Slide 23

Displaying a Sprite: An illustrations object with a picture Example: The cook, pizzas, and container in the Pizza Panic diversion Guide to Programming with Python

Slide 24

The Pizza Sprite Program Figure 11.6: Sample keep running of the Pizza Sprite program The pizza picture is not a foundation\'s piece, but rather a sprite. Manual for Programming with Python

Slide 25

The Pizza Sprite Program (proceeded) from livewires import recreations games.init(screen_width = 640, screen_height = 480, fps = 50) wall_image = games.load_image("wall.jpg", straightforward = False) games.screen.background = wall_image pizza_image = games.load_image("pizza.bmp") pizza = games.Sprite(image = pizza_image, x = 320, y = 240) games.screen.add(pizza) games.screen.mainloop() Guide to Programming with Python

Slide 26

Loading an Image for a Sprite pizza_image = games.load_image("pizza.bmp") In load_image() , default esteem for straightforward is True straightforward set to True permits foundation to appear through straightforward parts of picture Color of pixel at the upper-left corner of picture is its straightforward shading Guide to Programming with Python

Slide 27

Loading an Image for a Sprite (proceeded with) All parts of picture that are its straightforward shading permit foundation to appear through Here, pizza_image set to picture put away in pizza.bmp with straightforwardness Guide to Programming with Python

Slide 28

Loading an Image for a Sprite (proceeded with) Figure 11.7: Swiss cheddar picture On strong foundation to exploit straightforwardness. Manual for Programming with Python

Slide 29

Loading an Image for a Sprite (proceeded with) Figure 11.8: Swiss cheddar picture stacked two courses On left, straightforward True ; on right, straightforward False. Manual for Programming with Python

Slide 30

Creating a Sprite pizza = games.Sprite(image = pizza_image, x = 320, y = 240) Sprite is class for sprite picture is for picture x is for x-coordinate y is for y-coordinate Here, pizza gets Sprite object with pizza picture at (320,240) – focal point of screen Guide to Programming with Python

Slide 31

Adding a Sprite to the Screen games.screen.add(pizza) Sprite must be added to screen to be shown screen.add() adds sprite to screen Here, pizza is added to screen Guide to Programming with Python

Slide 32

Useful Sprite Properties Table 11.5: Useful Sprite properties Guide to Programming with Python

Slide 33

Useful Sprite Methods Table 11.6: Useful Sprite routines pizza_sprite.py Guide to Programming with Python

Slide 34

Displaying Text Can show content on screen Example: The player’s score in the Pizza Panic amusement Guide to Programming with Python

Slide 35

The Big Score Program Figure 11.9: Sample keep running of the Big Score program The score is shown after a Text item is instantiated. Manual for Programming with Python

Slide 36

Importing the shading Module from livewires import amusements, shading Module in livewires bundle Defines set of constants for hues Constants can be utilized with Text objects Example: color.black Guide to Programming with Python

Slide 37

Creating a Text Object score = games.Text(value = 1756521, size = 60, shading = color.black, x = 550, y = 30) Text is class for content on representation screen worth is for quality to be shown as content size is for stature in pixels shading is for shading Here, score gets Text article showed as 1756521 , 60 pixels high in dark at (550,30) Guide to Programming with Python

Slide 38

Adding a Text Object to the Screen games.screen.add(score) Text object must be added to screen to be shown Here, score is added to the screen Guide to Programming with Python

Slide 39

Useful Text Properties Table 11.7: Additional Text properties big_score.py Guide to Programming with Python

Slide 40

Displaying a Message is transitory content, vanishes after set timeframe Example: “Game Over!” in the Pizza Panic diversion Guide to Programming with Python

Slide 41

The You Won Program Figure 11.10: Sample keep running of the You Won program The message vanishes following a few moments. Manual for Programming with Python

Slide 42

Importing the shading Module from livewires import diversions, shading Constants can be utilized with Message items Guide to Programming with Python

Slide 43

Creating a Message Object won_message = games.Message( esteem = "You won!", size = 100, shading = color.red, x = games.screen.width/2, y = games.screen.height/2, lifetime = 250, after_death = games.screen.quit) Guide to Programming with Python

Slide 44

Creating a Message Object (proceeded with)

Recommended
View more...