用代码传递温暖,用技术点亮梦想
在高考备考的紧张氛围中,我们开发了一款充满正能量的屏保程序。这款屏保不仅能在电脑闲置时提供视觉美感,更重要的是,它展示了一系列激励人心的话语,为考生加油鼓劲。本文将详细介绍这款屏保的技术实现和情感设计。
这款屏保程序基于 Python 的 PyQt5 库开发,它提供了强大的 GUI 功能和图形渲染能力。整个程序采用面向对象的设计,核心是一个继承自 QWidget 的 Screensaver 类。
程序的主要组成部分包括:
import sys
import random
import time
from PyQt5.QtWidgets import QApplication, QWidget, QLabel
from PyQt5.QtCore import Qt, QTimer, QPoint
from PyQt5.QtGui import QFont, QColor, QPainter
class Screensaver(QWidget):
def __init__(self):
super().__init__()
# 先初始化所有参数属性
self.max_trails = 15
self.move_speed = 2 # 移动速度(像素/帧)
self.fade_speed = 1.5 # 淡出速度控制
self.positions = [] # 存储所有文本的位置、颜色和内容
self.time_stamps = [] # 存储每个文本的创建时间
self.directions = [] # 存储每个文本的移动方向
# 精心挑选的励志文本列表
self.texts = [
"你考的不是试,是前途和暮年的欢喜",
"关关难过关关过,前路漫漫亦灿灿",
"愿你合上笔盖的刹那,有侠客收剑入鞘的骄傲",
"笔锋所指处,皆是心之所向,愿合笔时如收刀入鞘般骄傲!",
"十二载星月为伴,今朝试锋,定当光芒万丈",
"你的考卷终将化作通向理想大学的云梯,拾级而上终见星辰",
"此刻奋笔疾书的每个字,都是未来人生的精彩伏笔",
"乾坤未定,你我皆是奔腾向前的黑马",
"愿提笔时惊风落雨,收卷日笑看云起",
"寒窗墨香终成剑,一朝出鞘动四方",
"且将新火试新茶,少年仗剑趁年华",
"鹏北海,凤朝阳,今携书剑路茫茫",
"春风得意马蹄疾,一日看尽长安花",
"不必追求完美答卷,只需写出青春无悔",
"错的每道题都是为了遇见对的人,对的每道题都是为了遇见更好的自己",
"高考只是人生车站,从容下车后还有万里山河待你丈量",
"备好2B铅笔,也请带上百分百的勇气",
"早餐要吃好,准考证别忘带,你平稳发挥就是最棒状态",
"当交卷铃声响起,整个世界都会为你的坚持鼓掌",
"把三年青春浓缩成笔尖锋芒,刺破迷茫照亮远方",
"此刻你不仅是考生,更是手握命运改写权的英雄",
"那些熬过的夜终将化作星光,铺就你的状元之路",
"少年应有鸿鹄志,当骑骏马踏平川",
"愿九月踏入的校园,正是你此刻心驰神往的方向",
"今日考场方寸地,明日天地任尔行",
"这场考试过后,你选择的世界正在向你奔来",
"现在写下的每个答案,都在勾勒未来人生的轮廓",
"金榜题名时,勿忘与恩师共赏这漫天彩霞",
"请相信:你的long类型努力终将转化为double型成功",
"人生不是单选题,但这次请坚定选择自己的最优解",
"用三年的函数积累,求导出最灿烂的极值人生",
"当交卷铃声如约而至,便是你开启新副本的入场音效",
"这场考试的隐藏奖励是:解锁无限可能的人生DLC"
]
# 初始化UI
self.initUI()
# 设置定时器,每40毫秒更新一次画面
self.timer = QTimer(self)
self.timer.timeout.connect(self.update)
self.timer.start(40)
def initUI(self):
# 设置窗口标题和全屏显示
self.setWindowTitle("高考加油屏保")
self.showFullScreen()
# 隐藏鼠标光标
self.setCursor(Qt.BlankCursor)
# 设置背景为黑色
self.setStyleSheet("background: black;")
# 退出提示标签,显示在屏幕右下角
self.status_label = QLabel("按 ESC 退出", self)
self.status_label.setStyleSheet("color: white; font-size: 16px;")
self.status_label.adjustSize()
self.status_label.move(10, self.height() - self.status_label.height() - 10)
def paintEvent(self, event):
# 创建绘图对象
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing)
current_time = time.time()
text_metrics = painter.fontMetrics()
# 设置字体为微软雅黑,大小为20
text_font = QFont("微软雅黑", 20)
painter.setFont(text_font)
# === 运动计算 ===
# 筛选出未完全消失的文本
survived_indices = []
for idx in range(min(len(self.positions), len(self.time_stamps), len(self.directions))):
age = current_time - self.time_stamps[idx]
alpha = 255 - int(self.fade_speed * age * 30)
if alpha > 0: # 保留未消失的点
survived_indices.append(idx)
# 更新存活点数据(严格同步截断)
self.positions = [self.positions[i] for i in survived_indices][:self.max_trails]
self.directions = [self.directions[i] for i in survived_indices][:self.max_trails]
self.time_stamps = [self.time_stamps[i] for i in survived_indices][:self.max_trails]
# === 移动计算 ===
new_positions = []
new_directions = []
for idx in range(len(self.positions)):
(pos, color, text) = self.positions[idx]
dx, dy = self.directions[idx]
# 计算新坐标(带边界约束)
text_width = text_metrics.width(text)
text_height = text_metrics.height()
new_x = pos.x() + dx
new_y = pos.y() + dy
# 边界反弹处理
if new_x < 0 or new_x > (self.width() - text_width):
dx = -dx * 0.8
new_x = max(0, min(new_x, self.width() - text_width))
if new_y < text_height or new_y > (self.height() - text_height):
dy = -dy * 0.8
new_y = max(text_height, min(new_y, self.height() - text_height))
new_positions.append((QPoint(int(new_x), int(new_y)), color, text))
new_directions.append((dx, dy))
# 更新数据
self.positions = new_positions
self.directions = new_directions
# === 生成新点 ===
# 当屏幕上的文本数量少于最大值时,生成新的文本
while len(self.positions) < self.max_trails:
new_text = random.choice(self.texts)
text_width = text_metrics.width(new_text)
text_height = text_metrics.height()
# 安全坐标生成(带异常处理)
try:
safe_x = random.randint(0, self.width() - text_width)
safe_y = random.randint(text_height, self.height() - text_height)
except ValueError:
safe_x = 0
safe_y = text_height
# 随机颜色,明亮色系
self.positions.append((
QPoint(safe_x, safe_y),
QColor(
random.randint(150, 255),
random.randint(150, 255),
random.randint(150, 255)
),
new_text
))
# 随机移动方向和速度
self.directions.append((
random.uniform(-self.move_speed, self.move_speed),
random.uniform(-self.move_speed, self.move_speed)
))
self.time_stamps.append(time.time())
# === 绘制所有点 ===
# 绘制屏幕上的所有文本,根据存在时间设置透明度
for (pos, color, text), ts in zip(self.positions, self.time_stamps):
age = current_time - ts
alpha = max(0, 255 - int(self.fade_speed * age * 50))
painter.setPen(QColor(
color.red(),
color.green(),
color.blue(),
alpha
))
painter.drawText(pos, text)
def keyPressEvent(self, event):
# 按ESC键退出屏保
if event.key() == Qt.Key_Escape:
self.close()
if __name__ == "__main__":
# Windows系统内存优化
if sys.platform == 'win32':
import ctypes
ctypes.windll.kernel32.SetProcessWorkingSetSize(-1, 0x100000, 0x200000)
app = QApplication(sys.argv)
ex = Screensaver()
sys.exit(app.exec_())
在 initUI 方法中,程序设置了全屏显示、隐藏鼠标光标和黑色背景,同时在右下角添加了"按 ESC 退出"的提示标签。
主要通过 paintEvent 方法实现,这是 PyQt 中绘制界面的核心方法,程序每 40 毫秒(通过定时器设置)调用一次该方法更新画面。
文本动画效果的实现步骤包括:
通过 keyPressEvent 方法监听键盘事件,当用户按下 ESC 键时退出屏保程序。
这款屏保的特别之处在于其内容设计。我们精心挑选了 36 条高考励志语录,涵盖了对高考意义的诠释、对未来的美好期许、考场的实用建议以及轻松幽默的鼓励。这些话语不仅能在视觉上吸引考生的注意力,更能在情感上给予他们支持和动力。
程序中还包含了一些性能优化措施:
这款高考加油屏保程序结合了技术实现和情感设计,不仅是一个视觉上吸引人的动画程序,更是一个能为考生带来正能量的情感支持工具。通过简洁的代码和精心的设计,我们希望能够为高考考生创造一个更加积极、鼓励的备考环境。