【Python剧情版游戏】优美精致的画风甜甜的剧情、很难不让人上头啊?你get到了嘛

导语

🐾

必备文案·甜甜的恋爱

⋆ • ⋆ ☽ 2021.11.30 ✯ ⋆ • ⋆

“ꀿªᵖᵖᵞ☺︎ꔛ♡盐于律己ᶜᵘᵗᵉ 甜以待人.”

大家好,我是木木子啦~

最近一段时间爱上了看动漫,各种动漫,剧名我就不说了!剧情可甜可甜,甜度爆表的那种。

能直接看出姨母笑😄😄所以今天这款小程序就出炉了~

你们的恋爱小程序准时上线:今天小编为大家带来——一波甜甜的文案+剧情版的动漫风小程序。

希望大家喜欢,大家来源码基地拿源码鸭,这款恋爱的剧情版小程序超甜的。

不信的话,你来看看?!!

正文

本文是基于tkinter做的界面化小程序,还设置了弹窗选择哦。

🎄如需完整的项目:🎄源码基地见——🎄主页左侧信息 免费拿资料!🎄

一、准备中

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,Image

class 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,Image

def 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 reference

  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 ['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 lb

story_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.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 = {
    '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 reference

  people_img = get_img(Image.open(now_data['people_img']), 100, 150)
  people.config(image=people_img)
  people.image=people_img #keep a reference

def 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 = {
  '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()

三、效果展示

第一组(魔鬼恋人)

视频效果展示——

【Python剧情版游戏】优美精致的画风甜甜的剧情上头且撩人

静态截图展示——

Part 01 运行界面:

​​Part 02 弹窗界面:

​​Part 03 结束界面:

点击的第一个选择结束页面是如下所示,不同的选择界面效果是不一样的,另一个选择的话等你们自己玩儿试试啦~

第二组(霸总的小娇妻)

静态截图展示——

噗~霸道总裁的梗,哈哈哈哈——这名字你们可以忽略的哈。

故事你们可以自由想象~我脑海已经有几十个故事跟结局了,小说动漫看多了,一眼就知道结局~

Part 01 运行界面:

​​Part 02 随意界面:

​​Part 03 结束界面:

总结

这款剧情版的小程序,优美精致的画风甜甜的剧情,你get到了嘛?仅娱乐哈哈哈,不能干啥~

如果喜欢💗记得三连哦~相遇即是缘分,嘿嘿,小编会写出更多更多稀奇古怪的小程序滴,记得关

注我哦!

🎯完整的免费源码领取处:找我吖!

滴滴我即可吖!

🎄文章汇总——

项目1.0 Python—2021 |已有文章汇总 | 持续更新,直接看这篇就够了

(更多内容+源码都在文章汇总哦!!欢迎阅读~)

  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

顾木子吖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值