如何在 pygame 窗口中设置点击时钟 [英] How to set a clicking clock in a pygame window

查看:75
本文介绍了如何在 pygame 窗口中设置点击时钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#!/usr/bin/python
# -*- coding: utf-8 -*-
import pygame
import sys
import datetime
import time


class TextPicture(pygame.sprite.Sprite):

    def __init__(self, speed, location):
        pygame.sprite.Sprite.__init__(self)
        now = datetime.datetime.now()
        text = now.strftime("%H:%M:%S")
        font = pygame.font.Font(None, 40)
        time_text = font.render(text, 1, [0, 0, 0])  # show now time
        self.image = time_text
        self.speed = speed
        self.rect = self.image.get_rect()
        self.rect.left, self.rect.top = location

    def move(self):
        self.rect = self.rect.move(self.speed)
        if self.rect.left < 0 or self.rect.left > screen.get_width() - self.image.get_width():
            self.speed[0] = -self.speed[0]
        if self.rect.top < 0 or self.rect.top > screen.get_height() - self.image.get_height():
            self.speed[1] = -self.speed[1]


pygame.init()
screen = pygame.display.set_mode([640, 480])
my_time_picture = TextPicture([1, 1], [50, 50])


while 1:
    screen.fill([255, 255, 255])
    my_time_picture.move()
    screen.blit(my_time_picture.image, my_time_picture.rect)
    pygame.display.flip()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

我正在设计一个可以在屏幕上移动的时钟.但是我的代码现在可以做的是显示一个不变的时间.我想点击并计算时间.我的不变时钟图片

I am designing a clock which can move in the screen. But what my code can do now is to show a invariant time. I want to clicking and count the time. My invariable clock picture

我想让我的时钟更漂亮,但不知道如何做.如果有人可以提出任何建议,我将不胜感激.

And I want to make my clock more beautiful, but don't know how. I will be super grateful if anyone can make any suggestions.

推荐答案

您可以使用 pygame.time.set_timer() 生成滴答事件",在事件处理循环中遇到时会更新时钟的图像.

You could do it by using pygame.time.set_timer() to make "tick events" be generated which cause the clock's image to be updated when encountered in the event processing loop.

为了更轻松地实现这一点,可以将 update() 方法添加到 DigitalClock 类(这是我重命名您的通用 TextPicture 类> class) 只更新图像,但不保留当前位置:

To make implementing this easier, an update() method could be added to the DigitalClock class (which is what I renamed your generic TextPicture class) which only updates the image, but leaving the current location alone:

import datetime
import sys
import time
import pygame

class DigitalClock(pygame.sprite.Sprite):
    def __init__(self, speed, location):
        pygame.sprite.Sprite.__init__(self)
        self.speed = speed
        self.font = pygame.font.Font(None, 40)
        self.rect = pygame.Rect(location, (0, 0))  # placeholder
        self.update()

    def update(self):
        location = self.rect.left, self.rect.top  # save position
        time_text = datetime.datetime.now().strftime("%H:%M:%S")
        self.image = self.font.render(time_text, 1, [0, 0, 0])
        self.rect = self.image.get_rect()
        self.rect.left, self.rect.top = location  # restore position

    def move(self):
        self.rect = self.rect.move(self.speed)
        if (self.rect.left < 0
            or self.rect.left > screen.get_width()-self.image.get_width()):
            self.speed[0] = -self.speed[0]
        if (self.rect.top < 0
            or self.rect.top > screen.get_height()-self.image.get_height()):
            self.speed[1] = -self.speed[1]

在此之后,您需要将处理修改为以下几行:

Following that, you'd need to modify the processing to be something along these lines:

pygame.init()
framerate_clock = pygame.time.Clock()
screen = pygame.display.set_mode([640, 480])
my_digital_clock = DigitalClock([1, 1], [50, 50])
TICK_EVENT = pygame.USEREVENT + 1
pygame.time.set_timer(TICK_EVENT, 1000)  # periodically create TICK_EVENT

while True:
    for event in pygame.event.get():
        if event.type == TICK_EVENT:
            my_digital_clock.update()  # call new method
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    screen.fill([255, 255, 255])
    my_digital_clock.move()
    screen.blit(my_digital_clock.image, my_digital_clock.rect)

    framerate_clock.tick(60)  # limit framerate
    pygame.display.flip()

您可以使用不同的字体和颜色使其更美观.一般来说,任何使它看起来更逼真的东西都会是一种改进.让数字字符之间的冒号闪烁(使用类似的技术)可能很酷.

You could make it more beautiful by using a different font and color. Generally anything that made it look more realistic would be an improvement. It might be cool to make the colons between the digit characters blink on and off (using a similar technique).

这篇关于如何在 pygame 窗口中设置点击时钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆