#!/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()
我正在设计一个可以在屏幕上移动的时钟。但是我的代码现在可以做的是显示一个不变的时间。我想点击并计算时间。 我不变的时钟图片
我想让我的时钟更漂亮,但不知道怎么做。如果有人能提出任何建议,我将不胜感激。