1. 程式人生 > 程式設計 >Python基於pygame實現單機版五子棋對戰

Python基於pygame實現單機版五子棋對戰

python實現的五子棋,能夠自動判斷輸贏,沒有是實現電腦對戰功能

原始碼下載:pygame五子棋

# 1、引入pygame 和 pygame.locals
import pygame
from pygame.locals import *
import time
import sys
 
initChessList = []
initRole = 1 # 代表白子下 2:代表當前是黑子下
resultFlag = 0
userFlag = True
 
class StornPoint():
 def __init__(self,x,y,value = 0):
  '''
  :param x: 代表x軸座標
  :param y: 代表y軸座標
  :param value: 當前座標點的棋子:0:沒有棋子 1:白子 2:黑子
  '''
  self.x = x
  self.y = y
  self.value = value
  pass
 
 
def initChessSquare(x,y):
 '''
 初始化棋盤的座標
 :param x:
 :param y:
 :return:
 '''
 # 使用二維列表儲存了棋盤是的座標系,和每個落子點的數值
 for i in range(15):  # 每一行的交叉點座標
  rowList = []
  for j in range(15): # 每一列的交叉點座標
   pointX = x + j*40
   pointY = y + i*40
   # value = 0
   sp = StornPoint(pointX,pointY,0)
   rowList.append(sp)
   pass
  initChessList.append(rowList)
 pass
 
# 處理事件
def eventHandler():
 global userFlag
 '''
 監聽各種事件
 :return:
 '''
 for event in pygame.event.get():
 
  global initRole
  # 監聽點積退出按鈕事件
  if event.type == QUIT:
   pygame.quit()
   sys.exit()
   pass
  # 監聽滑鼠點積事件
  if event.type == MOUSEBUTTONDOWN:
   x,y = pygame.mouse.get_pos() #
   print((x,y))
   i = j = 0
   for temp in initChessList:
    for point in temp:
     if x >= point.x - 15 and x <= point.x + 15 \
      and y >= point.y - 15 and y <= point.y + 15:
      # 當前區域沒有棋子,並且是白子下
      if point.value == 0 and initRole == 1 and userFlag:
       point.value = 1
       judgeResult(i,j,1)
       initRole = 2 # 切換棋子顏色
       pass
      elif point.value == 0 and initRole == 2 and userFlag:
       point.value = 2
       judgeResult(i,2)
       initRole = 1 # 切換棋子顏色
       pass
      break
      pass
     j += 1
     pass
    i += 1
    j = 0
   pass
  pass
 
 pass
 
# 判斷輸贏函式
def judgeResult(i,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 = True
    break
    pass
 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 = True
    break
    pass
 
 # 判斷東北方向的對角線是否出現了5連
 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
    break
    pass
   pass
  pass
 
 # 判斷西北方向的對角是否出現了五連
 for x,y in zip(range(j - 4,j + 5),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
    break
    pass
   pass
  pass
 
 if flag:
  resultFlag = value
  pass
 pass
 
# 載入素材
def main():
 global resultFlag,initChessList
 initChessSquare(27,27) # 初始化棋牌
 pygame.init()   # 初始化遊戲環境
 # 建立遊戲視窗
 screen = pygame.display.set_mode((620,620),0) # 第一個引數是元組:視窗的長和寬
 # 新增遊戲標題
 pygame.display.set_caption("五子棋小遊戲")
 # 圖片的載入
 background = pygame.image.load('images/bg.png')
 blackStorn = pygame.image.load('images/storn_black.png')
 whiteStorn = pygame.image.load('images/storn_white.png')
 winStornW = pygame.image.load('images/white.png')
 winStornB = pygame.image.load('images/black.png')
 rect = blackStorn.get_rect()
 
 while True:
  screen.blit(background,(0,0))
  # 更新棋盤棋子
  for temp in initChessList:
   for point in temp:
    if point.value == 1:
     screen.blit(whiteStorn,(point.x - 18,point.y - 18))
     pass
    elif point.value == 2:
     screen.blit(blackStorn,point.y - 18))
     pass
    pass
   pass
  # 如果已經判決出輸贏
  if resultFlag > 0:
   initChessList = []  # 清空棋盤
   initChessSquare(27,27) # 重新初始化棋盤
   if resultFlag == 1:
    screen.blit(winStornW,(50,100))
   else:
    screen.blit(winStornB,100))
   pass
  pygame.display.update()
 
  if resultFlag >0:
   time.sleep(3)
   resultFlag = 0
   pass
  eventHandler()
  pass
 
 pass
 
if __name__ == "__main__":
 main()
 pass

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。