python編寫五子棋游戲
更新時間:2021年05月25日 09:32:42 作者:MiManchi729
這篇文章主要為大家詳細介紹了python編寫五子棋游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了python編寫五子棋游戲的具體代碼,供大家參考,具體內(nèi)容如下
游戲代碼及部分注釋
import pygame #導(dǎo)入pygame游戲模塊 import time #調(diào)用time庫 import sys from pygame.locals import * initChessList = [] #保存的是棋盤坐標(biāo) initRole = 1 #1:代表白棋; 2:代表黑棋 resultFlag = 0 #結(jié)果標(biāo)志 class StornPoint(): def __init__(self,x,y,value): ''' :param x: 代表x軸坐標(biāo) :param y: 代表y軸坐標(biāo) :param value: 當(dāng)前坐標(biāo)點的棋子:0:沒有棋子 1:白子 2:黑子 ''' self.x = x #初始化成員變量 self.y = y self.value = value def initChessSquare(x,y): #初始化棋盤 for i in range(15): # 每一行的交叉點坐標(biāo) rowlist = [] for j in range(15): # 每一列的交叉點坐標(biāo) pointX = x+ j*40 pointY = y+ i*40 sp = StornPoint(pointX,pointY,0) rowlist.append(sp) initChessList.append(rowlist) def eventHander(): #監(jiān)聽各種事件 for event in pygame.event.get(): global initRole if event.type == QUIT:#事件類型為退出時 pygame.quit() sys.exit() if event.type == MOUSEBUTTONDOWN: #當(dāng)點擊鼠標(biāo)時 x,y = pygame.mouse.get_pos() #獲取點擊鼠標(biāo)的位置坐標(biāo) i=0 j=0 for temp in initChessList: for point in temp: if x>=point.x-10 and x<=point.x+10 and y>=point.y-10 and y<=point.y+10: if point.value == 0 and initRole == 1: #當(dāng)棋盤位置為空;棋子類型為白棋 point.value = 1 #鼠標(biāo)點擊時,棋子為白棋 judgeResult(i,j,1) initRole = 2 #切換角色 elif point.value == 0 and initRole ==2: #當(dāng)棋盤位置為空;棋子類型為黑棋 point.value = 2 #鼠標(biāo)點擊時,棋子為黑棋 judgeResult(i,j,2) initRole = 1 #切換角色 break j+=1 i+=1 j=0 def judgeResult(i,j,value): #橫向判斷 global resultFlag flag = False for x in range(j - 4, j + 5): # 橫向有沒有出現(xiàn)5連(在邊緣依次逐一遍歷,是否五個棋子的類型一樣) if x >= 0 and x + 4 < 15 : if initChessList[i][x].value == value and \ initChessList[i][x + 1].value == value and \ initChessList[i][x + 2].value == value and \ initChessList[i][x + 3].value == value and \ initChessList[i][x + 4].value == value : flag = True break pass for x in range(i - 4, i + 5): # 縱向有沒有出現(xiàn)5連(在邊緣依次逐一遍歷,是否五個棋子的類型一樣) if x >= 0 and x + 4 < 15: if initChessList[x][j].value == value and \ initChessList[x + 1][j].value == value and \ initChessList[x + 2][j].value == value and \ initChessList[x + 3][j].value == value and \ initChessList[x + 4][j].value == value: flag = True break pass # 先判斷東北方向的對角下輸贏 x 列軸, y是行軸 , i 是行 j 是列(右斜向)(在邊緣依次逐一遍歷,是否五個棋子的類型一樣) for x, y in zip(range(j + 4, j - 5, -1), range(i - 4, i + 5)): if x >= 0 and x + 4 < 15 and y + 4 >= 0 and y < 15: if initChessList[y][x].value == value and \ initChessList[y - 1][x + 1].value == value and \ initChessList[y - 2][x + 2].value == value and \ initChessList[y - 3][x + 3].value == value and \ initChessList[y - 4][x + 4].value == value: flag = True # 2、判斷西北方向的對角下輸贏 x 列軸, y是行軸 , i 是行 j 是列(左斜向)(在邊緣依次逐一遍歷,是否五個棋子的類型一樣) for x, y in zip(range(j - 4, j + 5), range(i - 4, i + 5)): if x >= 0 and x + 4 < 15 and y >= 0 and y + 4 < 15: if initChessList[y][x].value == value and \ initChessList[y + 1][x + 1].value == value and \ initChessList[y + 2][x + 2].value == value and \ initChessList[y + 3][x + 3].value == value and \ initChessList[y + 4][x + 4].value == value: flag = True if flag: #如果條件成立,證明五子連珠 resultFlag = value #獲取成立的棋子顏色 print("白棋贏" if value ==1 else "黑棋贏") # 加載素材 def main(): global initChessList,resultFlag initChessSquare(27,27) pygame.init() # 初始化游戲環(huán)境 screen = pygame.display.set_mode((620,620),0,0) # 創(chuàng)建游戲窗口 # 第一個參數(shù)是元組:窗口的長和寬 pygame.display.set_caption("陳曉超五子棋") # 添加游戲標(biāo)題 background = pygame.image.load("D:/cxc/4.png") #加載背景圖片 whiteStorn = pygame.image.load("D:/cxc/2.png") #加載白棋圖片 blackStorn = pygame.image.load("D:/cxc/1.png") #加載黑棋圖片 resultStorn = pygame.image.load("D:/cxc/3.png")#加載 贏 時的圖片 rect = blackStorn.get_rect() while True: screen.blit(background,(0,0)) for temp in initChessList: for point in temp: if point.value == 1: #當(dāng)棋子類型為1時,繪制白棋 screen.blit(whiteStorn,(point.x-18,point.y-18)) elif point.value == 2: #當(dāng)棋子類型為2時,繪制黑棋 screen.blit(blackStorn,(point.x-18,point.y-18)) if resultFlag >0: initChessList = [] # 清空棋盤 initChessSquare(27,27) # 重新初始化棋盤 screen.blit(resultStorn,(200,200)) #繪制獲勝時的圖片 pygame.display.update() #更新視圖 if resultFlag >0: time.sleep(3) resultFlag = 0 #置空之前的獲勝結(jié)果 eventHander() #調(diào)用之前定義的事件函數(shù) if __name__ == '__main__': main() #調(diào)用主函數(shù)繪制窗口 pass
運行后就會出現(xiàn)游戲的窗口,像這樣:
我們可以任意的在棋盤上落子,像這樣:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
淺談Python numpy創(chuàng)建空數(shù)組的問題
今天遇到一個小小的問題,是關(guān)于numpy創(chuàng)建空數(shù)組,今天特地整理了這篇文章,文中作出了非常詳細的介紹,對正在學(xué)習(xí)python的小伙伴們有很好的幫助,需要的朋友可以參考下2021-05-05Python繪圖系統(tǒng)之繪制散點圖,極坐標(biāo)和子圖
這篇文章主要為大家詳細介紹了如何基于Python實現(xiàn)一個繪圖系統(tǒng),可以支持繪制散點圖,極坐標(biāo)和子圖,文中的示例代碼講解詳細,感興趣的可以了解下2023-09-09進行數(shù)據(jù)處理的6個?Python?代碼塊分享
這篇文章主要介紹了進行數(shù)據(jù)處理6個Python代碼塊的分享,分享內(nèi)容有選取有空值的行、快速替換列值、對列進行分區(qū)、將一列分為多列等內(nèi)容,需要的朋友可以參考一下2022-04-04