前言
作者 :“程序员梨子”正文 必备文案·甜甜的恋爱
**文章简介 **:本篇文章主要利用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("", callback3)app = Tk()
app.title("Welcome")
pic= Picture()
pic.canvas.bind("", 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 = https://www.it610.com/article/{}
story_data = []
status ='dialogue'# 游戏数据
people_data = https://www.it610.com/article/json.loads( open('people.json', 'r', encoding='utf-8').read() )story_data = https://www.it610.com/article/json.loads( open('story.json', 'r', encoding='utf-8').read() )def get_img(image, box_w, box_h):
w, h = image.size
f1 = 1.0 * box_w / w # 1.0 forces float division in Python2
f2 = 1.0 * box_h / h
factor = 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_id
global dialogue_id
global dialogue_num
global people_data
global story_data
print(dialogue_num)
now_data = https://www.it610.com/article/{'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 select
global lb
select = 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 status
if status == 'dialogue' :
global story_id
global dialogue_id
global story_num
global dialogue_num
global people_data
global story_data
if dialogue_id + 1 < dialogue_num :
dialogue_id += 1
refresh()
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 = 0
dialogue_num = len(story_data[story_id]['dialogue'])
refresh()
else :
tkinter.messagebox.showinfo('结束','game over')def hidden_select() :
global select
global lb
global story_id
global dialogue_id
global 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 = 0
dialogue_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 = https://www.it610.com/article/{'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|【AI出牌器】第一次见这么“刺激”的斗地主,胜率高的关键因素竟是......
- Python|【Pygame小游戏】《坦克大战》,那些童年的游戏你还记得几个呢()
- python|机器视觉中彩色成像必须考虑的十个问题
- python|Python dataframe 多条件筛选/过滤数据的方法及函数isin,query,contains,loc的使用介绍
- python|【python】笔势识别 - (含缩小规格,坐标点转换为矩阵,点图连成线图,图片输出处理)
- Datawhale吃瓜教程|第27期 Datawhale 组队学习 吃瓜教程——西瓜书+南瓜书第三章 / 周志华《机器学习》
- 笔记|RL强化学习笔记(OpenAI gym建立自己的探索环境)
- Python|python opencv透视变换矫正文档