python游戏编程入门 免费-Python游戏编程入门4
Math和Graphics:Analog Clock示例程序
本章介绍Python的math模块,该模块可以执行计算,如常见的三角正弦函数、余弦函数、正切函数等。
使用正弦和余弦函数绘制圆
创建Anlog Clock示例程序
关于math模块
https://docs.python.org/3/library/math.html
https://docs.python.org/2.7/library/math.html
math.cos(x)
Return the cosine of x radians.
math.sin(x)
Return the sine of x radians.
math.tan(x)
Return the tangent of x radians.
math.degrees(x)
Convert angle x from radians to degrees.
math.radians(x)
Convert angle x from degrees to radians.
math.pi
The mathematical constant π = 3.141592..., to available precision.
math.e
The mathematical constant e = 2.718281..., to available precision.
注意:
对于负数的取模
余数应该是大于等于0小于该数绝对值的那个数,比方说(-5)%3=1,认为-5=3*(-2)+1
书中给出的示例可以优化。
Draw circle in hard way
import sys,pygame,math,random
from pygame.locals import *
pygame.init()
screen=pygame.display.set_mode((600,500))
pygame.display.set_caption("Circle Demo")
angle=0
color=random.randint(0,255),random.randint(0,255),random.randint(0,255)
while True:
for event in pygame.event.get():
if event.type==QUIT:
sys.exit()
keys=pygame.key.get_pressed()
if keys[K_ESCAPE]:
sys.exit()
screen.fill((255,255,255))
x=300
y=250
radius=200
circle_x=x+radius*math.cos(math.radians(angle))
circle_y=y+radius*math.sin(math.radians(angle))
pygame.draw.circle(screen,color,(int(circle_x),int(circle_y)),10,0)
angle+=1
if angle>359:
color=random.randint(0,255),random.randint(0,255),random.randint(0,255)
angle=0
pygame.display.update()
Anlog Clock示例程序
import sys,datetime,pygame,math
from pygame.locals import *
from datetime import datetime,date,time
pygame.init()
def print_text(font,x,y,text,color=(255,255,255)):
imgtext=font.render(text,True,color)
screen.blit(imgtext,(x,y))
def wrap_angle(angle):
return angle%360
#main program begins
screen=pygame.display.set_mode((600,500))
pygame.display.set_caption("Anlog Clock Demo")
font=pygame.font.Font(None,36)
orange=220,180,0
white=255,255,255
yellow=255,255,0
pink=255,100,100
pos_x=300
pos_y=250
radius=250
angle=360
#repeating loop
while True:
for event in pygame.event.get():
if event.type==QUIT:
sys.exit()
keys=pygame.key.get_pressed()
if keys[K_ESCAPE]:
sys.exit()
screen.fill((0,0,100))
#draw one step around the circle
pygame.draw.circle(screen,white,(pos_x,pos_y),radius,6)
#draw the clock numbers1-12
for n in range(1,13):
angle=math.radians(n*30-90)
x=(radius-20)*math.cos(angle)+pos_x-10
y=(radius-20)*math.sin(angle)+pos_y-10
print_text(font,int(x),int(y),str(n))
#get the time of day
today=datetime.today()
hours=today.hour%12
minutes=today.minute
seconds=today.second
#draw the hours hand
hour_angle=wrap_angle(hours*30-90)
hour_angle=math.radians(hour_angle)
hour_x=pos_x+math.cos(hour_angle)*(radius-80)
hour_y=pos_y+math.sin(hour_angle)*(radius-80)
target_h=(int(hour_x),int(hour_y))
pygame.draw.line(screen,pink,(pos_x,pos_y),target_h,25)
#draw the minutes hand
minute_angle=wrap_angle(minutes*6-90)
minute_angle=math.radians(minute_angle)
minute_x=pos_x+math.cos(minute_angle)*(radius-60)
minute_y=pos_y+math.sin(minute_angle)*(radius-60)
target_m=(int(minute_x),int(minute_y))
pygame.draw.line(screen,orange,(pos_x,pos_y),target_m,12)
#draw the seconds hand
second_angle=wrap_angle(seconds*6-90)
second_angle=math.radians(second_angle)
second_x=pos_x+math.cos(second_angle)*(radius-40)
second_y=pos_y+math.sin(second_angle)*(radius-40)
target_s=(int(second_x),int(second_y))
pygame.draw.line(screen,yellow,(pos_x,pos_y),target_s,6)
#cover the center
pygame.draw.circle(screen,white,(pos_x,pos_y),20)
print_text(font,0,0,str(hours)+":"+str(minutes)+":"+str(seconds))
pygame.display.update()
wrap_angle函数是多余的,因为在三角函数的运算中+-360°对于结果没有影响。
去除所有带有 wrap_angle()的部分,程序运行正常。
1、修改circle程序,以使得在每个角度绘制不同的形状,而不是绘制一个小的填充的圆。
import sys,pygame,math,random
from pygame.locals import *
pygame.init()
screen=pygame.display.set_mode((600,500))
pygame.display.set_caption("Circle Demo")
angle=0
color=random.randint(0,255),random.randint(0,255),random.randint(0,255)
while True:
for event in pygame.event.get():
if event.type==QUIT:
sys.exit()
keys=pygame.key.get_pressed()
if keys[K_ESCAPE]:
sys.exit()
screen.fill((0,0,0))
x=300
y=250
radius=200
circle_x=int(x+radius*math.cos(math.radians(angle)))
circle_y=int(y+radius*math.sin(math.radians(angle)))
if angle%3==0:
pygame.draw.circle(screen,color,(circle_x,circle_y),12)
elif angle%3==1:
pygame.draw.rect(screen,color,(circle_x-10,circle_y-10,20,20))
elif angle%3==2:
pygame.draw.ellipse(screen,color,(circle_x-15,circle_y-10,30,20))
angle+=1
if angle>359:
color=random.randint(0,255),random.randint(0,255),random.randint(0,255)
angle=0
pygame.display.update()
感觉画了一个会蠕动的小东西,有一点意思。
2、Analog Circle变得更好看些。这个我没去实现,我把时针的角度做了微调,感觉更符合习惯。现在
时针不会一直指向整点的位置了,而是会根据分钟数指向两个整点之间。
import sys,datetime,pygame,math
from pygame.locals import *
from datetime import datetime,date,time
pygame.init()
def print_text(font,x,y,text,color=(255,255,255)):
imgtext=font.render(text,True,color)
screen.blit(imgtext,(x,y))
def wrap_angle(angle):
return angle%360
#main program begins
screen=pygame.display.set_mode((600,500))
pygame.display.set_caption("Anlog Clock Demo")
font=pygame.font.Font(None,36)
orange=220,180,0
white=255,255,255
yellow=255,255,0
pink=255,100,100
pos_x=300
pos_y=250
radius=250
angle=360
#repeating loop
while True:
for event in pygame.event.get():
if event.type==QUIT:
sys.exit()
keys=pygame.key.get_pressed()
if keys[K_ESCAPE]:
sys.exit()
screen.fill((0,0,100))
#draw one step around the circle
pygame.draw.circle(screen,white,(pos_x,pos_y),radius,6)
#draw the clock numbers1-12
for n in range(1,13):
angle=math.radians(n*30-90)
x=(radius-20)*math.cos(angle)+pos_x-10
y=(radius-20)*math.sin(angle)+pos_y-10
print_text(font,int(x),int(y),str(n))
#get the time of day
today=datetime.today()
hours=today.hour%12
minutes=today.minute
seconds=today.second
#draw the hours hand
hour_angle=wrap_angle(hours*30-90+minutes*0.5) #对比原来的程序修改了这一行。
hour_angle=math.radians(hour_angle)
hour_x=pos_x+math.cos(hour_angle)*(radius-80)
hour_y=pos_y+math.sin(hour_angle)*(radius-80)
target_h=(int(hour_x),int(hour_y))
pygame.draw.line(screen,pink,(pos_x,pos_y),target_h,25)
#draw the minutes hand
minute_angle=wrap_angle(minutes*6-90)
minute_angle=math.radians(minute_angle)
minute_x=pos_x+math.cos(minute_angle)*(radius-60)
minute_y=pos_y+math.sin(minute_angle)*(radius-60)
target_m=(int(minute_x),int(minute_y))
pygame.draw.line(screen,orange,(pos_x,pos_y),target_m,12)
#draw the seconds hand
second_angle=wrap_angle(seconds*6-90)
second_angle=math.radians(second_angle)
second_x=pos_x+math.cos(second_angle)*(radius-40)
second_y=pos_y+math.sin(second_angle)*(radius-40)
target_s=(int(second_x),int(second_y))
pygame.draw.line(screen,yellow,(pos_x,pos_y),target_s,6)
#cover the center
pygame.draw.circle(screen,white,(pos_x,pos_y),20)
print_text(font,0,0,str(hours)+":"+str(minutes)+":"+str(seconds))
pygame.display.update()
看起来更符合习惯了。至于美观的要求就算了。
python游戏编程入门 免费-Python游戏编程入门4相关推荐
- python游戏编程入门 免费-Python游戏编程入门2
I/O.数据和字体:Trivia游戏 本章包括如下内容: Python数据类型 获取用户输入 处理异常 Mad Lib游戏 操作文本文件 操作二进制文件 Trivia游戏 其他的不说,我先去自己学习文 ...
- python入门编程软件免费-Python编程干货免费领取!!!
原标题:Python编程干货免费领取!!! 早在18 年,教育部就正式将人工智能.物联网.大数据处理正式划入高中新课标,这就意味着现在的学生16岁就要开始学习编程了! 开发岗位的高薪和人工智能的发展, ...
- python零基础电子书免费下载-零基础入门学习Python PDF 扫描版
给大家带来的一篇关于Python编程相关的电子书资源,介绍了关于Python.零基础.入门学习方面的内容,本书是由清华大学出版社出版,格式为PDF,资源大小59.3 MB,小甲鱼编写,目前豆瓣.亚马逊 ...
- python游戏编程入门 免费-python游戏编程入门 python游戏编程入门课
python游戏编程入门 python游戏编程入门课 什么是python游戏编程入门?首先我们需要认识什么是Python Python既是一个软件工具包,也是一种语言.Python软件包包含了一个名为 ...
- python游戏编程入门免费_python游戏编程入门 python游戏编程入门课
python游戏编程入门 python游戏编程入门课 什么是python游戏编程入门?首先我们需要认识什么是Python Python既是一个软件工具包,也是一种语言.Python软件包包含了一个名为 ...
- python入门编程软件免费-Python 3.7.0编程软件免费下载
软件介绍 Python是一种跨平台的计算机程序设计语言.是一种面向对象的动态类型语言,最初被设计用于编写自动化脚本(shell),已经具有十多年的发展历史,成熟且稳定.这种语言具有非常简捷而清晰的语法 ...
- python编程软件免费吗_MRT7-Python编程软件
MRT7-Python编程软件适用于儿童编程,由韩端科技推出,提供Boclky编程.Python代码编程等多种编程模式,支持配合设备进行使用,软件安装操作起来并不困难,用户可以根据自己的系统安装,软件 ...
- 经典按键java手机游戏_各种免费手机游戏可以玩了!
今天,Killer怎能不为大家分享一款超级实用的手机游戏模拟器破解软件,让你能玩到各种类型的免费游戏,喜欢玩游戏的小伙伴一定不要错过哦! | 海星模拟器破解版 首先,这是一款包含了各种类型游戏的游戏平 ...
- 免费python网络课程-2019年10种免费的Python学习课程
近年来,越来越多的人在学习Python.大部分人是为了探索Python提供的数据科学和机器学习库.也有些人学习Python是为了进行Web开发,还有许多人是为了编写脚本并将其自动化.现在为什么要学习P ...
最新文章
- 只需要4步即可在vue2中使用路由router
- 电影短视频营销白皮书
- shell脚本的简单学习
- linux下文件合并、分割、去重
- python opencv3 特征提取与描述 DoG SIFT hessian surf
- django报错:ImproperlyConfigured和AppRegistryNotReady
- mysql基础入门(参照b站黑马程序员整理)
- wps居中对齐不在中间_wps有时候居中对齐不是在中间
- PLSQL7配置免安装Oracle客户端
- uniapp,小程序,实现签名功能
- unity3D中导出webgl并使用js进行交互
- 上海图书馆及分馆特色
- 如何用python编写财务记账软件_Python实现简单的记账本功能
- 在Verilog里边 always@(*)语句是什么意思
- 苹果商城怎么调成中文_使用苹果手机时,你感觉最不舒服的地方有哪些?
- Python高级--逻辑回归、KNN回归比较
- 软件工程_绘制系统业务流程图
- Erase/Trim/Discard/Sanitize 区别详解
- Java函数式编程(Lambda表达式、Stream流用法)
- ChatGPT真的像媒体宣传的那样“四平八稳”吗?
热门文章
- DNS通道检测 国外学术界研究情况——研究方法:基于流量,使用机器学习分类算法居多,也有使用聚类算法的;此外使用域名zif low也有...
- LSM树——LSM 将B+树等结构昂贵的随机IO变的更快,而代价就是读操作要处理大量的索引文件(sstable)而不是一个,另外还是一些IO被合并操作消耗。...
- NXT节点搭建(二)环境搭建
- IOS-React-Native:No bundle URL present
- Selenium3+python自动化009- 截图
- hiho 1483 区间计数问题+二分答案
- asp.net webform 与asp.net mvc 混合开发项目总结
- OpenCV函数学习之cvLUT
- LRU的理解与Java实现
- Python之operator库