0Pricing
Python For Kids · 课时

PyGame 简介

学习游戏开发基础。

PyGame 简介 是 CoddyKit 上的免费 Python For Kids 课时。 这是第 3 节课,共 3 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Python For Kids 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Python For Kids 课程共包含 3 节课。

PyGame 简介

欢迎学习 PyGame!今天,我们将学习使用 PyGame 进行游戏开发的基础知识。PyGame 是一个 Python 库,可以帮助您创建包含图形、声音和用户交互的游戏。

PyGame 简介 — 插图 1

什么是 PyGame?

PyGame 是一个用于创建二维游戏的 Python 库。它提供了以下工具:

  • 显示图形
  • 处理用户输入
  • 添加音效和音乐
  • 创建动画

无论您想制作简单的游戏还是复杂的游戏,PyGame 都能让游戏开发变得更加轻松有趣!

安装 PyGame

要使用 PyGame,首先需要安装它。请在终端或命令提示符中使用以下命令:

pip install pygame

安装完成后,您就可以创建自己的第一个游戏了!

创建基本游戏窗口

让我们先使用 PyGame 创建一个基本的游戏窗口。这个窗口将成为游戏的基础。

示例:

import pygame

# Initializes PyGame
pygame.init()

# Sets up the game window
screen = pygame.display.set_mode((800, 600))  # Width: 800, Height: 600
pygame.display.set_caption("My First Game")

# Runs the game loop
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:  # Checks if the user closes the window
            running = False

# Quits PyGame
pygame.quit()

添加颜色

您可以使用 RGB 值为游戏窗口填充颜色。每种颜色都由红、绿、蓝三个分量定义。

示例:

import pygame

pygame.init()

# Sets up the game window
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Colorful Window")

# Colors in RGB
BLUE = (0, 0, 255)

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # Fills the screen with blue color
    screen.fill(BLUE)
    pygame.display.update()

pygame.quit()

绘制形状

PyGame 允许您绘制矩形、圆形和直线等形状。

  • pygame.draw.rect():绘制矩形
  • pygame.draw.circle():绘制圆形
  • pygame.draw.line():绘制直线

示例:

import pygame

pygame.init()

# Sets up the game window
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Drawing Shapes")

WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # Fills the screen with white color
    screen.fill(WHITE)

    # Draws a red rectangle
    pygame.draw.rect(screen, RED, (100, 100, 200, 150))

    # Draws a green circle
    pygame.draw.circle(screen, GREEN, (400, 300), 75)

    pygame.display.update()

pygame.quit()

处理用户输入

游戏通常需要用户输入,例如按键或点击鼠标。PyGame 允许您使用事件来处理输入。

示例:

import pygame

pygame.init()

screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("User Input")

WHITE = (255, 255, 255)

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:  # Checks if a key is pressed
            if event.key == pygame.K_SPACE:
                print("Spacebar was pressed!")
        elif event.type == pygame.MOUSEBUTTONDOWN:  # Checks if the mouse is clicked
            print("Mouse clicked at", event.pos)

    screen.fill(WHITE)
    pygame.display.update()

pygame.quit()

移动对象

要创建动画或交互式游戏,您可以在每一帧中更新对象的位置,从而移动对象。

示例:

import pygame

pygame.init()

screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Moving Object")

WHITE = (255, 255, 255)
BLUE = (0, 0, 255)

x, y = 50, 50  # Starting position of the rectangle
velocity = 5   # Speed of movement

running = True
while running:
    pygame.time.delay(30)  # Delays the loop for 30ms
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # Gets keys pressed
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        x -= velocity
    if keys[pygame.K_RIGHT]:
        x += velocity
    if keys[pygame.K_UP]:
        y -= velocity
    if keys[pygame.K_DOWN]:
        y += velocity

    screen.fill(WHITE)
    pygame.draw.rect(screen, BLUE, (x, y, 50, 50))  # Draws the moving rectangle
    pygame.display.update()

pygame.quit()
import pygame

pygame.init()

screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Question")

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    screen.fill((0, 255, 0))  # Fills the screen with green
    pygame.display.update()

pygame.quit()

做得很好!

您已经学会了使用 PyGame 进行游戏开发的基础知识,包括创建游戏窗口、绘制形状、处理用户输入和移动对象。PyGame 是一个强大的库,可以帮助您将游戏创意变为现实。请继续探索它的功能,创建属于自己的有趣且互动的游戏!

PyGame 简介 — 插图 10

常见问题解答

「PyGame 简介」课时是免费的吗?

是的 — 「PyGame 简介」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Python For Kids 课程的其余内容,请升级到 CoddyKit PRO。 Python For Kids 课程共包含 3 节课。

「PyGame 简介」这节课中我会学到什么?

学习游戏开发基础。 你通过在浏览器中直接运行的动手代码来练习 Python For Kids,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Python For Kids 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Python For Kids 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 3 节。

「PyGame 简介」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Python For Kids 课中编写并运行代码吗?

能。每节 Python For Kids 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. Python 库简介
  2. 使用数学库
  3. PyGame 简介
← 返回 Python For Kids