2025-03-10 03:10:22 +08:00

71 lines
1.9 KiB
Python

import pygame
import sys
import threading
import math
import websocket
import json
import time
sin_remote = 0
th1 = None
th2 = None
moon_addr = "127.0.0.1"
def on_open(wsapp):
print("打开新连接")
print("准备待命")
message = {
"id": "comet",
"sendto": "auth",
"content": "none"
}
wsapp.send(json.dumps(message))
def on_message(wsapp, message):
global sin_remote
print(f"接收到消息: {message}")
if message == "-3":
th1.start()
sin_remote = float(message)
def on_close(wsapp):
print("on_close")
def inet():
global sin_remote
wsapp = websocket.WebSocketApp(f"ws://{moon_addr}:8765",
on_open=on_open,
on_message=on_message,
on_close=on_close)
wsapp.run_forever()
def grap():
pygame.init()
width, height = 628, 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("TEST")
LOCAL_COLOR = (255, 255, 255, 64)
REMOTE_COLOR = [(0, 0, 255, 128), (255, 0, 0, 128)]
frequency = 0.01 # 波动频率
amplitude = 100 # 波动幅度
x = round(time.time()*1000)
while True:
x = round(time.time()*1000)
x = x % 628
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
#pygame.draw.rect(screen, (0, 0, 0), (x, 0, 5, height)) # 扫描线
y = height // 2 + amplitude * math.sin((x * frequency))
y_remote = height // 2 + amplitude * sin_remote
pygame.draw.rect(screen, (round(time.time()*1000+120)%256,round(time.time()*1000)%256,round(time.time()*1000+60)%256), (x, int(y_remote), 4, 4)) # 远程波形
pygame.draw.rect(screen, LOCAL_COLOR, (x, int(y), 2, 2)) # 本地波形
pygame.display.flip()
pygame.time.delay(10)
if __name__ == "__main__":
th1 = threading.Thread(target=grap)
th2 = threading.Thread(target=inet)
# 启动线程
th2.start()