[+] Track last update time
This commit is contained in:
+30
-25
@@ -1,6 +1,7 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import os
|
import os
|
||||||
import random
|
import random
|
||||||
|
import time
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from typing import NamedTuple
|
from typing import NamedTuple
|
||||||
|
|
||||||
@@ -35,10 +36,10 @@ COLORS = [RGB.from_hex(v) for v in {
|
|||||||
# Snow fall data structure
|
# Snow fall data structure
|
||||||
@dataclass
|
@dataclass
|
||||||
class SnowParticle:
|
class SnowParticle:
|
||||||
x: int # x position
|
x: float # x position
|
||||||
y: int # y position
|
y: float # y position
|
||||||
xv: int # x velocity
|
xv: float # x velocity in pixels per second
|
||||||
yv: int # y velocity
|
yv: float # y velocity in pixels per second
|
||||||
color: RGB # color
|
color: RGB # color
|
||||||
|
|
||||||
|
|
||||||
@@ -51,6 +52,7 @@ async def shell(reader: TelnetReaderUnicode, writer: TelnetWriterUnicode):
|
|||||||
x: int
|
x: int
|
||||||
y: int
|
y: int
|
||||||
snow: list[SnowParticle]
|
snow: list[SnowParticle]
|
||||||
|
last_update_ns: int
|
||||||
|
|
||||||
# Create snow particles
|
# Create snow particles
|
||||||
def create_snow(count: int | None) -> list[SnowParticle]:
|
def create_snow(count: int | None) -> list[SnowParticle]:
|
||||||
@@ -100,7 +102,9 @@ async def shell(reader: TelnetReaderUnicode, writer: TelnetWriterUnicode):
|
|||||||
particle.y = 0
|
particle.y = 0
|
||||||
|
|
||||||
# Draw snow particle
|
# Draw snow particle
|
||||||
writer.write(f'\x1b[{particle.y};{particle.x}H\x1b[38;2;{particle.color.r};{particle.color.g};{particle.color.b}m*')
|
writer.write(f'\x1b[{int(particle.y)};{int(particle.x)}H'
|
||||||
|
f'{particle.color.to_ansi_rgb()}*'
|
||||||
|
f'\x1b[0m')
|
||||||
|
|
||||||
# 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]:
|
||||||
@@ -129,24 +133,6 @@ async def shell(reader: TelnetReaderUnicode, writer: TelnetWriterUnicode):
|
|||||||
writer.write('\x1b[2J')
|
writer.write('\x1b[2J')
|
||||||
writer.write('\x1b[H')
|
writer.write('\x1b[H')
|
||||||
|
|
||||||
clear()
|
|
||||||
height, width = await get_size()
|
|
||||||
log.info(f"Size: {width}x{height}")
|
|
||||||
|
|
||||||
# Position the cat at center bottom by default
|
|
||||||
x, y = (width - ASCII_WIDTH) // 2, height - ASCII_HEIGHT
|
|
||||||
|
|
||||||
# Create snow particles
|
|
||||||
snow = create_snow(100)
|
|
||||||
|
|
||||||
# Draw the cat function
|
|
||||||
def draw_cat():
|
|
||||||
log.info(f"Drawing cat at {x}, {y}")
|
|
||||||
clear()
|
|
||||||
|
|
||||||
# Draw the cat
|
|
||||||
print_ascii(ASCII_CAT, x, y)
|
|
||||||
|
|
||||||
# Move the cat along the x-axis
|
# Move the cat along the x-axis
|
||||||
async def move(delta: int):
|
async def move(delta: int):
|
||||||
nonlocal x
|
nonlocal x
|
||||||
@@ -154,10 +140,17 @@ async def shell(reader: TelnetReaderUnicode, writer: TelnetWriterUnicode):
|
|||||||
|
|
||||||
# Update frame function
|
# Update frame function
|
||||||
async def update():
|
async def update():
|
||||||
# Move the cat
|
# Calculate the time since last update in seconds
|
||||||
draw_cat()
|
nonlocal last_update_ns
|
||||||
|
now = time.time_ns()
|
||||||
|
dt = (now - last_update_ns) / 1e9
|
||||||
|
last_update_ns = now
|
||||||
|
|
||||||
|
clear()
|
||||||
# Update snow
|
# Update snow
|
||||||
update_snow()
|
update_snow()
|
||||||
|
# Move the cat
|
||||||
|
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')
|
||||||
# Flush the output
|
# Flush the output
|
||||||
@@ -196,6 +189,18 @@ async def shell(reader: TelnetReaderUnicode, writer: TelnetWriterUnicode):
|
|||||||
if inp and await on_input(inp):
|
if inp and await on_input(inp):
|
||||||
return
|
return
|
||||||
|
|
||||||
|
# Initialize the shell
|
||||||
|
clear()
|
||||||
|
height, width = await get_size()
|
||||||
|
last_update_ns = time.time_ns()
|
||||||
|
log.info(f"Size: {width}x{height}")
|
||||||
|
|
||||||
|
# Position the cat at center bottom by default
|
||||||
|
x, y = (width - ASCII_WIDTH) // 2, height - ASCII_HEIGHT
|
||||||
|
|
||||||
|
# Create snow particles
|
||||||
|
snow = create_snow(100)
|
||||||
|
|
||||||
# Run listen and update in parallel
|
# Run listen and update in parallel
|
||||||
await asyncio.gather(listen_input(), listen_update())
|
await asyncio.gather(listen_input(), listen_update())
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user