坦克大战pygame理解

pygame.display.set_mode()

Initialize a window or screen for display
set_mode(size=(0, 0), flags=0, depth=0, display=0) -> Surface
This function will create a display Surface. The arguments passed in are requests for a display type. The actual created display will be the best possible match supported by the system.

The size argument is a pair of numbers representing the width and height. The flags argument is a collection of additional options. The depth argument represents the number of bits to use for color.

The Surface that gets returned can be drawn to like a regular Surface but changes will eventually be seen on the monitor.

If no size is passed or is set to (0, 0) and pygame uses SDL version 1.2.10 or above, the created Surface will have the same size as the current screen resolution. If only the width or height are set to 0, the Surface will have the same width or height as the screen resolution. Using a SDL version prior to 1.2.10 will raise an exception.

It is usually best to not pass the depth argument. It will default to the best and fastest color depth for the system. If your game requires a specific color format you can control the depth with this argument. Pygame will emulate an unavailable color depth which can be slow.

When requesting fullscreen display modes, sometimes an exact match for the requested size cannot be made. In these situations pygame will select the closest compatible match. The returned surface will still always match the requested size.

On high resolution displays(4k, 1080p) and tiny graphics games (640x480) show up very small so that they are unplayable. SCALED scales up the window for you. The game thinks it’s a 640x480 window, but really it can be bigger. Mouse events are scaled for you, so your game doesn’t need to do it.

The flags argument controls which type of display you want. There are several to choose from, and you can even combine multiple types using the bitwise or operator, (the pipe “|” character). If you pass 0 or no flags argument it will default to a software driven window. Here are the display flags you will want to choose from:
eg:
screen_width=700
screen_height=400
screen=pygame.display.set_mode([screen_width,screen_height])

pygame.Surface.fill()

fill Surface with a solid color
fill(color, rect=None, special_flags=0) -> Rect
Fill the Surface with a solid color. If no rect argument is given the entire Surface will be filled. The rect argument will limit the fill to a specific area. The fill will also be contained by the Surface clip area.

The color argument can be either a RGB sequence, a RGBA sequence or a mapped color index. If using RGBA, the Alpha (A part of RGBA) is ignored unless the surface uses per pixel alpha (Surface has the SRCALPHA flag).
即填充颜色

pygame.display.set_caption()

Set the current window caption
set_caption(title, icontitle=None) -> None
If the display has a window title, this function will change the name on the window. Some systems support an alternate shorter title to be used for minimized displays.

pygame.image.load()

load new image from a file
load(filename) -> Surface
load(fileobj, namehint="") -> Surface
Load an image from a file source. You can pass either a filename or a Python file-like object.

Pygame will automatically determine the image type (e.g., GIF or bitmap) and create a new Surface object from the data. In some cases it will need to know the file extension (e.g., GIF images should end in “.gif”). If you pass a raw file-like object, you may also want to pass the original filename as the namehint argument.

The returned Surface will contain the same color format, colorkey and alpha transparency as the file it came from. You will often want to call Surface.convert() with no arguments, to create a copy that will draw more quickly on the screen.

For alpha transparency, like in .png images, use the convert_alpha() method after loading so that the image has per pixel transparency.

Pygame may not always be built to support all image formats. At minimum it will support uncompressed BMP. If pygame.image.get_extended() returns ‘True’, you should be able to load most images (including PNG, JPG and GIF).

You should use os.path.join() for compatibility.

eg. asurf = pygame.image.load(os.path.join(‘data’, ‘bla.png’))

pygame.mixer.Sound

Load a new sound buffer from a filename, a python file object or a readable buffer object. Limited resampling will be performed to help the sample match the initialize arguments for the mixer. A Unicode string can only be a file pathname. A Python 2.x string or a Python 3.x bytes object can be either a pathname or a buffer object. Use the ‘file’ or ‘buffer’ keywords to avoid ambiguity; otherwise Sound may guess wrong. If the array keyword is used, the object is expected to export a version 3, C level array interface or, for Python 2.6 or later, a new buffer interface (The object is checked for a buffer interface first.)

The Sound object represents actual sound sample data. Methods that change the state of the Sound object will the all instances of the Sound playback. A Sound object also exports an array interface, and, for Python 2.6 or later, a new buffer interface.

The Sound can be loaded from an OGG audio file or from an uncompressed WAV.

pygame.mixer.Sound.set_volume()

set the playback volume for this Sound

pygame.time.get_ticks()

get the time in milliseconds
get_ticks() -> milliseconds
Return the number of milliseconds since pygame.init() was called. Before pygame is initialized this will always be 0.

pygame.display.flip()

Update the full display Surface to the screen
flip() -> None
This will update the contents of the entire display. If your display mode is using the flags pygame.HWSURFACE and pygame.DOUBLEBUF, this will wait for a vertical retrace and swap the surfaces. If you are using a different type of display mode, it will simply update the entire contents of the surface.

When using an pygame.OPENGL display mode this will perform a gl buffer swap.

pygame.display.update()

Update portions of the screen for software displays
update(rectangle=None) -> None
update(rectangle_list) -> None
This function is like an optimized version of pygame.display.flip() for software displays. It allows only a portion of the screen to updated, instead of the entire area. If no argument is passed it updates the entire Surface area like pygame.display.flip().

You can pass the function a single rectangle, or a sequence of rectangles. It is more efficient to pass many rectangles at once than to call update multiple times with single or a partial list of rectangles. If passing a sequence of rectangles it is safe to include None values in the list, which will be skipped.

This call cannot be used on pygame.OPENGL displays and will generate an exception.

发布了5 篇原创文章 · 获赞 0 · 访问量 96

猜你喜欢

转载自blog.csdn.net/qq_40586386/article/details/102572589