pygame写一个黑客帝国屏保
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | #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() |
效果:
1 | pyinstaller - F - w pygame_heike.py |
先将代码编译成exe,然后将exe后缀的程序改成scr(pygame_heike.exe->pygame_heike.scr)
再将文件pygame_heike.scr 和字体文件 复制到C:\Windows\System32下
鼠标移到桌面,右击,选择个性化,屏幕保护程序,选择pygame_heike。
本文来自博客园,作者:河北大学-徐小波,转载请注明原文链接:https://www.cnblogs.com/xuxiaobo/p/18459957

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步