hello,亲爱的诸位,大家好,我是小羊。随着新年的临近,为了那些沐浴在单身寂寞中的绅士们,我决定施展慈悲,将我心头的珍藏——表白神器,一一呈现给大家。虽然我自己从未亲身试过,但闻所未闻的美谈中,听说此神器号称百试百灵。如今,我将这份文艺之宝分享给大家,希望能给你的生活添一份浪漫。望各位不吝赐予点赞之情~
话不多说,先看看效果图:
看代码之前,别忘了先点个关注哟~
接下来,代码展示:
import sys
import pygame
import random
WIDTH, HEIGHT = 460, 300
bg_color = (255, 255, 255)
button_text_list = ['俺比肖战帅亿点', '俺的脾气好', '俺还会做饭', '俺还很体贴']
def show_like_interface(text, screen, color=(255, 0, 0)):
screen.fill(bg_color)
font = pygame.font.Font('SimSun.ttf', WIDTH // (len(text)))
text_render = font.render(text, True, color)
text_rect = text_render.get_rect()
text_rect.midtop = (WIDTH / 2, HEIGHT / 2)
screen.blit(text_render, text_rect)
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
def button(text, x, y, w, h, color, screen):
pygame.draw.rect(screen, color, (x, y, w, h))
font = pygame.font.Font('SimSun.ttf', 20)
text_render = font.render(text, True, (255, 255, 255))
text_rect = text_render.get_rect()
text_rect.center = ((x + w / 2), (y + h / 2))
screen.blit(text_render, text_rect)
def title(text, screen, scale, color=(0, 0, 0)):
font = pygame.font.Font('SimSun.ttf', WIDTH // (len(text) * 2))
text_render = font.render(text, True, color)
text_rect = text_render.get_rect()
text_rect.midtop = (WIDTH / scale[0], HEIGHT / scale[1])
screen.blit(text_render, text_rect)
def get_random_pos():
x, y = random.randint(20, WIDTH - 20), random.randint(20, HEIGHT - 20)
return x, y
def main():
text = "不行"
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("小羊的表白")
unlike_pos_x, unlike_pos_y = get_random_pos()
like_pos_x, like_pos_y = get_random_pos()
like_color = (216, 191, 216)
running = True
while running:
screen.fill(bg_color)
img = pygame.image.load("R-C.png")
img_rect = img.get_rect()
img_rect.midtop = 80, 10
screen.blit(img, img_rect)
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
mouse_pos = pygame.mouse.get_pos()
if mouse_pos[0] < like_pos_x + 100 and mouse_pos[0] > like_pos_x and mouse_pos[1] < like_pos_y + 50 and mouse_pos[1] > like_pos_y:
like_color = bg_color
running = False
mouse_pos = pygame.mouse.get_pos()
if mouse_pos[0] < unlike_pos_x + 100 and mouse_pos[0] > unlike_pos_x and \
mouse_pos[1] < unlike_pos_y + 50 and mouse_pos[1] > unlike_pos_y:
while True:
unlike_pos_x, unlike_pos_y = get_random_pos()
text = button_text_list[random.randint(0, len(button_text_list) - 1)]
if mouse_pos[0] < unlike_pos_x + 100 and mouse_pos[0] > unlike_pos_x and \
mouse_pos[1] < unlike_pos_y + 50 and mouse_pos[1] > unlike_pos_y:
continue
break
title('小姐姐,我注意你很久啦', screen, scale=[1.8, 10], color=(0, 0, 255))
title('做我女朋友好不好呀', screen, scale=[1.8, 3], color=(0, 0, 255))
button('好呢', like_pos_x, like_pos_y, 100, 50, like_color, screen)
button(text, unlike_pos_x, unlike_pos_y, 100, 50, (216, 191, 216), screen)
pygame.display.flip()
pygame.display.update()
pygame.time.Clock().tick(60)
show_like_interface('嘻嘻,俺好开心呀~', screen, color=(0, 0, 0))
if __name__ == '__main__':
main()
要记住一定要在同一文件夹下带有字体的文件包,如下: