Show me the power 2022-04-25 19:11 采纳率: 100%
浏览 63
已结题

Pygame 空格触发计时器

我最近在学习pygame,准备写一个小游戏,游戏内容大概是:两个人,p1操作q键, p2操作p键,相应的人按下按键,就开始计时最后再按一下相应的按键,停止计时,最后看谁按下相应按键的间隔最接近10秒
import pygame as pg
import sys

# Initialization
pg.init()
pg.font.init()
# Variables
scr_width = 800
scr_height = 700
scr_color = (230, 230, 230)
# Get font from the "Fonts" folder
inkfree = pg.font.Font('C:\\Windows\\Fonts\\tt1255m_.ttf', 80)
# Create the main screen
screen = pg.display.set_mode((scr_width, scr_height))
pg.display.set_caption("COUNT TO TEN")
# Defined the clock
clock = pg.time.Clock()
# Defined font
text = inkfree.render("COUNT TO TEN", True, (10, 10, 10))

# Game loop
while True:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            pg.quit()
            sys.exit()

    screen.blit(text, (62, 50))
    pg.display.flip()
    screen.fill(scr_color)
    pg.display.update()
    clock.tick(60)

我看了看网站里的其他博客,因为本人刚开始学习,都看不懂,请朋友们给点简单的方法谢谢
  • 写回答

1条回答 默认 最新

  • chuifengde 2022-04-25 20:20
    关注
    导入time库
    你参考一下,下面接你程序 ,按k键表示时间间隔
    text = inkfree.render("COUNT TO TEN", True, (10, 10, 10))
    screen.fill(scr_color)
    # Game loop
    pre = [[0, 0], [0, 0]]
    while True:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                pg.quit()
                sys.exit()
            elif event.type == pg.KEYDOWN:
                if event.key == pg.K_k:                
                    if pre[0][0] == 0:
                        pre[0][0] = time.time()
                    else:
                        pre[0][1] = time.time()
                        print(pre, pre[0][1] - pre[0][0])
        screen.blit(text, (62, 50))
        pg.display.flip()
        
        pg.display.update()
        clock.tick(60)
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 5月4日
  • 已采纳回答 4月26日
  • 创建了问题 4月25日

悬赏问题

  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择