问题描述
我想在这段代码中,在你画一条新线后,旧的线会在2秒后消失,在这2秒内,你不能再画线了.我很困惑为什么我的代码现在不起作用.基本上,我试图使用 tick 和一个变量来告诉我 2 秒过去了,但出于某种原因,它可以做任何事情.
from pygame import *在里面()大小 = 宽度,高度 = 650, 650屏幕 = display.set_mode(size)白色 = (255,255,255)黑色 = (0, 0, 0)红色 = (255, 100, 94)蓝色 = (112, 219, 255)绿色 = (138, 255, 142)黄色 = (255, 255, 158)颜色 = 绿色运行 = 真myClock = time.Clock()prevprev_pos = 无prev_pos = 无cur_pos = 无开始时间 = 无# 游戏循环在跑步的时候:对于 event.get() 中的 e:如果 e.type == 退出:运行 = 错误如果 e.type == MOUSEBUTTONDOWN:如果 e.button == 1:if cur_pos != None and prev_pos == None: #if 点击一次而不是两次start_time = time.get_ticks()prevprev_pos = prev_posprev_pos = cur_poscur_pos = e.posif prev_pos == None: #如果没有点击两次cur_pos = e.pos如果 e.button == 3:如果颜色 == 绿色:颜色 = 蓝色elif 颜色 == 蓝色:颜色 = 红色elif 颜色 == 红色:颜色 = 绿色if prev_pos != None: #当点击两次时,画线如果 time.get_ticks() - start_time
它只检查你点击时的 2 秒,你需要它检查每一帧.将 if prev_pos != None:
移出事件循环并将 e.pos
更改为 cur_pos
似乎几乎得到了您的追求.在这里和那里添加一点之后,我想我得到了你想要的
from pygame import *在里面()大小 = 宽度,高度 = 650, 650屏幕 = display.set_mode(size)黑色 = (0, 0, 0)红色 = (255, 100, 94)蓝色 = (112, 219, 255)绿色 = (138, 255, 142)黄色 = (255, 255, 158)颜色 = 绿色运行 = 真myClock = time.Clock()prev_pos = 无cur_pos = 无开始时间 = 无# 游戏循环在跑步的时候:对于 event.get() 中的 e:如果 e.type == 退出:运行 = 错误如果 e.type == MOUSEBUTTONDOWN:如果 e.button == 1:if cur_pos != None and prev_pos == None: #if 点击一次而不是两次start_time = time.get_ticks()prev_pos = cur_pos打印(开始")cur_pos = e.posif prev_pos == None: #如果没有点击两次cur_pos = e.pos如果 e.button == 3:如果颜色 == 绿色:颜色 = 蓝色elif 颜色 == 蓝色:颜色 = 红色elif 颜色 == 红色:颜色 = 绿色if prev_pos != None: #当点击两次时,画线如果 time.get_ticks() - start_time
如果 prev_pos != None 和 prev_prev_pos == None:start_time = time.get_ticks()prev_pos = cur_pos打印(开始")cur_pos = e.pos如果 cur_pos != None 和 prev_pos == None:prev_pos = cur_pos如果 prev__prev_pos == 无:cur_pos = e.pos
和
如果 prev_prev_pos != 无:如果 time.get_ticks() - start_timefrom pygame import *在里面()大小 = 宽度,高度 = 650, 650屏幕 = display.set_mode(size)黑色 = (0, 0, 0)红色 = (255, 100, 94)蓝色 = (112, 219, 255)绿色 = (138, 255, 142)黄色 = (255, 255, 158)颜色 = 绿色运行 = 真myClock = time.Clock()开始时间 = 无点数 = []次 = []最大行数 = 2# 游戏循环在跑步的时候:screen.fill(BLACK) #清屏对于 event.get() 中的 e:如果 e.type == 退出:运行 = 错误如果 e.type == MOUSEBUTTONDOWN:如果 e.button == 1:如果 len(points) 0: #如果列表中已经有一个点,则全部移动并添加新的位置开始时间.追加(时间.get_ticks())点.附加(点[-1])对于范围内的 i(len(points)-2,-1,-1):点[i] = 点[i - 1]点[0] = mouse.get_pos()else: #else 追加第一点点数.追加(鼠标.get_pos())如果 e.button == 3:如果颜色 == 绿色:颜色 = 蓝色elif 颜色 == 蓝色:颜色 = 红色elif 颜色 == 红色:颜色 = 绿色for i in range(len(points)-1,-1,-1): #for each point in pointsdraw.circle(屏幕,颜色,点[i],5)如果我<len(points) - 1: #after 第二个点,画线draw.line(screen,color,points[i],points[i+1])如果 time.get_ticks() - times[i - 1] >2000: #check 秒del points[i + 1] #删除最后一个点del times[i - 1] #删除时间display.flip()myClock.tick(60)放弃()
I would like to make it so that in this code, after you draw a new line, then the old one will disappear after 2 seconds, and during these 2 seconds, you cannot draw any more lines. I am confused as to why my code right now is not working. Basically, I am trying to use tick and a variable to tell me when 2 seconds have elapsed, but for some reason, it does do anything.
from pygame import *
init()
size = width, height = 650, 650
screen = display.set_mode(size)
WHITE = (255,255,255)
BLACK = (0, 0, 0)
RED = (255, 100, 94)
BLUE = (112, 219, 255)
GREEN = (138, 255, 142)
YELLOW = (255, 255, 158)
color = GREEN
running = True
myClock = time.Clock()
prevprev_pos = None
prev_pos = None
cur_pos = None
start_time = None
# Game Loop
while running:
for e in event.get():
if e.type == QUIT:
running = False
if e.type == MOUSEBUTTONDOWN:
if e.button == 1:
if cur_pos != None and prev_pos == None: #if clicked once and not twice
start_time = time.get_ticks()
prevprev_pos = prev_pos
prev_pos = cur_pos
cur_pos = e.pos
if prev_pos == None: #if not clicked twice
cur_pos = e.pos
if e.button == 3:
if color == GREEN:
color = BLUE
elif color == BLUE:
color = RED
elif color == RED:
color = GREEN
if prev_pos != None: #when clicked twice, draw line
if time.get_ticks() - start_time <= 2000:
screen.fill(BLACK)
draw.line(screen, color, prevprev_pos, prev_pos)
draw.line(screen, color, prev_pos, cur_pos)
draw.circle(screen, color, prev_pos, 5)
else: #after 2 seconds, reset pos of clicks, and clear screen
prev_pos = None
cur_pos = None
screen.fill(BLACK)
if cur_pos != None: #if clicked once, draw the circle
draw.circle(screen, color, cur_pos, 5)
display.flip()
myClock.tick(60)
quit()
It is only checking for the 2 seconds when you click, you need it to check every frame. Moving the if prev_pos != None:
out of the event loop and changing e.pos
to cur_pos
seemed to almost get what you were after. After adding a bit here and there, i think i got what you were after
from pygame import *
init()
size = width, height = 650, 650
screen = display.set_mode(size)
BLACK = (0, 0, 0)
RED = (255, 100, 94)
BLUE = (112, 219, 255)
GREEN = (138, 255, 142)
YELLOW = (255, 255, 158)
color = GREEN
running = True
myClock = time.Clock()
prev_pos = None
cur_pos = None
start_time = None
# Game Loop
while running:
for e in event.get():
if e.type == QUIT:
running = False
if e.type == MOUSEBUTTONDOWN:
if e.button == 1:
if cur_pos != None and prev_pos == None: #if clicked once and not twice
start_time = time.get_ticks()
prev_pos = cur_pos
print("start")
cur_pos = e.pos
if prev_pos == None: #if not clicked twice
cur_pos = e.pos
if e.button == 3:
if color == GREEN:
color = BLUE
elif color == BLUE:
color = RED
elif color == RED:
color = GREEN
if prev_pos != None: #when clicked twice, draw line
if time.get_ticks() - start_time <= 2000:
screen.fill(BLACK)
draw.line(screen, color, prev_pos, cur_pos)
draw.circle(screen, color, prev_pos, 5)
else: #after 2 seconds, reset pos of clicks, and clear screen
prev_pos = None
cur_pos = None
screen.fill(BLACK)
if cur_pos != None: #if clicked once, draw the circle
draw.circle(screen, color, cur_pos, 5)
display.flip()
myClock.tick(60)
quit()
if prev_pos != None and prev_prev_pos == None:
start_time = time.get_ticks()
prev_pos = cur_pos
print("start")
cur_pos = e.pos
if cur_pos != None and prev_pos == None:
prev_pos = cur_pos
if prev__prev_pos == None:
cur_pos = e.pos
and
if prev_prev_pos != None:
if time.get_ticks() - start_time <= 2000:
screen.fill(BLACK)
draw.line(screen, color, prev_pos, cur_pos)
draw.circle(screen, color, prev_pos, 5)
draw.circle(...)
else:
prev_pos = None
prev_prev_pos = None
cur_pos = None
screen.fill(BLACK)
from pygame import *
init()
size = width, height = 650, 650
screen = display.set_mode(size)
BLACK = (0, 0, 0)
RED = (255, 100, 94)
BLUE = (112, 219, 255)
GREEN = (138, 255, 142)
YELLOW = (255, 255, 158)
color = GREEN
running = True
myClock = time.Clock()
start_time = None
points = []
times = []
max_lines = 2
# Game Loop
while running:
screen.fill(BLACK) #clear the screen
for e in event.get():
if e.type == QUIT:
running = False
if e.type == MOUSEBUTTONDOWN:
if e.button == 1:
if len(points) < max_lines + 1: #if not max lines on screen
if len(points) > 0: #if there is already a point in list, move all across and add new pos to start
times.append(time.get_ticks())
points.append(points[-1])
for i in range(len(points)-2,-1,-1):
points[i] = points[i - 1]
points[0] = mouse.get_pos()
else: #else append first point
points.append(mouse.get_pos())
if e.button == 3:
if color == GREEN:
color = BLUE
elif color == BLUE:
color = RED
elif color == RED:
color = GREEN
for i in range(len(points)-1,-1,-1): #for every point in points
draw.circle(screen,color,points[i],5)
if i < len(points) - 1: #after second point, draw lines
draw.line(screen,color,points[i],points[i+1])
if time.get_ticks() - times[i - 1] > 2000: #check for seconds
del points[i + 1] #delete last point
del times[i - 1] #delete the time
display.flip()
myClock.tick(60)
quit()
这篇关于如何隐藏pygame中的特定点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!