본문 바로가기

코딩/파이썬 pygame

파이썬 게임 013. Pygame Documentation Summary

Game - Python - Pygame

아래 내용은 Pygame 2.x Documentation을 기준으로 한다.

pygame

Top level pygame package

 

pygame.get_init() -> bool
pygame.get_sdl_version() -> (major, minor, patch)
pygame.init() -> (numpass, numfail)
pygame.quit() -> None

pygame.Color

pygame object for color representations

 

Color(r, g, b) -> Color
Color(r, g, b, a=255) -> Color
Color(color_value) -> Color

color_value:
  pygame.Color
  str or tuple or list (r, g, b, a)
  HTML color format str ('#rrggbbaa')
  hex number str ('0xrrggbbaa')

Color(255, 255, 255, 255) // Color(0, 64, 64, 64) == Color(0, 3, 3, 3)
Color.<?>:
  r -> int
  g -> int
  b -> int
  a -> int
Color.update(r, g, b) -> None
Color.update(r, g, b, a=255) -> None
Color.update(color_value) -> None

pygame.display

pygame module to control the display window and screen

 

By default, the pygame display is a basic software driven framebuffer.

 

pygame.display.flip() -> None
pygame.display.get_driver() -> name

The name of the pygame display backend.
pygame.display.get_surface() -> Surface
pygame.display.get_window_size() -> tuple

It may differ from the size of the display surface if SCALED is used.
pygame.display.Info() -> VideoInfo
pygame.display.mode_ok(size, flags=0, depth=0, display=0) -> depth

It return 0 if the display mode cannot be set. Otherwise it return a pixel depth.
pygame.display.set_caption(title, icontitle=None) -> None
pygame.display.set_icon(Surface) -> None

Around 32 x 32. It can be called before pygame.display.set_mode().
pygame.display.set_mode(size=(0, 0), flags=0, depth=0, display=0, vsync=0) -> Surface

size:
  (width, height). (0, 0) --> current screen resolution.
    
flags:
  FULLSCREEN --> create a fullscreen display
  DOUBLEBUF --> recommended for HWSURFACE or OPENGL
  HWSURFACE --> hardware accelerated, only in FULLSCREEN
  OPENGL --> create an OpenGL-renderable display
  RESIZABLE --> display window should be sizeable
  NOFRAME --> display window will have no border or controls
  SCALED --> resolution depends on desktop size and scale graphics
  SHOWN --> window is opened in visible mode (default)
  HIDDEN --> window is opened in hidden mode
  multiple types --> (pygame.SCALED | pygame.NOFRAME)
pygame.display.update(rectangle=None) -> None
pygame.display.update(rectangle_list) -> None

Optimized version of pygame.display.flip() for software display.
It cannot be used on pygame.OPENGL display.

pygame.draw

pygame module for drawing shapes

 

pygame.draw.aaline(surface, color, start_pos, end_pos, blend=1) -> Rect
pygame.draw.aalines(surface, color, closed, points, blend=1) -> Rect
pygame.draw.arc(surface, color, rect, start_angle, stop_angle, width=1) -> Rect
pygame.draw.circle(surface, color, center, radius, width=0,
    draw_top_right=None, draw_top_left=None,
    draw_bottom_left=None, draw_bottom_right=None) -> Rect
pygame.draw.ellipse(surface, color, rect, width=0) -> Rect
pygame.draw.line(surface, color, start_pos, end_pos, width=1) -> Rect
pygame.draw.lines(surface, color, closed, points, width=1) -> Rect

closed: If Ture, drawn between the first and last points
pygame.draw.polygon(surface, color, points, width=0) -> Rect

points: tuple, list or pygame.math.Vector2. (e.g.) [(x1, y1), (x2, y2), (x3, y3)]
pygame.draw.rect(surface, color, rect, width=0, border_radius=0,
    border_top_left_radius=-1, border_top_right_radius=-1,
    border_bottom_left_radius=-1, border_bottom_right_radius=-1) -> Rect

width: =0 --> fill / >0 --> line thinkness / <0 --> not drawing

pygame.event

pygame module for interacting with events and queues

 

The event queue has an upper limit (128 for std. SDL 1.2).

To spped up queue processing, use pygame.event.set_blocked() to limit which events get queued.

User defined events should have a value in range of USEREVENT to NUMEVENTS - 1.

 

pygame.event.EventType.type:
  QUIT  --> none
  ACTIVEEVENT --> gain, state
  KEYDOWN --> key, mod, unicode, scancode
  KEYUP --> key, mod
  MOUSEMOTION --> pos, rel, buttons
  MOUSEBUTTONUP --> pos, button
  MOUSEBUTTONDOWN --> pos, button
  JOYAXISMOTION --> joy (deprecated), instance_id, axis, value
  JOYBALLMOTION --> joy (deprecated), instance_id, ball, rel
  JOYHATMOTION --> joy (deprecated), instance_id, hat, value
  JOYBUTTONUP --> joy (deprecated), instance_id, button
  JOYBUTTONDOWN --> joy (deprecated), instance_id, button
  VIDEORESIZE --> size, w, h
  VIDEOEXPOSE --> none
  USEREVENT -->  code

  AUDIODEVICEADDED --> which, iscapture
  
AUDIODEVICEREMOVED --> which, iscapture
  FINGERMOTION --> touch_id, finger_id, x, y, dx, dy
  FINGERDOWN --> touch_id, finger_id, x, y, dx, dy
  FINGERUP --> touch_id, finger_id, x, y, dx, dy
  MOUSEWHEEL --> which, flipped, x, y
  MULTIGESTURE --> touch_id, x, y, pinched, rotated, num_fingers
  TEXTEDITING -->  text, start, length
  TEXTINPUT --> text
  WINDOWEVENT --> event

  CONTROLLERDEVICEADDED --> device_index
  JOYDEVICEADDED --> device_index
  CONTROLLERDEVICEREMOVED --> instance_id
  JOYDEVICEREMOVED --> instance_id
  CONTROLLERDEVICEREMAPPED --> instance_id
pygame.event.clear(eventtype=None, pump=True) -> None
pygame.event.custom_type() -> int
pygame.event.get(eventtype=None, pump=True) -> Eventlist
pygame.event.get_grab() -> bool
pygame.event.poll() -> EventType instance

Returns a single event from the queue.
pygame.event.post(Event) -> None

Places the given event at the end of the event queue.
pygame.event.pump() -> None
pygame.event.set_allowed(type) -> None
pygame.event.set_allowed(typelist) -> None
pygame.event.set_allowed(None) -> None
pygame.event.set_blocked(type) -> None
pygame.event.set_blocked(typelist) -> None
pygame.event.set_blocked(None) -> None

If None is passed, all of events are blocked.
pygame.event.set_grab(bool) -> None

If True, it will lock all inputs into the program(mouse and keyboard).
pygame.event.wait(timeout) -> EventType instance

timeout : ms

pygame.image

pygame module for image transfer

 

loading images:

  JPG
  PNG
  GIF (non-animated)
  BMP
  PCX
  TGA (uncompressed)
  TIF
  LBM (and PBM)
  PBM (and PGM, PPM)
  XPM

 

saving images:

  BMP
  TGA
  PNG
  JPEG

 

pygame.image.frombuffer(bytes, size, format) -> Surface

format:
  P --> 8-bit palettized Surfaces
  RGB --> 24-bit image
  BGR --> 24-bit image, red and blue channels swapped.
  RGBX --> 32-bit image with unused space
  RGBA --> 32-bit image with an alpha channel
  ARGB --> 32-bit image with alpha channel first
pygame.image.load(filename) -> Surface
pygame.image.load(fileobj, namehint="") -> Surface
pygame.image.save(Surface, filename) -> None
pygame.image.save(Surface, fileobj, namehint="") -> None

pygame.key

pygame module to work with the keyboard

 

pygame.key.get_pressed() -> bools

The Return is 0 or 1.

pygame.mixer

pygame module for loading and playing sounds

 

pygame.mixer.Sound(filename) -> Sound
pygame.mixer.Sound(file=filename) -> Sound
pygame.mixer.Sound(file=object) -> Sound
pygame.mixer.Sound(buffer) -> Sound
pygame.mixer.Sound(buffer=buffer) -> Sound
pygame.mixer.Sound(object) -> Sound
pygame.mixer.Sound(array=object) -> Sound

Sound.play(loops=0, maxtime=0, fade_ms=0) -> Channel

pygame.mouse

pygame module to work with the mouse

 

pygame.mouse.get_pos() -> (x, y)
pygame.mouse.get_pressed(num_buttons=3) -> (button1, button2, button3)
pygame.mouse.get_pressed(num_buttons=5) -> (button1, button2, button3, button4, button5)

button1 = left, button2 = wheel, button3 = right
pygame.mouse.set_visible(bool) -> bool

pygame.Rect

pygame object for stroing rectangular coordinates

 

Rect(left, top, width, height) -> Rect
Rect((left, top), (width, height)) -> Rect
Rect(object) -> Rect
Rect.<?>:
  x, y
  top, left, bottom, right
  topleft, bottomleft, topright, bottomright
  midtop, midleft, midbottom, midright
  center, centerx, centery
  size, width, height
  w, h
Rect.move(x, y) -> Rect

x and y can be any interger, positive or negative. The Rect instance is not changed.
Rect.move_ip(x, y) -> None

The Rect instance changes.

pygame.sprite

pygame module with basic game object classes

 

The Group.draw() and Group.clear() methods require that each Sprite has a Surface.image and Surface.rect attributes.

 

pygame.sprite.Group(*sprites) -> Group

supported python operations:
  in, len, bool, iter

Group.add(*sprites) -> None
Group.clear(Surface_dest, background) -> None
Group.copy() -> Group
Group.draw(Surface) -> None
Group.empty() -> None
Group.has(*sprites) -> bool
Group.remove(*sprites) -> None
Group.sprites() -> sprite_list
Group.update(*args, **kwargs) -> None
pygame.sprite.GroupSingle(sprite=None) -> GroupSingle

property:
  GroupSingle.sprite
pygame.sprite.RenderUpdates(*sprites) -> RenderUpdates

RenderUpdates.draw(surface) -> Rect_list
pygame.sprite.Sprite(*groups) -> Sprite

Sprite.add(*groups) -> None
  # add the Sprite to groups
Sprite.alive() -> bool
Sprite.groups() -> group_list
Sprite.kill() -> None
  # remove the Sprite from all Groups
Sprite.remove(*groups) -> None
Sprite.update(*args, **kwargs) -> None
  # method to control Sprite behavior. It is called by Group.update() with whatever arguments you give it.

pygame.Surface

pygame object for representing images

 

There are three types of transparency supported in pygame: colorkeys, surface alphas, pixel alphas.

Per pixel alphas cannot be mixed with surface alphas and colorkeys.

 

Surface((width, height), flags=0, depth=0, masks=None) -> Surface
Surface((width, height), flags=0, Surface) -> Surface

Surfaces with 8-bit pixels use a color palette to map to 24-bit color.

flags:
  HWSURFACE --> creates the image in video memory
  SRCALPHA --> the pixel format will include a per-pixel alpha
Surface.blit(source, dest, area=None, special_flags=0) -> Rect

dest:
  points (top, left) or pygame.Rect

area:
  pygame.Rect. A smaller portion of the source to draw.

special_flags:
  BLEND_ADD
  BLEND_SUB
  BLEND_MULT
  BLEND_MIN
  BLEND_MAX

  BLEND_RGB_ADD
  BLEND_RGB_SUB
  BLEND_RGB_MULT
  BLEND_RGB_MIN
  BLEND_RGB_MAX

  BLEND_RGBA_ADD
  BLEND_RGBA_SUB
  BLEND_RGBA_MULT
  BLEND_RGBA_MIN
  BLEND_RGBA_MAX

  BLEND_PREMULTIPLIED

  BLEND_ALPHA_SDL2
Surface.convert(Surface=None) -> Surface
Surface.convert(depth, flags=0) -> Surface
Surface.convert(masks, flags=0) -> Surface
Surface.convert_alpha(Surface) -> Surface
Surface.convert_alpha() -> Surface
Surface.get_alpha() -> int_value
Surface.get_colorkey() -> RGB or None
Surface.get_height() -> height
Surface.get_rect(**kwargs) -> Rect

It starts at (0, 0) with the same size as the Surface.
**kwargs --> center(100, 100).
Surface.get_size() -> (width, height)
Surface.get_width() -> width
Surface.fill(color, rect=None, special_flags=0) -> Rect

special_flags:
  BLEND_ADD
  BLEND_SUB
  BLEND_MULT
  BLEND_MIN
  BLEND_MAX

  BLEND_RGB_ADD
  BLEND_RGB_SUB
  BLEND_RGB_MULT
  BLEND_RGB_MIN
  BLEND_RGB_MAX

  BLEND_RGBA_ADD
  BLEND_RGBA_SUB
  BLEND_RGBA_MULT
  BLEND_RGBA_MIN
  BLEND_RGBA_MAX
Surface.set_alpha(value, flags=0) -> None
Surface.set_alpha(None) -> None

0 is fully transparent and 255 is fully opaque.
Surface.set_colorkey(Color, flags=0) -> None
Surface.set_colorkey(None) -> Non

If None, the color key will be unset.

flags:
  RLEACCEL  # better performanced on non accelerated display

pygame.time

pygame module for monitoring time

 

pygame.time.Clock() -> Clock

Clock.get_time() --> milliseconds
Clock.get_fps() --> float  # averaging the last 10
Clock.get_rawtime() --> milliseconds  # not include any time used while Clock.tick() is delaying
Clock.tick(framerate=0) --> milliseconds
Clock.tick_busy_loop(framerate=0) --> milliseconds  # It uses pygame.time.dealy()
pygame.time.delay(milliseconds) -> time

It will pause for a given number of ms.
pygame.time.get_ticks() -> milliseconds
pygame.time.set_timer(eventid, milliseconds, once) -> None
-> pygame.time.set_timer(event, millis, loops=0) -> None

To disable the timer for an event, set the milliseconds argument to 0.
If the once argument is True, then only send the timer once.
pygame.time.wait(milliseconds) -> time

pygame.transform

pygame module to transform surfaces

 

pygame.transform.flip(Surface, xbool, ybool) -> Surface
pygame.transform.scale(Surface, (width, height), DestSurface = None) -> Surface

pygame.version

small module containing version information

 

pygame.version.SDL  # PygameVersion class
pygame.version.ver  # version as a string
pygame.version.vernum  # PygameVersion class