如何利用pygame實(shí)現(xiàn)簡單的五子棋游戲
前言
寫程序已經(jīng)丟掉很長一段時間了,最近覺得完全把技術(shù)丟掉可能是個死路,還是應(yīng)該撿起來,所以打算借CSDN來記錄學(xué)習(xí)過程, 由于以前沒事的時候斷斷續(xù)續(xù)學(xué)習(xí)過python和用flask框架寫過點(diǎn)web,所以第一步想撿起python,但是,單純學(xué)習(xí)python有點(diǎn)枯燥,正好看到pygame,感覺還挺簡單,所以想先寫個小游戲練練手。
準(zhǔn)備
python基礎(chǔ)相關(guān)準(zhǔn)備:
- python基礎(chǔ)知識準(zhǔn)備,廖雪峰的python基礎(chǔ)知識簡單好學(xué),熟悉python基本的語法, 鏈接地址
- pygame的基礎(chǔ)知識,參考目光博客的“用Python和Pygame寫游戲-從入門到精通”, 鏈接地址
- 安裝python 3.8.0 在python官網(wǎng)下載,不多說。
- 安裝pygame,命令:pip install pygame
- 如安裝較慢,可以參考如下命令,更改pip源為國內(nèi)鏡像站點(diǎn):
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
計(jì)劃
準(zhǔn)備完成五子棋單機(jī)人機(jī)游戲,目前已完成界面以及判定輸贏等功能,還未加入電腦AI,以后有時間再加(不知是否會坑),目前實(shí)現(xiàn)主要功能如下:
- 五子棋界面的繪制,鼠標(biāo)左鍵點(diǎn)擊落子(黑子先下,黑白子交替順序)。
- 判定黑子或白子五子連珠。
- 一方勝利后彈出提示,結(jié)束游戲。
游戲界面是下面這個樣子:

開始
設(shè)計(jì)思路
整個游戲的核心是將棋盤分成兩個層面,第一個層面是物理層面上的,代表在物理像素的位置,主要用于繪圖等操作,另外一個層面是將棋盤抽象成15*15的一個矩陣,黑子和白子是落在這個矩陣上的某個位置,具體位置用坐標(biāo)(i,j)(0<=i,j<15)來表示,主要用于判斷輸贏和落子等。
- 棋盤的繪制,網(wǎng)上有棋盤和黑白子的圖片資源可以下載使用,我下載后由于棋盤圖片格子線像素位置不太精確,所以自己用ps做了一張544544的木質(zhì)背景圖,然后用程序來繪制棋盤線(如果PS更熟悉點(diǎn)的話,建議棋盤格線之類就畫在棋盤背景圖上),棋盤格線上下左右空20像素,棋盤格子大小36像素,網(wǎng)上下載的棋子大小是3232像素的。
- 輸贏的判斷,由于未出輸贏的時候肯定沒有五子連成線的,所以只需要判斷最后落子位置的橫、豎、斜、反斜四個方向上有沒有五子連成線即可。
主要代碼
1、main函數(shù),pygame的主要控制流程,縮寫代碼如下:
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è)置時鐘
game_over = False
renju = Renju() # Renju是核心類,實(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ù)將黑子或者白子畫在棋盤上
if renju.check_over(): # 檢查是否存在五子連線,如存在則返回True
text = ''
if renju.black_turn: #check_at會切換落子的順序,所以輪到黑方落子,意味著最后落子方是白方,所以白方順利
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類,核心類,落子及判斷輸贏等操作,代碼如下:
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) # 棋盤格子位置相關(guān)???
color = (0, 0, 0) # 棋盤格子線顏色
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 # 切換黑白子順序
# 畫棋盤上的格子線,如果棋盤背景圖做的足夠精確,可省略此步驟
def _draw_board(self):
# 畫坐標(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):
# 畫橫線
pygame.draw.line(self._chessboard, self.color, self.points[0][x], self.points[self.lines-1][x])
# 畫豎線
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)] #橫、豎、斜、反斜 四個方向檢查
for d in direct:
if self._check_direct(d):
return True
return False
# 判斷最后一個棋子某個方向是否連成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 = [] # 存放在一條線上的棋子
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個點(diǎn)比較y值,如相差為4則連成5子
return True
else:
x1 = item.x
x2 = sorted_line[index].x
if abs(x1-x2) == 4: # 此點(diǎn)和第5個點(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
# 通過物理坐標(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類有幾個函數(shù)說明:
- init()方法主要做了幾件事:
- 載入資源,建立了_chessboard這個棋盤的surface對象
- 計(jì)算棋盤所有落子點(diǎn)的物理坐標(biāo),并存放如points屬性中,points是個二維數(shù)組,這樣points[i][j]就可以表示邏輯位置(i,j)所對應(yīng)的物理坐標(biāo)了。
- 調(diào)用_draw_board()方法,在_chessboard上畫格線及標(biāo)注等。
- drop_at(i,j)方法,在邏輯位置(i,j)落子,至于是落白子和黑子通過Renju類的控制開關(guān)black_turn來決定。畫圖,并將已落子信息存入ball_coord列表中。
- check_at(i,j)方法,通過遍歷ball_coord列表來查看(i,j)位置是否能落子。
- check_over()方法判斷是否存在五子連線的情況,主要通過調(diào)用_check_direct方法分別判斷四個方向上的情況。
- _check_direct(direct)方法是判斷五子連線的主要邏輯,通過判斷最后一顆落子的某個方向落子實(shí)現(xiàn)。
結(jié)束
主要功能大概是這些,源碼及程序中用到的圖片等可以在我的資源中下載,或者github下載, 下載地址
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。
相關(guān)文章
python輸出結(jié)果刷新及進(jìn)度條的實(shí)現(xiàn)操作
這篇文章主要介紹了python輸出結(jié)果刷新及進(jìn)度條的實(shí)現(xiàn)操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07
Python機(jī)器學(xué)習(xí)NLP自然語言處理Word2vec電影影評建模
本文是Python機(jī)器學(xué)習(xí)NLP自然語言處理系列文章,帶大家開啟一段學(xué)習(xí)自然語言處理 (NLP) 的旅程. 本篇文章主要學(xué)習(xí)NLP自然語言處理基本操作Word2vec電影影評建模2021-09-09
一文帶你了解Python列表生成式應(yīng)用的八重境界
在Python中有非常多且好用的技巧,其中使用最多的是列表生成式,往往可以將復(fù)雜的邏輯用簡單的語言來實(shí)現(xiàn),本文重點(diǎn)介紹列表生成式應(yīng)用的八重境界2022-09-09
python實(shí)現(xiàn)發(fā)送和獲取手機(jī)短信驗(yàn)證碼
這篇文章主要介紹了python實(shí)現(xiàn)發(fā)送和獲取手機(jī)短信驗(yàn)證碼的相關(guān)資料,講解了python如何解決接口測試獲取手機(jī)驗(yàn)證碼問題,感興趣的小伙伴們可以參考一下2016-01-01
Python wxpython模塊響應(yīng)鼠標(biāo)拖動事件操作示例
這篇文章主要介紹了Python wxpython模塊響應(yīng)鼠標(biāo)拖動事件操作,結(jié)合實(shí)例形式分析了Python使用wxpython模塊創(chuàng)建窗口、綁定事件及相應(yīng)鼠標(biāo)事件相關(guān)操作技巧,需要的朋友可以參考下2018-08-08

