pygame学习笔记(3):运动速率、时间、事件、文字
作者:junjie 发布时间:2023-05-20 21:19:38
1、运动速率
上节中,实现了一辆汽车在马路上由下到上行驶,并使用了pygame.time.delay(200)来进行时间延迟。看了很多参考材料,基本每个材料都会谈到不同配置机器下运动速率的问题,有的是通过设定频率解决,有的是通过设定速度解决,自己本身水平有限,看了几篇,觉得还是《Beginning Game Development with Python and Pygame》这里面提到一个方法比较好。代码如下,代码里更改的地方主要是main里的代码,其中利用clock=pygame.time.Clock()来定义时钟,speed=250.0定义了速度,每秒250像素,time_passed=clock.tick()为上次运行时间单位是毫秒,time_passed_seconds=time_passed/1000.0将单位改为秒,distance_moved=time_passed_seconds*speed时间乘以速度得到移动距离,这样就能保证更加流畅。
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()
clock=pygame.time.Clock()
looper=480
speed=250.0
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()
pygame.draw.rect(screen,[255,255,255],[310,(looper+132),83,132],0)
time_passed=clock.tick()
time_passed_seconds=time_passed/1000.0
distance_moved=time_passed_seconds*speed
looper-=distance_moved
if looper<-480:
looper=480
loadcar(looper)
2、事件
我理解的就是用来解决键盘、鼠标、遥控器等输入后做出什么反映的。例如上面的例子,可以通过按上方向键里向上来使得小车向上移动,按下向下,使得小车向下移动。当小车从下面倒出时,会从上面再出现,当小车从上面驶出时,会从下面再出现。代码如下。event.type == pygame.KEYDOWN用来定义事件类型,if event.key==pygame.K_UP这里是指当按下向上箭头时,车前进。if event.key==pygame.K_DOWN则相反,指按下向下箭头,车后退。
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()
looper=480
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key==pygame.K_UP:
looper=looper-50
if looper<-480:
looper=480
pygame.draw.rect(screen,[255,255,255],[310,(looper+132),83,132],0)
loadcar(looper)
if event.key==pygame.K_DOWN:
looper=looper+50
if looper>480:
looper=-480
pygame.draw.rect(screen,[255,255,255],[310,(looper-132),83,132],0)
loadcar(looper)
3、字体及字符显示
使用字体模块用来做游戏的文字显示,大部分游戏都会有诸如比分、时间、生命值等的文字信息。pygame主要是使用pygame.font模块来完成,常用到的一些方法是:
pygame.font.SysFont(None, 16),第一个参数是说明字体的,可以是"arial"等,这里None表示默认字体。第二个参数表示字的大小。如果无法知道当前系统中装了哪些字体,可以使用pygame.font.get_fonts()来获得所有可用字体。
pygame.font.Font("AAA.ttf", 16),用来使用TTF字体文件。
render("hello world!", True, (0,0,0), (255, 255, 255)),render方法用来创建文字。第一个参数是写的文字;第二个参数是否开启抗锯齿,就是说True的话字体会比较平滑,不过相应的速度有一点点影响;第三个参数是字体的颜色;第四个是背景色,无表示透明。
下面将上面的例子添加当前汽车坐标:
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()
def loadtext(xloc,yloc):
textstr='location:'+str(xloc)+','+str(yloc)
text_screen=my_font.render(textstr, True, (255, 0, 0))
screen.blit(text_screen, (50,50))
if __name__=='__main__':
pygame.init()
screen=pygame.display.set_caption('hello world!')
screen=pygame.display.set_mode([640,480])
my_font=pygame.font.SysFont(None,22)
screen.fill([255,255,255])
loadtext(310,0)
lineleft()
lineright()
linemiddle()
looper=480
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key==pygame.K_UP:
looper=looper-50
if looper<-132:
looper=480
if event.key==pygame.K_DOWN:
looper=looper+50
if looper>480:
looper=-132
loadtext(310,looper)
screen.fill([255,255,255])
loadtext(310,looper)
lineleft()
lineright()
linemiddle()
loadcar(looper)
这个例子里直接让背景重绘一下,就不会再像1、2里面那样用空白的rect去覆盖前面的模块了。
![](https://www.aspxhome.com/images/zang.png)
![](https://www.aspxhome.com/images/jiucuo.png)
猜你喜欢
- 刚刚换用windows7 64位旗舰版,使用其自带的iis7作为调试工具,今天调试一个ASP+ACCESS的网站的时候遇到了“ADODB.C
- 1 PHP官网地址:PHP界面:2 下载官网下载地址:Download PHP 或者点击这里下载3 安装① 按照下图下载 php。
- 利用字典dict来完成统计举例:a = [1, 2, 3, 1, 1, 2]dict = {}for key in a: dic
- 最近发生了很多事情,工作不开心,爱情无果而终,身边的小伙伴陆陆续续离职。虽然都不是会一下子击垮自己的事情,但是积攒起来,还是会有突然感到疲惫
- 本文实例讲述了Python3写入文件常用方法。分享给大家供大家参考。具体如下:''''' Creat
- 本文更多将会介绍三思在日常中经常会用到的,或者虽然很少用到,但是感觉挺有意思的一些函数。分二类介绍,分别是: 著名函数篇-经常用到的函数 非
- 一、介绍QQ空间相册的个性化利器,能对照片进行效果的优化、文字编辑等等。从设计上使用了创新的手法,尽量减少用户的思考。其中,通过界面的特殊表
- 在ASP中,如何获得ADO的连接信息? 具体方法见下列代码:<%Sub Connecti
- 一、为表创建自增长自段有两种,一种是不同的表使用各自的Sequence,方法如下: 1、在Oracle sequence首先创建sequen
- 在oracle中,函数和存储过程是经常使用到的,他们的语法中有很多相似的地方,可是也有它们的不同之处,这段时间刚学完函数与存储过程,来给自己
- 1.INSERT INTO SELECT语句 语句形式为:Insert into Table2(field1,field2,...) sel
- 在对列表的元素进行找寻时,会频繁的说到遍历的理念。对于复杂的遍历要求,如多个列表中查找就显然不适合用for循环。本篇所要带来的是zip()
- 要下午传上的.结果事一多,忘记了.好不容易回来 . 这个和 dh20156 的那个,是差不多的。 找不到合适的图片,也
- 列表是Python中最基本的数据结构,列表是最常用的Python数据类型,列表的数据项不需要具有相同的类型。列表中的每个元素都分配一个数字
- Oracle客户端精简后的文件,可以实现数据库的通信,直接和软件打包: 第一步:拷贝文件:主要是四个目录:bin,nls,oracore,N
- python是一个很有趣的语言,可以在命令行窗口运行。python中有很多功能强大的模块,这篇经验告诉你,如何使用python的pygal模
- 由于Django没有象rails一样指定项目的目录结构规范,很多人都对django项目的目录结构要如何组织而感到困惑。为此我又新创建了一个开
- 这是一个非常简单的解决方案,柱状图中每一条柱都是一个 div,数据的大小呈现在 div 的宽或高上。 查看演示 例子下载实现的原理
- 本文实例讲述了Python实现telnet服务器的方法。分享给大家供大家参考。具体实现方法如下:import threading class
- 前言我们在工作中的时候,会有这种需求:用户上传一个格式固定excel表格到网站上,然后程序负债解析内容并进行处理。我最近在工作中就遇到了,所