鱼C论坛

 找回密码
 立即注册
查看: 1471|回复: 6

[已解决]想实现两个进程同时进行

[复制链接]
发表于 2020-7-8 11:48:24 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. from tkinter import *
  2. import random
  3. from time import sleep
  4. import threading

  5. class Game():
  6.     tl = 10 #体力
  7.     x = 580
  8.     y = 380#随机出现范围

  9.     def __init__(self,game):
  10.         self.game = game


  11.     def body(self):
  12.         #设置界面大小
  13.         self.game.title('速度测试1.0')
  14.         self.game.geometry('600x400+10+10')
  15.         self.tl_Label = Label(self.game, text='您当前剩余体力为%s' % self.tl)
  16.         self.tl_Label.place(x=0,y=0)
  17.         self.add_botton()

  18.     def add_botton(self):
  19.         #设置按键大小
  20.         self.hit_Button = Button(self.game, text='敌人', activebackground='red',command = self.hit)
  21.         self.hit_Button.place(x=random.randint(0,self.x),y=random.randint(0,self.y))


  22.     def move_botton(self):
  23.         sleep(random.uniform(0,2))
  24.         self.hit_Button.place(x=random.randint(0,self.x),y=random.randint(0,self.y))

  25.     def move_all(self):
  26.         while True:
  27.             sleep(random.uniform(0,2))
  28.             self.hit_Button.place(x=random.randint(0,self.x),y=random.randint(0,self.y))

  29.     def hit(self):
  30.         self.hit_Button.place(x=9999, y=9999)
  31.         self.move_botton()
  32.         self.tl += 1
  33.         print('您当前剩余体力为%s' % self.tl)
  34.         self.tl_Label['text']='您当前剩余体力为%s' % self.tl

  35.     def missed(self):
  36.         pass

  37.     def gameover(self):
  38.         pass

  39. def startgame():
  40.     game = Tk()
  41.     MYgame = Game(game)
  42.     MYgame = MYgame.body()
  43.     game.mainloop()

  44. startgame()
复制代码


以前的代码,当时学习tkinter时自己设计的题目(其实就是类似打地鼠游戏,哈哈),想实现我点击“敌人”按钮,与“敌人”按钮自动刷新,两个并行;
既实现:如果我一段时间内没去点“敌人”按钮,他就自己会跑到其他位置,触发missed()实现体力扣减。
现在学了pygame是简单的,但是tkinter上想实现的话还没学习到相关知识,最好也可以给我说下需要了解什么模块或者内容才能解决;

@Twilight6 您点的外卖到了~
最佳答案
2020-7-8 12:25:44



  1. from tkinter import *
  2. import random
  3. from time import sleep
  4. from threading import Thread

  5. class Game():
  6.     tl = 10 #体力
  7.     x = 580
  8.     y = 380#随机出现范围

  9.     def __init__(self,game):
  10.         self.game = game
  11.         self.temp = []

  12.     def body(self):
  13.         #设置界面大小
  14.         self.game.title('速度测试1.0')
  15.         self.game.geometry('600x400+10+10')
  16.         self.tl_Label = Label(self.game, text='您当前剩余体力为%s' % self.tl)
  17.         self.tl_Label.place(x=0,y=0)
  18.         self.add_botton()

  19.     def add_botton(self):
  20.         #设置按键大小
  21.         self.hit_Button = Button(self.game, text='敌人', activebackground='red', command=self.hit)
  22.         self.hit_Button.place(x=random.randint(0, self.x), y=random.randint(0, self.y))
  23.         self.temp.append(self.hit_Button)
  24.         def func(temp):
  25.             if temp != []:
  26.                 sleep(2)   # 2秒后消失
  27.                 temp.pop().destroy()
  28.                 self.add_botton()
  29.                 self.missed()
  30.         t = Thread(target=func,args=(self.temp,))
  31.         t.start()




  32.     def move_botton(self):
  33.         sleep(random.uniform(0,2))
  34.         self.hit_Button.place(x=random.randint(0,self.x),y=random.randint(0,self.y))

  35.     def move_all(self):
  36.         while True:
  37.             sleep(random.uniform(0,2))
  38.             self.hit_Button.place(x=random.randint(0,self.x),y=random.randint(0,self.y))

  39.     def hit(self):
  40.         self.hit_Button.place(x=9999, y=9999)
  41.         self.move_botton()
  42.         self.tl += 1
  43.         print('您当前剩余体力为%s' % self.tl)
  44.         self.tl_Label['text']='您当前剩余体力为%s' % self.tl

  45.     def missed(self):
  46.         self.tl -= 1
  47.         print('您当前剩余体力为%s' % self.tl)
  48.         self.tl_Label['text']='您当前剩余体力为%s' % self.tl

  49.     def gameover(self):
  50.         pass

  51. def startgame():
  52.     game = Tk()
  53.     MYgame = Game(game)
  54.     MYgame = MYgame.body()
  55.     game.mainloop()

  56. startgame()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-7-8 12:25:44 | 显示全部楼层    本楼为最佳答案   



  1. from tkinter import *
  2. import random
  3. from time import sleep
  4. from threading import Thread

  5. class Game():
  6.     tl = 10 #体力
  7.     x = 580
  8.     y = 380#随机出现范围

  9.     def __init__(self,game):
  10.         self.game = game
  11.         self.temp = []

  12.     def body(self):
  13.         #设置界面大小
  14.         self.game.title('速度测试1.0')
  15.         self.game.geometry('600x400+10+10')
  16.         self.tl_Label = Label(self.game, text='您当前剩余体力为%s' % self.tl)
  17.         self.tl_Label.place(x=0,y=0)
  18.         self.add_botton()

  19.     def add_botton(self):
  20.         #设置按键大小
  21.         self.hit_Button = Button(self.game, text='敌人', activebackground='red', command=self.hit)
  22.         self.hit_Button.place(x=random.randint(0, self.x), y=random.randint(0, self.y))
  23.         self.temp.append(self.hit_Button)
  24.         def func(temp):
  25.             if temp != []:
  26.                 sleep(2)   # 2秒后消失
  27.                 temp.pop().destroy()
  28.                 self.add_botton()
  29.                 self.missed()
  30.         t = Thread(target=func,args=(self.temp,))
  31.         t.start()




  32.     def move_botton(self):
  33.         sleep(random.uniform(0,2))
  34.         self.hit_Button.place(x=random.randint(0,self.x),y=random.randint(0,self.y))

  35.     def move_all(self):
  36.         while True:
  37.             sleep(random.uniform(0,2))
  38.             self.hit_Button.place(x=random.randint(0,self.x),y=random.randint(0,self.y))

  39.     def hit(self):
  40.         self.hit_Button.place(x=9999, y=9999)
  41.         self.move_botton()
  42.         self.tl += 1
  43.         print('您当前剩余体力为%s' % self.tl)
  44.         self.tl_Label['text']='您当前剩余体力为%s' % self.tl

  45.     def missed(self):
  46.         self.tl -= 1
  47.         print('您当前剩余体力为%s' % self.tl)
  48.         self.tl_Label['text']='您当前剩余体力为%s' % self.tl

  49.     def gameover(self):
  50.         pass

  51. def startgame():
  52.     game = Tk()
  53.     MYgame = Game(game)
  54.     MYgame = MYgame.body()
  55.     game.mainloop()

  56. startgame()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-8 12:33:07 | 显示全部楼层


去学习下多线程、进程就好

线程模块 thread、threading,但是建议学 threading  因为这个模块有守护功能

进程模块 multiprocessing 进程里面有很多线程,各个线程都用同个内存地址空间,共用进程中的数据等

但是各个进程之间是互不干扰,每个进程拥有自己的内存地址

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-7-8 12:39:54 | 显示全部楼层
Twilight6 发表于 2020-7-8 12:33
去学习下多线程、进程就好

线程模块 thread、threading,但是建议学 threading  因为这个模块有守护 ...

好的,我先消化下~~~
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-8 12:41:22 | 显示全部楼层
yhhpf 发表于 2020-7-8 12:39
好的,我先消化下~~~


我的代码还有很多不行的地方...因为今天也是刚刚学的 现学现用了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-7-8 12:48:24 | 显示全部楼层
Twilight6 发表于 2020-7-8 12:41
我的代码还有很多不行的地方...因为今天也是刚刚学的 现学现用了

感谢大佬,知道哪些模块内容涉及到就成,剩下的就慢慢学了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-8 12:48:49 | 显示全部楼层
yhhpf 发表于 2020-7-8 12:48
感谢大佬,知道哪些模块内容涉及到就成,剩下的就慢慢学了


好~~~加油哈哈~

                               
登录/注册后可看大图
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-29 06:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表