```python
import pygame, sys
from pygame import *
WIDTH, HEIGHT = 640, 480
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
def hzk_offset(hz):
q, w = list(bytes(hz, encoding='gbk'))
return ((q - 161) * 94 + w - 161) * 32
def hz_buffer(hzstr):
buffers = []
f = open('/Users/shiwenxiang/PycharmProjects/12/pic/2.jpg', 'rb')
for z in hzstr:
f.seek(hzk_offset(z), 0)
buffers.append(list(f.read(32)))
f.close()
return buffers
def hz_matrix(hzstr, BOLD=0):
global tmp
buffers = hz_buffer(hzstr)
bit = [2 ** i for i in range(7, -1, -1)]
tmp = [''] * 16
if BOLD == 0:
str = '1', '0'
else:
str = '11', '00'
for n, buffer in enumerate(buffers):
for y in range(16):
for x in range(16):
if buffer[y * 2 + x // 8] & bit[x % 8]:
tmp[y] += str[0]
else:
tmp[y] += str[1]
return tmp
def draw_hz(hzstr, x, y, d=1, color=BLACK, BOLD=0):
matrix = hz_matrix(hzstr, BOLD)
for j, mat in enumerate(matrix):
for i, dot in enumerate(list(mat)):
if dot == '1':
pos_on_screen, radius = (x + i * d, y + j * d), d
draw.circle(screen, color, pos_on_screen, radius)
if __name__ == '__main__':
pygame.init()
screen = display.set_mode((WIDTH, HEIGHT), 0, 32)
display.set_caption("祝大家国庆节快乐!")
screen.fill(WHITE)
timer = pygame.time.Clock()
draw_hz('祝福伟大祖国', 80, 30)
draw_hz('更加繁荣昌盛', 80, 50)
draw_hz('我爱你,中国!', 80, 70)
draw_hz('祝福伟大祖国', 80, 120, 2, RED)
draw_hz('更加繁荣昌盛', 80, 160, 2, RED)
draw_hz('我爱你,中国!', 80, 200, 2, RED)
draw_hz('祝福伟大祖国', 80, 280, 2, RED, 1)
draw_hz('更加繁荣昌盛', 80, 330, 2, RED, 1)
draw_hz('我爱你,中国!', 80, 380, 2, RED, 1)
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
timer.tick(60)
display.update()