教你做python跨年表白神器 點贊收藏后,快去表白!!!
hello,大家好,我是Dream,馬上就跨年了,為了廣大的單身男性成員,我就慈悲一下,把我的存貨–表白神器拿出來了,百試百靈(雖然我一次也沒試過),今天分享給大家,別忘了給我點贊喲~
話不多說,先看效果圖:

從圖上看,很明顯這是一個選擇題,但代碼的神奇之處就在這里,當她把滑鼠拖到‘不行’的地方時,奇跡發生了,當當當~



螢屏上會輪番展示出你的優點,這是我的優點(我只是實話實說的喲)
最最最重要的是她關不掉視窗,重要的事說三遍:關不掉 關不掉 關不掉 就是關不掉!!!氣死她哈哈哈,,,(你好賤,我好愛)
她只能選擇好的,然后…(你懂得嘿嘿嘿)

說了折磨多,你們是不是非常期待我的代碼呀,看代碼之前,別忘了先點個關注喲~
接下來,代碼展示:
# sys是python的標準庫
# 提供了python運行時環境變數的操控
# sys.exit()用于結束游戲退出
import sys
import pygame
import random
# 游戲的高寬
WIDTH, HEIGHT = 640, 360
# 把顏色值(230, 230, 230)賦值給 bg_color 變數
# 三個整數依次是三原色中紅色、綠色和藍色的濃度值,濃度值是一個整數,最大為255,最小為0,
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('./font/simkai.ttf', WIDTH // (len(text)))
textRender = font.render(text, True, color)
textRect = textRender.get_rect()
textRect.midtop = (WIDTH / 2, HEIGHT / 2)
screen.blit(textRender, textRect)
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('./font/simkai.ttf', 20)
textRender = font.render(text, True, (0, 0, 0))
textRect = textRender.get_rect()
textRect.center = ((x + w / 2), (y + h / 2))
screen.blit(textRender, textRect)
# 標題
def title(text, screen, scale, color=(0, 0, 0)):
# pygame.font.Font("字體","字號",*)
font = pygame.font.Font('./font/simkai.ttf', WIDTH // (len(text) * 2))
# 使用已有的文本創建一個位圖image,回傳值為一個image;對于位圖可用get_height(),get_width()的方法獲得高與寬;True表示是否抗鋸齒,第三個為字體顏色,還可以有第四個為背景色,沒有時就為默認的透明;
textRender = font.render(text, True, color)
# Rect物件有一些重要的屬性,如:top,botton,letf、right表示上下左右
# width,height表示寬高 我有這些值之后,對于我們撰寫程式十分方便
textRect = textRender.get_rect()
# 中央x坐標整數值 頂部y坐標的整數值
textRect.midtop = (WIDTH / scale[0], HEIGHT / scale[1])
# 將位圖繪制到螢屏上,screen為建立的主屏;
screen.blit(textRender, textRect)
# 生成隨機的位置坐標
def get_random_pos():
x, y = random.randint(20, WIDTH - 20), random.randint(20, HEIGHT - 20)
return x, y
def main():
text = "不行"
# 在我們要動手用它完成我們的想法之前,電腦這個強迫癥需要我們檢查一遍,這個工具包是否完整,能否正常給我們提供幫助,而這個檢查的動作, pygame.init() 檢查,電腦上一些需要的硬體呼叫介面、基礎功能是否有問題,如果有,他會在程式運行之前就反饋給你,方便你進行排查和規避,
# 對pygame內部各種功能進行初始化創建及變數設定,比如pygmae里面的表單,鍵盤的使用的事件佇列,等等都需要我們pygame.init()初始化
pygame.init()
# 呼叫 display 模塊的 set_mode 函式,作用是初始化螢屏物件(也即視窗物件),此處傳入一個引數,即(640, 360)元組,這使得視窗的解析度是640*360
screen = pygame.display.set_mode((WIDTH, HEIGHT))
# 視窗標題
pygame.display.set_caption("小徐的表白")
# 不喜歡按鈕的初始位置和大小
unlike_pos_x = 330
unlike_pos_y = 250
unlike_pos_width = 100
unlike_pos_height = 50
# 喜歡按鈕的初始位置和大小
like_pos_x = 180
like_pos_y = 250
like_pos_width = 100
like_pos_height = 50
# 標識位,作為小姐姐之后點擊了同意后退出的標準
running = True
# 按鈕顏色
like_color = (216, 191, 216)
while running:
# 填充螢屏背景色
# 顯示視窗背景填充bg_color眼神
screen.fill(bg_color)
# 加載圖片,從檔案加載新圖片
img = pygame.image.load("./imgs/3.jpg")
# Surface物件與影像時一一對應關系
# 簡單理解在pygame里匯入的任何圖片都是Surface物件
# pygame使用內部定義的Surface物件表示所有載入的影像,其中get_rect()反法回傳一個覆寫影像的矩形Rect物件
# Rect物件有一些重要的屬性,如:top,botton,letf、right表示上下左右
# width,height表示寬高 我有這些值之后,對于我們撰寫程式十分方便
imgRect = img.get_rect()
# 圖片位置
# 中央x坐標整數值 頂部y坐標的整數值
imgRect.midtop = 80, 10
# 將一個影像繪制在一個影像上,及將img繪制在imgRect位置上,通過Rect物件上引導對圖片的繪制
screen.blit(img, imgRect)
# 監聽事件
# pygame.event.get() 的作用是獲取事件串列,事件串列內包含0個或多個事件物件 (點擊 滑鼠移動 關閉視窗)
# 依次賦值給 event 變數
for event in pygame.event.get():
# 檢測到滑鼠
if event.type == pygame.MOUSEBUTTONDOWN:
# 獲取滑鼠位置
mouse_pos = pygame.mouse.get_pos()
# 若點擊了喜歡按鈕,停止 while 回圈
if mouse_pos[0] < like_pos_x + like_pos_width and mouse_pos[0] > like_pos_x and mouse_pos[
1] < like_pos_y + like_pos_height 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 + unlike_pos_width and mouse_pos[0] > unlike_pos_x and \
mouse_pos[1] < unlike_pos_y + unlike_pos_height 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 + unlike_pos_width and mouse_pos[0] > unlike_pos_x and \
mouse_pos[1] < unlike_pos_y + unlike_pos_height and mouse_pos[1] > unlike_pos_y:
continue
break
title('寶貝,我觀察你很久了', screen, scale=[1.8, 10])
title('做我女朋友好不好呀', screen, scale=[1.8, 3])
button('好的', like_pos_x, like_pos_y, like_pos_width,
like_pos_height, like_color, screen)
button(text, unlike_pos_x, unlike_pos_y, unlike_pos_width,
unlike_pos_height, (216, 191, 216), screen)
# 顯示游戲
# 重繪螢屏,以使最近的繪制操作生效,
pygame.display.flip()
# 對視窗進行更新
pygame.display.update()
# 創建Clock物件,用于操作時間
# tick(60)控制幀速度,即視窗重繪速度,每秒鐘60次幀重繪,視頻中每次展示的靜態影像稱為幀
pygame.time.Clock().tick(60)
show_like_interface('我就知道小姐姐你也喜歡我~', screen, color=(0, 0, 0))
# 表示程式的主入口,所以以后為了避免該檔案被外部檔案呼叫,一般建議加上
if __name__ == '__main__':
main()
當然在運行這個代碼之前,你還需要在同一路徑下裝上這個楷體語言包
還有這張圖片喲~

祝你表白成功,在新的一年里可以和自己喜歡的人在一起!
如果你喜歡的話,就不要吝惜你的一鍵三連了~
謝謝大家!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/242942.html
標籤:其他
上一篇:完全背包問題1
下一篇:用java實作的簡易貪吃蛇游戲
