Introduction
:material-circle-edit-outline: 约 385 个字 :material-image-multiple-outline: 2 张图片 :material-clock-time-two-outline: 预计阅读时间 4 分钟
Pygame¶
- Wraps SDL
Modules¶
Game Loop¶
-
- Check for inputs
-
- update states
-
- draw another frame base on the current situation
MVC¶
-
- architecutal style for GUI
-
- Model, View, Controller
Model¶
-
- well-defined interface for data processing
-
- database
View¶
-
- visualization
-
- rendering
Controller¶
-
- user input
-
- update model and view actions
Object-Oriented Models¶
Tip
-
- Object: instance of a class
-
- Class: blueprint for objects
-
- Inheritance: subclass inherits from superclass
-
- Polymorphism: subclass can override methods of superclass
-
- 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
-
- Model: has references to objects and calls their update methods
-
- View: draw()
-
- Controller: user input
Graphics¶
-
- Engine: graphics algorithms embedded
-
- Colors: RGB, Alpha
The Surface¶
-
- 2D array of pixels,returned from display.set_mode()
The difference between class and module
-
- The display Surface is visible to the user
-
- background surface is copyed to the display surface while rendering
-
- 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:
-
- x,y,width,height
- x,y,width,height
-
- Using rect with draw:
Pygame Images¶
-
- image formats: PNG, JPEG, BMP, GIF, etc
-
- load image: image.load(filename)
-
- convert() method to convert image to the same format as the display surface
-
- blit() method to draw the image on the display surface
-
- Blit optimization: blit() is faster than draw(), graphics harware handles the entire BLIT
-
- colorkey
- set_colorkey() method to set the transparent color
-
- surface transformation: scale, rotate, flip
- surface.transform.scale(surface, (width, height))
- surface.transform.rotate(surface, angle)
- surface.transform.flip(surface, xbool, ybool)