pygame写一个黑客帝国屏保

代码:

#coding=utf-8

import os,sys,re,time
import pygame
import random
from win32api import GetSystemMetrics
from tkinter import messagebox

#pyinstaller -F -w pygame_heike.py

pygame.init()
pygame.display.set_caption("黑客帝国屏幕保护")

percent = 1
screen_width = GetSystemMetrics(0)
screen_height = GetSystemMetrics(1)
window_width = int(screen_width*percent)
window_height = int(screen_height*percent)

dt = 0
clock = pygame.time.Clock()

screen = pygame.display.set_mode((window_width, window_height))

#停止处理输入法事件
pygame.key.stop_text_input()

font_path = os.path.join(os.path.dirname(sys.argv[0]), 'simsun.ttc')
fonts = []
fontSizes = []
fontLineStarts = []
fontSpends = []
for i in range(0, 500):
    font_size = int((500-i)*0.03)
    font = pygame.font.Font(font_path, font_size)
    
    fontSizes.append(font_size)
    fontLineStarts.append(0)
    fontSpends.append(random.randint(1,7))
    fonts.append(font)
    
def read_directory(path):
    resfiles = []
    for root, dirs, files in os.walk(path):
        for file in files:
            if not file.endswith(".php"):
                continue
            full_path = os.path.join(root, file)
            full_path = full_path.replace("\\", "/")
            (linenum, neirong) = read_file(full_path)
            if linenum > 500:
                resfiles.append(neirong)
    
    #print(resfiles)
    #sys.exit(0)
    return resfiles
            
def read_file(path):
    file = open(path, 'r', encoding='utf-8')
    neirong = file.read()
    file.close()
    
    filelines = [i for i in neirong.split("\n") if i.strip()]
    linenum = len(filelines)
    neirong = "\n".join(filelines)
    
    return linenum, neirong


neirongs = read_directory("D:/workspace/workspace_company/oldoa-test/vendor")
neirong = neirongs[random.randint(0, len(neirongs)-1)]
#print(neirong)
#sys.exit(0)

maxline = 100
filelines = [i for i in neirong.split("\n")]
start = random.randint(0, len(filelines)-16)
lines = filelines[start:start+maxline]

runNum = 0
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
            
    keys_pressed = pygame.key.get_pressed()
    
    #ESC键
    if keys_pressed[pygame.K_ESCAPE]:
        running = False
        
    if runNum % 200 == 0:
        pass
        #start = random.randint(0, len(filelines)-16)
        #lines = filelines[start:]
        
    screen.fill("black")
    
    for i,line in enumerate(lines):
        fields = [f for f in line]
        starty = int(fontLineStarts[i])
        fontLineStarts[i] -= fontSpends[i]*dt

        if starty < 0:
            starty = len(fields)-1
        
        if fontLineStarts[i] < 0:
            fontLineStarts[i] = len(fields)-1
        
        fields = fields[starty:] + fields[:starty]
        
        r = 0
        g = 70
        b = 0
        for j,field in enumerate(fields):
            color = (r, g, b)
            font = fonts[j]
            font_size = fontSizes[j]
            rect_text = font.render(field, 1, color)
            screen.blit(rect_text, (i*font_size*2, j*font_size))
            r += 1
            g += 2
            b += 3
            
            if r > 255:
                r = 0
            if g > 255:
                g = 0
            if b > 255:
                b = 0
                
    runNum += 1
        
    #更新显示
    pygame.display.flip()
    #pygame.display.update()
    
    dt = clock.tick(60) / 600
    
pygame.quit()

  

效果:

 

pyinstaller -F -w pygame_heike.py

先将代码编译成exe,然后将exe后缀的程序改成scr(pygame_heike.exe->pygame_heike.scr)

再将文件pygame_heike.scr 和字体文件 复制到C:\Windows\System32下

鼠标移到桌面,右击,选择个性化,屏幕保护程序,选择pygame_heike。

 

posted @ 2024-10-12 10:13  河北大学-徐小波  阅读(16)  评论(0编辑  收藏  举报