如何停止一秒钟,并根据第二个

时间:2018-02-05 21:20:20

标签: python pygame

我正在尝试为我的朋友和我做一个点击测试“游戏”。不过,我对下一步做什么感到有点困惑。到目前为止,这是我的代码:

import os, sys, math, pygame, pygame.mixer, time
from pygame.locals import *

black = (0,0,0)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
white = (255,255,255)
click = 0
time = 0
timer = pygame.time.get_ticks()
run_me = True

screen_size = screen_Width, sceen_height = 600, 400
screen = pygame.display.set_mode(screen_size)
pygame.display.set_caption('click speed test')


Fps = 60
fps_clock = pygame.time.Clock()

while run_me:

    for event in pygame.event.get():
        if event.type == pygame.MOUSEBUTTONDOWN:
            time = timer + 1*1000
        if time = 1
            for event in pygame.event.get():
                if event.type == pygame.MOUSEBUTTONDOWN:
                    clicks + 1

我想向用户显示他在一秒钟内设置了多少次点击。我该怎么做?

谢谢你>。<

2 个答案:

答案 0 :(得分:2)

要打印多少次点击,让我们看看这个如何在屏幕上显示文字的惊人答案(答案:Bartlomiej Lewandowsk,原始答案:link

  

您可以在其上创建包含文字的表面。为此,请看一下   简短的例子:

pygame.font.init() # you have to call this at the start, 
                   # if you want to use this module.
myfont = pygame.font.SysFont('Comic Sans MS', 30)
     

这将创建一个新对象,您可以在其上调用render方法。

     

textsurface = myfont.render('Some Text', False, (0, 0, 0))

     

这会创建一个新的曲面,其上已经绘制了文本。最后你   只需将文本表面blit到主屏幕上即可。

screen.blit(textsurface,(0,0)) 
     

请记住,每次文本更改时,您都必须重新创建表面>再次,看到新的   文本。

我修改了你的代码,看看,我将在下面解释

import  sys, pygame
from pygame.locals import *

#colors
black = (0,0,0)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
white = (255,255,255)
#vars
clicks = 0
run_me = True
#init
pygame.init()
pygame.font.init()
#display init
screen_size = screen_Width, sceen_height = 600, 400
screen = pygame.display.set_mode(screen_size)
pygame.display.set_caption('click speed test')
myfont = pygame.font.SysFont('Comic Sans MS', 30)


Fps = 60
clock = pygame.time.Clock()
first_click = False
frst_time = 0
nxt_time = 0

while run_me:

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

        if event.type == pygame.MOUSEBUTTONDOWN:
            if first_click == False:
                frst_time = clock.tick_busy_loop() / 1000
                first_click = True
                clicks += 1
            elif(first_click == True):
                if nxt_time < 5:
                    clicks += 1
    if first_click == True and nxt_time < 5:
        nxt_time += clock.tick_busy_loop()/1000

    textsurface = myfont.render(str(clicks), False,black)
    textsurface2 = myfont.render(str(nxt_time), False, black)
    screen.fill(white)
    screen.blit(textsurface,(0,0))
    screen.blit(textsurface2,(0,100))

    pygame.display.update()

首先,我创建了一个名为first_click的变量,以便在玩家第一次点击时启动时钟。请记住,第一个tick()可以从pygame.init()中获取时间。

其次,nxt_time < 5:此声明仅在低于5时才会使点击次数增加(在您的情况下,它将为1)

第三,tick()仅获取每个滴答之间的间隔时间。这不会累积时间。所以,要把时间加起来。我做了变量nxt_time。此变量保存时间值。从第一次点击开始的时间。

BTW:tick_busy_loop比仅仅勾选更准确。但它使用更多的CPU。

最后

  1. 永远不要遗漏pygame.init()
  2. 永远不要遗漏pygame.display.update()
  3. 永远不要遗漏quit action

答案 1 :(得分:1)

我使用import pygame as pg pg.init() screen = pg.display.set_mode((600, 400)) fps_clock = pg.time.Clock() FPS = 60 GRAY = pg.Color('gray12') WHITE = pg.Color('white') FONT = pg.font.Font(None, 42) clicks = 0 start_time = 0 passed_time = 0 run_me = True while run_me: for event in pg.event.get(): if event.type == pg.QUIT: run_me = False elif event.type == pg.MOUSEBUTTONDOWN: if event.button == 1: # Left mouse button. # Start the timer if it's stopped. if start_time == 0: start_time = pg.time.get_ticks() if passed_time < 1: # Count the clicks. clicks += 1 # Press the right mouse button to reset the timer and clicks. elif event.button == 3: start_time = 0 passed_time = 0 clicks = 0 if passed_time < 1 and start_time != 0: # Calculate the passed time. / 1000 to convert it to seconds. passed_time = (pg.time.get_ticks() - start_time) / 1000 time_surface = FONT.render('Time: {}'.format(passed_time), True, WHITE) clicks_surface = FONT.render('Clicks: {}'.format(clicks), True, WHITE) screen.fill(GRAY) screen.blit(time_surface, (30, 30)) screen.blit(clicks_surface, (30, 70)) pg.display.flip() fps_clock.tick(FPS) pg.quit() 来计算自第一次点击以来的通过时间(查看评论)。

SLId    DL1Id   DL2Id   Debit   Credit  CurId ExchangeRate  Cnt    
------------------------------------------------------------------------------
S1        D1     D4     2000      0       1      1000        2  
S1        D1     D4       0     6000      1      1500        4  
S1        D1     D4     6000      0       1      1200        5  
S2        D2     D4     4000      0       2      1000        4  
S2        D2     D4        0    2000      2      1000        2  
S2        D2     D4     3000      0       2      1500        2