一、準備 python基礎相關準備:pygame的基礎知識,參考目光博客的“用Python和Pygame寫游戲-從入門到精通”安python 3.8.0 在python官網下載,不多說。
安裝pygame,命令:pip install pygame
如安裝較慢,可以參考如下命令,更改pip源為國內鏡像站點:
本章相關資源素材關注公眾號python社區營
二、計劃
準備完成五子棋單機人機游戲,目前已完成界面以及判定輸贏等功能,還未加入電腦AI,以后有時間再加(不知是否會坑),目前實現主要功能如下:五子棋界面的繪制,鼠標左鍵點擊落子(黑子先下,黑白子交替順序)。
判定黑子或白子五子連珠。
一方勝利后彈出提示,結束游戲。
游戲界面是下面這個樣子:
三、開始
設計思路
整個游戲的核心是將棋盤分成兩個層面,第一個層面是物理層面上的,代表在物理像素的位置,主要用于繪圖等操作,另外一個層面是將棋盤抽象成15*15的一個矩陣,黑子和白子是落在這個矩陣上的某個位置,具體位置用坐標(i,j)(0<=i,j<15)來表示,主要用于判斷輸贏和落子等。棋盤的繪制,網上有棋盤和黑白子的圖片資源可以下載使用,我下載后由于棋盤圖片格子線像素位置不太精確,所以自己用ps做了一張544544的木質背景圖,然后用程序來繪制棋盤線(如果PS更熟悉點的話,建議棋盤格線之類就畫在棋盤背景圖上),棋盤格線上下左右空20像素,棋盤格子大小36像素,網上下載的棋子大小是3232像素的。
輸贏的判斷,由于未出輸贏的時候肯定沒有五子連成線的,所以只需要判斷最后落子位置的橫、豎、斜、反斜四個方向上有沒有五子連成線即可。
四、主要代碼main函數,pygame的主要控制流程,縮寫代碼如下:
def main():pygame.init() #pygame初始化size = width,height = 544,544screen = pygame.display.set_mode(size, 0, 32)pygame.display.set_caption('五子棋')font = pygame.font.Font('simhei.ttf', 48)clock = pygame.time.Clock()#設置時鐘game_over = Falserenju = Renju()# Renju是核心類,實現落子及輸贏判斷等renju.init() # 初始化while True:clock.tick(20)# 設置幀率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:# 按下的是鼠標左鍵i,j = renju.get_coord(event.pos)# 將物理坐標轉換成矩陣的邏輯坐標if renju.check_at(i, j):# 檢查(i,j)位置能否被占用,如未被占用返回Truerenju.drop_at(i, j)# 在(i,j)位置落子,該函數將黑子或者白子畫在棋盤上if renju.check_over():# 檢查是否存在五子連線,如存在則返回Truetext = ''if renju.black_turn:#check_at會切換落子的順序,所以輪到黑方落子,意味著最后落子方是白方,所以白方順利text = '白方獲勝,游戲結束!'else:text = '黑方獲勝,游戲結束!'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 = Trueelse: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)# 棋盤格子位置相關???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):# 畫坐標數字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])# 判斷是否已產生勝方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 Truereturn False# 判斷最后一個棋子某個方向是否連成5子,direct:(1,0),(0,1),(1,1),(1,-1)def _check_direct(self, direct):dt_x, dt_y = directlast = 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'].yif dt_x == 0:if x == 0:line_ball.append(ball['coord'])continueif dt_y == 0:if y == 0:line_ball.append(ball['coord'])continueif x*dt_y == y*dt_x:line_ball.append(ball['coord'])if len(line_ball) >= 5:# 只有5子及以上才繼續判斷sorted_line = sorted(line_ball)for i,item in enumerate(sorted_line): index = i+4if index < len(sorted_line):if dt_x == 0:y1 = item.yy2 = sorted_line[index].yif abs(y1-y2) == 4:# 此點和第5個點比較y值,如相差為4則連成5子return Trueelse:x1 = item.xx2 = sorted_line[index].xif abs(x1-x2) == 4: # 此點和第5個點比較x值,如相差為4則連成5子return Trueelse:breakreturn False# 檢查(i,j)位置是否已占用def check_at(self, i, j):for item in self.ball_coord:if (i,j) == item['coord']:return Falsereturn True# 通過物理坐標獲取邏輯坐標def get_coord(self, pos):x, y = posi, j = (0, 0)oppo_x = x - self.leftif oppo_x > 0:i = round(oppo_x / self.space)# 四舍五入取整oppo_y = y - self.topif oppo_y > 0:j = round(oppo_y / self.space)return (i, j)復制代碼
Renju類有幾個函數說明:init()方法主要做了幾件事:載入資源,建立了_chessboard這個棋盤的surface對象
計算棋盤所有落子點的物理坐標,并存放如points屬性中,points是個二維數組,這樣points[i][j]就可以表示邏輯位置(i,j)所對應的物理坐標了。
調用_draw_board()方法,在_chessboard上畫格線及標注等。drop_at(i,j)方法,在邏輯位置(i,j)落子,至于是落白子和黑子通過Renju類的控制開關black_turn來決定。畫圖,并將已落子信息存入ball_coord列表中。
check_at(i,j)方法,通過遍歷ball_coord列表來查看(i,j)位置是否能落子。
check_over()方法判斷是否存在五子連線的情況,主要通過調用_check_direct方法分別判斷四個方向上的情況。
_check_direct(direct)方法是判斷五子連線的主要邏輯,通過判斷最后一顆落子的某個方向落子實現。
結束