|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- from tkinter import *
- import random
- from time import sleep
- import threading
- class Game():
- tl = 10 #体力
- x = 580
- y = 380#随机出现范围
- def __init__(self,game):
- self.game = game
- def body(self):
- #设置界面大小
- self.game.title('速度测试1.0')
- self.game.geometry('600x400+10+10')
- self.tl_Label = Label(self.game, text='您当前剩余体力为%s' % self.tl)
- self.tl_Label.place(x=0,y=0)
- self.add_botton()
- def add_botton(self):
- #设置按键大小
- self.hit_Button = Button(self.game, text='敌人', activebackground='red',command = self.hit)
- self.hit_Button.place(x=random.randint(0,self.x),y=random.randint(0,self.y))
- def move_botton(self):
- sleep(random.uniform(0,2))
- self.hit_Button.place(x=random.randint(0,self.x),y=random.randint(0,self.y))
- def move_all(self):
- while True:
- sleep(random.uniform(0,2))
- self.hit_Button.place(x=random.randint(0,self.x),y=random.randint(0,self.y))
- def hit(self):
- self.hit_Button.place(x=9999, y=9999)
- self.move_botton()
- self.tl += 1
- print('您当前剩余体力为%s' % self.tl)
- self.tl_Label['text']='您当前剩余体力为%s' % self.tl
- def missed(self):
- pass
- def gameover(self):
- pass
- def startgame():
- game = Tk()
- MYgame = Game(game)
- MYgame = MYgame.body()
- game.mainloop()
- startgame()
复制代码
以前的代码,当时学习tkinter时自己设计的题目(其实就是类似打地鼠游戏,哈哈),想实现我点击“敌人”按钮,与“敌人”按钮自动刷新,两个并行;
既实现:如果我一段时间内没去点“敌人”按钮,他就自己会跑到其他位置,触发missed()实现体力扣减。
现在学了pygame是简单的,但是tkinter上想实现的话还没学习到相关知识,最好也可以给我说下需要了解什么模块或者内容才能解决;
@Twilight6 您点的外卖到了~
- from tkinter import *
- import random
- from time import sleep
- from threading import Thread
- class Game():
- tl = 10 #体力
- x = 580
- y = 380#随机出现范围
- def __init__(self,game):
- self.game = game
- self.temp = []
- def body(self):
- #设置界面大小
- self.game.title('速度测试1.0')
- self.game.geometry('600x400+10+10')
- self.tl_Label = Label(self.game, text='您当前剩余体力为%s' % self.tl)
- self.tl_Label.place(x=0,y=0)
- self.add_botton()
- def add_botton(self):
- #设置按键大小
- self.hit_Button = Button(self.game, text='敌人', activebackground='red', command=self.hit)
- self.hit_Button.place(x=random.randint(0, self.x), y=random.randint(0, self.y))
- self.temp.append(self.hit_Button)
- def func(temp):
- if temp != []:
- sleep(2) # 2秒后消失
- temp.pop().destroy()
- self.add_botton()
- self.missed()
- t = Thread(target=func,args=(self.temp,))
- t.start()
- def move_botton(self):
- sleep(random.uniform(0,2))
- self.hit_Button.place(x=random.randint(0,self.x),y=random.randint(0,self.y))
- def move_all(self):
- while True:
- sleep(random.uniform(0,2))
- self.hit_Button.place(x=random.randint(0,self.x),y=random.randint(0,self.y))
- def hit(self):
- self.hit_Button.place(x=9999, y=9999)
- self.move_botton()
- self.tl += 1
- print('您当前剩余体力为%s' % self.tl)
- self.tl_Label['text']='您当前剩余体力为%s' % self.tl
- def missed(self):
- self.tl -= 1
- print('您当前剩余体力为%s' % self.tl)
- self.tl_Label['text']='您当前剩余体力为%s' % self.tl
- def gameover(self):
- pass
- def startgame():
- game = Tk()
- MYgame = Game(game)
- MYgame = MYgame.body()
- game.mainloop()
- startgame()
复制代码
|
|