samedi 9 mai 2020

How to use time.get_tick()

When I left click, a line should show from the origin, and if left clicked with two seconds, white circles should appear. After two seconds, another line is drawn and then after two seconds the previous line should disappear. Right now, when I click, a line shows from the origin and then when I click again, two lines show.

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()

Aucun commentaire:

Enregistrer un commentaire