yhhpf 发表于 2020-7-8 11:48:24

想实现两个进程同时进行

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 您点的外卖到了~{:9_236:}

Twilight6 发表于 2020-7-8 12:25:44




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()

Twilight6 发表于 2020-7-8 12:33:07



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

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

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

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

yhhpf 发表于 2020-7-8 12:39:54

Twilight6 发表于 2020-7-8 12:33
去学习下多线程、进程就好

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

好的,我先消化下~~~{:9_240:}

Twilight6 发表于 2020-7-8 12:41:22

yhhpf 发表于 2020-7-8 12:39
好的,我先消化下~~~

我的代码还有很多不行的地方...因为今天也是刚刚学的 现学现用了{:9_240:}

yhhpf 发表于 2020-7-8 12:48:24

Twilight6 发表于 2020-7-8 12:41
我的代码还有很多不行的地方...因为今天也是刚刚学的 现学现用了

感谢大佬,知道哪些模块内容涉及到就成,剩下的就慢慢学了{:9_231:}

Twilight6 发表于 2020-7-8 12:48:49

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

好~~~加油哈哈~
https://xxx.ilovefishc.com/forum/202005/27/132745rjvcvw1z2148jthd.gif
页: [1]
查看完整版本: 想实现两个进程同时进行