如何利用pygame實(shí)現(xiàn)簡(jiǎn)單的五子棋游戲
前言
寫(xiě)程序已經(jīng)丟掉很長(zhǎng)一段時(shí)間了,最近覺(jué)得完全把技術(shù)丟掉可能是個(gè)死路,還是應(yīng)該撿起來(lái),所以打算借CSDN來(lái)記錄學(xué)習(xí)過(guò)程, 由于以前沒(méi)事的時(shí)候斷斷續(xù)續(xù)學(xué)習(xí)過(guò)python和用flask框架寫(xiě)過(guò)點(diǎn)web,所以第一步想撿起python,但是,單純學(xué)習(xí)python有點(diǎn)枯燥,正好看到pygame,感覺(jué)還挺簡(jiǎn)單,所以想先寫(xiě)個(gè)小游戲練練手。
準(zhǔn)備
python基礎(chǔ)相關(guān)準(zhǔn)備:
- python基礎(chǔ)知識(shí)準(zhǔn)備,廖雪峰的python基礎(chǔ)知識(shí)簡(jiǎn)單好學(xué),熟悉python基本的語(yǔ)法, 鏈接地址
- pygame的基礎(chǔ)知識(shí),參考目光博客的“用Python和Pygame寫(xiě)游戲-從入門(mén)到精通”, 鏈接地址
- 安裝python 3.8.0 在python官網(wǎng)下載,不多說(shuō)。
- 安裝pygame,命令:pip install pygame
- 如安裝較慢,可以參考如下命令,更改pip源為國(guó)內(nèi)鏡像站點(diǎn):
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
計(jì)劃
準(zhǔn)備完成五子棋單機(jī)人機(jī)游戲,目前已完成界面以及判定輸贏等功能,還未加入電腦AI,以后有時(shí)間再加(不知是否會(huì)坑),目前實(shí)現(xiàn)主要功能如下:
- 五子棋界面的繪制,鼠標(biāo)左鍵點(diǎn)擊落子(黑子先下,黑白子交替順序)。
- 判定黑子或白子五子連珠。
- 一方勝利后彈出提示,結(jié)束游戲。
游戲界面是下面這個(gè)樣子:
開(kāi)始
設(shè)計(jì)思路
整個(gè)游戲的核心是將棋盤(pán)分成兩個(gè)層面,第一個(gè)層面是物理層面上的,代表在物理像素的位置,主要用于繪圖等操作,另外一個(gè)層面是將棋盤(pán)抽象成15*15的一個(gè)矩陣,黑子和白子是落在這個(gè)矩陣上的某個(gè)位置,具體位置用坐標(biāo)(i,j)(0<=i,j<15)來(lái)表示,主要用于判斷輸贏和落子等。
- 棋盤(pán)的繪制,網(wǎng)上有棋盤(pán)和黑白子的圖片資源可以下載使用,我下載后由于棋盤(pán)圖片格子線(xiàn)像素位置不太精確,所以自己用ps做了一張544544的木質(zhì)背景圖,然后用程序來(lái)繪制棋盤(pán)線(xiàn)(如果PS更熟悉點(diǎn)的話(huà),建議棋盤(pán)格線(xiàn)之類(lèi)就畫(huà)在棋盤(pán)背景圖上),棋盤(pán)格線(xiàn)上下左右空20像素,棋盤(pán)格子大小36像素,網(wǎng)上下載的棋子大小是3232像素的。
- 輸贏的判斷,由于未出輸贏的時(shí)候肯定沒(méi)有五子連成線(xiàn)的,所以只需要判斷最后落子位置的橫、豎、斜、反斜四個(gè)方向上有沒(méi)有五子連成線(xiàn)即可。
主要代碼
1、main函數(shù),pygame的主要控制流程,縮寫(xiě)代碼如下:
def main(): pygame.init() #pygame初始化 size = width,height = 544,544 screen = pygame.display.set_mode(size, 0, 32) pygame.display.set_caption('五子棋') font = pygame.font.Font('simhei.ttf', 48) clock = pygame.time.Clock() #設(shè)置時(shí)鐘 game_over = False renju = Renju() # Renju是核心類(lèi),實(shí)現(xiàn)落子及輸贏判斷等 renju.init() # 初始化 while True: clock.tick(20) # 設(shè)置幀率 for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() if event.type == pygame.MOUSEBUTTONDOWN and (not game_over): if event.button == 1: # 按下的是鼠標(biāo)左鍵 i,j = renju.get_coord(event.pos) # 將物理坐標(biāo)轉(zhuǎn)換成矩陣的邏輯坐標(biāo) if renju.check_at(i, j): # 檢查(i,j)位置能否被占用,如未被占用返回True renju.drop_at(i, j) # 在(i,j)位置落子,該函數(shù)將黑子或者白子畫(huà)在棋盤(pán)上 if renju.check_over(): # 檢查是否存在五子連線(xiàn),如存在則返回True text = '' if renju.black_turn: #check_at會(huì)切換落子的順序,所以輪到黑方落子,意味著最后落子方是白方,所以白方順利 text = '白方獲勝,游戲結(jié)束!' else: text = '黑方獲勝,游戲結(jié)束!' gameover_text = font.render(text, True, (255,0,0)) renju.chessboard().blit(gameover_text, (round(width/2-gameover_text.get_width()/2), round(height/2-gameover_text.get_height()/2))) game_over = True else: print('此位置已占用,不能在此落子') screen.blit(renju.chessboard(),(0,0)) pygame.display.update() pygame.quit()
2、renju類(lèi),核心類(lèi),落子及判斷輸贏等操作,代碼如下:
Position = namedtuple('Position', ['x', 'y']) class Renju(object): background_filename = 'chessboard.png' white_chessball_filename = 'white_chessball.png' black_chessball_filename = 'black_chessball.png' top, left, space, lines = (20, 20, 36, 15) # 棋盤(pán)格子位置相關(guān)??? color = (0, 0, 0) # 棋盤(pán)格子線(xiàn)顏色 black_turn = True # 黑子先手 ball_coord = [] # 記錄黑子和白子邏輯位置 def init(self): try: self._chessboard = pygame.image.load(self.background_filename) self._white_chessball = pygame.image.load(self.white_chessball_filename).convert_alpha() self._black_chessball = pygame.image.load(self.black_chessball_filename).convert_alpha() self.font = pygame.font.SysFont('arial', 16) self.ball_rect = self._white_chessball.get_rect() self.points = [[] for i in range(self.lines)] for i in range(self.lines): for j in range(self.lines): self.points[i].append(Position(self.left + i*self.space, self.top + j*self.space)) self._draw_board() except pygame.error as e: print(e) sys.exit() def chessboard(self): return self._chessboard # 在(i,j)位置落子 def drop_at(self, i, j): pos_x = self.points[i][j].x - int(self.ball_rect.width/2) pos_y = self.points[i][j].y - int(self.ball_rect.height/2) ball_pos = {'type':0 if self.black_turn else 1, 'coord':Position(i,j)} if self.black_turn: # 輪到黑子下 self._chessboard.blit(self._black_chessball, (pos_x, pos_y)) else: self._chessboard.blit(self._white_chessball, (pos_x, pos_y)) self.ball_coord.append(ball_pos) # 記錄已落子信息 self.black_turn = not self.black_turn # 切換黑白子順序 # 畫(huà)棋盤(pán)上的格子線(xiàn),如果棋盤(pán)背景圖做的足夠精確,可省略此步驟 def _draw_board(self): # 畫(huà)坐標(biāo)數(shù)字 for i in range(1, self.lines): coord_text = self.font.render(str(i), True, self.color) self._chessboard.blit(coord_text, (self.points[i][0].x-round(coord_text.get_width()/2), self.points[i][0].y-coord_text.get_height())) self._chessboard.blit(coord_text, (self.points[0][i].x-coord_text.get_width(), self.points[0][i].y-round(coord_text.get_height()/2))) for x in range(self.lines): # 畫(huà)橫線(xiàn) pygame.draw.line(self._chessboard, self.color, self.points[0][x], self.points[self.lines-1][x]) # 畫(huà)豎線(xiàn) pygame.draw.line(self._chessboard, self.color, self.points[x][0], self.points[x][self.lines-1]) # 判斷是否已產(chǎn)生勝方 def check_over(self): if len(self.ball_coord)>8: # 只有黑白子已下4枚以上才判斷 direct = [(1,0),(0,1),(1,1),(1,-1)] #橫、豎、斜、反斜 四個(gè)方向檢查 for d in direct: if self._check_direct(d): return True return False # 判斷最后一個(gè)棋子某個(gè)方向是否連成5子,direct:(1,0),(0,1),(1,1),(1,-1) def _check_direct(self, direct): dt_x, dt_y = direct last = self.ball_coord[-1] line_ball = [] # 存放在一條線(xiàn)上的棋子 for ball in self.ball_coord: if ball['type'] == last['type']: x = ball['coord'].x - last['coord'].x y = ball['coord'].y - last['coord'].y if dt_x == 0: if x == 0: line_ball.append(ball['coord']) continue if dt_y == 0: if y == 0: line_ball.append(ball['coord']) continue if x*dt_y == y*dt_x: line_ball.append(ball['coord']) if len(line_ball) >= 5: # 只有5子及以上才繼續(xù)判斷 sorted_line = sorted(line_ball) for i,item in enumerate(sorted_line): index = i+4 if index < len(sorted_line): if dt_x == 0: y1 = item.y y2 = sorted_line[index].y if abs(y1-y2) == 4: # 此點(diǎn)和第5個(gè)點(diǎn)比較y值,如相差為4則連成5子 return True else: x1 = item.x x2 = sorted_line[index].x if abs(x1-x2) == 4: # 此點(diǎn)和第5個(gè)點(diǎn)比較x值,如相差為4則連成5子 return True else: break return False # 檢查(i,j)位置是否已占用 def check_at(self, i, j): for item in self.ball_coord: if (i,j) == item['coord']: return False return True # 通過(guò)物理坐標(biāo)獲取邏輯坐標(biāo) def get_coord(self, pos): x, y = pos i, j = (0, 0) oppo_x = x - self.left if oppo_x > 0: i = round(oppo_x / self.space) # 四舍五入取整 oppo_y = y - self.top if oppo_y > 0: j = round(oppo_y / self.space) return (i, j)
Renju類(lèi)有幾個(gè)函數(shù)說(shuō)明:
- init()方法主要做了幾件事:
- 載入資源,建立了_chessboard這個(gè)棋盤(pán)的surface對(duì)象
- 計(jì)算棋盤(pán)所有落子點(diǎn)的物理坐標(biāo),并存放如points屬性中,points是個(gè)二維數(shù)組,這樣points[i][j]就可以表示邏輯位置(i,j)所對(duì)應(yīng)的物理坐標(biāo)了。
- 調(diào)用_draw_board()方法,在_chessboard上畫(huà)格線(xiàn)及標(biāo)注等。
- drop_at(i,j)方法,在邏輯位置(i,j)落子,至于是落白子和黑子通過(guò)Renju類(lèi)的控制開(kāi)關(guān)black_turn來(lái)決定。畫(huà)圖,并將已落子信息存入ball_coord列表中。
- check_at(i,j)方法,通過(guò)遍歷ball_coord列表來(lái)查看(i,j)位置是否能落子。
- check_over()方法判斷是否存在五子連線(xiàn)的情況,主要通過(guò)調(diào)用_check_direct方法分別判斷四個(gè)方向上的情況。
- _check_direct(direct)方法是判斷五子連線(xiàn)的主要邏輯,通過(guò)判斷最后一顆落子的某個(gè)方向落子實(shí)現(xiàn)。
結(jié)束
主要功能大概是這些,源碼及程序中用到的圖片等可以在我的資源中下載,或者github下載, 下載地址
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Selenium中免登錄的實(shí)現(xiàn)方法option詳解
在selenium中有很多種可以實(shí)現(xiàn)網(wǎng)站的免登錄,option就是其中的一種做法,這篇文章主要介紹了Selenium中免登錄的實(shí)現(xiàn)方法option,需要的朋友可以參考下2022-12-12利用python計(jì)算均值、方差和標(biāo)準(zhǔn)差(Numpy和Pandas)
這篇文章主要給大家介紹了關(guān)于利用python計(jì)算均值、方差和標(biāo)準(zhǔn)差的相關(guān)資料,Numpy在Python中是一個(gè)通用的數(shù)組處理包,它提供了一個(gè)高性能的多維數(shù)組對(duì)象和用于處理這些數(shù)組的工具,它是使用Python進(jìn)行科學(xué)計(jì)算的基礎(chǔ)包,需要的朋友可以參考下2023-11-11python如何利用plt.legend()添加圖例代碼示例
用python的matplotlib畫(huà)圖時(shí),往往需要加圖例說(shuō)明,下面這篇文章主要給大家介紹了關(guān)于python如何利用plt.legend()添加圖例的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-11-11