python編寫(xiě)五子棋游戲
本文實(shí)例為大家分享了python編寫(xiě)五子棋游戲的具體代碼,供大家參考,具體內(nèi)容如下
游戲代碼及部分注釋
import pygame#導(dǎo)入pygame游戲模塊import time #調(diào)用time庫(kù)import sysfrom pygame.locals import *initChessList = [] #保存的是棋盤(pán)坐標(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)點(diǎn)的棋子:0:沒(méi)有棋子 1:白子 2:黑子’’’self.x = x #初始化成員變量self.y = yself.value = valuedef initChessSquare(x,y): #初始化棋盤(pán) for i in range(15): # 每一行的交叉點(diǎn)坐標(biāo)rowlist = []for j in range(15): # 每一列的交叉點(diǎn)坐標(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)聽(tīng)各種事件 for event in pygame.event.get():global initRoleif event.type == QUIT:#事件類型為退出時(shí) pygame.quit() sys.exit()if event.type == MOUSEBUTTONDOWN: #當(dāng)點(diǎn)擊鼠標(biāo)時(shí) x,y = pygame.mouse.get_pos() #獲取點(diǎn)擊鼠標(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)棋盤(pán)位置為空;棋子類型為白棋 point.value = 1 #鼠標(biāo)點(diǎn)擊時(shí),棋子為白棋 judgeResult(i,j,1) initRole = 2#切換角色elif point.value == 0 and initRole ==2: #當(dāng)棋盤(pán)位置為空;棋子類型為黑棋 point.value = 2 #鼠標(biāo)點(diǎn)擊時(shí),棋子為黑棋 judgeResult(i,j,2) initRole = 1#切換角色break j+=1i+=1j=0def judgeResult(i,j,value): #橫向判斷 global resultFlag flag = False for x in range(j - 4, j + 5): # 橫向有沒(méi)有出現(xiàn)5連(在邊緣依次逐一遍歷,是否五個(gè)棋子的類型一樣)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 = Truebreakpass for x in range(i - 4, i + 5): # 縱向有沒(méi)有出現(xiàn)5連(在邊緣依次逐一遍歷,是否五個(gè)棋子的類型一樣)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 = Truebreakpass # 先判斷東北方向的對(duì)角下輸贏 x 列軸, y是行軸 , i 是行 j 是列(右斜向)(在邊緣依次逐一遍歷,是否五個(gè)棋子的類型一樣) 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、判斷西北方向的對(duì)角下輸贏 x 列軸, y是行軸 , i 是行 j 是列(左斜向)(在邊緣依次逐一遍歷,是否五個(gè)棋子的類型一樣) 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)建游戲窗口 # 第一個(gè)參數(shù)是元組:窗口的長(zhǎng)和寬 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')#加載 贏 時(shí)的圖片 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時(shí),繪制白棋 screen.blit(whiteStorn,(point.x-18,point.y-18))elif point.value == 2:#當(dāng)棋子類型為2時(shí),繪制黑棋 screen.blit(blackStorn,(point.x-18,point.y-18))if resultFlag >0: initChessList = [] # 清空棋盤(pán) initChessSquare(27,27) # 重新初始化棋盤(pán) screen.blit(resultStorn,(200,200)) #繪制獲勝時(shí)的圖片pygame.display.update()#更新視圖if resultFlag >0: time.sleep(3) resultFlag = 0 #置空之前的獲勝結(jié)果eventHander() #調(diào)用之前定義的事件函數(shù)if __name__ == ’__main__’: main()#調(diào)用主函數(shù)繪制窗口 pass
運(yùn)行后就會(huì)出現(xiàn)游戲的窗口,像這樣:
我們可以任意的在棋盤(pán)上落子,像這樣:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 無(wú)線標(biāo)記語(yǔ)言(WML)基礎(chǔ)之WMLScript 基礎(chǔ)第1/2頁(yè)2. 讀大數(shù)據(jù)量的XML文件的讀取問(wèn)題3. 利用CSS3新特性創(chuàng)建透明邊框三角4. 解析原生JS getComputedStyle5. PHP循環(huán)與分支知識(shí)點(diǎn)梳理6. ASP基礎(chǔ)入門(mén)第三篇(ASP腳本基礎(chǔ))7. css代碼優(yōu)化的12個(gè)技巧8. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)9. ASP實(shí)現(xiàn)加法驗(yàn)證碼10. JSP+Servlet實(shí)現(xiàn)文件上傳到服務(wù)器功能
