import pygame
import psutil
import sys
def get_system_performance():
cpu_percent = psutil.cpu_percent(interval=1) # 获取CPU使用率
memory_info = psutil.virtual_memory() # 获取内存使用信息
memory_percent = memory_info.percent # 提取百分比使用率
return cpu_percent, memory_percent # 返回CPU和内存的使用率
def main():
pygame.init() # 初始化所pygame
size = (400, 300) # 定义窗口大小为400x300像素
screen = pygame.display.set_mode(size) # 创建一个显示窗口并设置尺寸
pygame.display.set_caption("系统性能可视化工具") # 设置窗口标题
clock = pygame.time.Clock() # 创建一个时钟对象,用于控制帧率
x, y = size[0] // 2, size[1] // 2 # 初始化圆心位置在窗口中心
while True:
for event in pygame.event.get(): # 遍历所有事件
if event.type == pygame.QUIT: # 如果用户点击关闭按钮
pygame.quit() # 退出
sys.exit() # 退出程序
# 获取系统性能信息
cpu_percent, memory_percent = get_system_performance() # 调用函数获取当前CPU和内存使用率
# 根据CPU和内存使用率分别调整速度
speed_x = int(cpu_percent / 20 + 1) # 使用CPU使用率调整水平速度
speed_y = int(memory_percent / 20 + 1) # 使用内存使用率调整垂直速度
# 更新位置
x += speed_x # 更新圆心的x坐标
y += speed_y # 更新圆心的y坐标
# 边界检测,如果圆超出窗口边界,则反向移动
if x >= size[0] or x <= 0:
speed_x = -speed_x
if y >= size[1] or y <= 0:
speed_y = -speed_y
screen.fill((0, 0, 0)) # 填充屏幕背景色为黑色
pygame.draw.circle(screen, (0, 255, 0), (x, y), 20) # 在新位置绘制绿色圆圈
pygame.display.flip() # 更新整个显示表面到屏幕上
clock.tick(60) # 控制帧率为每秒60帧
if __name__ == '__main__':
main() # 程序启动时调用main函数执行主程序逻辑
上一篇:Python AES128解密器