pygame学习笔记(2):画点的三种方法和动画实例


Posted in Python onApril 15, 2015

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()

pygame学习笔记(2):画点的三种方法和动画实例

素材:ok1.jpg

pygame学习笔记(2):画点的三种方法和动画实例

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)
Python 相关文章推荐
Python编程之属性和方法实例详解
May 19 Python
python抽象基类用法实例分析
Jun 04 Python
python使用电子邮件模块smtplib的方法
Aug 28 Python
python 获取list特定元素下标的实例讲解
Apr 09 Python
用Python实现校园通知更新提醒功能
Nov 23 Python
详解PyQt5信号与槽的几种高级玩法
Mar 24 Python
Python如何通过百度翻译API实现翻译功能
Apr 02 Python
Python基于pillow库实现生成图片水印
Sep 14 Python
python 星号(*)的多种用途
Sep 21 Python
Python实现Kerberos用户的增删改查操作
Dec 14 Python
分享一个python的aes加密代码
Dec 22 Python
Python djanjo之csrf防跨站攻击实验过程
May 14 Python
python实现telnet客户端的方法
Apr 15 #Python
pygame学习笔记(1):矩形、圆型画图实例
Apr 15 #Python
Python远程桌面协议RDPY安装使用介绍
Apr 15 #Python
在Gnumeric下使用Python脚本操作表格的教程
Apr 14 #Python
使用Python构建Hopfield网络的教程
Apr 14 #Python
使用C语言扩展Python程序的简单入门指引
Apr 14 #Python
在Python中封装GObject模块进行图形化程序编程的教程
Apr 14 #Python
You might like
解析:通过php socket并借助telnet实现简单的聊天程序
2013/06/18 PHP
php强大的时间转换函数strtotime
2016/02/18 PHP
Laravel Intervention/image图片处理扩展包的安装、使用与可能遇到的坑详解
2017/11/14 PHP
设置下载不需要倒计时cookie(倒计时代码)
2008/11/19 Javascript
一些常用的JS功能函数(2009-06-04更新)
2009/06/04 Javascript
javascript深入理解js闭包
2010/07/03 Javascript
js当一个变量为函数时 应该注意的一点细节小结
2011/12/29 Javascript
文本框输入时 实现自动提示(像百度、google一样)
2012/04/05 Javascript
jquery创建一个ajax关键词数据搜索实现思路
2013/02/26 Javascript
jQuery插件Elastislide实现响应式的焦点图无缝滚动切换特效
2015/04/12 Javascript
JavaScript实现99乘法表及隔行变色实例代码
2016/02/24 Javascript
js return返回多个值,通过对象的属性访问方法
2017/02/21 Javascript
Vue.JS实现垂直方向展开、收缩不定高度模块的JS组件
2018/06/19 Javascript
JS使用canvas中的measureText方法测量字体宽度示例
2019/02/02 Javascript
JS对象和字符串之间互换操作实例分析
2019/02/02 Javascript
微信小程序蓝牙连接小票打印机实例代码详解
2019/06/03 Javascript
vue 开发企业微信整合案例分析
2019/12/02 Javascript
js布局实现单选按钮控件
2020/01/17 Javascript
js HTML DOM EventListener功能与用法实例分析
2020/04/27 Javascript
[27:39]Ti4 循环赛第二日 LGD vs Fnatic
2014/07/11 DOTA
Python的Flask框架中Flask-Admin库的简单入门指引
2015/04/07 Python
python插入数据到列表的方法
2015/04/30 Python
Python书单 不将就
2017/07/11 Python
python json.loads兼容单引号数据的方法
2018/12/19 Python
解决pycharm导入numpy包的和使用时报错:RuntimeError: The current Numpy installation (‘D:\\python3.6\\lib\\site-packa的问题
2020/12/08 Python
canvas绘图按照contain或者cover方式适配并居中显示
2019/02/18 HTML / CSS
linux面试题参考答案(5)
2016/11/05 面试题
设计模式的基本要素是什么
2014/04/21 面试题
师范毕业生自荐信
2013/10/17 职场文书
消防安全管理制度
2014/02/01 职场文书
小学教师节活动总结
2015/03/20 职场文书
课题研究阶段性总结
2015/08/13 职场文书
廉洁自律承诺书2016
2016/03/25 职场文书
导游词之河北滦平金山岭长城
2019/10/16 职场文书
无线电知识基础入门篇
2022/02/18 无线电
Python中np.random.randint()参数详解及用法实例
2022/09/23 Python