''' 整體思路 植物生長分7個階段,遵循以下規則 1> 每經過一回合就進入下一階段 2> 部分階段是敏感期(2,4,6)臨近區域的植物'過壯'就死亡(我設計成15) 3> 死亡時,會留下遺骸,遺骸的生命力是負值,是hp的相反數(越大的植物死亡,腐化時間越長) 4> 遺骸每回合恢復1,恢復到0之前,種子無法在這里萌發 5> 后四個生育階段,都會釋放種子 分別釋放 1231個 備注 1> 因為我的錯誤 規則2>并沒有起到預期的作用 2> 246階段死亡,hp = -hp,7階段死亡就成了hp = -hp*4 這里純粹是我個人的疏忽 個人認為的要點: 1> 棋盤的復制, 同回合的棋子不相互影響!!!!!!格子只受上回合棋盤的影響. 假定AB臨近 A根據周邊(包括B)變成的 新A ,'B 變 新B '的程序參考的應該是 舊A 而不是新A 2> 如果棋盤是 雙層嵌套的list ,請注意"深淺拷貝" ''' from copy import deepcopy from random import sample,randint import pygame import os # 圖形界面是pygame,只有這個模塊需要安裝 ''' aound_dict全域常量,只讀不改,內容是 (0, 0), (-1, 0), (1, 1), (1, 0), (-1, 1), (0, -1), (-1, -1), (1, -1), (0, 1)} 是以(0,0)為中心的 3x3 區域的相對坐標 ''' around_dict = {(i,j) for i in range(-1,2) for j in range(-1,2) } # around_dict應當是串列,是我錯寫成集合了,但不影響結果 #-----------------匯入結束----------------------- def ask(z): # z是坐標,二元元組 # 回傳該坐標在上回合的值 # 超范圍的默認為0 x,y = z if 0 <= x < x_max and 0 <= y < y_max: return max_copy[y][x] return 0 def probe(z): # 判斷坐標是否越界 x,y = z return 0 <= x < x_max and 0 <= y < y_max def around(x,y): return sum(list(map(ask,around_dict))) # 回傳 x,y 為中心的3x3區域的"總生命值" # 我當初寫錯了.... # 應當是 ''' def around(x,y): around_dict = [(x+i,y+j) for i,j in around_dict] return sum(list(map(ask,around_dict))) ''' def release_seed(x,y,n): # 以x,y為中心 釋放n枚種子 l = [(x+i,y+j)for i,j in sample(k49,n)] l = filter(probe,l) # 過濾越界的種子 seeds.update(set(l)) # set成 集合 過濾重復元素 # 因為seed本身就是set, set(l)有些多余 def drow(): # 繪制界面的函式 screen.fill((255,255,255)) for x in range(x_max): for y in range(y_max): hp = max_list[y][x] pygame.draw.rect(screen, colors[hp], (x * 5, y * 5, 5, 5)) # colors是字典 key是hp,value是對應顏色的RGB值 pygame.display.update() #-----------------函式定義結束------------------- x_max = 180 y_max = 120 max_list = [[0 for i in range(x_max)]for j in range(y_max)] # max_list 是"棋盤",以雙重串列的形式儲存每一塊的值,這里初始化棋盤 max_copy = [] # max_copy 是"棋盤的拷貝",此處相當于宣告變數 seeds = {(90,60)} # 最開始的種子 k49 = [(i,j)for i in range(-3,4)for j in range(-3,4)] # 和aound_dict類似,但是這個是7x7范圍的表,以0,0為中心,所以是-3到3 def run_one(): # 這是個每個回合都要執行的函式 global max_copy,max_list max_copy = deepcopy(max_list) #deepcopy是copy模塊的內容,用來拷貝棋盤,不懂的請自行了解"深淺拷貝" #ss = 10 - len(seeds)//30 # ss是"發芽率" 因圖形不好看廢棄這個設定 # 這是是要根據種子數量,實作"動態發芽率",種子少,發芽率高 # 種子多,發芽率低,用來限制草地規模 for x,y in seeds: if max_list[y][x] : pass else: max_list[y][x] = 1 #if randint(1,10)<=ss else 0 # seeds 是所有"種子"的串列, 如果種子落到了有植物(>0)或是荒地(<0)的格子,則什么都不會發生 # 如果恰好為0,則發芽,hp更新成1 # '#if randint(1,10)<=ss else 0'是廢棄的發芽率設定 seeds.clear() # 清空種子 for x in range(x_max): for y in range(y_max): # 兩層for遍歷所有方格 hp = max_list[y][x] # 讀取hp # 4-7 1231 # 連續的if 不同的hp有不同的處理流程 if not hp: pass elif hp < 0: hp += 1 elif hp in {2,4,6}: if around(x,y) > 15 : hp = -hp*1 if hp-3 > 0: release_seed(x,y,hp-3) hp+=1 elif hp in {1,3,5}: hp += 1 if hp == 5: release_seed(x,y,2) elif hp == 7: release_seed(x,y,1) hp = -7*4 max_list[y][x] = hp #-----------------pygame時間---------------- pygame.init() screen = pygame.display.set_mode((900,600)) pygame.display.set_caption("begin") fps = 100 fclock = pygame.time.Clock() colors = {-7: (181, 181, 181), -6: (181, 181, 181), -5: (181, 181, 181), -4: (181, 181, 181), -3: (181, 181, 181),-2: (181, 181, 181), -1: (181, 181, 181), 0:(232, 232, 232),1: (154, 255, 154), 2: (154, 255, 154), 3: (154, 255, 154),4: (0, 255, 0), 5: (0, 255, 0), 6: (0, 255, 0), 7: (0, 255, 0) } colors.update( {i:(181, 181, 181)for i in range(-35,-7)} ) go = True # go = False # go是暫停用的....按空格暫停/繼續 time = 0 num = 0 # time 是用來控制更新頻率的 # 因為我的更改 現在time是虛設.... while True: for event in pygame.event.get(): if event.type == pygame.QUIT: os._exit(0) elif event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: go = not go if go: time += 100 if time >= 50: run_one() drow() num+=1 pygame.display.set_caption("第%d輪"%(num)) time = 0 else: time+=0 fclock.tick(fps)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/169929.html
標籤:其他
上一篇:JVM系列【5】JVM常用指令
下一篇:JWT實作單點登錄
