wssking Python Note: Use 15 pygame - Definitions screen, load image

and the loading of the screen is defined pygame image: 

example:

Import SYS
Import pygame

class Fly: # Loading image plane, and determines the position of the image
def __init __ (self, screen) : # FLy class defines an attribute to Screen, for instance the image parameters passed over, screen will fly the receiving instance of the parameter screen
self.screen screen =
self.image = pygame.image.load (r'images \ ship.png ') # define a property image, save for Loading image
self.rect = self.image.get_rect () # here the above image processing is defined as a rectangle, and stores the variable rect
self.screen_rect = screen.get_rect () # screen also be defined as a rectangle, ease of handling, the save screen_rect variable

self.rect.centerx = self.screen_rect.centerx # center of the rectangular image, and the center of a screen rectangular alignment (CenterX parametric image is a rectangle))
self.rect. # bottom = self.screen_rect.bottom the bottom of the image rectangle, and a rectangular bottom of the screen, is aligned

def position (self): # define a method for transferring image and its position
self.screen.blit (self.image, self.rect) # blit () function is used to acquire image information stored two parameters (image file address, location of the image)


class settings: # define a class, for width, height and color of the screen background is set
DEF the __init __ (Self):
self.screen_width = 800
self.screen_height = 400
self.bg_color = (230, 230, 230)


DEF run_play ():
Settings = Settings () # Settings instantiated class
pygame.init () # initialize the pygame
pygame.display.set_caption ( "XXX") # screen window defined title content
screen = pygame.display. set_mode (# define the width and height of the window screen, obtained by the class attribute Settings
(settings.screen_width, settings.screen_height) #pygame.display.set_mode parameters within, must be a list, it must be added in brackets ()
)

Fly = Fly (screen) # Fly class instantiation, and passes the contents of the screen above the window screen
while True: # define a while loop for constantly refresh the screen contents
for x in pygame.event.get (): # define a for loop, motion detection for mouse and keyboard 
if x.type == pygame.QUIT: # exit actions when a user is detected when calling sys.exit () to close the program
sys.exit ()
screen.fill (settings.bg_color) # defined by the screen background color, constantly refresh the screen window
fly.position () # method Fly class loading position, for displaying image information (images and position)
pygame.display.flip () #flip () function is used to display the most recent screen

run_play ()

Guess you like

Origin www.cnblogs.com/wssking/p/11573714.html