首页 > 解决方案 > 如何使用 time.get_tick()

问题描述

当我左键单击时,应该从原点显示一条线,如果左键单击两秒钟,应该出现白色圆​​圈。两秒钟后,绘制另一条线,然后两秒钟后,前一条线应消失。现在,当我单击时,从原点显示一条线,然后当我再次单击时,显示两条线。

from pygame import *
import random

init()
size = width, height = 700, 700
screen = display.set_mode(size)
button = 0

BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
RED = (255, 0, 0)
WHITE = (255, 255, 255)
color = RED

mx = 0
my = 0
cx = 0
cy = 0


def drawScene(screen, button):
  if button == 1:
    draw.circle(screen, RED, (mx,my), 5)
    draw.line(screen, color, (mx,my),(lx,ly), 2)
    cx = lx
    cy = ly   
    draw.circle(screen, RED, (mx,my), 5)

    display.flip()
  if button == 3:
    draw.line(screen, color, (mx,my),(lx,ly), 2)
    draw.circle(screen, color, (lx,ly), 5)

    display.flip()


running = True
myClock = time.Clock()


# Game Loop
while running:
  lx = mx
  ly = my


  for evnt in event.get():             # checks all events that happen
    if evnt.type == QUIT:
      running = False

    if evnt.type == MOUSEBUTTONDOWN:
      mx,my = evnt.pos
      button = evnt.button

      if time.get_ticks() <= 2000 and time.get_ticks() > 0 :
        draw.circle(screen, WHITE, (mx,my), 5)
      else:
        draw.line(screen, WHITE, (cx,cy),(lx,ly), 2)

      if button == 3:
        if  color == RED:
          color = BLUE
        elif color == BLUE:
          color = GREEN
        elif color == GREEN:
          color = RED


  drawScene(screen, button)

  myClock.tick(60) 

quit()

标签: pythonloopsif-statementpygame

解决方案


让我们先从设置一个基本窗口开始

from pygame import *
import random

init()
size = width, height = 700, 700
screen = display.set_mode(size)
button = 0

BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
RED = (255, 0, 0)
WHITE = (255, 255, 255)
color = RED
running = True
myClock = time.Clock()


def drawScene():
    screen.fill(BLACK)
    display.update() #update is the same as flip()

# Game Loop
while running:
    myClock.tick(60)
    drawScene()


    for evnt in event.get():             # checks all events that happen
        if evnt.type == QUIT:
            running = False

在帮助其他人解决这个问题(我链接的问题)之后,我发现使用列表是最好的主意

因此,让我们为圆圈所在的点和线连接的点创建一个列表

points = [] #put this before the drawScene function so it can reference it

现在,当用户点击时,我们要将点添加到列表中并在屏幕上绘制

def drawScene():
    screen.fill(BLACK)

    for point in points:
        draw.circle(screen,color,point,5)

    display.update() #update is the same as flip()
if evnt.type == MOUSEBUTTONDOWN:
    points.append(evnt.pos)

现在我们可以通过点击在屏幕上创建点。让我们创建线条

我们想从原点开始,所以在开始时将原点添加到列表中

points = [(0,0)]

现在我们要遍历每个点并在它们之间画一个圆和一条线。所以将循环更改为

for i in range(1,len(points)): #loop through all but the first point as you dont want circle at origin
        draw.circle(screen,WHITE,points[i],5)
        draw.line(screen,color,points[i - 1],points[i])

现在我们可以画线和圆了。但是如果我们在 2 秒内单击,我们只想绘制圆圈,所以让我们创建一个计时器,我们将为每一行制作一个,所以让我们列出一个列表,因为每行在 2 秒后消失

timers = []

一旦我们点击,我们想要启动计时器,我们可以这样做

if evnt.type == MOUSEBUTTONDOWN:
    points.append(evnt.pos)
    timers.append(time.get_ticks())        

这将获取当前时间(以毫秒为单位),一旦它是 2 秒,我们就想知道,所以如果我们得到当前时间并减去开始时间,我们就会得到差异,所以

for i in range(len(timers)-1,-1,-1):
    if time.get_ticks() - timers[i] > 2000: #if been 2 seconds
        del timers[i]  #delete the timer
        del points[i + 1] #delete the point for the line

这在 2 秒后变得非常接近新线移动到原点,要解决这个问题,我们需要删除原点

del points[i]

但现在线条消失后,它从最后点击的地方开始,而不是从原点开始。所以让我们检查是否没有线,确保原点是第一个点

if len(points) == 1:
    points[0] = (0,0)

太棒了,最后一件事是2秒内出现的圆圈,这也意味着超过1行所以

if points[0] != (0,0) or len(points) > 2:
    draw.circle(screen,WHITE,points[i],5)

这是完整的代码:

from pygame import *
import random

init()
size = width, height = 700, 700
screen = display.set_mode(size)
button = 0

BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
RED = (255, 0, 0)
WHITE = (255, 255, 255)
color = RED
running = True
myClock = time.Clock()

points = [(0,0)]
timers = []

def drawScene():
    screen.fill(BLACK)

    for i in range(1,len(points)): #loop through all but the first point as you dont want circle at origin
        if points[0] != (0,0) or len(points) > 2:
            draw.circle(screen,WHITE,points[i],5)
        draw.line(screen,color,points[i - 1],points[i])

    for i in range(len(timers)-1,-1,-1):
        if time.get_ticks() - timers[i] > 2000: #if been 2 seconds
            del timers[i]  #delete the timer
            del points[i] #delete the point for the line

    if len(points) == 1:
        points[0] = (0,0)

    display.update() #update is the same as flip()

# Game Loop
while running:
    myClock.tick(60)
    drawScene()


    for evnt in event.get():             # checks all events that happen
        if evnt.type == QUIT:
            running = False
        if evnt.type == MOUSEBUTTONDOWN:
            points.append(evnt.pos)
            timers.append(time.get_ticks())


推荐阅读