如何使上一行在python中消失? [英] How can you make the previous line disappear in python?

查看:192
本文介绍了如何使上一行在python中消失?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个从(0,0)开始的星座.前两行必须在两秒钟的延迟后消失,并且在两秒钟的延迟内单击左键时,应该会出现白色圆​​圈.我不知道为什么我的计时器不工作,我不确定如何使这条线消失.也没有出现圆圈.这是我的代码

I'm making a constellation that starts from (0,0). The previous line has to disappear after a two second delay and when the left is clicked within the two second delay, white circles should appear. I don't know why my timer isn't working and I'm not sure how to make the line disappear. Also the circles don't appear. This is my code

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)
RED = (255, 0, 0)
BLUE = (0,0,255)
WHITE = (255,255,255)
colors = (RED,GREEN,BLUE)
time.set_timer(USEREVENT, 2000)
mx = 0
my = 0

def drawScene(screen, button):
 if button == 1:
  draw.circle(screen,RED,(mx,my), 5)
  draw.line(screen,RED,(mx,my),(lx,ly),2)
  draw.circle(screen,RED,(lx,ly), 5)
  display.flip()
 if button == 3:
  draw.line(screen,random.choice(colors),(mx,my),(lx,ly),2)
  draw.circle(screen,random.choice(colors),(lx,ly), 5)
  display.flip()


running = True
myClock = time.Clock()
start = time.get_ticks()
# 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
   cx,cy = mouse.get_pos()
   draw.circle(screen,WHITE,(cx,cy),5)
  if evnt.type == USEREVENT:
   my_event = event.Event(USEREVENT)
   time.set_timer(my_event , 2000)   
 drawScene(screen, button)

 myClock.tick(60)                     # waits long enough to have 60 fps



quit()

推荐答案

我希望这就是您想要的,并且注释清晰.

I hope this is what you were wanting and the comments are clear.

from pygame import * 
import random
init()
size = width, height = 700, 700
screen = display.set_mode(size)

BLACK  = (0,       0,    0)
RED    = (255,     0,    0)
GREEN  = (0,     255,    0)
BLUE   = (0,       0,  255)
WHITE  = (255,   255,  255)
colors = (RED, GREEN, BLUE)
time.set_timer(USEREVENT, 2000)
mx = 0
my = 0

def drawScene(screen, button, prev_point, new_point):
  if button == 1:
    draw.circle(screen, RED,   prev_point, 5)
    draw.circle(screen, WHITE, new_point,  5)
    draw.line  (screen, RED,   prev_point, new_point, 2)
    display.flip()
  if button == 3:
    draw.line  (screen, random.choice(colors), prev_point, new_point,2)
    draw.circle(screen, random.choice(colors), new_point, 5)
    display.flip()


running = True
myClock = time.Clock()
prev_render = time.get_ticks() #time in ticks that last render occured
prev_point = (mx, my) #where previous line ended


#function for when to re draw the scene
#ternary if. if override == True, then return true. Else return if time since last update > 2secs
rerender = lambda time_since_update, override: (time_since_update>2000, True)[override]

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

    if evnt.type == MOUSEBUTTONDOWN:
      override = True #force window to rerender
      new_point = evnt.pos #(mx, my)
      button = evnt.button

    #rerender window only if necessary. (personal preference)
    updated_at = time.get_ticks()
    dt = updated_at-prev_render #time difference
    if(rerender(dt, override)):
      screen.fill(BLACK)
      drawScene(screen, button, prev_point, new_point)
      prev_point = new_point
      prev_render = updated_at

    display.update()
    myClock.tick(60)                     # waits long enough to have 60 fps



quit()

这篇关于如何使上一行在python中消失?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆