Pygame - Python game programming entry

Import pygame >>>
>>> Print (pygame.ver)
1.9.2a0
  If there is no error, it should be installed a ~

  If the error can not find the module is likely to be a problem installed version.

The specified module could not be found.

Path 1.2 pygame documentation

  pygame documentation is very complete, when we encounter usage problems, take a look at the document, many problems would be solved.

  Pygame Documentation:% of your Python installation path% / python / Lib / site-packages / pygame / docs / index.html

  (Different versions of Python and pygame documentation location may be different, I've just posted their own path, your information, the library installed it wants to look at your own Python installation path; if that trouble, there are online documents attached: www.pygame.org/docs/)

  That pygame module in which it, docs has also been given:

模块 简介
pygame.BufferProxy An array protocol view of surface pixels
pygame.cdrom How to access and control the CD audio devices.
pygame.Color Color representation.
pygame.cursors Loading and compiling cursor images.
pygame.display Configure the display surface.
pygame.draw Drawing simple shapes like lines and ellipses to surfaces.
pygame.event Manage the incoming events from various input devices and the windowing platform.
pygame.examples Various programs demonstrating the use of individual pyame modules.
pygame.font Loading and rendering Truetype fonts.
pygame.freetype Enhanced Pygame module for loading and rendering font faces.
pygame.gfxdraw Anti-aliasing draw functions.
pygame.image Loading, saving, and transferring of surfaces.
pygame.joystick Manage the joystick devices.
pygame.key Manage the keyboard device.
pygame.locals Pygame constants.
pygame.mixer Load and play sounds
pygame.mouse Manage the mouse device and display.
pygame.movie Video playback from MPEG movies.
pygame.mixer.music Play streaming music tracks.
pygame.Overlay Access advanced video overlays.
pygame Top level functions to manage Pygame.
pygame.PixelArray Manipulate image pixel data.
pygame.Rect Flexible container for a rectangle.
pygame.scrap Native clipboard access.
pygame.sndarray Manipulate sound sample data.
Higher Level Objects to Represent pygame.sprite Game Images.
pygame.Surface for Objects and Images The Screen.
pygame.surfarray the Manipulate Image Pixel Data.
pygame.tests the Pygame the Test.
pygame.time the Manage Timing and frameRate.
pygame.transform the Resize Images and Move .

  Introduction was all very intuitive, but here is not much explanation. Module which provides a more direct operation (only relative), an image like paste; also provides a relatively high level of abstraction of the operation, like a wizard module (sprite). Anyway, we have a general understanding of what to use, go to conscientiously study and related things.

 

2. Our first task - to add the game background
2.1 before loading background, we have to have a window, right? Do it ~

import pygame # import pygame library
from pygame.locals import * # import pygame library constants
from sys import exit # import sys exit function library

Resolution # window defined
SCREEN_WIDTH = 480
SCREEN_HEIGHT = 640

# initialize game
pygame.init () # initialize the pygame
Screen with pygame.display.set_mode = ([SCREEN_WIDTH, SCREEN_HEIGHT]) # window initialization for display
pygame.display.set_caption ( 'This is my first pygame- program') # set the window title

# Event loop (main Loop)
the while True:

# processed games Exit
# taken from the message queue loop
for Event in pygame.event.get ():
IF event.type == pygame.quit:
pygame.quit ()
Exit ()

  Result of the program:

 

  Although the dark, we are not able to do, but he was taken the first step -

2.2 Next to several parts of the program focuses on interpretation:

line 2: pygame.locals module which contains a lot of pygame constants need to use, such as a flag (flags) set_mode inside the window, the message event (event) type and so on. In addition, the program wants to use the constant pygame.locals module inside the pygame, you can only use "from pygame.locals import *".

line 3: Python is a manifestation of simplicity, what features you need, what would import function. Here we need to use the exit function to close the window, it is from sys import library.

line 5: the actual resolution of the background image is 480 * 800, but my computer screen resolution is only 1280 * 800, so I do change it "short" a.

line 10: pygame.init () will initialize all imported pygame module. But when a module error occurs, this function does not throw an exception, relatively, init returns a tuple, including the successful initialization of the module as well as the number of errors that occur module number (). In this example, the output of init () Returns information can be seen,

>>> ================================ RESTART ============== ==================
>>>
(6, 0)
if you'd prefer, you can manually one by one module initialization, so that you can catch the thrown exception.

line 11: set_mode () function creates a display panel (surface), it has three parameters, a first surface is the resolution, i.e. the size of the window, in the form of an input tuple (width, height), if not entered or set to (0, 0), then the system will set the resolution of the surface current screen resolution (pygame uses sdl version 1.2.10 or above); a second flag (the flags), i.e. surface of the display mode is selected, the following Table:

#pygame.FULLSCREEN create a fullscreen display
#pygame.DOUBLEBUF recommended for HWSURFACE or OPENGL
#pygame.HWSURFACE hardware accelerated, only in FULLSCREEN
#pygame.OPENGL create an OpenGL renderable display
#pygame.RESIZABLE display window should be sizeable
#pygame.NOFRAME display window will have no border or controls

The third parameter is the bit depth (depth), that is, how many bits to represent the color of the document suggesting a need to set this parameter, the system selects the optimal value.

line 15: In our main program, we need an event loop (loop) to continuously detect user interaction during the operation. pygame.event.get () method from the event queue can get a list of all events, we use a loop continuously detect every event, as soon as we find quit the event, we will quit the game to close the window. Another point worth thinking about, if we do not set an exit operations, our black window will become what? As a result, when we click the red cross on the top right corner of the window, quit the program still can detect an event, but the window is not closed, because we do not set up operations related to it.

2.3 enshrined background

  With the above knowledge, we have a general understanding of the framework program, then into the topic! Loading background image ~

  Note that we add a few sentences to ~ (. · Ω ·.)

 

import pygame # import pygame library
from pygame.locals import * # import pygame library constants
from sys import exit # import sys exit function library

Resolution # window defined
SCREEN_WIDTH = 480
SCREEN_HEIGHT = 640

# initialize game
pygame.init () # initialize the pygame
Screen with pygame.display.set_mode = ([SCREEN_WIDTH, SCREEN_HEIGHT]) # initialization window
pygame.display.set_caption ( 'This is my first pygame-program ') # set the window title

BACKGROUND FIG. # Loading
background = pygame.image.load ( 'resources / image / background.png') # new

# Event loop (main Loop)
the while True:

# Draw the background
screen.blit (background, (0, 0)) new new #
# updating the screen
the pygame.display.update () new new #

# game processing exits
# loop taken from the message queue
for event in pygame.event.get () :
IF event.type == pygame.quit:
pygame.quit ()
Exit ()

Result of the program:

 

We have added three statements in the original program. The first sentence is loading background images, resources folders to keep your files together py oh; the second sentence used to draw images, surface.blit () is considered a very commonly used functions, the first parameter is the image resources, and the second parameter determines the position of the image is placed (coordinates of the upper left corner); third statement is to update the screen, it is to draw a good background to "brush" up. Here one more point, on pygame.display.flip () and pygame.display.update (), the document says, update optimized version is more like a flip, flip the main difference is the overall refresh of the screen (entire), and update is partial refresh (portion). Finally, to think about a problem, if we move to the back of two new statements while loop above will happen? In this demo will not be significantly affected, but when we later add other image elements on the screen, a perform update operations, there is no background.

Guess you like

Origin www.cnblogs.com/hyhy904/p/11058044.html