最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

pygame實現(xiàn)五子棋游戲

 更新時間:2019年10月29日 11:03:12   作者:冰風漫天  
這篇文章主要為大家詳細介紹了pygame實現(xiàn)五子棋游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了pygame五子棋游戲的具體代碼,供大家參考,具體內(nèi)容如下

1.設(shè)置棋盤

五子棋標準棋盤是15x15的,如果我們每個格子的大小是40x40的話,棋盤應該是40x(15-1)=560的寬度,我們在四面各保留60的邊距,那么窗口的長寬各是40x(15-1)+60x2

# -*- coding=utf-8 -*-
import random
import pygame
pygame.init()

space = 60 # 四周留下的邊距
cell_size = 40 # 每個格子大小
cell_num = 15
grid_size = cell_size * (cell_num - 1) + space * 2 # 棋盤的大小
screencaption = pygame.display.set_caption('FIR')
screen = pygame.display.set_mode((grid_size,grid_size)) #設(shè)置窗口長寬

while True:
 for event in pygame.event.get():
  if event.type == pygame.QUIT:
  pygame.quit()
  exit()

 screen.fill((0,0,150)) # 將界面設(shè)置為藍色

 for x in range(0,cell_size*cell_num,cell_size):
 pygame.draw.line(screen,(200,200,200),(x+space,0+space),(x+space,cell_size*(cell_num-1)+space),1)
 for y in range(0,cell_size*cell_num,cell_size):
 pygame.draw.line(screen,(200,200,200),(0+space,y+space),(cell_size*(cell_num-1)+space,y+space),1)
 
 pygame.display.update() # 必須調(diào)用update才能看到繪圖顯示

2.落子

首先我們定義一個chess_arr數(shù)組用于存儲落到棋盤上的棋子

chess_arr = []

然后在游戲主循環(huán)監(jiān)聽下鼠標彈起事件,然后在捕捉到鼠標彈起事件時獲取鼠標位置并把位置添加進chess_arr

for event in pygame.event.get():
 ……
 
  if event.type == pygame.MOUSEBUTTONUP: # 鼠標彈起
  x, y = pygame.mouse.get_pos() # 獲取鼠標位置
  chess_arr.append((x,y))

最后我們在pygame.display.update()前將棋子繪制出來看看效果

可以看到,現(xiàn)在已經(jīng)能點出棋子了,但是棋子的位置不是縱橫線的交叉點,所以我們必須對鼠標位置進行取整,不能把x,y這個位置加的這么隨意,處理下x,y位置的代碼如下

for event in pygame.event.get():
 ……
  if event.type == pygame.MOUSEBUTTONUP: # 鼠標彈起
  x, y = pygame.mouse.get_pos() # 獲取鼠標位置
  xi = int(round((x - space)*1.0/cell_size)) # 獲取到x方向上取整的序號
  yi = int(round((y - space)*1.0/cell_size)) # 獲取到y(tǒng)方向上取整的序號
  if xi>=0 and xi<cell_num and yi>=0 and yi<cell_num:
   chess_arr.append((xi*cell_size+space,yi*cell_size+space))

現(xiàn)在發(fā)現(xiàn)落子位置靠譜多了

為了代碼的可讀性更好點,以后一些棋盤計算更方便,我們把放入chess_arr數(shù)組的絕對坐標改成放入的是格子的序號,也就是把

chess_arr.append((xi*cell_size+space,yi*cell_size+space))

改成

chess_arr.append((xi,yi))

然后在畫棋子的地方也稍作修改,把

pygame.draw.circle(screen,(205,205,205), [x, y], 16,16)

改成

pygame.draw.circle(screen,(205,205,205), [x*cell_size+space, y*cell_size+space], 16,16)

接下來還有個問題,因為進到chess_arr數(shù)組前并沒有判斷某一位置是否已經(jīng)有棋子,存在重復落子的情況,所以這邊還要多加個判斷,因為python語言夠強大,可以直接判斷是否包含tuple或者數(shù)組,所以只要多加一個(xi,yi) not in chess_arr的判斷就好了,開不開森~

for event in pygame.event.get():
  ……
  if event.type == pygame.MOUSEBUTTONUP: # 鼠標彈起
  ……
  if xi>=0 and xi<cell_num and yi>=0 and yi<cell_num and (xi,yi) not in chess_arr:
   chess_arr.append((xi,yi))

為免代碼偏差,先更新下目前的完整代碼

# -*- coding=utf-8 -*-
import random
import pygame
from pygame.locals import MOUSEBUTTONUP
pygame.init()

space = 60 # 四周留下的邊距
cell_size = 40 # 每個格子大小
cell_num = 15
grid_size = cell_size * (cell_num - 1) + space * 2 # 棋盤的大小
screencaption = pygame.display.set_caption('FIR')
screen = pygame.display.set_mode((grid_size,grid_size)) #設(shè)置窗口長寬

chess_arr = []

while True:
 for event in pygame.event.get():
  if event.type == pygame.QUIT:
  pygame.quit()
  exit()
 
  if event.type == pygame.MOUSEBUTTONUP: # 鼠標彈起
  x, y = pygame.mouse.get_pos() # 獲取鼠標位置
  xi = int(round((x - space)*1.0/cell_size)) # 獲取到x方向上取整的序號
  yi = int(round((y - space)*1.0/cell_size)) # 獲取到y(tǒng)方向上取整的序號
  if xi>=0 and xi<cell_num and yi>=0 and yi<cell_num and (xi,yi) not in chess_arr:
   chess_arr.append((xi,yi))

 screen.fill((0,0,150)) # 將界面設(shè)置為藍色

 for x in range(0,cell_size*cell_num,cell_size):
 pygame.draw.line(screen,(200,200,200),(x+space,0+space),(x+space,cell_size*(cell_num-1)+space),1)
 for y in range(0,cell_size*cell_num,cell_size):
 pygame.draw.line(screen,(200,200,200),(0+space,y+space),(cell_size*(cell_num-1)+space,y+space),1)

 for x, y in chess_arr:
 pygame.draw.circle(screen,(205,205,205), [x*cell_size+space, y*cell_size+space], 16,16)

 pygame.display.update() # 必須調(diào)用update才能看到繪圖顯示

3.區(qū)分黑白子

這里常規(guī)的想法可能有這么兩種:1.chess_arr理論應該是黑白相間的,一個隔一個不同顏色畫就好了(這種在不考慮正規(guī)比賽五手兩打或者讓子的情況下是沒問題的) 2.往chess_arr里填(x,y)時多填一個黑白標記改成(x,y,flag) ,這里我們選擇第二種方案
首先,我們?nèi)侄x個flag變量

flag = 1 # 1黑 2白

我們把

if xi>=0 and xi<cell_num and yi>=0 and yi<cell_num and (xi,yi) not in chess_arr:
   chess_arr.append((xi,yi))

這里改成

if xi>=0 and xi<cell_num and yi>=0 and yi<cell_num and (xi,yi,1) not in chess_arr and (xi,yi,2) not in chess_arr:
   chess_arr.append((xi,yi,flag))
   flag = 2 if flag == 1 else 2

再把畫棋的

for x, y in chess_arr:
 pygame.draw.circle(screen,(205,205,205), [x*cell_size+space, y*cell_size+space], 16,16)

改成

for x, y, c in chess_arr:
 chess_color = (30,30,30) if c == 1 else (225,225,225)
 pygame.draw.circle(screen, chess_color, [x*cell_size+space, y*cell_size+space], 16,16)

現(xiàn)在看起來有點像那么回事了

4.判斷輸贏

判斷輸贏的關(guān)鍵當然還是使用chess_arr這個數(shù)組,這個數(shù)組用來判斷勝利并不太方便,我們把它轉(zhuǎn)一個15*15的二維數(shù)組來計算,轉(zhuǎn)換代碼如下

m = [[0]*15 for i in range(15)] # 先定義一個15*15的全0數(shù)組
for x, y, c in chess_arr:
 m[y][x] = 1 # 上面有棋則標1

我們把這代碼一起放到一個check_win(chess_arr, flag)函數(shù)里,用于判斷某一方是否勝利,基本流程是分別判斷最后一顆落下的子的橫線、豎線、斜線上是不是有5個以上子,有則返回True,函數(shù)代碼如下:

def get_one_dire_num(lx, ly, dx, dy, m):
 tx = lx
 ty = ly
 s = 0
 while True:
 tx += dx
 ty += dy
 if tx < 0 or tx >= cell_num or ty < 0 or ty >= cell_num or m[ty][tx] == 0: return s
 s+=1

def check_win(chess_arr, flag):
 m = [[0]*cell_num for i in range(cell_num)] # 先定義一個15*15的全0的數(shù)組,不能用[[0]*cell_num]*cell_num的方式去定義因為一位數(shù)組會被重復引用
 for x, y, c in chess_arr:
 if c == flag:
  m[y][x] = 1 # 上面有棋則標1
 lx = chess_arr[-1][0] # 最后一個子的x
 ly = chess_arr[-1][1] # 最后一個子的y
 dire_arr = [[(-1,0),(1,0)],[(0,-1),(0,1)],[(-1,-1),(1,1)],[(-1,1),(1,-1)]] # 4個方向數(shù)組,往左+往右、往上+往下、往左上+往右下、往左下+往右上,4組判斷方向
 
 for dire1,dire2 in dire_arr:
 dx, dy = dire1
 num1 = get_one_dire_num(lx, ly, dx, dy, m)
 dx, dy = dire2
 num2 = get_one_dire_num(lx, ly, dx, dy, m)
 if num1 + num2 + 1 >= 5: return True

 return False

判斷函數(shù)完成了,我們再定一個全局變量用于保存游戲狀態(tài)

game_state = 1 # 游戲狀態(tài)1.表示正常進行 2.表示黑勝 3.表示白勝

我們在鼠標添加棋子的代碼后面做下修改,調(diào)用判斷勝利的函數(shù)

if check_win(chess_arr, flag):
  game_state = 2 if flag == 1 else 3
  else:
  flag = 2 if flag == 1 else 1

最后在pygame.display.update()前加個游戲狀態(tài)判斷,用于顯示獲勝文字

if game_state != 1:
 myfont = pygame.font.Font(None,60)
 white = 210,210,0
 win_text = "%s win"%('black' if game_state == 2 else 'white')
 textImage = myfont.render(win_text, True, white)
 screen.blit(textImage, (260,320))

另外,鼠標事件判斷處也要做下修改,判斷下游戲狀態(tài)是不是游戲中

if game_state == 1 and event.type == pygame.MOUSEBUTTONUP: # 鼠標彈起

至此主要的代碼都完整了,下面是效果圖

最后貼下完整程序,這邊沒有做禁手的判斷和判輸,后面有時間再處理

# -*- coding=utf-8 -*-
import random
import pygame
from pygame.locals import MOUSEBUTTONUP
pygame.init()

space = 60 # 四周留下的邊距
cell_size = 40 # 每個格子大小
cell_num = 15
grid_size = cell_size * (cell_num - 1) + space * 2 # 棋盤的大小
screencaption = pygame.display.set_caption('FIR')
screen = pygame.display.set_mode((grid_size,grid_size)) #設(shè)置窗口長寬

chess_arr = []
flag = 1 # 1黑 2白
game_state = 1 # 游戲狀態(tài)1.表示正常進行 2.表示黑勝 3.表示白勝

def get_one_dire_num(lx, ly, dx, dy, m):
 tx = lx
 ty = ly
 s = 0
 while True:
 tx += dx
 ty += dy
 if tx < 0 or tx >= cell_num or ty < 0 or ty >= cell_num or m[ty][tx] == 0: return s
 s+=1

def check_win(chess_arr, flag):
 m = [[0]*cell_num for i in range(cell_num)] # 先定義一個15*15的全0的數(shù)組,不能用[[0]*cell_num]*cell_num的方式去定義因為一位數(shù)組會被重復引用
 for x, y, c in chess_arr:
 if c == flag:
  m[y][x] = 1 # 上面有棋則標1
 lx = chess_arr[-1][0] # 最后一個子的x
 ly = chess_arr[-1][1] # 最后一個子的y
 dire_arr = [[(-1,0),(1,0)],[(0,-1),(0,1)],[(-1,-1),(1,1)],[(-1,1),(1,-1)]] # 4個方向數(shù)組,往左+往右、往上+往下、往左上+往右下、往左下+往右上,4組判斷方向
 
 for dire1,dire2 in dire_arr:
 dx, dy = dire1
 num1 = get_one_dire_num(lx, ly, dx, dy, m)
 dx, dy = dire2
 num2 = get_one_dire_num(lx, ly, dx, dy, m)
 if num1 + num2 + 1 >= 5: return True

 return False

while True:
 for event in pygame.event.get():
  if event.type == pygame.QUIT:
  pygame.quit()
  exit()
 
  if game_state == 1 and event.type == pygame.MOUSEBUTTONUP: # 鼠標彈起
  x, y = pygame.mouse.get_pos() # 獲取鼠標位置
  xi = int(round((x - space)*1.0/cell_size)) # 獲取到x方向上取整的序號
  yi = int(round((y - space)*1.0/cell_size)) # 獲取到y(tǒng)方向上取整的序號
  if xi>=0 and xi<cell_num and yi>=0 and yi<cell_num and (xi,yi,1) not in chess_arr and (xi,yi,2) not in chess_arr:
   chess_arr.append((xi,yi,flag))
   if check_win(chess_arr, flag):
   game_state = 2 if flag == 1 else 3
   else:
   flag = 2 if flag == 1 else 1

 screen.fill((0,0,150)) # 將界面設(shè)置為藍色

 for x in range(0,cell_size*cell_num,cell_size):
 pygame.draw.line(screen,(200,200,200),(x+space,0+space),(x+space,cell_size*(cell_num-1)+space),1)
 for y in range(0,cell_size*cell_num,cell_size):
 pygame.draw.line(screen,(200,200,200),(0+space,y+space),(cell_size*(cell_num-1)+space,y+space),1)

 for x, y, c in chess_arr:
 chess_color = (30,30,30) if c == 1 else (225,225,225)
 pygame.draw.circle(screen, chess_color, [x*cell_size+space, y*cell_size+space], 16,16)

 if game_state != 1:
 myfont = pygame.font.Font(None,60)
 white = 210,210,0
 win_text = "%s win"%('black' if game_state == 2 else 'white')
 textImage = myfont.render(win_text, True, white)
 screen.blit(textImage, (260,320))
 
 pygame.display.update() # 必須調(diào)用update才能看到繪圖顯示

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 對numpy中的transpose和swapaxes函數(shù)詳解

    對numpy中的transpose和swapaxes函數(shù)詳解

    今天小編就為大家分享一篇對numpy中的transpose和swapaxes函數(shù)詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08
  • 值得收藏的10道python 面試題

    值得收藏的10道python 面試題

    本文給大家分享值得收藏的10道python 面試題,非常不錯,具有一定收藏價值,需要的朋友可以參考下
    2019-04-04
  • python關(guān)于圖片和base64互轉(zhuǎn)的三種方式

    python關(guān)于圖片和base64互轉(zhuǎn)的三種方式

    無論使用cv2、PIL還是直接讀取圖片的方法進行圖片與Base64的轉(zhuǎn)換,核心步驟都涉及到二進制格式的轉(zhuǎn)換,每種方法的基本過程都是:Base64轉(zhuǎn)二進制,然后二進制轉(zhuǎn)圖片,或反向操作,這些方法均基于二進制與圖片轉(zhuǎn)換的基本原理
    2024-09-09
  • python基于chardet識別字符編碼的方法

    python基于chardet識別字符編碼的方法

    chardet?是一個流行的 Python 庫,用于檢測文本文件的字符編碼,本文就來介紹一下python基于chardet識別字符編碼的方法,具有一定的參考價值,感興趣的可以了解一下
    2025-01-01
  • Python函數(shù)裝飾器實現(xiàn)方法詳解

    Python函數(shù)裝飾器實現(xiàn)方法詳解

    這篇文章主要介紹了Python函數(shù)裝飾器實現(xiàn)方法,結(jié)合實例形式較為詳細的分析了Python函數(shù)裝飾器的概念、功能、用法及相關(guān)操作注意事項,需要的朋友可以參考下
    2018-12-12
  • Pytorch搭建簡單的卷積神經(jīng)網(wǎng)絡(CNN)實現(xiàn)MNIST數(shù)據(jù)集分類任務

    Pytorch搭建簡單的卷積神經(jīng)網(wǎng)絡(CNN)實現(xiàn)MNIST數(shù)據(jù)集分類任務

    這篇文章主要介紹了Pytorch搭建簡單的卷積神經(jīng)網(wǎng)絡(CNN)實現(xiàn)MNIST數(shù)據(jù)集分類任務,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-03-03
  • python中if及if-else如何使用

    python中if及if-else如何使用

    在本篇文章里小編給大家整理的是關(guān)于python中if及if-else使用方法,需要的朋友們可以參考下。
    2020-06-06
  • 詳解PyTorch nn.Embedding() 嵌入

    詳解PyTorch nn.Embedding() 嵌入

    在自然語言處理(NLP)中,將文本序列轉(zhuǎn)化為數(shù)字序列(tokenid)后,為了使模型能更好地理解這些數(shù)字背后的含義,引入了嵌入層(Embedding)通過簡單的示例,可以看出Embedding的獲取過程及其在理解語言中的關(guān)鍵作用
    2024-11-11
  • Python執(zhí)行時間的幾種計算方法

    Python執(zhí)行時間的幾種計算方法

    這篇文章主要介紹了Python執(zhí)行時間的幾種計算方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-07-07
  • python的scipy實現(xiàn)插值的示例代碼

    python的scipy實現(xiàn)插值的示例代碼

    這篇文章主要介紹了python的scipy實現(xiàn)插值的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-11-11

最新評論

灵宝市| 奈曼旗| 苗栗市| 固原市| 武邑县| 浦县| 河源市| 武乡县| 沐川县| 水富县| 罗甸县| 临夏县| 兴隆县| 仙居县| 弥勒县| 都匀市| 吴江市| 房产| 会东县| 东乡族自治县| 台东县| 麟游县| 蒲城县| 南京市| 朝阳区| 清涧县| 建瓯市| 盖州市| 聊城市| 固镇县| 上蔡县| 崇义县| 抚宁县| 凤城市| 札达县| 高台县| 乌恰县| 蒙山县| 石阡县| 黑河市| 麻栗坡县|