python表白神器,這是屬於程式設計師的浪漫
阿新 • • 發佈:2020-12-19
本文的文字及圖片來源於網路,僅供學習、交流使用,不具有任何商業用途,版權歸原作者所有,如有問題請及時聯絡我們以作處理
一、前言
七夕來臨,送給程式設計師一個變白神器,這是屬於我們程式設計師的浪漫。
二、所用到得庫
import pygame import random import sys
三、調整介面大小
# 根據背景圖大小,設定遊戲螢幕大小 WIDTH, HEIGHT = 1024, 576 # 不全屏 screen = pygame.display.set_mode((WIDTH, HEIGHT), 0, 32) # 全屏 # screen = pygame.display.set_mode((WIDTH, HEIGHT), pygame.FULLSCREEN, 32)pygame.display.set_caption('小姐姐,你的快遞到了。')
四、新增介面資訊及座標:
# 新增文字資訊 def title(text, screen, scale, color=(0, 0, 0)): font = pygame.font.SysFont('SimHei', 27) textRender = font.render(text, True, color) # 初始化文字的座標 screen.blit(textRender, (WIDTH / scale[0], HEIGHT / scale[1])) # 按鈕 defbutton(text, x, y, w, h, color, screen): pygame.draw.rect(screen, color, (x, y, w, h)) font = pygame.font.SysFont('SimHei', 20) textRender = font.render(text, True, (255, 255, 255)) textRect = textRender.get_rect() textRect.center = ((x+w/2), (y+h/2)) screen.blit(textRender, textRect)# 生成隨機的位置座標 def get_random_pos(): x, y = random.randint(10, 600), random.randint(20, 500) return x, y
五、完整程式碼:
# 點選答應按鈕後顯示的頁面 def show_like_interface(screen): screen.fill((255, 255, 255)) background1 = pygame.image.load('2.png').convert() screen.blit(background1, (0, 0)) pygame.display.update() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() def main(): pygame.init() clock = pygame.time.Clock() # 新增背景音樂 pygame.mixer.music.load('手寫的從前-周杰倫.mp3') pygame.mixer.music.play(-1, 20) pygame.mixer.music.set_volume(0.5) # 設定不同意按鈕屬性 unlike_pos_x = 130 unlike_pos_y = 375 unlike_pos_width = 450 unlike_pos_height = 55 unlike_color = (115, 76, 243) # 設定同意按鈕屬性 like_pos_x = 130 like_pos_y = 280 like_pos_width = 450 like_pos_height = 55 like_color = (115, 76, 243) running = True while running: # 填充視窗 screen.fill((255, 255, 255)) # 新增背景圖 background = pygame.image.load('1.png').convert() screen.blit(background, (0, 0)) # 獲取滑鼠座標 pos = pygame.mouse.get_pos() # 判斷滑鼠位置,不同意時,按鈕不斷變化 if pos[0] < unlike_pos_x + unlike_pos_width + 5 and pos[0] > unlike_pos_x - 5 and pos[1] < unlike_pos_y + unlike_pos_height + 5 and pos[1] > unlike_pos_y - 5: while True: unlike_pos_x, unlike_pos_y = get_random_pos() if pos[0] < unlike_pos_x + unlike_pos_width + 5 and pos[0] > unlike_pos_x - 5 and pos[1] < unlike_pos_y + unlike_pos_height + 5 and pos[1] > unlike_pos_y - 5: continue break # 設定標題及按鈕文字資訊 title('1.如果有一天我向你表白,你會怎麼樣?', screen, scale=[8, 3]) button('A.你小子終於開竅了,你敢表白我就敢答應!', like_pos_x, like_pos_y, like_pos_width, like_pos_height, like_color, screen) button('B.我拿你當閨蜜,你居然想睡我!果斷同意!', unlike_pos_x, unlike_pos_y, unlike_pos_width, unlike_pos_height, unlike_color, screen) # 設定關閉選項屬性 for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() # 當滑鼠點選同意按鈕後,跳轉結束頁面 if pos[0] < like_pos_x + like_pos_width + 5 and pos[0] > like_pos_x - 5 and pos[1] < like_pos_y + like_pos_height + 5 and pos[1] > like_pos_y - 5: if event.type == pygame.MOUSEBUTTONDOWN: show_like_interface(screen) pygame.display.flip() pygame.display.update() clock.tick(60) main()