pygame学习笔记(2):画点的三种方法和动画实例
作者:junjie 发布时间:2021-09-02 19:59:51
1、单个像素(画点)
利用pygame画点主要有三种方法:
方法一:画长宽为1个像素的正方形
import pygame,sys
pygame.init()
screen=pygame.display.set_caption('hello world!')
screen=pygame.display.set_mode([640,480])
screen.fill([255,255,255])
pygame.draw.rect(screen,[0,0,0],[150,50,1,1],1) #画1*1的矩形,线宽为1,这里不能是0,因为1*1无空白区域。
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()
方法二:画个直径为1的圆
import pygame,sys
pygame.init()
screen=pygame.display.set_caption('hello world!')
screen=pygame.display.set_mode([640,480])
screen.fill([255,255,255])
pygame.draw.circle(screen,[0,0,0],[150,200],1,1)
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()
方法三:这种方法并不是画上去的,而是改变了surface上某个点的颜色,这样看上去像是画了一个点screen.set_at()。另外,如果要得到某个像素的颜色,可以使用screen.get_at()。
import pygame,sys
pygame.init()
screen=pygame.display.set_caption('hello world!')
screen=pygame.display.set_mode([640,480])
screen.fill([255,255,255])
screen.set_at([150,150],[255,0,0])#将150,150改为红色。
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()
2、连接多个点形成线
pygame.draw.lines()方法可以将多个点连接成为线。该方法有5个参数:surface表面、颜色、闭合线或者非闭合线(如果闭合为True,否则为False),点的列表,线宽。pygame.draw.lines(surface,[color],False/True,plotpoints,1)。下面的例子画出了一条马路,具体如下:
import pygame,sys
def lineleft(): #画马路左边界
plotpoints=[]
for x in range(0,640):
y=-5*x+1000
plotpoints.append([x,y])
pygame.draw.lines(screen,[0,0,0],False,plotpoints,5)
pygame.display.flip()
def lineright():#画马路右边界
plotpoints=[]
for x in range(0,640):
y=5*x-2000
plotpoints.append([x,y])
pygame.draw.lines(screen,[0,0,0],False,plotpoints,5)
pygame.display.flip()
def linemiddle():#画马路中间虚线
plotpoints=[]
x=300
for y in range(0,480,20):
plotpoints.append([x,y])
if len(plotpoints)==2:
pygame.draw.lines(screen,[0,0,0],False,plotpoints,5)
plotpoints=[]
pygame.display.flip()
pygame.init()
screen=pygame.display.set_caption('hello world!')
screen=pygame.display.set_mode([640,480])
screen.fill([255,255,255])
lineleft()
lineright()
linemiddle()
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()
3、引用图像
在pygame中引用图像最简单的以夷伐夷是image函数。下面在马路的实例中,加入一辆汽车。首先pygame.image.load()函数从硬盘加载一个图像,并创建一个名为my_car的对象。这里,my_car是一个surface,不过是存在内存中,并未显示出来,然后用blit(块移)方法将my_car复制到screen表面上,从而显示出来。具体代码如下:
import pygame,sys
def lineleft():
plotpoints=[]
for x in range(0,640):
y=-5*x+1000
plotpoints.append([x,y])
pygame.draw.lines(screen,[0,0,0],False,plotpoints,5)
pygame.display.flip()
def lineright():
plotpoints=[]
for x in range(0,640):
y=5*x-2000
plotpoints.append([x,y])
pygame.draw.lines(screen,[0,0,0],False,plotpoints,5)
pygame.display.flip()
def linemiddle():
plotpoints=[]
x=300
for y in range(0,480,20):
plotpoints.append([x,y])
if len(plotpoints)==2:
pygame.draw.lines(screen,[0,0,0],False,plotpoints,5)
plotpoints=[]
pygame.display.flip()
def loadcar(): #载入car图像
my_car=pygame.image.load('ok1.jpg') #当前文件夹下的ok1.jpg文件
screen.blit(my_car,[320,320])
pygame.display.flip()
pygame.init()
screen=pygame.display.set_caption('hello world!')
screen=pygame.display.set_mode([640,480])
screen.fill([255,255,255])
lineleft()
lineright()
linemiddle()
loadcar()
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()
素材:ok1.jpg
4、动画
计算机动画实际上就是把图像从一个地方移动到另一个地方,同时几个连接动作交待显示就会产生逼真的效果。因此,在做动画中,最基本要考虑的因素主要是三个,一是时间,什么时间移动,多长时间变下一个动作,二是位置,从什么位置到什么位置,三是动作,前后两个动作的连续性。在这个例子中,因为车是俯视的,所以车轮转动实际是看不到的,所以不用考虑连续动作的变化,而是只考虑车的位置和多长时间移动即可。第一步pygame.time.delay()来实现时间延迟;第二步利用pygame.draw.rect()把原来位置的图像覆盖掉;第三步screen.blit()在新位置引入图像。下面的例子实现了汽车从驶入到驶出的过程。
import pygame,sys
def lineleft():
plotpoints=[]
for x in range(0,640):
y=-5*x+1000
plotpoints.append([x,y])
pygame.draw.lines(screen,[0,0,0],False,plotpoints,5)
pygame.display.flip()
def lineright():
plotpoints=[]
for x in range(0,640):
y=5*x-2000
plotpoints.append([x,y])
pygame.draw.lines(screen,[0,0,0],False,plotpoints,5)
pygame.display.flip()
def linemiddle():
plotpoints=[]
x=300
for y in range(0,480,20):
plotpoints.append([x,y])
if len(plotpoints)==2:
pygame.draw.lines(screen,[0,0,0],False,plotpoints,5)
plotpoints=[]
pygame.display.flip()
def loadcar(yloc):
my_car=pygame.image.load('ok1.jpg')
locationxy=[310,yloc]
screen.blit(my_car,locationxy)
pygame.display.flip()
if __name__=='__main__':
pygame.init()
screen=pygame.display.set_caption('hello world!')
screen=pygame.display.set_mode([640,480])
screen.fill([255,255,255])
lineleft()
lineright()
linemiddle()
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()
for looper in range(480,-140,-50):
pygame.time.delay(200)
pygame.draw.rect(screen,[255,255,255],[310,(looper+132),83,132],0)
loadcar(looper)


猜你喜欢
- upyter Notebook已经逐渐取代IDE成为了多平台上写简单Python脚本或应用的几家选择。Jupyter Notebook可以通
- df.sort_index()实现按索引排序,默认以从小到大的升序方式排列,如希望按降序排列,传入ascending = Falseimpo
- php5.2新增的json功能是非常受欢迎的,但是经过测试发现, json_encode对中文的处理是有问题的, 1.不能处理GB编码,所有
- 内容摘要:一般情况下,如果我们想了解股市行情,必须登录专业股票网站,输入公司代码才能看到具体股价。其实,利用XMLHTTP协议,我们每个人都
- python 3.x 环境下,使用h5py加载HDF5文件,查看keys,如下:>>> import h5py>&g
- web2.0的标志是Ajax的异步通信的发掘,给我们带来像google map,google suggest 这样令人惊叹的东西。而Ajax
- 1、githubgithub地址:https://github.com/MengFangui/VueProjectTemplate2、web
- 官方文档中关于super的定义说的不是很多,大致意思是返回一个代理对象让你能够调用一些继承过来的方法,查找的机制遵循mro规则,最常用的情况
- 本文为大家分享了Linux环境下mysql5.6.24自动安装脚本代码,供大家参考,具体内容如下说明:一、本脚本仅供测试使用,若正式环境想要
- matplotlib中的字体文件被封装在font_manager这个子模块中,fontManager.ttflist这个列表涵盖了所有Mat
- 自己的小Python项目好几天没有写了,今天打开PyCharm准备继续写,突然发现之前的激活码被取消不能用了,本来激情满满的准备干活啦!之前
- replace()方法返回当前old换成new,可选择的替代限制到最大数量的字符串的副本。语法以下是replace()方法的语法
- 本文主要用python实现了对网站的模拟登录。通过自己构造post数据来用Python实现登录过程。当你要模拟登录一个网站时,首先要搞清楚网
- 搜索答案搜索不到,自己试了一把.首先生成一维数组a =np.array([1,2,3,4,5,6,7,8,9])>>> p
- 读写文件是最常见的IO操作。Python内置了读写文件的函数,用法和C是兼容的。读写文件前,我们先必须了解一下,在磁盘上读写文件的功能都是由
- 在上一篇Python接口自动化测试系列文章:Python接口自动化浅析requests请求封装原理,主要通过源码分析,总结出一套简洁的请求类
- tkinter 是 Python 的标准 GUI 库。Python 使用 tkinter 可以快速的创建 GUI 应用程序。由于 tkint
- APSchedulerAPScheduler 四个组件分别为:调度器(scheduler)、触发器(trigger),作业存储(job st
- 概要:Oracle关系数据库系统以其卓越的性能获得了广泛的应用,而保证数据库的安全性 是数据库管理工作的重要内容。本文是笔者在总结Oracl
- Python 语言里有许多(而且是越来越多)的高级特性,是 Python 发烧友们非常喜欢的。在这些人的眼里,能够写出那些一般开发者看不懂的