標籤:多線程 python 物件導向 打怪 python遊戲
今天看到很久以前在學校的時候的一個虛擬碼,講述的是一個打怪的故事,今天用python做一下練習。
虛擬碼如下:
class 怪物 { char 怪物名; int 血; 怪物(怪物名n,血n) /*建構函式*/ { 怪物名=怪物名n; 血=血n; } 被打函數() { 血減少; } 判斷函數() { if (血==0) printf("你勝利了!"); } 玩遊戲() { while (血 > 0) { 被打函數(); 判斷函數(); } } }
import os,random,time,threading,random,string##定義一個monster類,屬性有名字,血量,被打方法,判斷方法class monster(): def __init__(self, name, blood): self.name = name self.blood = blood print ‘Hahaha.....I am a tiger monster‘ def hit(self, n, times): self.blood = self.blood - n if self.blood < 0: self.blood = 0 print ‘Round %s monster\‘s blood - %s‘ % (times, n) print ‘monster\‘s blood is %s‘ % self.blood def checklife(self): if self.blood <= 0: print ‘Finally, you win!‘##定義一個hunter類,屬性有名字、攻擊血量,打怪方法,休息方法class hunter(): def __init__(self, name, hitblo): self.hitblo = hitblo self.name = name def hitmonster(self): print ‘I am hunter-%s , my hitnum is %s‘ % (self.name, self.hitblo) print ‘Ouye...hunter hit monster %s blood‘ % self.hitblo return self.hitblo def haverest(self): print ‘hunter %s need have a rest.‘ % self.name time.sleep(random.random())##定義一個全域怪物tigerglobal mm = monster(‘tiger‘, 800)numround = 1 ##定義一個計數變數,計算打鬥回合##鎖lock1 = threading.Lock()##定義遊戲流程函數,內建的if判斷是必要的,否則最後一次打鬥判斷,3個線程while都是true,但是第一次打怪已經結束流程了,後面兩線程等待鎖釋放之後,還是使用之前的true判斷def playgame(hname, hitblo): print ‘%s :fighting begin...\n‘ %hname h = hunter(hname, hitblo) while m.blood >0: lock1.acquire() try: if m.blood >0: h.hitmonster() h.haverest() global numround m.hit(h.hitblo, numround) m.checklife() numround+=1 print ‘------------------------------------------‘ finally: lock1.release() ##定義一個函數,隨機產生hunter資訊def mkobj(n): global hunters hunters = [] for i in range(0,n): strname = string.join(random.sample([‘a‘,‘b‘,‘c‘,‘d‘,‘e‘,‘f‘,‘g‘,‘h‘,‘i‘,‘j‘], 3)).replace(" ","") hitnum = random.randrange(60, 151) hunters.append(hunter(strname,hitnum))mkobj(3)t0 = threading.Thread(target=playgame, args=(hunters[0].name, hunters[0].hitblo,))t1 = threading.Thread(target=playgame, args=(hunters[1].name, hunters[1].hitblo,))t2 = threading.Thread(target=playgame, args=(hunters[2].name, hunters[2].hitblo,))t0.start()t1.start()t2.start()t0.join()t1.join()t2.join()
執行結果還蠻有意思的
Hahaha.....I am a tiger monsterafg :fighting begin...gdh :fighting begin...cbf :fighting begin...I am hunter-afg , my hitnum is 119Ouye...hunter hit monster 119 bloodhunter afg need have a rest.Round 1 monster‘s blood - 119monster‘s blood is 681------------------------------------------I am hunter-gdh , my hitnum is 109Ouye...hunter hit monster 109 bloodhunter gdh need have a rest.Round 2 monster‘s blood - 109monster‘s blood is 572------------------------------------------I am hunter-cbf , my hitnum is 64Ouye...hunter hit monster 64 bloodhunter cbf need have a rest.Round 3 monster‘s blood - 64monster‘s blood is 508------------------------------------------I am hunter-afg , my hitnum is 119Ouye...hunter hit monster 119 bloodhunter afg need have a rest.Round 4 monster‘s blood - 119monster‘s blood is 389------------------------------------------I am hunter-gdh , my hitnum is 109Ouye...hunter hit monster 109 bloodhunter gdh need have a rest.Round 5 monster‘s blood - 109monster‘s blood is 280------------------------------------------I am hunter-cbf , my hitnum is 64Ouye...hunter hit monster 64 bloodhunter cbf need have a rest.Round 6 monster‘s blood - 64monster‘s blood is 216------------------------------------------I am hunter-afg , my hitnum is 119Ouye...hunter hit monster 119 bloodhunter afg need have a rest.Round 7 monster‘s blood - 119monster‘s blood is 97------------------------------------------I am hunter-gdh , my hitnum is 109Ouye...hunter hit monster 109 bloodhunter gdh need have a rest.Round 8 monster‘s blood - 109monster‘s blood is 0Finally, you win!------------------------------------------
【python學習】多線程 與 物件導向 結合的一個經典例子