pygame实现时钟效果

yizhihongxing

下面是关于用Pygame实现时钟效果的完整攻略,包含了步骤、代码示例和详细说明。

步骤

  1. 导入Pygame库。因为本文所讲的内容涉及到窗口绘图操作,所以需要用到Pygame库。

python
import pygame

  1. 初始化Pygame。在继续之前,需要对Pygame进行初始化。

python
pygame.init()

  1. 设定窗口大小。根据自己的需要,设定窗口的大小。

python
screen_width = 400
screen_height = 400
screen = pygame.display.set_mode((screen_width, screen_height))

  1. 设定字体样式。在后续绘制时钟时,需要指定字体的样式和字体大小。

python
font_size = 50
font = pygame.font.Font(None, font_size)

  1. 设定颜色。为时钟和文字指定颜色。

python
bg_color = (255, 255, 255) # 背景颜色
clock_color = (0, 0, 0) # 时钟的颜色
text_color = (255, 0, 0) # 文字的颜色

  1. 获取当前时间。可以使用Python的time模块获取当前时间。

python
current_time = time.strftime('%H:%M:%S')

  1. 在屏幕上绘制时钟。使用Pygame的draw模块绘制时钟。

python
pygame.draw.circle(screen, clock_color, (int(screen_width/2), int(screen_height/2)), int(screen_width/3), 3) # 画一个圆
for i in range(12):
angle = i*30*math.pi/180
pos = (int(screen_width/2+math.sin(angle)*screen_width/5), int(screen_height/2-math.cos(angle)*screen_width/5))
pygame.draw.circle(screen, clock_color, pos, 10, 0) # 画12个刻度线
pygame.draw.line(screen, text_color, (int(screen_width/2), int(screen_height/2)), (int(screen_width/2+math.sin(hour_angle)*screen_width/8), int(screen_height/2-math.cos(hour_angle)*screen_width/8)), int(screen_width/80)) # 时针
pygame.draw.line(screen, text_color, (int(screen_width/2), int(screen_height/2)), (int(screen_width/2+math.sin(minute_angle)*screen_width/6), int(screen_height/2-math.cos(minute_angle)*screen_width/6)), int(screen_width/160)) # 分针
pygame.draw.line(screen, text_color, (int(screen_width/2), int(screen_height/2)), (int(screen_width/2+math.sin(second_angle)*screen_width/5), int(screen_height/2-math.cos(second_angle)*screen_width/5)), int(screen_width/320)) # 秒针

  1. 在屏幕上绘制文字。为了更好地显示当前时间,在时钟下面绘制当前时间的文字。

python
text = font.render(current_time, True, text_color)
text_rect = text.get_rect()
text_rect.centerx = int(screen_width/2)
text_rect.top = int(screen_height/2)+int(screen_width/3)
screen.blit(text, text_rect) # 在屏幕上绘制文字

  1. 刷新屏幕。完成绘制之后,需要刷新屏幕。

python
pygame.display.flip()

  1. 监听事件。在程序最后,监听用户的事件并退出程序。

python
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()

示例

示例一

import pygame
import math
import time
import sys

pygame.init()

# 设定窗口大小
screen_width = 400
screen_height = 400
screen = pygame.display.set_mode((screen_width, screen_height))

# 设定字体和大小
font_size = 50
font = pygame.font.Font(None, font_size)

# 设定颜色
bg_color = (255, 255, 255) # 背景颜色
clock_color = (0, 0, 0) # 时钟的颜色
text_color = (255, 0, 0) # 文字的颜色

while True:
    # 获取当前时间
    current_time = time.strftime('%H:%M:%S')

    # 计算时针、分针、秒针的角度
    hour_angle = int(current_time.split(':')[0])*30*math.pi/180
    minute_angle = int(current_time.split(':')[1])*6*math.pi/180
    second_angle = int(current_time.split(':')[2])*6*math.pi/180

    # 绘制时钟和文字
    screen.fill(bg_color)
    pygame.draw.circle(screen, clock_color, (int(screen_width/2), int(screen_height/2)), int(screen_width/3), 3)
    for i in range(12):
        angle = i*30*math.pi/180
        pos = (int(screen_width/2+math.sin(angle)*screen_width/5), int(screen_height/2-math.cos(angle)*screen_width/5))
        pygame.draw.circle(screen, clock_color, pos, 10, 0)
    pygame.draw.line(screen, text_color, (int(screen_width/2), int(screen_height/2)), (int(screen_width/2+math.sin(hour_angle)*screen_width/8), int(screen_height/2-math.cos(hour_angle)*screen_width/8)), int(screen_width/80))
    pygame.draw.line(screen, text_color, (int(screen_width/2), int(screen_height/2)), (int(screen_width/2+math.sin(minute_angle)*screen_width/6), int(screen_height/2-math.cos(minute_angle)*screen_width/6)), int(screen_width/160))
    pygame.draw.line(screen, text_color, (int(screen_width/2), int(screen_height/2)), (int(screen_width/2+math.sin(second_angle)*screen_width/5), int(screen_height/2-math.cos(second_angle)*screen_width/5)), int(screen_width/320))
    text = font.render(current_time, True, text_color)
    text_rect = text.get_rect()
    text_rect.centerx = int(screen_width/2)
    text_rect.top = int(screen_height/2)+int(screen_width/3)
    screen.blit(text, text_rect)

    # 刷新屏幕
    pygame.display.flip()

    # 监听退出事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

示例二

import pygame
import math
import time
import sys

pygame.init()

# 设定窗口大小
screen_width = 500
screen_height = 500
screen = pygame.display.set_mode((screen_width, screen_height))

# 设定字体和大小
font_size = 80
font = pygame.font.Font(None, font_size)

# 设定颜色
bg_color = (255, 255, 255) # 背景颜色
clock_color = (0, 0, 0) # 时钟的颜色
text_color = (255, 0, 0) # 文字的颜色

while True:
    # 获取当前时间
    current_time = time.strftime('%H:%M:%S')

    # 计算时针、分针、秒针的角度
    hour_angle = int(current_time.split(':')[0])*30*math.pi/180
    minute_angle = int(current_time.split(':')[1])*6*math.pi/180
    second_angle = int(current_time.split(':')[2])*6*math.pi/180

    # 绘制时钟和文字
    screen.fill(bg_color)
    pygame.draw.circle(screen, clock_color, (int(screen_width/2), int(screen_height/2)), int(screen_width/3), 3)
    for i in range(60):
        angle = i*6*math.pi/180
        pos_outer = (int(screen_width/2+math.sin(angle)*screen_width/3), int(screen_height/2-math.cos(angle)*screen_width/3))
        if i % 5 == 0:
            pygame.draw.circle(screen, clock_color, pos_outer, 10, 0)
        else:
            pos_inner = (int(screen_width/2+math.sin(angle)*screen_width/3.5), int(screen_height/2-math.cos(angle)*screen_width/3.5))
            pygame.draw.circle(screen, clock_color, pos_inner, 5, 0)
    pygame.draw.line(screen, text_color, (int(screen_width/2), int(screen_height/2)), (int(screen_width/2+math.sin(hour_angle)*screen_width/5), int(screen_height/2-math.cos(hour_angle)*screen_width/5)), int(screen_width/20))
    pygame.draw.line(screen, text_color, (int(screen_width/2), int(screen_height/2)), (int(screen_width/2+math.sin(minute_angle)*screen_width/3.5), int(screen_height/2-math.cos(minute_angle)*screen_width/3.5)), int(screen_width/30))
    pygame.draw.line(screen, text_color, (int(screen_width/2), int(screen_height/2)), (int(screen_width/2+math.sin(second_angle)*screen_width/3), int(screen_height/2-math.cos(second_angle)*screen_width/3)), int(screen_width/60))
    text = font.render(current_time, True, text_color)
    text_rect = text.get_rect()
    text_rect.centerx = int(screen_width/2)
    text_rect.top = int(screen_height/4)*3
    screen.blit(text, text_rect)

    # 刷新屏幕
    pygame.display.flip()

    # 监听退出事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

结束语

以上就是使用Pygame实现时钟效果的完整攻略,包括了步骤、代码示例和详细说明。希望大家可以参考本文修改自己的代码,达到更好的效果。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:pygame实现时钟效果 - Python技术站

(0)
上一篇 2023年6月2日
下一篇 2023年6月2日

相关文章

  • Python 从文件中读取字符串,保留要打印的变量

    【问题标题】:Python read strings from file, preserving variables to be printedPython 从文件中读取字符串,保留要打印的变量 【发布时间】:2023-04-04 02:46:02 【问题描述】: 我正在制作一个 Python 脚本,它将从列表中随机选择一个响应。 为了填充这个列表,我想从文…

    Python开发 2023年4月6日
    00
  • Python 创建格式化字符串方法

    Python 创建格式化字符串是一个非常常用的操作,常用于输出带有特定格式的字符串。Python 提供了多种方式来创建格式化字符串,本文将为您详细讲解。 使用百分号(%)格式化字符串 使用 % 创建格式化字符串是 Python 最初提供的一种格式化字符串的方法,它是早期 C 语言的格式化字符串方法的一种简单模拟。在使用时,我们可以使用 % 等特殊字符来格式化…

    python 2023年6月5日
    00
  • 基于Python实现新年倒计时

    下面是关于“基于Python实现新年倒计时”的完整攻略: 1. 准备工作 在开始编写代码之前,我们需要安装Python(建议使用Python3.x版本)、在代码编辑器中打开Python文件并创建计时器函数。 2. 创建计时器函数 接下来,我们需要创建一个名为“Countdown”的新函数来实现倒计时的功能。代码段如下: import time def Cou…

    python 2023年6月2日
    00
  • Python+Opencv实现图像模板匹配详解

    这里是“Python+Opencv实现图像模板匹配详解”的攻略,主要介绍了使用Python和OpenCV实现图像模板匹配的过程,以及一些示例说明。 1. 简介 图像模板匹配是指在一副图像中查找给定的目标图像的位置。它是计算机视觉中的基本问题之一,也是许多更复杂问题的基础。在本教程中,我们将使用Python和OpenCV来实现基本的图像模板匹配。 2. 实现过…

    python 2023年5月18日
    00
  • 如何在Python中使用SQLObject ORM操作数据库?

    SQLObject是一个Python ORM(对象关系映射)库,它提供了一种简单的方式来操作关系型数据库。使用SQLObject,我们可以使用Python代码来创建、读取、更新删除关系数据库中的数据。以下是如何在Python中使用SQLObject ORM操作的完整使用攻略,包括连接数据库、创建表、插入数据、查询数据等步骤。同时,提供了两个示例以便更好理解如…

    python 2023年5月12日
    00
  • Python3读取文件常用方法实例分析

    以下是针对“Python3读取文件常用方法实例分析”的完整攻略: Python3读取文件常用方法实例分析 1. 文件读取基础知识 在Python中,我们可以使用内置的open()函数来打开一个文件,然后读取或者写入其中的内容。open()函数的第一个参数是文件的路径,第二个参数是以何种模式打开文件,如下: f = open(‘file.txt’, ‘r’) …

    python 2023年6月5日
    00
  • Python Selenium破解滑块验证码最新版(GEETEST95%以上通过率)

    标题:Python Selenium破解滑块验证码最新版(GEETEST95%以上通过率) 介绍:本文将介绍使用Python和Selenium库破解GEETEST滑块验证码的方法。通过模拟人类滑动的方式,实现95%以上的高通过率。 步骤:一、准备工作1. 安装Python3;2. 安装Selenium库和Chrome浏览器驱动;3. 安装Pillow库和Nu…

    python 2023年6月6日
    00
  • Python结合Sprak实现计算曲线与X轴上方的面积

    这里给出Python结合Spark实现计算曲线与X轴上方的面积的详细攻略。 简介 首先,我们需要明确一下该任务的目标:我们需要计算一段曲线与X轴之间的面积。假设我们已经有了一个数学函数 $f(x)$,我们需要计算该函数在区间 [a, b] 上与 X 轴之间的面积,这个面积可以表示为定积分 $\int_a^b{f(x)dx}$。而计算定积分可以通过数值积分的方…

    python 2023年6月6日
    00
合作推广
合作推广
分享本页
返回顶部