【Pygame小游戏】剧情流推荐:什么样的游戏才能获得大家的喜欢呢?(魔鬼恋人、霸总娇妻版)
前言
🚀 作者 :“程序員梨子”
🚀 **文章簡介 **:本篇文章主要利用tkinter界面化的小游戲啦!
🚀 **文章源碼獲取 **: 為了感謝每一個關注我的小可愛💓每篇文章的項目源碼都是無償分
享滴💓👇👇👇👇
點這里藍色這行字體自取,需要什么源碼記得說標題名字哈!私信我也可!
🚀 歡迎小伙伴們 點贊👍、收藏?、留言💬
?正文🐾
必備文案·甜甜的戀愛
“?a???????鹽于律己???? 甜以待人.”
你們的戀愛小程序準時上線:今天小編為大家帶來——一波甜甜的文案+劇情版的動漫風小程序。
希望大家喜歡,大家來拿源碼鴨,這款戀愛的劇情版小程序超甜的。不信的話,你來看看?!!
截圖展示——
(魔鬼戀人第一組)
運行界面:
彈窗界面:
結束界面:
點擊的第一個選擇結束頁面是如下所示,不同的選擇界面效果是不一樣的,另一個選擇的話等你們
自己玩兒試試啦~
截圖展示——
(霸總的小嬌妻)
噗~霸道總裁的梗,哈哈哈哈——這名字你們可以忽略的哈。
故事你們可以自由想象~我腦海已經有幾十個故事跟結局了,小說動漫看多了,一眼就知道結局~
運行界面:
隨意界面:
結束界面:
正式的代碼環境——
1)素材準備
嗯哼——我準備了2組劇情圖片、但是劇情我只準備了一組,難得編了!哈哈哈,大家自由發揮哈
第一組圖片:(網上隨便找的)
第二組圖片:(借鑒魔鬼戀人)
2)劇情小簡介
咳咳咳......可能寫的比較幼稚小尷尬😅就不說了,等下直接看代碼然后看視頻展示效果。
3)環境安裝
本文的環境模塊:Python3、Pycharm、tkinter(界面化)、pillow(圖片)、messagebox(彈窗設計)。
模塊安裝:
pip install +模塊名 或帶鏡像源:pip install -i https://pypi.douban.com/simple/ +模塊名1)圖片設置
讀取圖片、設置不同的位置文字粗細、大小、圖片順序等等。
from tkinter import * from PIL import ImageTk,Imageclass Picture():def __init__(self):image1 =Image.open('picture//1.jpg')self.background_image = ImageTk.PhotoImage(image1)self.w = self.background_image.width()self.h = self.background_image.height()app.geometry('%dx%d+0+0' % (self.w,self.h)) self.canvas = Canvas(app, width=self.w, height=self.h,bg='pink')self.canvas.pack() def showpicture(self,num):image1 =Image.open('picture//'+str(num)+'.jpg')self.background_image = ImageTk.PhotoImage(image1)self.canvas.create_image(0,0,anchor=NW, image=self.background_image)def showtext(self,str):titleFont = ('微軟雅黑', 20, 'bold')self.canvas.create_text(self.w/2, 0.75*(self.h), text=str,font = titleFont)def showchoice(self,str1,str2):titleFont = ('微軟雅黑', 10, 'bold')self.canvas.create_text(0.3*(self.w), 0.8*(self.h), text=str1,font = titleFont)self.canvas.create_text(0.7*(self.w), 0.8*(self.h), text=str2,font = titleFont)def callback3(event):if (event.y>700)and(event.y<750):if event.x<280:pic.showpicture(3)pic.showtext('這是第三張圖')else:pic.showpicture(4)pic.showtext('這是第四張圖')def callback2(event):pic.showpicture(2)pic.showtext('這是第二張圖')pic.showchoice('三','四')pic.canvas.bind("<Button-1>", callback3)app = Tk() app.title("Welcome") pic= Picture() pic.canvas.bind("<Button-1>", callback2) pic.showpicture(1) pic.showtext('這是第一張圖')app.mainloop()2)界面設置、彈窗設置
運行界面、圖片、下一步彈窗界面文字圖片。
import tkinter as tk from PIL import ImageTk,Imagedef resize(w, h, w_box, h_box, pil_image): ''' resize a pil_image object so it will fit into a box of size w_box times h_box, but retain aspect ratio 對一個pil_image對象進行縮放,讓它在一個矩形框內,還能保持比例 ''' f1 = 1.0*w_box/w # 1.0 forces float division in Python2 f2 = 1.0*h_box/h factor = min([f1, f2]) #print(f1, f2, factor) # test # use best down-sizing filter width = int(w*factor) height = int(h*factor) return pil_image.resize((width, height), Image.ANTIALIAS)app = tk.Tk() app.title("魔鬼戀人——劇情版") app.geometry("1000x600+80+30") app.minsize(1000, 600) app.maxsize(1000, 600)body = tk.PanedWindow(app,showhandle = False,sashrelief = 'sunken',orient='vertical') body.pack(fill="both", expand=1)image = Image.open('picture//1.jpg') w, h = image.size image_resized = resize(w, h, 1000, 450, image) im = ImageTk.PhotoImage(image_resized) body_img = tk.Canvas(app,width=1000,height=450) body_img.create_image(500,225,image = im) body.add(body_img)box = tk.PanedWindow(body,showhandle = False,sashrelief = 'sunken',orient="horizontal") body.add(box)image = Image.open('picture//2.jpg') hw, hh = image.size himage_resized = resize(hw,hh, 100, 150, image) him = ImageTk.PhotoImage(himage_resized) head_img = tk.Label(box,width=100,height=150,compound='center',image= him) box.add(head_img)m_box = tk.Frame(box) box.add(m_box)name = tk.Label(m_box, text="小老鼠", anchor='w', relief='groove', pady=5, padx=10) name.place(x=10, y=10)message_content = tk.StringVar() message_content.set('喵喵喵~喵喵喵~喵喵喵~喵喵喵~喵喵喵~喵喵喵~喵喵喵~喵喵喵~喵喵喵~')def next():message_content.set('吱吱吱~吱吱吱~吱吱吱~吱吱吱~吱吱吱~吱吱吱~吱吱吱~吱吱吱~吱吱吱~')img_open = Image.open('picture//4.jpg')nw, nh = img_open.size nimage_resized = resize(nw,nh, 100, 150, img_open) nim = ImageTk.PhotoImage(nimage_resized)head_img.config(image=nim) head_img.image=nim #keep a referenceselect = tk.Toplevel()select.geometry("300x200+430+230")select.minsize(300, 200)select.maxsize(300, 200)question = tk.Label(select, text="請選擇:", anchor='w')question.pack()lb = tk.Listbox(select, height=4)for item in ['python','tkinter','widget']:lb.insert('end',item)lb.pack()select.mainloop()name = tk.Button(m_box, text="→", anchor='w', relief='raised', padx=10, command=next) name.place(x=750, y=10)message = tk.Message(m_box,width=800,textvariable =message_content, relief='sunken') message.place(x=10, y=50, width=800, height=80) app.mainloop()3)主程序
?界面大小、標題、全局設置、游戲數據等。
import tkinter as tk from PIL import ImageTk,Image import json import tkinter.messagebox# 初始全局變量 global story_id global dialogue_id global story_num global dialogue_num global people_data global story_data global status global select global lbstory_id = 0 dialogue_id = 0 story_num = 0 dialogue_num = 0 people_data = {} story_data = [] status = 'dialogue'# 游戲數據 people_data = json.loads( open('people.json', 'r', encoding='utf-8').read() )story_data = json.loads( open('story.json', 'r', encoding='utf-8').read() )def get_img(image, box_w, box_h):w, h = image.sizef1 = 1.0 * box_w / w # 1.0 forces float division in Python2 f2 = 1.0 * box_h / hfactor = min([f1, f2])# use best down-sizing filter width = int(w*factor)height = int(h*factor)image_resized = image.resize((width, height), Image.ANTIALIAS)return ImageTk.PhotoImage(image_resized)def refresh() :global story_idglobal dialogue_idglobal dialogue_numglobal people_dataglobal story_dataprint(dialogue_num)now_data = {'word': story_data[story_id]['dialogue'][dialogue_id]['word'],'name': people_data[story_data[story_id]['dialogue'][dialogue_id]['people']]['name'],'people_img': people_data[story_data[story_id]['dialogue'][dialogue_id]['people']]['img'],'bg_img': story_data[story_id]['bg']}print(json.dumps(now_data))content_word.set(now_data['word'])content_name.set(now_data['name'])background_img = get_img(Image.open(now_data['bg_img']), 1000, 450)background.config(image=background_img) background.image=background_img #keep a referencepeople_img = get_img(Image.open(now_data['people_img']), 100, 150)people.config(image=people_img)people.image=people_img #keep a referencedef show_select() :global selectglobal lbselect = tk.Toplevel()select.geometry('300x200+430+230')select.minsize(300, 200)select.maxsize(300, 200)question = tk.Label(select, text='請選擇:', anchor='w')question.pack()lb = tk.Listbox(select, height=4)for item in story_data[story_id]['options']:lb.insert('end',item['option'])lb.pack()name = tk.Button(select, text='確定', anchor='w', relief='raised', padx=10, command=hidden_select)name.pack()select.mainloop()def next_dialogue():global statusif status == 'dialogue' :global story_idglobal dialogue_idglobal story_numglobal dialogue_numglobal people_dataglobal story_dataif dialogue_id + 1 < dialogue_num :dialogue_id += 1refresh()else :if len(story_data[story_id]['options']) > 1 :show_select()status = 'select'elif len(story_data[story_id]['options']) == 1 :story_id = story_data[story_id]['options'][0]['to']dialogue_id = 0dialogue_num = len(story_data[story_id]['dialogue'])refresh()else :tkinter.messagebox.showinfo('結束','game over')def hidden_select() :global selectglobal lbglobal story_idglobal dialogue_idglobal dialogue_num#print( json.dumps(select) )print( lb.curselection() )if lb.curselection() :id = lb.curselection()[0]story_id = story_data[story_id]['options'][id]['to']dialogue_id = 0dialogue_num = len(story_data[story_id]['dialogue'])select.destroy()refresh()# 設置主窗口 app = tk.Tk() app.title('魔鬼戀人——劇情版') app.geometry('1000x600+80+30') app.minsize(1000, 600) app.maxsize(1000, 600)# 初始化數據 story_num = len( story_data ) dialogue_num = len( story_data[story_id]['dialogue'] )content_word = tk.StringVar() content_name = tk.StringVar()now_data = {'word': story_data[story_id]['dialogue'][dialogue_id]['word'],'name': people_data[story_data[story_id]['dialogue'][dialogue_id]['people']]['name'],'people_img': people_data[story_data[story_id]['dialogue'][dialogue_id]['people']]['img'],'bg_img': story_data[story_id]['bg'] }content_word.set(now_data['word']) content_name.set(now_data['name']) background_img = get_img(Image.open(now_data['bg_img']), 1000, 450) people_img = get_img(Image.open(now_data['people_img']), 100, 150)#生成組件 body = tk.PanedWindow(app,showhandle = False,sashrelief = 'sunken',orient='vertical')background = tk.Label(body,width=1000,height=450,compound='center',image=background_img)box = tk.PanedWindow(body,showhandle = False,sashrelief = 'sunken',orient='horizontal')people = tk.Label(box,width=100,height=150,compound='center',image=people_img)dialogue_box = tk.Frame(box)name = tk.Label(dialogue_box, textvariable =content_name, anchor='w', relief='groove', pady=5, padx=10)next_btn = tk.Button(dialogue_box, text='→', anchor='w', relief='raised', padx=10, command=next_dialogue)word = tk.Message(dialogue_box,width=800,textvariable =content_word, relief='sunken')#組件布局 body.pack(fill='both', expand=1)body.add(background)body.add(box)box.add(people)box.add(dialogue_box)name.place(x=10, y=10)next_btn.place(x=750, y=10)word.place(x=10, y=50, width=800, height=80)app.mainloop()總結
這款劇情版的小程序,優美精致的畫風甜甜的劇情,你get到了嘛?僅娛樂哈哈哈,不能干啥~
如果喜歡💗記得三連哦~相遇即是緣分,嘿嘿,小編會寫出更多更多稀奇古怪的小程序滴,記得關
注我哦!
注小編獲取更多精彩內容!記得點擊傳送門哈👇
記得三連哦! 如需打包好的源碼+素材免費分享滴!!傳送門
總結
以上是生活随笔為你收集整理的【Pygame小游戏】剧情流推荐:什么样的游戏才能获得大家的喜欢呢?(魔鬼恋人、霸总娇妻版)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Pygame小游戏】《坦克大战》,那些
- 下一篇: 【Python小脚本】实现王者农药自动刷