您好,登錄后才能下訂單哦!
這篇文章主要介紹了python如何實(shí)現(xiàn)表白神器,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
Python能夠干嘛?
可以做日常任務(wù),比如自動(dòng)備份你的MP3;
可以做網(wǎng)站,很多著名的網(wǎng)站像知乎、YouTube就是Python寫的;
可以做網(wǎng)絡(luò)游戲的后臺(tái),很多在線游戲的后臺(tái)都是Python開發(fā)的。
上面說的這些本人并沒有實(shí)現(xiàn)過;
但是我知道Python可以做一些有趣的東西,比如仿制抖音表白小軟件;
本人也是剛剛學(xué)習(xí)Python,這個(gè)腳本通過百度找到的,然后自己也重新寫了一遍,加深了映像,最主要的還是思路要清晰;
流程:
1、創(chuàng)建一個(gè)游戲屏幕
2、加載title
3、加載button,
4、當(dāng)鼠標(biāo)移動(dòng)到 '算了吧' 上面的時(shí)候 重加加載桌面并隨機(jī)生成一個(gè) '算了吧' 坐標(biāo);
5、當(dāng)鼠標(biāo)移動(dòng)到 ‘好呀'上面時(shí) 顯示不同的title
以下就是Python腳本:
import pygame import random # 設(shè)置游戲屏幕大小 這是一個(gè)常量 WIDTH, HEIGHT = 640, 480 screen = pygame.display.set_mode((WIDTH, HEIGHT), 0, 32) pygame.display.set_caption('FROM一個(gè)喜歡你很久的小哥哥') # 標(biāo)題 def title(text, screen, scale, color=(255, 0, 0)): font = pygame.font.SysFont('SimHei', WIDTH//(len(text)*2)) textRender = font.render(text, True, color) # 獲取此圖片的矩形框 # textRect = textRender.get_rect() # textRect.midtop = (WIDTH/scale[0], HEIGHT/scale[1]) # screen.blit(textRender, textRect) # 初始化文字的坐標(biāo) screen.blit(textRender, (WIDTH/scale[0], HEIGHT/scale[1])) # 按鈕 def button(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, (0, 0, 0)) textRect = textRender.get_rect() textRect.center = ((x+w/2), (y+h/2)) screen.blit(textRender, textRect) # 生成隨機(jī)的位置坐標(biāo) def get_random_pos(): x, y = random.randint(20, 620), random.randint(20, 460) return x, y # 點(diǎn)擊喜歡按鈕后顯示的頁(yè)面 def show_like_interface(text, screen, color=(255, 0, 0)): screen.fill((255, 255, 255)) font = pygame.font.SysFont('SimHei', 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() def main(): pygame.init() clock = pygame.time.Clock() unlike_pos_x = 330 unlike_pos_y = 250 unlike_pos_width = 80 unlike_pos_height = 40 unlike_color = (0, 191, 255) like_pos_x = 180 like_pos_y = 250 like_pos_width = 80 like_pos_height = 40 like_color = (0, 191, 255) running = True while running: # 填充窗口 screen.fill((255, 255, 255)) img = pygame.image.load('d:/love2.png') imgRect = img.get_rect() imgRect.midtop = int(WIDTH / 1.3), HEIGHT // 7 screen.blit(img, imgRect) # 獲取坐標(biāo) 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('小姐姐,我觀察你很久了', screen, scale=[5, 8]) title('做我女朋友好不好呀', screen, scale=[5, 4]) button('好呀', like_pos_x, like_pos_y, like_pos_width, like_pos_height, like_color, screen) button('算了吧', 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: pygame.quit() 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: show_like_interface('我就知道小姐姐你也喜歡我~', screen, color=(255, 0, 0)) pygame.display.flip() pygame.display.update() clock.tick(60) main()
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“python如何實(shí)現(xiàn)表白神器”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。