用pygame做個(gè)簡(jiǎn)單的五子棋游戲

準(zhǔn)備 python基礎(chǔ)相關(guān)準(zhǔn)備:

1.pygame的基礎(chǔ)知識(shí),參考目光博客的“用Python和Pygame寫游戲-從入門到精通”

2.安裝python 3.8.0 在python官網(wǎng)下載,不多說。

3.安裝pygame,命令:pip install pygame

4.如安裝較慢,可以參考如下命令,更改pip源為國(guó)內(nèi)鏡像站點(diǎn):

5.pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

6.討論群313074041 有為解決問題及需要相關(guān)素材群內(nèi)提供

計(jì)劃

準(zhǔn)備完成五子棋單機(jī)人機(jī)游戲,目前已完成界面以及判定輸贏等功能,還未加入電腦AI,以后有時(shí)間再加(不知是否會(huì)坑),目前實(shí)現(xiàn)主要功能如下:

五子棋界面的繪制,鼠標(biāo)左鍵點(diǎn)擊落子(黑子先下,黑白子交替順序)。

判定黑子或白子五子連珠。

一方勝利后彈出提示,結(jié)束游戲。

游戲界面是下面這個(gè)樣子:


開始

設(shè)計(jì)思路

整個(gè)游戲的核心是將棋盤分成兩個(gè)層面,第一個(gè)層面是物理層面上的,代表在物理像素的位置,主要用于繪圖等操作,另外一個(gè)層面是將棋盤抽象成15*15的一個(gè)矩陣,黑子和白子是落在這個(gè)矩陣上的某個(gè)位置,具體位置用坐標(biāo)(i,j)(0<=i,j<15)來(lái)表示,主要用于判斷輸贏和落子等。

1.棋盤的繪制,網(wǎng)上有棋盤和黑白子的圖片資源可以下載使用,我下載后由于棋盤圖片格子線像素位置不太精確,所以自己用ps做了一張544544的木質(zhì)背景圖,然后用程序來(lái)繪制棋盤線(如果PS更熟悉點(diǎn)的話,建議棋盤格線之類就畫在棋盤背景圖上),棋盤格線上下左右空20像素,棋盤格子大小36像素,網(wǎng)上下載的棋子大小是3232像素的。

2.輸贏的判斷,由于未出輸贏的時(shí)候肯定沒有五子連成線的,所以只需要判斷最后落子位置的橫、豎、斜、反斜四個(gè)方向上有沒有五子連成線即可。

主要代碼

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è)置時(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會(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類,核心類,落子及判斷輸贏等操作,代碼如下:

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)] #橫、豎、斜、反斜 四個(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 = [] # 存放在一條線上的棋子

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

# 通過物理坐標(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類有幾個(gè)函數(shù)說明:

1.init()方法主要做了幾件事:

1.)載入資源,建立了_chessboard這個(gè)棋盤的surface對(duì)象

2.)計(jì)算棋盤所有落子點(diǎn)的物理坐標(biāo),并存放如points屬性中,points是個(gè)二維數(shù)組,這樣points[i][j]就可以表示邏輯位置(i,j)所對(duì)應(yīng)的物理坐標(biāo)了。

3.)調(diào)用_draw_board()方法,在_chessboard上畫格線及標(biāo)注等。

1.drop_at(i,j)方法,在邏輯位置(i,j)落子,至于是落白子和黑子通過Renju類的控制開關(guān)black_turn來(lái)決定。畫圖,并將已落子信息存入ball_coord列表中。

2.check_at(i,j)方法,通過遍歷ball_coord列表來(lái)查看(i,j)位置是否能落子。

3.check_over()方法判斷是否存在五子連線的情況,主要通過調(diào)用_check_direct方法分別判斷四個(gè)方向上的情況。

4._check_direct(direct)方法是判斷五子連線的主要邏輯,通過判斷最后一顆落子的某個(gè)方向落子實(shí)現(xiàn)。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容