【Pygame】 游戏开发 第二课 图形绘制 & 键鼠事件

  • 图形绘制
    • 圆形绘制
    • 绘制矩形
    • 绘制直线
    • 绘制圆弧
    • 案例
  • 键鼠事件
    • 键盘事件
    • 鼠标事件

图形绘制

圆形绘制

格式:

pygame.draw.circle(surface, color, center, radius, width=0, draw_top_right=None, draw_top_left=None, draw_bottom_left=None, draw_bottom_right=None)

参数:

  • surface: 需要绘制的表面
  • color: RGB 格式, 圆形的颜色
  • center: 元组格式 (x, y), 圆心坐标
  • radius: 圆形半径
  • width: 圆形厚度
  • draw_top_right: 布尔类型, 只画右上角 1/4, 默认为 None
  • draw_top_left: 布尔类型, 只画左上角 1/4, 默认为 None
  • draw_bottom_right: 布尔类型, 只画右下角 1/4, 默认为 None
  • draw_bottom_left: 布尔类型, 只画左下角 1/4, 默认为 None

例子:

import pygame
from pygame.locals import *
import sys# 初始化 pygame
pygame.init()# 设置窗口大小
screen = pygame.display.set_mode((600, 400))# 设置背景颜色
screen.fill((255, 205, 232))# 设置标题
pygame.display.set_caption("绘制圆")# 绘制圆
pygame.draw.circle(screen, (184, 241, 237), (200, 200), 100, 12, draw_top_left=True)
pygame.draw.circle(screen, (217, 184, 241), (400, 200), 100, 12)# 更新显示
pygame.display.update()# 捕获游戏事件
typelist = [QUIT]while True:# 获取事件for event in pygame.event.get():# 接收到退出事件, 退出程序if event.type in typelist:sys.exit()  # 退出

输出结果:

绘制矩形

格式:

pygame.draw.rect(surface, color, rect, width=0, border_radius=0, border_top_left_radius=-1, border_top_right_radius=-1, border_bottom_left_radius=-1, border_bottom_right_radius=-1)

参数:

  • surface: 需要绘制的表面
  • color: RGB 格式, 颜色
  • rect: 元组格式 (x, y, w, h), 矩形的坐标和尺寸
  • width: 矩形粗细
  • border_radius: 使矩形有圆角, 圆角的半径
  • border_top_left_radius: 矩形左上角的圆角半径
  • border_top_right_radius: 矩形右上角的圆角半径
  • border_bottom_left_radius: 矩形左下角的圆角半径
  • border_bottom_right_radius: 矩形右下角的圆角半径

例子:

import pygame
from pygame.locals import *
import sys# 初始化 pygame
pygame.init()# 设置窗口大小
screen = pygame.display.set_mode((600, 400))# 设置背景颜色
screen.fill((255, 205, 232))# 设置标题
pygame.display.set_caption("绘制矩形")# 绘制矩形
pygame.draw.rect(screen, (184, 241, 204), (50, 200, 150, 150), 12, border_radius=25)  # 圆角矩形
pygame.draw.rect(screen, (184, 241, 237), (250, 200, 150, 150), 12, border_top_right_radius=25)  # 右上角圆角
pygame.draw.rect(screen, (217, 184, 241), (450, 200, 120, 120), 12)# 更新显示
pygame.display.update()# 捕获游戏事件
typelist = [QUIT]while True:# 获取事件for event in pygame.event.get():# 接收到退出事件, 退出程序if event.type in typelist:sys.exit()  # 退出

输出结果:

绘制直线

格式:

pygame.draw.line(surface, color, start_pos, end_pos, width=1)

参数:

  • surface: 需要绘制的表面
  • color: RGB 格式, 颜色
  • start_pos: 元组格式 (x, y), 起始坐标
  • end_pos: 元组格式 (x, y), 结束坐标
  • width: 线粗细

例子:

import pygame
from pygame.locals import *
import sys# 初始化 pygame
pygame.init()# 设置窗口大小
screen = pygame.display.set_mode((600, 400))# 设置背景颜色
screen.fill((255, 205, 232))# 设置标题
pygame.display.set_caption("绘制线")# 绘制线
pygame.draw.line(screen, (184, 241, 237), (200, 200), (400, 200), 10)  # 直线
pygame.draw.line(screen, (217, 184, 241), (200, 200), (400, 300), 10)  # 斜线# 更新显示
pygame.display.update()# 捕获游戏事件
typelist = [QUIT]while True:# 获取事件for event in pygame.event.get():# 接收到退出事件, 退出程序if event.type in typelist:sys.exit()  # 退出

输出结果:

绘制圆弧

格式:

pygame.draw.arc(surface, color, rect, start_angle, stop_angle, width=1)

参数:

  • surface: 需要绘制的表面
  • color: RGB 格式, 颜色
  • rect: 元组格式 (x, y, w, h), 矩形的坐标和尺寸
  • start_angle: 起始弧度
  • stop_angle: 结束弧度
  • width: 圆弧粗细

例子:

import pygame
from pygame.locals import *
import sys
import math# 初始化 pygame
pygame.init()# 设置窗口大小
screen = pygame.display.set_mode((600, 400))# 设置背景颜色
screen.fill((255, 205, 232))# 设置标题
pygame.display.set_caption("绘制圆弧")# 绘制圆弧
pygame.draw.arc(screen, (184, 241, 237), (100, 100, 200, 200), math.radians(0), math.radians(180), 10)
pygame.draw.arc(screen, (217, 184, 241), (400, 100, 200, 250), math.radians(90), math.radians(270), 10)# 更新显示
pygame.display.update()# 捕获游戏事件
typelist = [QUIT]while True:# 获取事件for event in pygame.event.get():# 接收到退出事件, 退出程序if event.type in typelist:sys.exit()  # 退出

输出结果:

通过math.radians()方法, 将角度值转换为弧度.

案例

pygame 实现矩形移动:

import pygame
from pygame.locals import *
import sys
import time# 初始化 pygame
pygame.init()# 设置窗口大小
screen = pygame.display.set_mode((500, 500))# 设置背景颜色
screen.fill((255, 205, 232))# 设置标题
pygame.display.set_caption("移动的矩形")# 捕获游戏事件
typelist = [QUIT]# 矩形的初始横坐标
pos_x = 300
pos_y = 250# 矩形移动距离
vel_x = 2
vel_y = 1while True:# 获取事件for event in pygame.event.get():# 接收到退出事件, 退出程序if event.type in typelist:sys.exit()  # 退出# 刷新背景screen.fill((255, 205, 232))# 刷新矩形pos_x += vel_xpos_y += vel_y# 边缘反弹if pos_x >= 400 or pos_x <= 0:vel_x = -vel_xif pos_y >= 400 or pos_y <= 0:vel_y = -vel_y# 绘制矩形pygame.draw.rect(screen, (217, 184, 241), (pos_x, pos_y, 100, 100), 0)time.sleep(0.01)# 更新显示pygame.display.update()

输出结果:

键鼠事件

键盘事件

pygame.key.get_pressed()会返回一个按键列表, 用 True / False 表示键盘各个键是否按下.

格式:

keys = pygame.key.get_pressed()

例子:

import pygame
from pygame.locals import *
import sys# 初始化 pygame
pygame.init()# 设置窗口大小
screen = pygame.display.set_mode((600, 400))# 设置字体和字号 (仿宋)
myFont = pygame.font.Font("C:\Windows\Fonts\simfang.ttf", 50)# 设置背景颜色
screen.fill((255, 205, 232))# 设置标题
pygame.display.set_caption("键盘事件")# 捕获游戏事件
typelist = [QUIT]while True:# 获取事件for event in pygame.event.get():# 接收到退出事件, 退出程序if event.type  == QUIT:sys.exit()  # 退出# 获取按键keys = pygame.key.get_pressed()# 刷新背景screen.fill((255, 205, 232))# 键盘事件if keys[K_LEFT]:# 显示文字text_img1 = myFont.render("按下左箭头", True, (184, 241, 237))screen.blit(text_img1, (10, 50))if keys[K_RIGHT]:text_img2 = myFont.render("按下右箭头", True, (184, 241, 237))screen.blit(text_img2, (10, 120))# 更新显示pygame.display.update()

输出结果:

鼠标事件

event.pos获取事件坐标, event.rel获取事件相对位置.

例子:

import pygame
from pygame.locals import *
import sys# 初始化 pygame
pygame.init()# 设置窗口大小
screen = pygame.display.set_mode((600, 400))# 设置字体和字号 (仿宋)
myFont = pygame.font.Font("C:\Windows\Fonts\simfang.ttf", 50)# 设置背景颜色
screen.fill((255, 205, 232))# 设置标题
pygame.display.set_caption("鼠标事件")# 捕获游戏事件
typelist = [QUIT]# 初始化变量
mouse_x = mouse_y = 0  # 鼠标位置
move_x = move_y = 0
mouse_down = 0  # 按键按下
mouse_down_x = mouse_down_y = 0  # 按键按下位置
mouse_up = 0  # 按键抬起
mouse_up_x = mouse_up_y = 0  # 按键按下位置while True:# 获取事件for event in pygame.event.get():# 接收到退出事件, 退出程序if event.type  == QUIT:sys.exit()  # 退出# 获取鼠标位置elif event.type == MOUSEMOTION:mouse_x, mouse_y = event.posmove_x, move_y = event.rel# 获取鼠标按键elif event.type == MOUSEBUTTONDOWN:mouse_down = event.buttonmouse_down_x, mouse_down_y = event.poselif event.type == MOUSEBUTTONUP:mouse_up = event.buttonmouse_up_x, mouse_up_y = event.pos# 获取按键keys = pygame.key.get_pressed()# 刷新背景screen.fill((255, 205, 232))# 鼠标事件text_img1 = myFont.render("鼠标位置: " + str(mouse_x) + ", " + str(mouse_y), True, (184, 241, 237))text_img2 = myFont.render("鼠标相对位置: " + str(move_x) + ", " + str(move_y), True, (184, 241, 237))text_img3 = myFont.render("鼠标按钮按下: " + str(mouse_down) + ", " + str(mouse_down_x) + ", " + str(mouse_down_y), True, (184, 241, 237))text_img4 = myFont.render("鼠标按钮抬起: " + str(mouse_up) + ", " + str(mouse_up_x) + ", " + str(mouse_up_y), True, (184, 241, 237))screen.blits([(text_img1, (10, 50)), (text_img2, (10, 100)), (text_img3, (10, 150)), (text_img4, (10, 200))])# 更新显示pygame.display.update()

输出结果:

Pygame 游戏开发 图形绘制 键鼠事件相关推荐

  1. python和pygame游戏开发指南_学习记录

    <python和pygame游戏开发指南> Making Games With Python and Palme [美]Ai Sweigart 著,李强 译,2015.12第一版 文章目录 ...

  2. Pygame 游戏开发 实战 1

    Pygame 游戏开发 第三课 实战 1 动态彩色圆环 选择数字 指针时钟 动态彩色圆环 通过圆形和线的绘制, 和文字的添加, 来实现一个简单的数字选择小游戏. 代码: import pygame f ...

  3. 《Python和Pygame游戏开发指南》——2.16 pygame.display.update()函数

    本节书摘来自异步社区<Python和Pygame游戏开发指南>一书中的第2章,第2.16节,作者[美]Al Sweigart(斯维加特), 李强 译,更多章节内容可以访问云栖社区" ...

  4. 01-初识 pygame 游戏开发

    你好,我是悦创.接下来三十天,我将持续更新 Python pygame 的基础游戏开发教程.文章都会对应视频教程,视频教程将在公众号:AI悦创,发布. 目录 本次,文章目标: Python 的第三方库 ...

  5. 《Python和Pygame游戏开发指南》——1.12 图书中的文本折行

    本节书摘来自异步社区<Python和Pygame游戏开发指南>一书中的第1章,第1.12节,作者[美]Al Sweigart(斯维加特), 李强 译,更多章节内容可以访问云栖社区" ...

  6. Pygame按键编码及基本键鼠侦测

    Pygame按键编码及基本键鼠侦测 目录 Pygame按键编码 键鼠侦测 Pygame按键编码 按键 Pygame按键编码 回退 K_BACKSPACE 制表 K_TAB 清除 K_CLEAN 回车 ...

  7. Android游戏开发中绘制游戏触摸轨迹的曲线图

    本篇文章主要来讲解怎样绘制游戏触摸轨迹的曲线图. 我们在onTouchEvent方法中,可以获取到触摸屏幕时手指触摸点的x.y坐标,如何用这些点形成一条无规则轨迹并把这条无规则轨迹曲线显示在屏幕上就是 ...

  8. PC平台下ARPG游戏的操作感之键鼠主角控制

    关键词 ARPG:即时战斗的RPG游戏 键鼠:普通PC机的外设,键盘与鼠标 主角控制:控制主角执行动作或发出其他指令 一.'操作感' 时下'打击感'一词已经被宣传ARPG的大陆水军们吹的一片火热,然而 ...

  9. pygame 游戏开发

    简单的战旗游戏开发学习 在网上找寻教程之后搞出了这么个雏形 游戏介绍 游戏实现了战斗场景的回合制玩法: 对战双方每个生物每一轮有一次行动机会,可以行走或攻击对方. 每个生物属性有:行走范围,速度,生命 ...

最新文章

  1. SQL Server数据库大型应用解决方案总结(转载)
  2. ActiveMQ入门案例-生产者代码实现
  3. ecmobile实现支付宝支付和百度云推送遇到的问题及解决方案(android)
  4. mysql截取字符串最后两位_Mysql字符串截取函数SUBSTRING的用法说明
  5. BlockingQueue的使用
  6. 深度学习笔记(26) 卷积神经网络
  7. Swift 与众不同的地方
  8. java 无法import_ImportError:无法导入名称X
  9. Android模拟器的模拟键盘区不见了怎么办
  10. 如何实现一张图片覆盖窗体 - 回复 客栈老人 的问题
  11. 未知错误 ID:-2147467259 操作必须使用一个可更新的查询
  12. R语言与临床模型预测——LASSO回归,单因素多因素cox,差异表达分析,Venn图,森林图,列线图,矫正曲线,ROC全套代码及解析——第五部分 批量cox回归分析 本专栏可免费答疑
  13. 计算机computer英语划分音节,computer是什么意思
  14. 改善网页性能的5种方法
  15. 国内最长的地铁投影画廊在上海地铁诞生
  16. poco库开发mysql_Poco数据库操作
  17. 2019腾讯区块链白皮书(附完整版下载)
  18. EngineerCMS利用梦想CAD控件MXdraw进行图纸在线编辑,保存到服务器
  19. 利用Matplotlib绘制莫比乌斯带
  20. Oracle--加锁的方法

热门文章

  1. 3、python学习笔记第三课:程序格式和构成
  2. leetcode 547. 朋友圈 C语言
  3. 32位/64位操作系统的最大支持内存的空间
  4. 去哪儿2017校园招聘笔试题——获得文件扩展名filename extension
  5. vue 安装不上,报错,解决办法如下
  6. 解决CentOS下boost安装后不能使用的问题
  7. 致远项目管理SPM系统进度控制之进度对比分析
  8. Maven配置中央仓库
  9. 2023届【校招】安全面试题和岗位总结(字节、百度、腾讯、美团等大厂)
  10. Python Unit Test - 3 pydoc