Python做游戏,首选这3个库,新手到进阶都能覆盖👇
新手入门(2D简单小游戏)
Pygame:最经典2D游戏库,上手快、文档全,适合做贪吃蛇、飞机大战、俄罗斯方块。
核心流程:初始化→设置窗口→加载素材→游戏循环(事件监听+逻辑更新+画面绘制)→退出。
极简示例(空白窗口):import pygame
pygame.init()
screen = pygame.display.set_mode((640,480))
pygame.display.set_caption("我的第一个游戏")
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.flip()
pygame.quit()Arcade:比Pygame更现代,自带碰撞检测、精灵动画,不用自己写复杂逻辑,适合做像素风闯关、射击游戏,对新手友好度更高。
3D游戏首选
Panda3D:专业3D游戏引擎,支持光影、物理引擎、多人联机,能做大型3D游戏;新手也能快速搭出3D场景,门槛比Unity低。
懒人快速做游戏
Ren'Py:主打文字冒险/视觉小说,几乎不用写复杂逻辑,拖放式+简单脚本就能做剧情向游戏,适合做恋爱养成、解谜文字游戏。
直接复制就能玩,方向键控制,碰到边界或自己就结束👇import pygame
import random
# 初始化
pygame.init()
WIDTH, HEIGHT = 600, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("贪吃蛇")
clock = pygame.time.Clock()
# 颜色定义
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
# 蛇参数
snake_block = 20
snake_speed = 10
snake_list = []
snake_length = 1
# 初始位置
x1 = WIDTH / 2
y1 = HEIGHT / 2
x1_change = 0
y1_change = 0
# 食物生成
food_x = round(random.randrange(0, WIDTH - snake_block) / snake_block) * snake_block
food_y = round(random.randrange(0, HEIGHT - snake_block) / snake_block) * snake_block
def draw_snake(snake_block, snake_list):
for x in snake_list:
pygame.draw.rect(screen, GREEN, [x[0], x[1], snake_block, snake_block])
# 游戏循环
game_over = False
game_close = False
while not game_over:
while game_close:
screen.fill(BLACK)
font = pygame.font.SysFont(None, 50)
text = font.render("游戏结束!按 Q退出 按 C重玩", True, RED)
screen.blit(text, (WIDTH/6, HEIGHT/3))
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
game_over = True
game_close = False
if event.key == pygame.K_c:
# 重置游戏
x1 = WIDTH/2
y1 = HEIGHT/2
x1_change = 0
y1_change = 0
snake_length = 1
snake_list = []
food_x = round(random.randrange(0, WIDTH - snake_block)/snake_block)*snake_block
food_y = round(random.randrange(0, HEIGHT - snake_block)/snake_block)*snake_block
game_close = False
# 事件监听
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT and x1_change == 0:
x1_change = -snake_block
y1_change = 0
elif event.key == pygame.K_RIGHT and x1_change == 0:
x1_change = snake_block
y1_change = 0
elif event.key == pygame.K_UP and y1_change == 0:
y1_change = -snake_block
x1_change = 0
elif event.key == pygame.K_DOWN and y1_change == 0:
y1_change = snake_block
x1_change = 0
# 边界检测
if x1 >= WIDTH or x1 < 0 or y1 >= HEIGHT or y1 < 0:
game_close = True
x1 += x1_change
y1 += y1_change
screen.fill(BLACK)
# 画食物
pygame.draw.rect(screen, RED, [food_x, food_y, snake_block, snake_block])
# 更新蛇身
snake_head = []
snake_head.append(x1)
snake_head.append(y1)
snake_list.append(snake_head)
if len(snake_list) > snake_length:
del snake_list[0]
# 碰自己检测
for x in snake_list[:-1]:
if x == snake_head:
game_close = True
draw_snake(snake_block, snake_list)
pygame.display.update()
# 吃食物加分
if x1 == food_x and y1 == food_y:
food_x = round(random.randrange(0, WIDTH - snake_block)/snake_block)*snake_block
food_y = round(random.randrange(0, HEIGHT - snake_block)/snake_block)*snake_block
snake_length += 1
clock.tick(snake_speed)
pygame.quit()
quit()