python編寫五子棋游戲
本文實例為大家分享了python編寫五子棋游戲的具體代碼,供大家參考,具體內容如下
游戲代碼及部分注釋
import pygame#導入pygame游戲模塊import time #調用time庫import sysfrom pygame.locals import *initChessList = [] #保存的是棋盤坐標initRole = 1#1:代表白棋; 2:代表黑棋resultFlag = 0 #結果標志class StornPoint(): def __init__(self,x,y,value):’’’:param x: 代表x軸坐標:param y: 代表y軸坐標:param value: 當前坐標點的棋子:0:沒有棋子 1:白子 2:黑子’’’self.x = x #初始化成員變量self.y = yself.value = valuedef initChessSquare(x,y): #初始化棋盤 for i in range(15): # 每一行的交叉點坐標rowlist = []for j in range(15): # 每一列的交叉點坐標 pointX = x+ j*40 pointY = y+ i*40 sp = StornPoint(pointX,pointY,0) rowlist.append(sp)initChessList.append(rowlist)def eventHander(): #監聽各種事件 for event in pygame.event.get():global initRoleif event.type == QUIT:#事件類型為退出時 pygame.quit() sys.exit()if event.type == MOUSEBUTTONDOWN: #當點擊鼠標時 x,y = pygame.mouse.get_pos() #獲取點擊鼠標的位置坐標 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: #當棋盤位置為空;棋子類型為白棋 point.value = 1 #鼠標點擊時,棋子為白棋 judgeResult(i,j,1) initRole = 2#切換角色elif point.value == 0 and initRole ==2: #當棋盤位置為空;棋子類型為黑棋 point.value = 2 #鼠標點擊時,棋子為黑棋 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): # 橫向有沒有出現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 = Truebreakpass for x in range(i - 4, i + 5): # 縱向有沒有出現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 = Truebreakpass # 先判斷東北方向的對角下輸贏 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() # 初始化游戲環境 screen = pygame.display.set_mode((620,620),0,0) # 創建游戲窗口 # 第一個參數是元組:窗口的長和寬 pygame.display.set_caption('陳曉超五子棋')# 添加游戲標題 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: #當棋子類型為1時,繪制白棋 screen.blit(whiteStorn,(point.x-18,point.y-18))elif point.value == 2:#當棋子類型為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 #置空之前的獲勝結果eventHander() #調用之前定義的事件函數if __name__ == ’__main__’: main()#調用主函數繪制窗口 pass
運行后就會出現游戲的窗口,像這樣:
我們可以任意的在棋盤上落子,像這樣:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章:
