首先,我们需要安装Pygame。打开终端输入以下命令:
pip install pygame
安装完成后,让我们来创建第一个Pygame窗口:
import pygame# 初始化Pygamepygame.init()# 创建游戏窗口screen = pygame.display.set_mode((800, 600)) # 设置窗口大小为800x600pygame.display.set_caption("我的第一个Pygame窗口") # 设置窗口标题# 游戏主循环running = Truewhile running: # 处理事件 for event in pygame.event.get(): if event.type == pygame.QUIT: # 如果点击关闭按钮 running = False # 设置背景色为白色 screen.fill((255, 255, 255)) # 更新显示 pygame.display.flip()# 退出游戏pygame.quit()
小贴士: 记得每次使用Pygame时都要调用pygame.init()进行初始化,结束时用pygame.quit()清理资源哦!
绘制图形和文字来点有趣的!让我们在窗口中画些基本图形:
import pygamepygame.init()screen = pygame.display.set_mode((800, 600))pygame.display.set_caption("绘制图形")# 设置颜色RED = (255, 0, 0)BLUE = (0, 0, 255)GREEN = (0, 255, 0)# 设置字体font = pygame.font.Font(None, 36)running = Truewhile running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False screen.fill((255, 255, 255)) # 画一个红色矩形 pygame.draw.rect(screen, RED, (100, 100, 200, 100)) # 画一个蓝色圆形 pygame.draw.circle(screen, BLUE, (400, 300), 50) # 画一条绿色线 pygame.draw.line(screen, GREEN, (500, 400), (700, 400), 5) # 显示文字 text = font.render("Hello, Pygame!", True, (0, 0, 0)) screen.blit(text, (300, 50)) pygame.display.flip()pygame.quit()
重点提示: RGB颜色值的范围是0-255,比如(255, 0, 0)代表纯红色。
添加动画效果让我们创建一个会移动的小球:
import pygamepygame.init()screen = pygame.display.set_mode((800, 600))pygame.display.set_caption("移动的小球")# 小球的初始位置和速度ball_x = 400ball_y = 300ball_speed_x = 5ball_speed_y = 5ball_radius = 20clock = pygame.time.Clock() # 用于控制游戏帧率running = Truewhile running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # 更新小球位置 ball_x += ball_speed_x ball_y += ball_speed_y # 碰到边界就反弹 if ball_x <= ball_radius or ball_x >= 800 - ball_radius: ball_speed_x = -ball_speed_x if ball_y <= ball_radius or ball_y >= 600 - ball_radius: ball_speed_y = -ball_speed_y # 绘制画面 screen.fill((255, 255, 255)) pygame.draw.circle(screen, (255, 0, 0), (int(ball_x), int(ball_y)), ball_radius) pygame.display.flip() clock.tick(60) # 限制帧率为60FPSpygame.quit()
小贴士: pygame.time.Clock()可以帮助我们控制游戏的运行速度,避免游戏在不同性能的电脑上速度不一致。
处理键盘输入最后,让我们来看看如何响应键盘操作:
import pygamepygame.init()screen = pygame.display.set_mode((800, 600))pygame.display.set_caption("键盘控制")# 玩家方块的位置player_x = 400player_y = 300player_speed = 5running = Truewhile running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # 获取按键状态 keys = pygame.key.get_pressed() # 根据按键移动方块 if keys[pygame.K_LEFT]: player_x -= player_speed if keys[pygame.K_RIGHT]: player_x += player_speed if keys[pygame.K_UP]: player_y -= player_speed if keys[pygame.K_DOWN]: player_y += player_speed # 限制方块在窗口内 player_x = max(0, min(player_x, 750)) player_y = max(0, min(player_y, 550)) # 绘制画面 screen.fill((255, 255, 255)) pygame.draw.rect(screen, (0, 255, 0), (player_x, player_y, 50, 50)) pygame.display.flip()pygame.quit()
动手练习: 试着修改代码,让方块变成其他形状,或者改变移动速度和颜色!
今天的Python学习之旅就到这里啦!记得动手敲代码,创建属于你自己的游戏。有了这些基础,你已经可以开始制作简单的游戏啦!比如可以尝试制作一个简单的弹球游戏,或者是飞机大战?
祝大家学习愉快,Python学习节节高!记住乐乐的座右铭:编程学习最重要的就是动手实践!下次再见啦!🎮✨