python实现好看的时钟效果

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

编译后的到的结果为:

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

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

(0)

相关推荐

  • python+PyQT实现系统桌面时钟

    用Python + PyQT写的一个系统桌面时钟,刚学习Python,写的比较简陋,但是基本的功能还可以. 功能: ①窗体在应用程序最上层,不用但是打开其他应用后看不到时间 ②左键双击全屏,可以做小屏保使用,再次双击退出全屏. ③系统托盘图标,主要参考PyQt4源码目录中的PyQt4\examples\desktop\systray下的程序 ④鼠标右键,将程序最小化 使用时需要heart.svg放在源代码同级目录下,[文件可在PyQt4示例代码目录下PyQt4\examples\desktop\

  • Python Tkinter模块实现时钟功能应用示例

    本文实例讲述了Python Tkinter模块实现时钟功能.分享给大家供大家参考,具体如下: 本机测试效果: 完整代码: # coding=utf-8 from Tkinter import * import _tkinter import math import time from threading import Thread class Clock: def __init__(self, master, x, y, width, height, radius): ''' :param ma

  • python控制台显示时钟的示例

    复制代码 代码如下: #!/usr/bin/env python# coding: utf-8### show time in console#import sysimport time raws = '''.--. |  | `--`  . /| | ------. ---` `------. ---| ---`.  . `--| |.--- `--. ---`.--- |--. `--`.--. `  | |.--. |--| `--`.--. `--| ---`'''.strip()num

  • Python实现模拟时钟代码推荐

    Python实现模拟时钟代码推荐 # coding=utf8 import sys, pygame, math, random 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,

  • Python+Pyqt实现简单GUI电子时钟

    本文实例为大家分享了Python+Pyqt实现简单GUI电子时钟的具体代码,供大家参考,具体内容如下 突发奇想想用GUI做一个简单的电子时钟界面,利用pyqt模块也很方便,代码如下: from PyQt5.QtGui import * from PyQt5.QtCore import * from PyQt5.QtWidgets import QWidget,QApplication,QLCDNumber,QVBoxLayout,QMessageBox,QPushButton import sy

  • python实现简易数码时钟

    最近迷上了Python,要说为什么呢?Python语法简单,功能强大,有广泛的第三方库能快速编程实现自己的想法(无需重复去造轮子).就像某位前辈说的:"人生苦短,学会偷懒-",配置好sublime text照着网上教程直接上手写个小程序入门. 先插张图,计算机技术的演进过程,总结的还是挺到位的. 安装好Python环境,引入需要用到的库: import threading import turtle import time 引入time库后使用localtime()方法可以获取当前服务

  • python使用turtle库绘制时钟

    Python函数库众多,而且在不断更新,所以学习这些函数库最有效的方法,就是阅读Python官方文档.同时借助Google和百度. 本文介绍的turtle库对应的官方文档地址 绘制动态钟表的基本思路如下(面向对象的编程): 使用5个turtle对象 1个turtle:绘制外表盘 3个turtle:模拟表针行为 1个turtle:输出表盘上文字 根据实时时间使用ontimer()函数更新表盘画面,显示效果如下: 相关函数的使用在程序中进行了详细的注释,代码如下: # -*- coding: utf

  • Python实现时钟显示效果思路详解

    语言:Python IDE:Python.IDE 1.编写时钟程序,要求根据时间动态更新 2.代码思路 需求:5个Turtle对象, 1个绘制外表盘+3个模拟表上针+1个输出文字 Step1:建立Turtle对象并初始化 Step2:静态表盘绘制 Step3:根据时钟更新表针位置与时间信息 基本库:Turtle.datetime 3.代码段 from turtle import * from datetime import * def Skip(step): penup() forward(st

  • python实现简易动态时钟

    本文实例为大家分享了python实现简易动态时钟的具体代码,供大家参考,具体内容如下 from turtle import * from datetime import * #移动到指定位置 def skip(step): penup() forward(step) pendown() #画指针 def drawpointer(name, length): reset() skip(-length*0.1) begin_poly() forward(length*1.1) end_poly()

  • python基于Kivy写一个图形桌面时钟程序

    Kivy 是一个开源的 Python 第三方库,可以用来快速开发应用程序. 它有如下三个特点: 跨平台 Kivy 编写的程序可在 Linux,Windows,OS X,Android,iOS 和 Raspberry Pi 上运行. 商业友好 Kivy 基于 MIT 许可证进行开源,可以进行免费的商业使用. GPU 加速 Kivy 的图像引擎基于 Open ES 2 构建,性能出众. 除此之外 Kivy 也存在一些缺点,比如: 非原生的图形界面: 打包后的体积很大: 缺乏社区支持: 缺乏足够的示例

随机推荐