1. **pygame.display**
用于管理游戏窗口的显示和设置。
- **函数**:
- `pygame.display.set_mode((width, height))`:创建一个窗口或屏幕。
- `pygame.display.set_caption(title)`:设置窗口标题。
- `pygame.display.flip()`:更新整个屏幕。
- `pygame.display.update(rect)`:更新窗口的指定区域。
- **代码示例**:
```python
import pygame pygame.init() screen = pygame.display.set_mode((640, 480)) pygame.display.set_caption("Pygame Window") pygame.display.flip()
```
2. **pygame.draw**
用于绘制各种形状,如线条、矩形、圆形等。
- **函数**:
- `pygame.draw.line(surface, color, start_pos, end_pos, width)`:绘制直线。
- `pygame.draw.rect(surface, color, rect, width)`:绘制矩形。
- `pygame.draw.circle(surface, color, center, radius, width)`:绘制圆形。
- `pygame.draw.polygon(surface, color, pointlist, width)`:绘制多边形。
- **代码示例**:
```python
import pygame
pygame.init()
screen = pygame.display.set_mode((640, 480))
screen.fill((255, 255, 255)) # 填充背景色
pygame.draw.rect(screen, (255, 0, 0), (50, 50, 200, 100)) # 绘制红色矩形
pygame.display.flip()
```
3. **pygame.image**
用于加载和操作图像。
- **函数**:
- `pygame.image.load(filename)`:加载图像文件。
- `pygame.image.save(surface, filename)`:保存图像到文件。
- **代码示例**:
```python
import pygame
pygame.init()
screen = pygame.display.set_mode((640, 480))
image = pygame.image.load('example.png')
screen.blit(image, (0, 0)) # 绘制图像
pygame.display.flip()
```
4. **pygame.font**
用于创建和渲染文本。
- **函数**:
- `pygame.font.Font(None, size)`:创建字体对象。
- `pygame.font.Font.render(text, antialias, color)`:渲染文本。
- **代码示例**:
```python
import pygame
pygame.init()
screen = pygame.display.set_mode((640, 480))
font = pygame.font.Font(None, 74)
text = font.render('Hello Pygame', True, (0, 128, 0))
screen.blit(text, (200, 200))
pygame.display.flip()
```
5. **pygame.mixer**
用于播放声音和音乐。
- **函数**:
- `pygame.mixer.init()`:初始化混音器。
- `pygame.mixer.Sound(filename)`:加载声音文件。
- `pygame.mixer.music.load(filename)`:加载音乐文件。
- `pygame.mixer.music.play()`:播放音乐。
- **代码示例**:
```python
import pygame
pygame.init()
pygame.mixer.init()
sound = pygame.mixer.Sound('example.wav')
sound.play()
```
6. **pygame.event**
用于处理事件,如用户输入。
- **函数**:
- `pygame.event.get()`:获取所有事件。
- `pygame.event.poll()`:获取一个事件。
- `pygame.event.post(event)`:将事件放入事件队列。
- **代码示例**:
```python
import pygame
pygame.init()
screen = pygame.display.set_mode((640, 480))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
```
7. **pygame.time**
用于处理时间相关的操作。
- **函数**:
- `pygame.time.Clock()`:创建时钟对象,用于控制帧率。
- `pygame.time.get_ticks()`:获取自 pygame 初始化以来的毫秒数。
- **代码示例**:
```python
import pygame
pygame.init()
screen = pygame.display.set_mode((640, 480))
clock = pygame.time.Clock()
while True:
clock.tick(60) # 限制帧率为每秒 60 帧
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
```
8. **pygame.sprite**
用于管理和更新精灵(游戏对象)。
- **函数**:
- `pygame.sprite.Sprite`:创建一个精灵类。
- `pygame.sprite.Group`:管理多个精灵的组。
- `pygame.sprite.Group.add(sprite)`:将精灵添加到组。
- `pygame.sprite.Group.update()`:更新所有精灵。
- `pygame.sprite.Group.draw(surface)`:在屏幕上绘制所有精灵。
- **代码示例**:
```python
import pygame
pygame.init()
screen = pygame.display.set_mode((640, 480))
class MySprite(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.Surface((50, 50))
self.image.fill((255, 0, 0))
self.rect = self.image.get_rect()
self.rect.center = (320, 240)
sprite = MySprite()
all_sprites = pygame.sprite.Group(sprite)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
all_sprites.update()
screen.fill((0, 0, 0))
all_sprites.draw(screen)
pygame.display.flip()
```
这些模块和函数是 Pygame 库的基础,后续将更新更多内容。