python pygame--倒计时

import pygame,sys,time,datetime

class decTime(object):
   #将秒转化为时分秒 def __init__(self,totalTime): self.sec = totalTime self.hour = int(self.sec / 3600) self.sec = self.sec % 3600 self.minute = int(self.sec / 60) self.sec = int(self.sec % 60) #时间减 def subTime(self): if self.sec > 0: self.sec -= 1 else: if self.minute > 0: self.minute -= 1 self.sec = 59 else: if self.hour > 0: self.hour -= 1 self.minute = 59 self.sec = 59 else: return 0 if __name__=='__main__': pygame.init()   #窗口宽,高 size = width,height = 600,100 screen = pygame.display.set_mode(size)   #title pygame.display.set_caption("倒计时……")   #背景色 bg = (255,255,255)   #字体 my_font = pygame.font.Font(None,40)   #行高 line_height = my_font.get_linesize() position = 0 screen.fill(bg)   strtime = '2017-7-19 00:00:00' now = datetime.datetime.now() deadline = datetime.datetime.strptime(strtime,'%Y-%m-%d %H:%M:%S') subtime = (deadline - now).seconds temp = decTime(subtime) while (temp.hour>0) or (temp.minute>0) or (temp.sec>0): screen.fill(bg) ch = str(temp.hour)+':'+str(temp.minute)+':'+str(temp.sec) screen.blit(my_font.render(ch,True,(0,255,0)),(0,position)) temp.subTime() time.sleep(1)      #休眠1秒刷新屏幕 pygame.display.flip()
效果图:

转载于:https://www.cnblogs.com/evidd/p/7202630.html

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的Python Pygame打地鼠图片计时游戏代码,其中包含了计时器、得分计算和随机出现的地鼠图片: ```python import pygame import random # 初始化 Pygame pygame.init() # 游戏窗口大小 WINDOW_WIDTH = 800 WINDOW_HEIGHT = 600 # 设置游戏窗口 game_window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT)) pygame.display.set_caption("打地鼠") # 载入地鼠图片 mouse_image = pygame.image.load("mouse.png") # 游戏开始时间 start_time = 0 # 游戏得分 score = 0 # 地鼠出现的时间间隔(单位:毫秒) mouse_interval = 1000 # 游戏时钟 game_clock = pygame.time.Clock() # 游戏结束标志 game_over = False # 游戏主循环 while not game_over: # 处理事件 for event in pygame.event.get(): if event.type == pygame.QUIT: game_over = True # 处理鼠标点击事件 if event.type == pygame.MOUSEBUTTONDOWN: mouse_x, mouse_y = pygame.mouse.get_pos() # 判断是否点击到地鼠 if mouse_rect.collidepoint(mouse_x, mouse_y): score += 1 # 绘制背景 game_window.fill((255, 255, 255)) # 绘制计时器 current_time = pygame.time.get_ticks() if start_time == 0: start_time = current_time else: elapsed_time = current_time - start_time time_left = max(0, 30 - elapsed_time // 1000) font = pygame.font.SysFont(None, 30) text = font.render("Time: " + str(time_left), True, (0, 0, 0)) game_window.blit(text, (10, 10)) if time_left == 0: game_over = True # 绘制得分 font = pygame.font.SysFont(None, 30) text = font.render("Score: " + str(score), True, (0, 0, 0)) game_window.blit(text, (WINDOW_WIDTH - 100, 10)) # 绘制地鼠 current_time = pygame.time.get_ticks() if current_time - mouse_last_time >= mouse_interval: mouse_x = random.randint(0, WINDOW_WIDTH - mouse_rect.width) mouse_y = random.randint(0, WINDOW_HEIGHT - mouse_rect.height) mouse_rect = mouse_image.get_rect().move(mouse_x, mouse_y) mouse_last_time = current_time game_window.blit(mouse_image, mouse_rect) # 更新屏幕 pygame.display.update() # 控制游戏帧率 game_clock.tick(60) # 游戏结束 pygame.quit() ``` 需要注意的是,此代码中需要提前准备好“mouse.png”地鼠图片。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值