python之pygame
目录
pygame.dispaly, pygame.event, pygame.draw:
pygame.dispaly:
pygame有且仅有一个屏幕;左上角坐标(0,0);以像素为单位。
#屏幕尺寸和模
pygame.display.set_mode(r = (0,0), flag = 0)
#r是游戏屏幕分辨率,以元组形式输入(weight, height)
#flag用来控制显示类型,可用 | 组合使用,常用标签有:
pygame.RESIZABLE #窗口大小可调
pygame.NOFRAME #窗口没有边界显示
pygame.FULLSCREEN #窗口全屏显示
(注意每种显示要配合相应的处理机制)vinfo = pygame.display.Info()
#产生一个显示信息对象VideoInfo,表达当前屏幕参数信息
vinfo.current_w #当前显示模式或窗口的像素宽度
vinfo.current_h #当前显示模式或窗口的像素高度pygame.VIDEORESIZE
#这是一种窗口大小更改的事件
#事件发生后,返回event.size元组,包含新窗口的宽度和高度
.size[0] #宽度,也可以用event.w
.size[1] #高度,也可以用event.h
#返回参数仅在事件发生时有用
#example
if event.type == pygame.VIDEORESIZE:size = width, height = event.size[0], event.size[1]screen = pygame.display.set_mode(size, pygame.RESIZABLE)
#窗口标题和图标
pygame.display.set_caption(title, icontitle = None)
#title设置窗口的标题内容
#icontitle设置图表化后的小标题,小标题可选,部分系统没有pygame.display.set_icon(surface)
#设置窗口的图标效果
#图标是一个Surface对象pygame.display.get_caption()
#返回当前设置窗口的标题和小标题内容,(title, icontitle)
#窗口感知和刷新
pygame.display.get_active()
#当窗口在系统中显示(屏幕绘制/非图标化)时返回True,否则返回False
#可以用来判断游戏窗口是否被最小化pygame.display.flip()
#重新绘制整个窗口pygame.display.update()
#仅重新绘制窗口中有变化的区域,相比.flip()执行更快
pygame.event:
#键盘事件
pygame.event.KEYDOWN #键盘按下事件
pygame.event.KEYUP #键盘释放事件
#属性
event.key #按键的常量名称
event.mod #按键修饰符的组合值event.mod = KMOD_ALT|KMOD_SHIFT #修饰符的按位或运算
#鼠标事件
#鼠标移动事件
pygame.event.MOUSEMOTION
#属性
event.pos #鼠标当前坐标值(x,y),相对于窗口左上角
event.rel #鼠标相对运动距离(x,y),相对于上次事件
event.rel #鼠标按钮状态(a,b,c),对应于鼠标的三个键(左 中 右)pygame.event.MOUSEBUTTONUP #鼠标释放事件
pygame.event.MOUSEBUTTONDOWN #鼠标按下事件
#属性
event.pos #鼠标当前坐标值(x,y)
event.button #鼠标按下键编号,左 中 右对应1 2 3
#example
#鼠标,键盘事件处理
import pygame, sys
pygame.init()
screen = pygame.display.set_mode((600, 400))
pygame.display.set_caption("Pygame事件处理")
while True:for event in pygame.event.get():if event.type == pygame.QUIT:sys.exit()elif event.type == pygame.KEYDOWN:if event.unicode == "":print("[KEYDOWN]:", "#", event.key, event.mod)else:print("[KEYDOWN]:", event.unicode, event.key, event.mod)elif event.type == pygame.MOUSEMOTION:print("[MOUSEMOTION]:", event.pos, event.rel, event.buttons)elif event.type == pygame.MOUSEBUTTONUP:print("[MOUSEBUTTONUP]:", event.pos, event.button)elif event.type == pygame.MOUSEBUTTONDOWN:print("[MOUSEBUTTONDOWN]:", event.pos, event.button)pygame.display.update()
pygame.event.get()
从事件列表中获得事件列表
for event in pygame.event.get():if event.type == pygame.QUIT:sys.exit()
可以增加参数,获得某类或某些类事件:
pygame.event.get(type)
pygame.event.get(typelist)
pygame.event.poll()
从事件队列中获得一个事件,事件将从事件队列中删除,如果事件队列为空,则返回event.NOEVENT
while True:event = pygame.event.poll()
pygame.event.clear()
从事件队列中删除事件,默认删除所有事件,可以增加参数,删除某类或某些类事件
pygame.event.clear(type)
pygame.event.clear(typelist)
pygame.draw:
官方文档
pygame库用来绘制形状的类
参数列表中的Surface是当前绘制屏幕的名称,color是RGB色彩模式下的颜色,例如 黑色(0, 0, 0),白色(255, 255, 255),width = 0是绘制形状的边的宽度,如果不传入的话width=0默认为填充
pygame.draw.rect(Surface, color, Rect, width=0)
绘制一个矩形
参数Rect是矩形参数,格式为[x, y, width, height]
examples for pygame.draw.rect
pygame.draw.circle(Surface, color, pos, radius, width=0)
以某点为圆心绘制一个圆形
参数pos是圆心的位置,radius是半径大小
examples for pygame.draw.circle
pygame.draw.ellipse(Surface, color, Rect, width=0)
绘制一个椭圆
通过给出矩形的参数,从而绘制一个内切于矩形的椭圆
examples fo pygame.draw.ellipse
pygame.draw.arc(Surface, color, Rect, start_angle, stop_angle, width=1)
绘制椭圆的一部分
两个角度分别是起始和结束角度,最右边为0度
examples for pygame.draw.arc
pygame.draw.line(Surface, color, start_pos, end_pos, width=1)
绘制一条直线
参数两个pos分别是起始位置和结束位置
examples for pygame.draw.line
pygame.draw.lines(Surface, color, closed, pointlist, width=1)
绘制多条连续直线
参数closed为True时首尾相连构成封闭,pointlist为顶点坐标
examples fo pygame.draw.lines
A big example for this module
# Import a library of functions called 'pygame'
import pygame
from math import pi# Initialize the game engine
pygame.init()# Define the colors we will use in RGB format
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)# Set the height and width of the screen
size = [400, 300]
screen = pygame.display.set_mode(size)pygame.display.set_caption("Example code for the draw module")# Loop until the user clicks the close button.
done = False
clock = pygame.time.Clock()while not done:# This limits the while loop to a max of 10 times per second.# Leave this out and we will use all CPU we can.clock.tick(10)for event in pygame.event.get(): # User did somethingif event.type == pygame.QUIT: # If user clicked closedone = True # Flag that we are done so we exit this loop# All drawing code happens after the for loop and but# inside the main while done==False loop.# Clear the screen and set the screen backgroundscreen.fill(WHITE)# Draw on the screen a GREEN line from (0,0) to (50.75) # 5 pixels wide.pygame.draw.line(screen, GREEN, [0, 0], [50, 30], 5)# Draw on the screen a GREEN line from (0,0) to (50.75) # 5 pixels wide.pygame.draw.lines(screen, BLACK, False, [[0, 80], [50, 90], [200, 80], [220, 30]], 5)# Draw on the screen a GREEN line from (0,0) to (50.75) # 5 pixels wide.pygame.draw.aaline(screen, GREEN, [0, 50], [50, 80], True)# Draw a rectangle outlinepygame.draw.rect(screen, BLACK, [75, 10, 50, 20], 2)# Draw a solid rectanglepygame.draw.rect(screen, BLACK, [150, 10, 50, 20])# Draw an ellipse outline, using a rectangle as the outside boundariespygame.draw.ellipse(screen, RED, [225, 10, 50, 20], 2)# Draw an solid ellipse, using a rectangle as the outside boundariespygame.draw.ellipse(screen, RED, [300, 10, 50, 20])# This draws a triangle using the polygon commandpygame.draw.polygon(screen, BLACK, [[100, 100], [0, 200], [200, 200]], 5)# Draw an arc as part of an ellipse. # Use radians to determine what angle to draw.pygame.draw.arc(screen, BLACK, [210, 75, 150, 125], 0, pi / 2, 2)pygame.draw.arc(screen, GREEN, [210, 75, 150, 125], pi / 2, pi, 2)pygame.draw.arc(screen, BLUE, [210, 75, 150, 125], pi, 3 * pi / 2, 2)pygame.draw.arc(screen, RED, [210, 75, 150, 125], 3 * pi / 2, 2 * pi, 2)# Draw a circlepygame.draw.circle(screen, BLUE, [60, 250], 40)# Go ahead and update the screen with what we've drawn.# This MUST happen after all the other drawing commands.pygame.display.flip()# Be IDLE friendly
pygame.quit()
python之pygame相关推荐
- pygame是python的一个库吗,python学习pygame,,基本库导入impor
python学习pygame,,基本库导入impor 基本库导入 import pygame import sys from pygame.locals import * 初始化 pygame.ini ...
- 《Python和Pygame游戏开发指南》——2.16 pygame.display.update()函数
本节书摘来自异步社区<Python和Pygame游戏开发指南>一书中的第2章,第2.16节,作者[美]Al Sweigart(斯维加特), 李强 译,更多章节内容可以访问云栖社区" ...
- 《Python和Pygame游戏开发指南》——1.12 图书中的文本折行
本节书摘来自异步社区<Python和Pygame游戏开发指南>一书中的第1章,第1.12节,作者[美]Al Sweigart(斯维加特), 李强 译,更多章节内容可以访问云栖社区" ...
- 在电脑上安装python的步骤-python中pygame安装过程(超级详细)
安装时是需要设置python环境变量的,下载python的时候底下有个小框框(没有默认选中) Add Python 3.7 to PATH需要选择的,如果没有选择的话,需要自己设置,我一般比较懒,卸载 ...
- python教程是用什么博客写的-用Python和Pygame写游戏-从入门到精通(目录)
目光博客一开始,就有一个将pygame好好介绍一遍的宏伟计划,历时四个月,在各位朋友的关怀鞭策下,如今(2011/8/26)理论学习的部分似乎已经都完成了,在次列一个目录,方便查询.介绍还不是很全,下 ...
- Py之pygame:Python的pygame库的简介、安装、使用方法详细攻略
Py之pygame:Python的pygame库的简介.安装.使用方法详细攻略 目录 pygame库的简介 pygame库的安装 pygame库的使用方法 pygame库的简介 PyPoice是SDL ...
- pip install pygame_使用 Python 和 Pygame 模块构建一个游戏框架!
这系列的第一篇通过创建一个简单的骰子游戏来探究 Python.现在是来从零制作你自己的游戏的时间. 在我的这系列的第一篇文章 中, 我已经讲解如何使用 Python 创建一个简单的.基于文本的骰子游戏 ...
- python之pygame安装教程_Python中pygame安装方法图文详解
搜索热词 本文实例讲述了Python中pygame安装方法.分享给大家供大家参考,具体如下: 这里主要描述一下我们怎样来安装pygame 可能很多人像我一样,发现了pygame是个好东东,但是就是不知 ...
- pygame为游戏添加背景_万能的Python和Pygame模块构建一个游戏框架
通过创建一个简单的骰子游戏来探究 Python.现在是来从零制作你自己的游戏的时间. 在我的这系列的第一篇文章中, 我已经讲解如何使用 Python 创建一个简单的.基于文本的骰子游戏.这次,我将展示 ...
- python pygame模块怎么写游戏_使用 Python 和 Pygame 模块构建一个游戏框架
这系列的第一篇通过创建一个简单的骰子游戏来探究 Python.现在是来从零制作你自己的游戏的时间. 在我的这系列的第一篇文章 中, 我已经讲解如何使用 Python 创建一个简单的.基于文本的骰子游戏 ...
最新文章
- f-free 查看系统中空闲和使用的内存
- matlab设计长度为50的滤波器,实验5 基于Matlab的数字滤波器设计
- css之px自动转rem—sublime 插件CSSREM
- HEVC视频编码技术
- 第一个linux桌面,Ubuntu 4.10 “Warty Warthog”:回顾第一个Ubuntu Linux桌面
- 中国移动2016年NAT独立设备集采:迪普科技连续三年入围
- 极限编程 Extreme Programming (中英文对照)图形解释
- 递推 hdu 1330
- Eclipse使用教程
- 企业微信电脑版显示连不上服务器,钉钉环境部分Anroid手机,微信、企业微信pc版,登录失败的解决办法...
- 使用uCommand执行复杂的任务
- Tms320F28335中软件触发信号采样(ADC)
- 数据库:sql 递归
- 乘法表输出及其扩展(附带部分代码书写习惯) C++实现
- ST-Link 转JLink
- 图形学(5)多边形的扫描转换(下)
- Android悬浮窗适配全机型,包含8.0,小米魅族华为悬浮窗权限适配demo看这一篇就够了
- 深度学习基础--分类网络
- 视频编解码中的temporal id和layer id(x265,HM,VTM)
- 基于Matlab模拟3维偶极子天线