[O] Fix stutters
This commit is contained in:
+35
-21
@@ -37,6 +37,8 @@ COLORS = [RGB.from_hex(v) for v in {
|
|||||||
# Snow fall data structure
|
# Snow fall data structure
|
||||||
@dataclass
|
@dataclass
|
||||||
class SnowParticle:
|
class SnowParticle:
|
||||||
|
last_x: int # last rendered x position (pixels)
|
||||||
|
last_y: int # last rendered y position
|
||||||
x: float # x position
|
x: float # x position
|
||||||
y: float # y position
|
y: float # y position
|
||||||
xv: float # x velocity in pixels per second
|
xv: float # x velocity in pixels per second
|
||||||
@@ -66,6 +68,9 @@ async def shell(reader: TelnetReaderUnicode, writer: TelnetWriterUnicode):
|
|||||||
|
|
||||||
snow = []
|
snow = []
|
||||||
|
|
||||||
|
# Sort snow particles by y position
|
||||||
|
snow.sort(key=lambda p: p.y)
|
||||||
|
|
||||||
for _ in range(count):
|
for _ in range(count):
|
||||||
# Generate random x and y position
|
# Generate random x and y position
|
||||||
x = random.randint(0, width)
|
x = random.randint(0, width)
|
||||||
@@ -77,38 +82,48 @@ async def shell(reader: TelnetReaderUnicode, writer: TelnetWriterUnicode):
|
|||||||
# Generate random color
|
# Generate random color
|
||||||
color = random.choice(COLORS)
|
color = random.choice(COLORS)
|
||||||
|
|
||||||
snow.append(SnowParticle(x, y, xv, yv, color))
|
snow.append(SnowParticle(round(x), round(y), x, y, xv, yv, color))
|
||||||
|
|
||||||
return snow
|
return snow
|
||||||
|
|
||||||
# Update snow particles
|
# Update snow particles
|
||||||
def update_snow(dt: float):
|
def update_snow(dt: float):
|
||||||
|
log.info(f'Updating snow particles: {dt}')
|
||||||
nonlocal snow
|
nonlocal snow
|
||||||
# Erase snow particles
|
|
||||||
for particle in snow:
|
buf = ""
|
||||||
# Erase snow particle
|
|
||||||
writer.write(f'\x1b[{particle.y};{particle.x}H\x1b[0m ')
|
|
||||||
|
|
||||||
# Update snow particles
|
# Update snow particles
|
||||||
for particle in snow:
|
for p in snow:
|
||||||
# Update position
|
# Update position
|
||||||
particle.x += particle.xv * dt
|
p.x += p.xv * dt
|
||||||
particle.y += particle.yv * dt
|
p.y += p.yv * dt
|
||||||
|
|
||||||
# Wrap around the screen
|
# Wrap around the screen
|
||||||
if particle.x >= width:
|
if p.x >= width:
|
||||||
particle.x = 0
|
p.x = 1
|
||||||
elif particle.x < 0:
|
elif p.x < 1:
|
||||||
particle.x = width - 1
|
p.x = width - 1
|
||||||
|
|
||||||
if particle.y >= height:
|
if p.y >= height:
|
||||||
particle.y = 0
|
p.y = 1
|
||||||
particle.xv, particle.yv = rand_velocity()
|
p.xv, p.yv = rand_velocity()
|
||||||
|
|
||||||
# Draw snow particle
|
# Check if position changed
|
||||||
writer.write(f'\x1b[{round(particle.y)};{round(particle.x)}H'
|
if round(p.y) != p.last_y:
|
||||||
f'{particle.color.to_ansi_rgb()}*'
|
# Erase old position
|
||||||
f'\x1b[0m')
|
buf += f'\x1b[{p.last_y};{p.last_x}H\x1b[0m '
|
||||||
|
|
||||||
|
# Draw snow particle
|
||||||
|
buf += (f'\x1b[{round(p.y)};{round(p.x)}H'
|
||||||
|
f'{p.color.to_ansi_rgb()}*'
|
||||||
|
f'\x1b[0m')
|
||||||
|
|
||||||
|
# Update last position
|
||||||
|
p.last_x, p.last_y = round(p.x), round(p.y)
|
||||||
|
|
||||||
|
# Write buffer
|
||||||
|
writer.write(buf)
|
||||||
|
|
||||||
# Get the size of the terminal
|
# Get the size of the terminal
|
||||||
async def get_size() -> tuple[int, int]:
|
async def get_size() -> tuple[int, int]:
|
||||||
@@ -150,10 +165,9 @@ async def shell(reader: TelnetReaderUnicode, writer: TelnetWriterUnicode):
|
|||||||
dt = (now - last_update_ns) / 1e9
|
dt = (now - last_update_ns) / 1e9
|
||||||
last_update_ns = now
|
last_update_ns = now
|
||||||
|
|
||||||
clear()
|
|
||||||
# Update snow
|
# Update snow
|
||||||
update_snow(dt)
|
update_snow(dt)
|
||||||
# Move the cat
|
# Draw cat
|
||||||
print_ascii(ASCII_CAT, x, y)
|
print_ascii(ASCII_CAT, x, y)
|
||||||
# Move the cursor to the bottom
|
# Move the cursor to the bottom
|
||||||
writer.write('\x1b[9999;9999H')
|
writer.write('\x1b[9999;9999H')
|
||||||
|
|||||||
Reference in New Issue
Block a user