- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在python中慢慢画一条线,这样画的 Action 实际上是肉眼可见的。
我试着把它放在一个循环中并每次都增加距离,但我从来没有成功过。事情是什么都不会出现 3 秒,然后整条线会出现,这与我想要完成的相反。我没有成功使用 pygame.display.delay()
功能。唯一有效的方法是将 clock.tick 设置为一些非常糟糕的值,例如 clock.tick(300000)
但这只会使整个程序变得非常滞后。
def draw_red_line(i):
y = 0
while y < 300:
pygame.draw.line(screen, RED, (i*100+50, 0), (i*100+50, y))
y+=0.01
最佳答案
在这种情况下使用 sleep 不是一个好主意,因为它会减慢整个线程(这是单线程模型中的整个程序)。
最好保留有关线路的某种状态信息,并根据实时时间(例如:经过的毫秒数)逐秒推进线路的“增长”。
这意味着需要将线分成段,最小的线段是单个像素。使用 Midpoint Line Algorithm , 是确定一条线上所有像素的有效方法。一旦确定了所有“线部分”,就可以根据耗时简单地更新线的终点。
这是我之前写的一些代码,给定一对点,返回一个像素列表。midpoint.py
:
def __plotLineLow( x0,y0, x1,y1 ):
points = []
dx = x1 - x0
dy = y1 - y0
yi = 1
if dy < 0:
yi = -1
dy = -dy
D = 2*dy - dx
y = y0
for x in range( x0, x1 ):
points.append( (x,y) )
if D > 0:
y = y + yi
D = D - 2*dx
D = D + 2*dy
return points
def __plotLineHigh( x0,y0, x1,y1 ):
points = []
dx = x1 - x0
dy = y1 - y0
xi = 1
if dx < 0:
xi = -1
dx = -dx
D = 2*dx - dy
x = x0
for y in range( y0, y1 ):
points.append( (x,y) )
if D > 0:
x = x + xi
D = D - 2*dy
D = D + 2*dx
return points
def linePoints( pointA, pointB ):
""" Generate a list of integer points on the line pointA -> pointB """
x0, y0 = pointA
x1, y1 = pointB
points = []
if ( abs(y1 - y0) < abs(x1 - x0) ):
if ( x0 > x1 ):
points += __plotLineLow( x1, y1, x0, y0 )
else:
points += __plotLineLow( x0, y0, x1, y1 )
else:
if ( y0 > y1 ):
points += __plotLineHigh( x1, y1, x0, y0 )
else:
points += __plotLineHigh( x0, y0, x1, y1 )
return points
if __name__ == "__main__":
#midPoint( (597, 337), (553, 337) )
print( str( linePoints( (135, 295), (135, 304) ) ) )
SlowLine
的演示代码类(class)。
import pygame
import random
import time
import sys
from midpoint import linePoints # Midpoint line algorithm
# Window size
WINDOW_WIDTH = 400
WINDOW_HEIGHT = 400
SKY_BLUE = ( 30, 30, 30)
SKY_RED = (200, 212, 14)
# Global millisecond count since start
NOW_MS = 0
class SlowLine():
def __init__( self, pixels_per_second, x0,y0, x1,y1, colour=SKY_RED ):
self.points = linePoints( ( x0, y0 ), ( x1, y1 ) )
self.pixel_count = len( self.points )
self.speed = pixels_per_second
self.start_point = self.points[0] # start with a single-pixel line
self.end_point = self.points[0]
self.pixel_cursor = 0 # The current end-pixel
self.last_update = 0 # Last time we updated
self.colour = colour
self.fully_drawn = False
def update(self):
global NOW_MS
if ( self.fully_drawn == True ):
# nothing to do
pass
else:
# How many milliseconds since the last update() call?
if ( self.last_update == 0 ):
self.last_update = NOW_MS
time_delta = 0
else:
time_delta = NOW_MS - self.last_update
self.last_udpate = NOW_MS
# New pixels to add => speed * time
new_pixel_count = time_delta * self.speed / 1000 # this may loose precision with very small speeds
if ( new_pixel_count + self.pixel_cursor > self.pixel_count ):
# We're out of pixels
self.end_point = self.points[-1]
self.full_drawn = True
else:
# Grow the line by <new_pixel_count> pixels
self.pixel_cursor += new_pixel_count
self.end_point = self.points[ int( self.pixel_cursor ) ]
def draw( self, screen ):
pygame.draw.line( screen, self.colour, self.start_point, self.end_point )
### MAIN
pygame.init()
SURFACE = pygame.HWSURFACE|pygame.DOUBLEBUF|pygame.RESIZABLE
WINDOW = pygame.display.set_mode( ( WINDOW_WIDTH, WINDOW_HEIGHT ), SURFACE )
pygame.display.set_caption("Slow Line Movement")
# Create some random lines
lines = []
for i in range( 20 ):
rand_speed = random.randint( 1, 50 )
rand_x0 = random.randint( 0, WINDOW_WIDTH )
rand_y0 = random.randint( 0, WINDOW_HEIGHT )
rand_x1 = random.randint( 0, WINDOW_WIDTH )
rand_y1 = random.randint( 0, WINDOW_HEIGHT )
lines.append( SlowLine( rand_speed, rand_x0, rand_y0, rand_x1, rand_y1 ) )
# Main event loop
clock = pygame.time.Clock()
done = False
while not done:
NOW_MS = pygame.time.get_ticks()
# Update the line lengths
for l in lines:
l.update()
# Handle user-input
for event in pygame.event.get():
if ( event.type == pygame.QUIT ):
done = True
# Movement keys
keys = pygame.key.get_pressed()
if ( keys[pygame.K_UP] ):
print("up")
elif ( keys[pygame.K_DOWN] ):
print("down")
elif ( keys[pygame.K_LEFT] ):
print("left")
elif ( keys[pygame.K_RIGHT] ):
print("right")
elif ( keys[pygame.K_q] and ( keys[pygame.K_RCTRL] or keys[pygame.K_LCTRL] ) ):
print("^Q")
done = True
# Update the window, but not more than 60fps
WINDOW.fill( SKY_BLUE )
for l in lines:
l.draw( WINDOW )
pygame.display.flip()
# Clamp FPS
clock.tick_busy_loop(60)
pygame.quit()
关于python-3.x - 如何在Python中慢慢画一条线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57618029/
我想以 headless 模式(屏幕上根本没有 GUI)将 JPanel 绘制到 BufferedImage 中。 final JPanel panel = createPanel(); panel.
我是 Canvas 的新手,正在尝试创建看起来逼真的 float 粒子动画。 目前,我正在创建 400 个随机散布在 400x400 Canvas 上的粒子。 然后,在每个 requestAnimat
有没有办法在悬停时停止悬 float 画? :hover 这是一个显示动画的链接: https://codepen.io/youbiteme/pen/RprPrN 最佳答案 只需为您的 svg 悬停添
我想在谷歌地图上绘制覆盖图,其中除了特定点周围 1.5 公里半径以外的所有内容都被遮蔽了。为此,我尝试使用带有大量边框的圆圈,所以我会在边框中放置透明中心和覆盖颜色来实现这一点,但它无法渲染。
我正在尝试通过扩展类 UIView 来创建自定义 View ,该类可以在自定义 View 的中心显示一个圆圈。为了添加自定义绘图,我重写了 draw(_ rect: CGRect) 方法,如下所示。
我是一名优秀的程序员,十分优秀!