# -*- coding: utf-8 -*-
"""
Created on Thu Jan 13 21:52:53 2022
@author: shiyu
"""
import sys
import pygame
import time
from random import randint
import os
def text_rect(size,text,wordcolor,bgcolor,pos):
f = pygame.font.Font('C:/Windows/Fonts/SimHei.ttf',size)
text = f.render(text,True,wordcolor,bgcolor)
textRect =text.get_rect()
textRect.center = pos
return [text,textRect]
def get_resource_path(relative_path):
if getattr(sys,'frozen',False):
base_path=sys._MEIPASS
else:
base_path=os.path.abspath('.')
return os.path.join(base_path,relative_path)
p1_path=get_resource_path(os.path.join('resources','p1.png'))
p2_path=get_resource_path(os.path.join('resources','p2.png'))
pygame.init()
screen = pygame.display.set_mode((700,400))
pygame.display.set_caption('来自一位喜欢你的小哥哥')
f1 = text_rect(30,"小姐姐我观察你很久了",(0,0,0),(255,255,255),(160,100))
f2 = text_rect(20,"算了吧",(0,0,0),(230,230,230),(660,370))
f3 = text_rect(30,"做我女朋友好不好?",(0,0,0),(255,255,255),(145,150))
f4 = text_rect(20,"好啊",(0,0,0),(230,230,230),(30,370))
p1 = pygame.image.load(p1_path).convert_alpha()
p2 = pygame.image.load(p2_path).convert_alpha()
pygame.display.set_icon(p2)
while True:
screen.fill((255,255,255))
screen.blit(p1, (450,100))
screen.blit(*f1)
screen.blit(*f3)
screen.blit(*f2)
screen.blit(*f4)
x,y=pygame.mouse.get_pos()
if f2[1].left<x<f2[1].right and f2[1].top<y<f2[1].bottom:
p=-f2[1].left
q=700-f2[1].right
if p>q:
t=p;p=q;q=t
x_pos=randint(p,q)
m=380-f2[1].bottom
n=-f2[1].top
if m>n:
t=m;m=n;n=t
y_pos=randint(m,n)
f2[1]=f2[1].move(x_pos, y_pos)
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN and f4[1].left<x<f4[1].\
right and f4[1].top<y<f4[1].bottom:
pygame.quit()
sys.exit()
# if event.type == pygame.QUIT:
# pygame.quit()
# sys.exit()
time.sleep(0.01)
pygame.display.flip()
打包参考
https://blog.csdn.net/qq_42972591/article/details/122483721