Python+Pygame實現(xiàn)彩色五子棋游戲
項目簡介
之前學python的時候 寫了個游戲來練手 用的是 pygame 沒有別的依賴
只用了一兩百行的代碼就實現(xiàn)了 整體來說功能并不算完整
項目背后的故事
這個項目是在大學的時候
偶然一個機遇交一個小朋友Python時 小朋友大概10多歲 正在打算上初一
小朋友分非常非常非常聰明?。?!
當時給他講東西 他很快就可以接受 立馬就可以模仿出來
小朋友會的東西很多 其中一項我非常感興趣哈哈 — 圍棋 好像還是業(yè)余挺高的那種(不好意思 我不太懂段位)
好像是什么定段之后就可以打職業(yè)那種?對我來說是非常非常厲害的存在了
當時我還讓他簡單的交了交我如何下圍棋 以及圍棋的一些概念
除了五子棋之外 當時還寫了 貪吃蛇、掃雷等等這些游戲
還給他講了爬蟲相關的東西 還有HTML啊CSS之類的
當時有一個游戲叫 “人類資源機器(HumanResource)” 游戲是一個通過簡單編程 控制小人來實現(xiàn)目標的游戲

就是這個游戲!當時我很驚訝 他過關速度非??欤「愕梦覊毫Χ忌蟻砹斯?/p>
當時還準備了幾頁的 “課本” 方便小朋友以后能夠回看

項目擴展思路
當然圍棋其實也是一個道理 只是計算勝負、計算氣的邏輯會不一樣
可以改進一下 使用鼠標來落子會更有意思
大家可以參考一下 主項目在GitHub上 除了單機版以外還有一個局域網(wǎng)版
運行截圖


安裝依賴
pip install pygame
或者
pip3 install pygame
運行游戲
將游戲代碼保存后 直接運行即可
上下左右移動光標 空格落子
import pygame
# 初始化
pygame.init()
# 設置窗口標題
screencaption=pygame.display.set_caption('Gobang')
# 設置大小
screen=pygame.display.set_mode([350,285])
# 初始化字體
myfont=pygame.font.Font(None,30)
textImage=myfont.render("Hello Pygame",True,[255,255,255])
screen.blit(textImage,(100,100))
# 棋子狀態(tài)0為空 1為白色 2為黑色
status_list = {}
for i in range(0, 15*18):
status_list[i] = 0
#print(status_list)
clock = pygame.time.Clock()
# 0 是白棋走 1是黑棋走
flag = 0
# 將要繪制的棋子的位置
movex = 1
movey = 1
while True:
clock.tick(30)
# 繪制棋盤
screen.fill([255,255,255])
for i in range(0, 15):
pygame.draw.line(screen,[0,0,0],[0,i*20],[280,i*20],2)
for i in range(0, 15):
pygame.draw.line(screen,[0,0,0],[i*20,0],[i*20,280],2)
# 繪制棋子
for x in range(0, 15):
for y in range(0, 15):
if status_list[x*15 + y] == 1:
pygame.draw.circle(screen,[255,0,0],[ 2 + y * 20,2 + x*20],10)
elif status_list[x*15 + y] == 2:
pygame.draw.circle(screen,[0,0,0],[ 2 + y * 20, 2 + x*20],10)
# 判斷是否獲勝
# X軸的判定
if y < 11:
# 白棋獲勝
if status_list[x*15 + y] == 1 and status_list[x*15 + y + 1] == 1 and status_list[x*15 + y + 2] == 1 and status_list[x*15 + y + 3] == 1 and status_list[x*15 + y + 4] == 1:
print("白棋勝利")
# break
# 黑棋獲勝
if status_list[x*15 + y] == 2 and status_list[x*15 + y + 1] == 2 and status_list[x*15 + y + 2] == 2 and status_list[x*15 + y + 3] == 2 and status_list[x*15 + y + 4] == 2:
print("黑棋勝利")
# break
# 判斷是否獲勝
# Y軸的判定
if x < 11:
if status_list[x*15 + y] == 1 and status_list[(x+1)*15 + y] == 1 and status_list[(x+2)*15 + y] == 1 and status_list[(x+3)*15 + y] == 1 and status_list[(x+4)*15 + y] == 1:
print("白棋勝利")
# break
if status_list[x*15 + y] == 2 and status_list[(x+1)*15 + y] == 2 and status_list[(x+2)*15 + y] == 2 and status_list[(x+3)*15 + y] == 2 and status_list[(x+4)*15 + y] == 2:
print("黑棋勝利")
# break
# 判斷是否獲勝
# 斜著判斷 正對角線
if status_list[x*15 + y] == 1 and status_list[(x+1)*15 + (y+1)] == 1 and status_list[(x+2)*15 + (y+2)] == 1 and status_list[(x+3)*15 + (y+3)] == 1 and status_list[(x+4)*15 + (y+4)] == 1:
print("白棋勝利")
# break
if status_list[x*15 + y] == 2 and status_list[(x+1)*15 + (y+1)] == 2 and status_list[(x+2)*15 + (y+2)] == 2 and status_list[(x+3)*15 + (y+3)] == 2 and status_list[(x+4)*15 + (y+4)] == 2:
print("黑棋勝利")
# break
# 判斷是否獲勝
# 斜著判斷 反對角線
if status_list[x*15 + y] == 1 and status_list[(x+1)*15 + (y-1)] == 1 and status_list[(x+2)*15 + (y-2)] == 1 and status_list[(x+3)*15 + (y-3)] == 1 and status_list[(x+4)*15 + (y-4)] == 1:
print("白棋勝利")
# break
if status_list[x*15 + y] == 2 and status_list[(x+1)*15 + (y-1)] == 2 and status_list[(x+2)*15 + (y-2)] == 2 and status_list[(x+3)*15 + (y-3)] == 2 and status_list[(x+4)*15 + (y-4)] == 2:
print("黑棋勝利")
# break
# 繪制落棋位置
pygame.draw.circle(screen,[0,0,0],[ 2 + movex*20, 2 + movey*20],10,3)
# 繪制文字 顯示到誰落棋子
if flag == 0:
textImage=myfont.render("White",True,[255,0,0])
else:
textImage=myfont.render("Black",True,[0,0,255])
screen.blit(textImage,(290,10))
# 判斷事件
for event in pygame.event.get():
# 退出事件
if event.type==pygame.QUIT:
pygame.quit()
quit()
# 鍵盤事件
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
if movex > 0:
movex = movex - 1
if event.key == pygame.K_RIGHT:
if movex < 14:
movex = movex + 1
if event.key == pygame.K_UP:
if movey > 0:
movey = movey - 1
if event.key == pygame.K_DOWN:
if movey < 14:
movey = movey + 1
if event.key == pygame.K_SPACE:
if flag == 0:
if status_list[movey * 15 + movex] == 0:
status_list[movey * 15 + movex] = 1
flag = 1
elif flag == 1:
if status_list[movey * 15 + movex] == 0:
status_list[movey * 15 + movex] = 2
flag = 0
# 刷新頁面
pygame.display.flip()
print("Done!")
以上就是Python+Pygame實現(xiàn)彩色五子棋游戲的詳細內(nèi)容,更多關于Python Pygame彩色五子棋游戲的資料請關注腳本之家其它相關文章!
相關文章
python+OpenCV人臉識別考勤系統(tǒng)實現(xiàn)的詳細代碼
作為一個基于人臉識別算法的考勤系統(tǒng)的設計與實現(xiàn)教程,以下內(nèi)容將提供詳細的步驟和代碼示例。本教程將使用 Python 語言和 OpenCV 庫進行實現(xiàn),需要的朋友可以參考下2023-05-05
利用Python第三方庫xlwt寫入數(shù)據(jù)到Excel工作表實例代碼
大家應該都知道xlwt是python中寫入到excel的庫,下面這篇文章主要給大家介紹了關于利用Python第三方庫xlwt寫入數(shù)據(jù)到Excel工作表的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-07-07
python判斷一個集合是否包含了另外一個集合中所有項的方法
這篇文章主要介紹了python判斷一個集合是否包含了另外一個集合中所有項的方法,涉及Python集合操作的相關技巧,需要的朋友可以參考下2015-06-06
matplotlib 范圍選區(qū)(SpanSelector)的使用
這篇文章主要介紹了matplotlib 范圍選區(qū)(SpanSelector)的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-02-02

