python开发制作好看的时钟效果


Posted in Python onMay 02, 2022

使用python制作好看的时钟,供大家参考,具体内容如下

游戏用到初高中使用的三角函数等知识开发,长话短说,上完整程序。

#-*- coding:utf-8 -*-
import sys,random,math,pygame
from pygame.locals import *
from datetime import datetime,date,time
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
pygame.init()
screen=pygame.display.set_mode([600,500])
pygame.display.set_caption("AnalogClock")
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=260
while True:
    screen.fill([0,0,0])
    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])
    pygame.draw.circle(screen,white,(pos_x,pos_y),radius,6)
    for n in range(1,13):
        angle=math.radians(n*(360/12)-90)
        x=math.cos(angle)*(radius-20)-10
        y=math.sin(angle)*(radius-20)-10
        print_text(font, pos_x+x, pos_y+y, str(n))
    today=datetime.today()
    hours=today.hour % 12
    minutes=today.minute
    seconds=today.second
    hour_angle=wrap_angle(hours*(360/12)-90)
    hour_angle=math.radians(hour_angle)
    hour_x=math.cos(hour_angle)*(radius-80)
    hour_y=math.sin(hour_angle)*(radius-80)
    target=(pos_x+hour_x,pos_y+hour_y)
    pygame.draw.line(screen,pink,(pos_x,pos_y),target,25)
    min_angle=wrap_angle(minutes*(260/60)-90)
    min_angle=math.radians(min_angle)
    min_x=math.cos(min_angle)*(radius-60)
    min_y=math.sin(min_angle)*(radius-60)
    target=(pos_x+min_x,pos_y+min_y)
    pygame.draw.line(screen,orange,(pos_x,pos_y),target,12)
    sec_angle=wrap_angle(seconds*(360/60)-90)
    sec_angle=math.radians(sec_angle)
    sec_x=math.cos(sec_angle)*(radius-40)
    sec_y=math.sin(sec_angle)*(radius-40)
    target=(pos_x+sec_x,pos_y+sec_y)
    pygame.draw.line(screen,yellow,(pos_x,pos_y),target,6)
    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开发制作好看的时钟效果

是不是挺有趣的,游戏开发就是要用到很多的算法,以后的路还得慢慢的努力了。

以上就是本文的全部内容,希望对大家的学习有所帮助。


Tags in this post...

Python 相关文章推荐
python 实现插入排序算法
Jun 05 Python
使用python的chardet库获得文件编码并修改编码
Jan 22 Python
使用Python生成url短链接的方法
May 04 Python
pygame播放音乐的方法
May 19 Python
Python中几个比较常见的名词解释
Jul 04 Python
Python的多态性实例分析
Jul 07 Python
Linux 下 Python 实现按任意键退出的实现方法
Sep 25 Python
python实现人脸识别代码
Nov 08 Python
Python cookbook(字符串与文本)在字符串的开头或结尾处进行文本匹配操作
Apr 20 Python
python 日志模块logging的使用场景及示例
Jan 04 Python
python中编写函数并调用的知识点总结
Jan 13 Python
浅谈盘点5种基于Python生成的个性化语音方法
Feb 05 Python
关于的python五子棋的算法
python开发人人对战的五子棋小游戏
python pygame 开发五子棋双人对弈
May 02 #Python
Python开发简易五子棋小游戏
May 02 #Python
Python开发五子棋小游戏
python获取带有返回值的多线程
May 02 #Python
总结三种用 Python 作为小程序后端的方式
You might like
php 发送带附件邮件示例
2014/01/23 PHP
php加速器eAccelerator的配置参数、API详解
2014/05/05 PHP
php+mysqli使用面向对象方式更新数据库实例
2015/01/29 PHP
php简单实现无限分类树形列表的方法
2015/03/27 PHP
mouse_on_title.js
2006/08/25 Javascript
Javascript 代码也可以变得优美的实现方法
2009/06/22 Javascript
js 覆盖和重载 函数
2009/09/25 Javascript
location.search在客户端获取Url参数的方法
2010/06/08 Javascript
jQuery+ajax实现顶一下,踩一下效果
2010/07/17 Javascript
JavaScript.The.Good.Parts阅读笔记(一)假值与===运算符
2010/11/16 Javascript
javascript模拟php函数in_array
2015/04/27 Javascript
JS实现仿新浪黄色经典滑动门效果代码
2015/09/27 Javascript
JavaScript电子时钟倒计时
2016/01/09 Javascript
用js控件div的滚动条,让它在内容更新时自动滚到底部的实现方法
2016/10/27 Javascript
AngularJS入门教程之Helloworld示例
2016/12/25 Javascript
JavaScript中Promise的使用详解
2017/02/26 Javascript
Vue.js仿Metronic高级表格(一)静态设计
2017/04/17 Javascript
React Native之ListView实现九宫格效果的示例
2017/08/02 Javascript
bootstrap switch开关组件使用方法详解
2017/08/22 Javascript
vue搜索和vue模糊搜索代码实例
2019/05/07 Javascript
nodejs提示:cross-device link not permitted, rename错误的解决方法
2019/06/10 NodeJs
微信小程序tabBar 返回tabBar不刷新页面
2019/07/25 Javascript
[09:37]DOTA2卡尔工作室 英雄介绍圣堂刺客篇
2013/06/13 DOTA
Python内置函数bin() oct()等实现进制转换
2012/12/30 Python
Python格式化css文件的方法
2015/03/10 Python
Python实现字典的key和values的交换
2015/08/04 Python
使用Python的Twisted框架编写非阻塞程序的代码示例
2016/05/25 Python
Python编程pygal绘图实例之XY线
2017/12/09 Python
python聚类算法解决方案(rest接口/mpp数据库/json数据/下载图片及数据)
2019/08/28 Python
欧洲最大的婴幼儿服装及内衣公司:Petit Bateau(小帆船)
2016/08/16 全球购物
加拿大最大的钻石商店:Peoples Jewellers
2018/01/01 全球购物
Android面试题及答案
2015/09/04 面试题
NET程序员上机面试题
2015/05/23 面试题
工作会议通知
2015/04/15 职场文书
2019入党申请书范文3篇
2019/08/21 职场文书
Window server 2012 R2 AD域的组策略相关设置
2022/04/28 Servers