Skip to content

Introduction

:material-circle-edit-outline: 约 385 个字 :material-image-multiple-outline: 2 张图片 :material-clock-time-two-outline: 预计阅读时间 4 分钟

Pygame

  • Wraps SDL

Modules

alt text

Game Loop

    1. Check for inputs
    1. update states
    1. draw another frame base on the current situation

MVC

    1. architecutal style for GUI
    1. Model, View, Controller

Model

    1. well-defined interface for data processing
    1. database

View

    1. visualization
    1. rendering

Controller

    1. user input
    1. update model and view actions

Object-Oriented Models

Tip

    1. Object: instance of a class
    1. Class: blueprint for objects
    1. Inheritance: subclass inherits from superclass
    1. Polymorphism: subclass can override methods of superclass
    1. Encapsulation: data hiding
  • use objects for items in my game
  • use classes to define objects
  • each object knows its own state, and so is part of the model
  • each object can draw itself, so is part of the view

Tip

    1. Model: has references to objects and calls their update methods
    1. View: draw()
    1. Controller: user input

Graphics

    1. Engine: graphics algorithms embedded
    1. Colors: RGB, Alpha

The Surface

    1. 2D array of pixels,returned from display.set_mode()

The difference between class and module

Classes are blueprints that allow you to create instances with attributes and bound functionality. Classes support inheritance, metaclasses, and descriptors.
Modules can't do any of this, modules are essentially singleton instances of an internal module class, and all their globals are attributes on the module instance. You can manipulate those attributes as needed (add, remove and update), but take into account that these still form the global namespace for all code defined in that module.

    1. The display Surface is visible to the user
    1. background surface is copyed to the display surface while rendering
    1. get_at and set_at methods to access pixel values, set colors of it

Draw module

  • surface is a parameter
  • draw.rect(surface, color, rect)
  • draw.circle(surface, color, center, radius)
  • draw.line(surface, color, start_pos, end_pos, width)
  • draw.arc(surface, color, rect, start_angle, stop_angle, width)
    surface.fill(color): to fill the entire function
  • draw.polygon(surface,color,points,width=1)
  • Rect object:
      1. x,y,width,height alt text
  • Using rect with draw:

Pygame Images

    1. image formats: PNG, JPEG, BMP, GIF, etc
    1. load image: image.load(filename)
    1. convert() method to convert image to the same format as the display surface
    1. blit() method to draw the image on the display surface
    1. Blit optimization: blit() is faster than draw(), graphics harware handles the entire BLIT
    1. colorkey
    2. set_colorkey() method to set the transparent color
    1. surface transformation: scale, rotate, flip
    2. surface.transform.scale(surface, (width, height))
    3. surface.transform.rotate(surface, angle)
    4. surface.transform.flip(surface, xbool, ybool)