Python涔嬪悍濞佺敓鍛芥父鎴忕殑瑙e喅鍔炴硶

杩欑瘒鏂囩珷涓昏涓哄ぇ瀹惰缁嗕粙缁嶄簡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浜戝叾瀹冪浉鍏虫枃绔狅紒



鏈枃URL锛http://www.odweb.cn/news_show.html?id=145