杩欑瘒鏂囩珷涓昏涓哄ぇ瀹惰缁嗕粙缁嶄簡Python涔嬪悍濞佺敓鍛芥父鎴忕殑绠鍗曠ず渚嬶紝鍏锋湁涓瀹氱殑鍙傝冧环鍊硷紝鍙互鐢ㄦ潵鍙傝冧竴涓嬨
鎰熷叴瓒g殑灏忎紮浼达紝涓嬮潰涓璧疯窡闅弍hp鏁欑▼鐨勯洴闆潵鐪嬬湅鍚э紒
鍓嶈█
搴峰▉鐢熷懡娓告垙璁捐骞朵笉闅撅紝鎴戠殑鎬濊矾灏辨槸鍊熷姪pygame杩涜澶栬鐨勫睍绀猴紝鏈杩戜竴娈垫椂闂寸殑娓告垙椤圭洰閮芥槸浣跨敤pygame杩涜鐨勶紝鍋氳捣鏉ユ瘮杈冮『鍒┿傚唴閮ㄤ唬鐮佺殑瀹炵幇涔熸瘮杈冪畝鍗曟牴鎹粬鐨勮鍒欐垜浠渶瑕佺殑鏄娆$殑璁$畻鍜屽垽鏂紝鍐嶅埛鏂版暟缁勩
涓銆佸悍濞佺敓鍛芥父鎴忚鍒
褰撳懆鍥翠粎鏈1涓垨娌℃湁瀛樻椿缁嗚優鏃讹紝 鍘熸潵鐨勫瓨娲荤粏鑳炶繘鍏ユ浜$姸鎬併傦紙妯℃嫙鐢熷懡鏁伴噺绋灏戯級褰撳懆鍥存湁2涓垨3涓瓨娲荤粏鑳炴椂锛 缃戞牸淇濇寔鍘熸牱銆傚綋鍛ㄥ洿鏈4涓強浠ヤ笂瀛樻椿缁嗚優鏃讹紝鍘熸潵鐨勫瓨娲荤粏鑳炰害杩涘叆姝讳骸鐘舵併傦紙妯℃嫙鐢熷懡鏁伴噺杩囧锛夊綋鍛ㄥ洿鏈3涓瓨娲荤粏鑳炴椂锛岀┖鐧界綉鏍煎彉鎴愬瓨娲荤粏鑳炪傦紙妯℃嫙绻佹畺锛
浜屻佽璁℃祦绋
1. 寮曞叆搴
浠g爜濡備笅锛堢ず渚嬶級锛
浠g爜濡備笅锛
<code>
import sys
import random
import numpy as np
import pygame</code>
Python瀹炴垬涔嬪疄鐜板悍濞佺敓鍛芥父鎴
2. 璁捐鎬濊矾
鍦ㄨ繖绡囧崥瀹㈤噷闈㈡垜浠疄鐜颁簡涓嬮潰绗竴涓浘鐨勫唴瀹广
鎰熷叴瓒g殑鏈嬪弸涔熷彲浠ュ姞涓嬮潰鐨勬濊矾锛
3. 璁捐绐楀彛
棣栧厛鎴戜滑鍊熺敤浜嗕箣鍓嶇殑pygame绐楀彛鐨勪唬鐮侊紝瀹炵幇绐楀彛鐨勮皟鐢ㄣ備笌涔嬪墠鏈夋墍涓嶅悓鐨勫彉鍖栨槸锛屾垜浠湪灞曞紑绐楀彛涔嬪墠棣栧厛纭畾澶у皬銆傛垜浠疄鐜颁簡娓告垙澶у皬鐨勮嚜瀹氫箟锛岀獥鍙eぇ灏忕殑鑷畾涔夛紝鍙互涓庡悗闈㈡父鎴忚璁″鍔犵粺涓鎬э紝澧炲姞缇庤鎬с
涓棿鐨勯儴鍒嗕唬鐮佹槸涓浜涢鑹茬殑璋冨嚭锛屽拰绐楀彛鐨勫~鍏呫
浠g爜濡備笅锛
<code>
import sys
import random
import numpy as np
import pygame
pygame.init()#鍒濆鍖杋nit()鍙婅缃
n=int(input("璇疯緭鍏ラ樁鏁帮細"))
size=width,height=50*n+2,50*n+2
screen=pygame.display.set_mode(size)#绐楀彛澶у皬
pygame.display.set_caption("搴峰▉鐢熷懡娓告垙")#绐楀彛鍚嶅瓧
icon=pygame.image.load("Icon.jpg")
pygame.display.set_icon(icon)
BLACK=pygame.Color("black")
GAINSBORO=pygame.Color("gainsboro")
MOCCASIN=pygame.Color("moccasin")
WHITE=pygame.Color("white")
screen.fill(MOCCASIN)
fps=5
fclock=pygame.time.Clock()#鍒涘缓涓涓狢lock瀵硅薄鐢ㄤ簬鎿嶄綔鏃堕棿
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT: # 鐐瑰嚮浜嗛鍑
sys.exit() # 閫鍑
pygame.display.update() # 瀵规樉绀虹獥鍙h繘琛屾洿鏂帮紝榛樿绐楀彛鍏ㄩ儴閲嶇粯
fclock.tick(fps) # 绐楀彛鍒锋柊閫熷害锛屾瘡绉3娆
</code>
Python瀹炴垬涔嬪疄鐜板悍濞佺敓鍛芥父鎴
涓夈佸垵濮嬬敓鍛界煩闃
鎴戜滑閫氳繃寰幆浜х敓闅忔満鏁版潵浜х敓闅忔満鐨勫垵濮嬬敓鍛姐
random.randint(a,b)锛氫骇鐢熶竴涓綅浜巃-b涔嬮棿鐨勯殢鏈烘暣鏁帮紝鍖呮嫭a锛宐銆
浠g爜濡備笅锛
<code>
## 鐢熸垚鍒濆鐢熷懡
a=[]
for i in range(0,n):
a.append([])
for j in range(0,n):
a[i].append(random.randint(0,1))</code>
Python瀹炴垬涔嬪疄鐜板悍濞佺敓鍛芥父鎴
鍥涖佸懆鍥寸敓鍛界殑妫娴
鎴戜滑灏嗙敓鍛藉懆鍥村叓涓綅缃殑绱㈠紩宸煎瓨鏀惧湪涓涓垪琛ㄤ腑锛岄氳繃寰幆妫娴嬫湁鏁堜綅缃笂鐢熷懡鐨勪釜鏁帮紝瀛樻斁鍏ュ垪琛ㄤ腑銆傚寘鍚袱涓垽鏂
1.棣栧厛瑕佸垽鏂綅缃殑鏈夋晥鎬
2.鍙︿竴涓槸鍒ゆ柇鏄惁鏈夌敓鍛
浠g爜濡備笅锛
<code>
direction = [[-1, -1], [0, -1], [1, -1], [1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0]]
c = []
# 璁$畻鍛ㄥ洿鐢熷懡涓暟
for i in range(0, n):
c.append([])
for j in range(0, n):
count = 0 # 姣忎竴涓柟鏍
for o in direction:
ide = np.array([i, j]) + np.array(o)
# 淇濊瘉鍒ゆ柇鐨勪綅缃湪鑼冨洿鍐咃紝閽堝杈圭晫鏂规牸
if 0 <= ide[0] < n and 0 <= ide[1] < n:
if a[ide[0]][ide[1]] == 1:
count += 1
c[i].append(count)
</code>
Python瀹炴垬涔嬪疄鐜板悍濞佺敓鍛芥父鎴
浜斻佺敓鍛界殑鍙樺寲
灏嗘瘡涓綅缃笂鐨勭敓鍛芥牴鎹鏁板垪琛ㄨ繘琛屾洿鏂帮紝鏇存柊鐢熷懡鐭╅樀銆
浠g爜濡備笅锛
<code>
## 鎸夌収鐢熷懡鐨勫彂灞曡寰嬭繘琛屾柊涓杞殑鐢熼潰鍙樺寲
for i in range(0, n):
for j in range(0, n):
if c[i][j] <= 1 or c[i][j] >= 4:#褰撶敓鍛界█灏戞垨鑰呰繃澶氭椂鐢熷懡姝讳骸
a[i][j] = 0
elif c[i][j] == 3:#褰撶敓鍛界殑鍛ㄥ洿鏈変笁涓敓鍛芥椂锛岀敓鎴愭柊鐢熷懡
a[i][j] = 1
</code>
Python瀹炴垬涔嬪疄鐜板悍濞佺敓鍛芥父鎴
鍏佺敓鍛界殑灞曠ず
鐢诲嚭鐢熷懡鐨勬柟鏍煎緢绠鍗曪紝鎴戜滑璋冪敤pygame鐨勭粯鍒跺浘褰唬鐮侊細
pygame.draw.rect(screen, BLACK, (i*50, j*50, 50, 50))锛氳繖鏍峰睍绀哄嚭鏉ョ殑鏄竴涓~鍏呯殑鐭╁舰锛屽睍绀哄湪锛歴creen涓婏紝鍒濆浣嶇疆涓猴細(i * 50, j * 50)锛屽ぇ灏忎负锛(50, 50)锛岄鑹蹭负锛歜lack锛岄粯璁よ竟妗嗕负0锛屽嵆鍏ㄩ儴濉厖鐨勭煩褰紝褰撳悗闈㈡坊鍔犱竴涓暟瀛楁椂锛屼唬琛ㄦ棤濉厖锛屾绾夸负鏁板瓧澶у皬鐨勭煩褰紝濡傦細pygame.draw.rect(screen, GAINSBORO, (i*50, j*50, 50, 50),2)浠h〃涓嶅~鍏咃紝妗嗙嚎涓2
閫氳繃鐢诲浘閲嶅彔锛屾垜浠疄鐜版瘡涓敓鍛戒箣闂寸殑闂撮殧鐨勬牱寮忥紝澧炲己娓叉煋鏁堟灉銆
浠g爜濡備笅锛
<code>
for i in range(0, n):
for j in range(0, n):
if a[i][j]==1:
#鍏堢敾涓涓弧濉厖鐨勬柟鏍硷紝鏈夌敓鍛芥柟鏍
pygame.draw.rect(screen, BLACK, (i*50, j*50, 50, 50))
#鍐嶇敾涓涓笉濉厖锛屾绾夸负2鐨勬柟鏍硷紝濂楀湪涓婇潰鐨勬柟鏍间笂闈
pygame.draw.rect(screen, GAINSBORO, (i*50, j*50, 50, 50),2)
else:#鏃犵敓鍛芥柟鏍
pygame.draw.rect(screen, WHITE, (i*50, j*50, 50, 50))
pygame.draw.rect(screen, GAINSBORO, (i*50, j*50, 50, 50),2)
</code>
Python瀹炴垬涔嬪疄鐜板悍濞佺敓鍛芥父鎴
涓冦佸畬鏁翠唬鐮
浠g爜濡備笅锛
<code>
"""
# -*- coding: utf-8 -*-
# @Time : 2021/4/23 0023 17:14
# @Author : 婧愭潵寰堝阀
# @FileName: 搴峰▉鐢熷懡娓告垙2.py
# @Software: PyCharm
# @Blog 锛歨ttps://blog.csdn.net/qq_44793283
"""
import sys
import random
import numpy as np
import pygame
pygame.init()#鍒濆鍖杋nit()鍙婅缃
n=int(input("璇疯緭鍏ラ樁鏁帮細"))
size=width,height=50*n+2,50*n+2
screen=pygame.display.set_mode(size)#绐楀彛澶у皬
pygame.display.set_caption("搴峰▉鐢熷懡娓告垙")#绐楀彛鍚嶅瓧
icon=pygame.image.load("Icon.jpg")
pygame.display.set_icon(icon)
BLACK=pygame.Color("black")
GAINSBORO=pygame.Color("gainsboro")
MOCCASIN=pygame.Color("moccasin")
WHITE=pygame.Color("white")
screen.fill(MOCCASIN)
fps=1
fclock=pygame.time.Clock()#鍒涘缓涓涓狢lock瀵硅薄鐢ㄤ簬鎿嶄綔鏃堕棿
## 鐢熸垚鍒濆鐢熷懡
a=[]
for i in range(0,n):
a.append([])
for j in range(0,n):
a[i].append(random.randint(0,1))
## 鍏釜鏂逛綅鐨勭储寮曞彉鍖
direction = [[-1, -1], [0, -1], [1, -1], [1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0]]
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT: # 鐐瑰嚮浜嗛鍑
sys.exit() # 閫鍑
c = []
# 璁$畻鍛ㄥ洿鐢熷懡涓暟
for i in range(0, n):
c.append([])
for j in range(0, n):
count = 0 # 姣忎竴涓柟鏍
for o in direction:
ide = np.array([i, j]) + np.array(o)
# 淇濊瘉鍒ゆ柇鐨勪綅缃湪鑼冨洿鍐咃紝閽堝杈圭晫鏂规牸
if 0 <= ide[0] < n and 0 <= ide[1] < n:
if a[ide[0]][ide[1]] == 1:
count += 1
c[i].append(count)
## 鎸夌収鐢熷懡鐨勫彂灞曡寰嬭繘琛屾柊涓杞殑鐢熼潰鍙樺寲
for i in range(0, n):
for j in range(0, n):
if c[i][j] <= 1 or c[i][j] >= 4:#褰撶敓鍛界█灏戞垨鑰呰繃澶氭椂鐢熷懡姝讳骸
a[i][j] = 0
elif c[i][j] == 3:#褰撶敓鍛界殑鍛ㄥ洿鏈変笁涓敓鍛芥椂锛岀敓鎴愭柊鐢熷懡
a[i][j] = 1
for i in range(0, n):
for j in range(0, n):
if a[i][j]==1:
#鍏堢敾涓涓弧濉厖鐨勬柟鏍硷紝鏈夌敓鍛芥柟鏍
pygame.draw.rect(screen, BLACK, (i*50, j*50, 50, 50))
#鍐嶇敾涓涓笉濉厖锛屾绾夸负2鐨勬柟鏍硷紝濂楀湪涓婇潰鐨勬柟鏍间笂闈
pygame.draw.rect(screen, GAINSBORO, (i*50, j*50, 50, 50),2)
else:#鏃犵敓鍛芥柟鏍
pygame.draw.rect(screen, WHITE, (i*50, j*50, 50, 50))
pygame.draw.rect(screen, GAINSBORO, (i*50, j*50, 50, 50),2)
print(np.array(a))
pygame.display.update() # 瀵规樉绀虹獥鍙h繘琛屾洿鏂帮紝榛樿绐楀彛鍏ㄩ儴閲嶇粯
fclock.tick(fps) # 绐楀彛鍒锋柊閫熷害锛屾瘡绉3娆
</code>
Python瀹炴垬涔嬪疄鐜板悍濞佺敓鍛芥父鎴
鎬荤粨
杩欎釜娓告垙鐨勫疄闄呰璁″苟涓嶆槸寰堝洶闅撅紝鎴戜滑闇瑕佸皢鍏蜂綋鐨勬濊矾鐞嗛『锛屽摢涓姝ラ鍏堣繘琛岋紝闇瑕佹垜浠噯澶囧摢浜涘瓨鍌ㄧ殑鐭╅樀绛夌瓑銆傚敮涓鐨勫集璺槸鍛ㄥ洿鐢熷懡涓暟鐨勬娴嬶紝鎴戠殑鏂规硶鏄┓涓炬硶锛屽皢姣忎釜浣嶇疆绱㈠紩鍙樊鎵嬪姩璁$畻瀛樺偍璧锋潵銆傚湪灏忕殑璁$畻閲忎笅锛岃繖灞傚惊鐜绠楀苟涓嶄細娴垂寰堝鏃堕棿锛屽鏋滄偍瀵规鏈夊ソ鐨勫缓璁杩庝氦娴併備篃娆㈣繋瀵瑰悗闈㈢殑娓告垙缁撴潫杩涜浜ゆ祦锛屽悗缁垜鍙互琛ヤ笂缁撴潫娓告垙鐨勬柟娉曘
鍒版杩欑瘒鍏充簬Python瀹炴垬涔嬪疄鐜板悍濞佺敓鍛芥父鎴忕殑鏂囩珷灏变粙缁嶅埌杩欎簡,鏇村鐩稿叧Python瀹炵幇搴峰▉鐢熷懡娓告垙鍐呭璇锋悳绱hp鏁欑▼浠ュ墠鐨勬枃绔犳垨缁х画娴忚涓嬮潰鐨勭浉鍏虫枃绔犲笇鏈涘ぇ瀹朵互鍚庡澶氭敮鎸乸hp鏁欑▼锛
浠ヤ笂灏辨槸Python涔嬪悍濞佺敓鍛芥父鎴忕殑瑙e喅鍔炴硶鐨勮缁嗗唴瀹癸紝鏇村淇℃伅璇峰叧娉∣D浜戝叾瀹冪浉鍏虫枃绔狅紒