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

 更新时间:2015年04月15日 09:15:09   投稿:junjie  
这篇文章主要介绍了pygame学习笔记(2):画点的三种方法和动画实例,本文讲解了单个像素(画点)、连接多个点形成线、引用图像、动画完整实例,需要的朋友可以参考下

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)

相关文章

  • Python 函数简单易理解版

    Python 函数简单易理解版

    本文将用简单易解的描述方法对Python 函数做一个详情介绍,感兴趣的朋友可以参考下文
    2021-08-08
  • python gravis库实现图形数据可视化实例探索

    python gravis库实现图形数据可视化实例探索

    这篇文章主要为大家介绍了python gravis库实现图形数据可视化实例探索,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2024-02-02
  • OpenCV搞定腾讯滑块验证码的实现代码

    OpenCV搞定腾讯滑块验证码的实现代码

    这篇文章主要介绍了OpenCV搞定腾讯滑块验证码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-05-05
  • Python Pandas中loc和iloc函数的基本用法示例

    Python Pandas中loc和iloc函数的基本用法示例

    无论是loc还是iloc都是pandas中数据筛选的函数,下面这篇文章主要给大家介绍了关于Python Pandas中loc和iloc函数的基本用法示例,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
    2022-07-07
  • 快速解释如何使用pandas的inplace参数的使用

    快速解释如何使用pandas的inplace参数的使用

    这篇文章主要介绍了快速解释如何使用pandas的inplace参数的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-07-07
  • 详解Python开启线程和线程池的方法

    详解Python开启线程和线程池的方法

    这篇文章主要介绍了Python开启线程和线程池的方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2024-03-03
  • python字典排序实例详解

    python字典排序实例详解

    这篇文章主要介绍了python字典排序实现方法,实例分析了Python字典排序的相关技巧,需要的朋友可以参考下
    2015-05-05
  • 使用python编写android截屏脚本双击运行即可

    使用python编写android截屏脚本双击运行即可

    使用python编写一个截屏的脚本,双击运行脚本就OK,截屏成功后会将截屏文件已当前时间命名,并保存在存放脚本的当前路径的screenshot文件夹下
    2014-07-07
  • Python VTK计算曲面的高斯曲率和平均曲率

    Python VTK计算曲面的高斯曲率和平均曲率

    这篇文章主要介绍了Python VTK计算曲面的高斯曲率和平均曲率,如何使用户Python版本的VTK计算曲面的高斯曲率并映射在曲面上。本例中使用了两个不同的表面,每个表面根据其高斯曲率和平均曲率着色,需要的朋友可以参考一下
    2022-04-04
  • Python变量的定义和运算符的使用

    Python变量的定义和运算符的使用

    这篇文章主要介绍了Python变量的定义和运算符的使用,Python和C/Java不同,在定义变量的时候不需要显示的指定变量的类型,在赋值的时候自动就会确定类型,需要的朋友可以参考下
    2023-05-05

最新评论