python 基于Tkinter的姻缘测试器
前期吐槽:
还记得8年前,大概是八年级的时候,那时候自己很帅。。
《名字打架》这款小游戏还是挺火的。。
突然想起来,就用Tkinter做了个。。类似的。。很挫,不过编程,就是自由,想怎么编就怎么编。。
改了个名字,叫姻缘测试器。。
其实真的很无聊,就是巩固下Tkinter。。没什么意思。。大神可以别看。。
算法
大概长个样子
代码
# -*- coding: utf-8 -*- from Tkinter import * from ttk import Frame, Button, Style, Entry, Label import tkMessageBox import re import urllib, Queue, sgmllib import time import threading import tkFileDialog import random import hashlib class NameFighter(Frame): txt = "" area = None en1 = None en2 = None def __init__(self,parent): Frame.__init__(self,parent) self.parent = parent self.initUI() #事件绑定,用来实时显示text self.parent.bind("<<A>>", lambda evt:self.area.insert(END,self.txt)) #事件绑定,让Text跟踪到最新一行 self.parent.bind("<<CurrentView>>", lambda evt:self.area.see(END)) #事件绑定,让goBtn恢复正常 self.parent.bind("<<NormalGoBtn>>", lambda evt: self.goBtn.config(state = NORMAL)) def do_figting(self,moves,n1,n2,b1,b2): try: self.txt = "如果两人能够坚持越久轮,爱情就越稳固!\n" self.parent.event_generate("<<A>>",when="tail")#触发bind的事件,也就是显示文本 self.parent.event_generate("<<CurrentView>>",when="tail")#触发bind的事件,也就是显示文本 time.sleep(1) for i in range(5,0,-1): self.txt = "倒计时:"+str(i)+"秒!\n" self.parent.event_generate("<<A>>",when="tail")#触发bind的事件,也就是显示文本 self.parent.event_generate("<<CurrentView>>",when="tail")#触发bind的事件,也就是显示文本 time.sleep(1) self.txt = n1 + " 初始血量:"+ str(b1)+" \n" + n2 + " 初始血量:"+str(b2)+" \n" self.parent.event_generate("<<A>>",when="tail")#触发bind的事件,也就是显示文本 time.sleep(1) flag = 1 while b1 > 0 and b2 > 0: self.txt = "\n=========================第"+str(flag)+"轮=========================\n" self.parent.event_generate("<<A>>",when="tail")#触发bind的事件,也就是显示文本 self.parent.event_generate("<<CurrentView>>",when="tail")#触发bind的事件,也就是显示文本 time.sleep(1) tempb = random.randint(300,400) tempm = random.randint(0,len(moves)-1) if flag % 2 == 0: b2 -= tempb if b2 < 0 : self.txt = n2+" 不堪重负,被 "+n1+"活活打死了...\n" else: self.txt = n1+" "+moves[tempm]+" "+n2+" 造成"+str(tempb)+"的伤害!" self.txt += n2+"剩下血量:"+str(b2)+"\n" else: b1 -= tempb if b1 < 0 : self.txt = n1+" 不堪重负,被 "+n2+"活活打死了...\n" else: self.txt = n2+" "+moves[tempm]+" "+n1+" 造成"+str(tempb)+"的伤害!" self.txt += n1+"剩下血量:"+str(b1)+"\n" flag += 1 self.parent.event_generate("<<A>>",when="tail")#触发bind的事件,也就是显示文本 self.parent.event_generate("<<CurrentView>>",when="tail")#触发bind的事件,也就是显示文本 time.sleep(1.5) self.txt = "\n======================一切都结束了======================\n" self.parent.event_generate("<<A>>",when="tail")#触发bind的事件,也就是显示文本 self.parent.event_generate("<<CurrentView>>",when="tail")#触发bind的事件,也就是显示文本 time.sleep(1) #真正的逻辑,以3为基准 if b1 % 3 == 0 and b2 % 3 == 0: self.txt = "恭喜"+n1+" 和 "+n2+"!你们非常适合在一起!\n" elif b1 % 3 == 0 or b2 % 3 == 0: self.txt = "恭喜"+n1+" 和 "+n2+"!因为有一个人比较迁就的关系,你们还算比较适合在一起!\n" else: self.txt = "哎!"+n1+" 和 "+n2+"!貌似不合适?不想说什么!\n" self.parent.event_generate("<<A>>",when="tail")#触发bind的事件,也就是显示文本 self.parent.event_generate("<<CurrentView>>",when="tail")#触发bind的事件,也就是显示文本 finally: self.parent.event_generate("<<NormalGoBtn>>",when="tail")#触发bind的事件,让goBtn恢复 def onGo(self): maleName = self.en1.get() famaleName = self.en2.get() n1 = maleName.encode("utf8") n2 = famaleName.encode("utf8") md5_1 = hashlib.md5() md5_1.update(n1) md5_1 = md5_1.hexdigest().upper() md5_2 = hashlib.md5() md5_2.update(str(n2)) md5_2 = md5_2.hexdigest().upper() blood_1 = 0 for i in range(len(md5_1)): blood_1 += ord(md5_1[i]) print blood_1 blood_2 = 0 for i in range(len(md5_2)): blood_2 += ord(md5_2[i]) print blood_2 if maleName == "" or famaleName == "": tkMessageBox.showinfo(‘逗比‘,‘你不写名字我怎么算\n‘ ) else: moves = [‘咬了一口‘,‘踢了一下‘,‘挠了一下‘,‘放出了大海无量打中‘,‘舔了一下‘,‘搓了一下‘,‘吼了一下‘] self.goBtn.config(state=DISABLED) #先置为disabled do_figting_thread = threading.Thread(target=self.do_figting,args = (moves,n1,n2,blood_1,blood_2,)) do_figting_thread.start() def onHelp(self): tkMessageBox.showinfo(‘玩法‘,‘随便点点,即使是逗比也会玩呢\n‘ ) def initUI(self): #大小,位置 w = 600 h = 400 sw = self.parent.winfo_screenwidth() sh = self.parent.winfo_screenheight() x = (sw-w)/2 y = (sh-h)/2 self.parent.geometry(‘%dx%d+%d+%d‘ % (w,h,x,y)) self.parent.title("哈尔滨马家沟男子职业技术学院出品!电脑算命-姻缘测试器") #风格 self.style = Style() self.style.theme_use("default") Style().configure("TButton", padding = (0, 5, 0, 5), font=‘serif 10‘) #控件位置调控 self.columnconfigure(1, weight = 1) self.columnconfigure(2, pad = 7) self.rowconfigure(3, weight = 1) self.rowconfigure(5, pad = 7) #标签 lb1 = Label(self, text = "请输入男方姓名: ") lb1.grid(row = 0, column = 0, sticky = W) lb2 = Label(self, text = "请输入女方姓名: ") lb2.grid(row = 1, column = 0, sticky = W) #输入点Entry self.en1 = Entry(self) self.en1.grid(row = 0, column = 1, columnspan = 1, sticky = W+E) self.en2 = Entry(self) self.en2.grid(row = 1, column = 1, columnspan = 1, sticky = W+E) #显示结果的text & Scrollbar self.area = Text(self, bg = ‘black‘, fg = ‘green‘) self.area.grid(row = 2,column = 0, columnspan = 2, rowspan = 4, sticky = E+W+S+N) self.area.see(1000.0) self.sb = Scrollbar(self) self.sb.grid(row = 2, column = 1, rowspan = 4, sticky = E+W+S+N) self.area[‘yscrollcommand‘] = self.sb.set #text往下走,触发scrollbar的set() self.sb[‘command‘] = self.area.yview #scrollbar的拖动,触发text的view移动 #Go按钮 self.goBtn = Button(self, text = "Go!",command = self.onGo) self.goBtn.grid(row = 0, column = 2) lb3 = Label(self, text = "电脑算命-绝对准确, made by ouyang! ") lb3.grid(row = 6, column = 1, sticky = E) #Help按钮 helpBtn = Button(self, text = "怎么玩?",command = self.onHelp) helpBtn.grid(row = 6, column = 2) #最终绑定 self.pack(fill = BOTH,expand = 1) def main(): root = Tk() root.geometry("250x150+300+300") app = NameFighter(root) root.mainloop() if __name__ == ‘__main__‘: main()
重点关注
事件的绑定和触发
事件触发的时候,把事件放到tail最好,不然容易乱。。。when="tail"
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。