這篇文章旨在介紹一個雙人的五子棋程式。再次重申,本人不擅長對程式碼的可讀性進行優化,所以可能有些雜亂(在所難免)。
先瞅一眼效果圖:
請注意,這個棋子……是這麼圓潤立體!本程式不需任何素材圖片,完全用程式碼繪製所需的影象,因此這樣立體的棋子十分難能可貴。那麼,這究竟是如何做到的呢?別急,聽我慢慢道來。
首先,一個好的程式必須配有高階大氣的文字。對於博大精深的中文,gbk或utf-8的編碼宣告自然是非常必要的。於是,就有了第一行程式碼:
#coding:utf-8
然後,當然是模組的匯入。本次所需的模組不多,只有sys、pygame和random。其中pygame需要用pip工具進行安裝。
import sys
import pygame
import random
接下來,我們定義一個函數:do(),裡面輸入我們所需要的程式碼。至於為何要定義函數,這是因為在遊戲結束後需要重新執行該程式,因而不可避免地要將全部的程式程式碼輸入一個函數中,並呼叫這個函數。
def do():
然後,就是最重磅的棋子繪製函數,我們先看黑棋:
def black(x, y):
a = 30
b = 30
c = 30
d = 8
for i in range(50):
pygame.draw.circle(screen, (a, b, c), [19.5 + 32 * x, 19.5 + 32 * y], 111 / d)
a += 0.3
b += 0.3
c += 0.3
d += 0.2
pygame.display.update()
這裡的x和y是繪製黑棋的位置,暫且先不管。可以看到,這一個圓潤的棋子是有50個同心圓組成。這些同心圓的顏色逐個變淺,相鄰兩個圓的顏色差值不變。因此,我們只需要使圓的直徑(或半徑)呈曲線變化,就可以使繪製的棋子邊緣非常圓潤。作為一個初二的學生,我立馬想到了反比例函數。因此,「d=8」「111/d」和「d+=0.2」實際上是使同心圓的半徑隨迴圈變數的變化呈一個偏移的反比例函數,這樣就可以營造一種圓潤的視感。
同理,白棋的繪製也是遵循類似的方式。在此不在贅述,只給出程式碼:
def white(x, y):
a = 200
b = 200
c = 200
d = 8
for i in range(50):
pygame.draw.circle(screen, (a, b, c), [19.5 + 32 * x, 19.5 + 32 * y], 111 / d)
a += 0.3
b += 0.3
c += 0.3
d += 0.2
pygame.display.update()
接下來,是冗長無味的棋盤繪製:
pygame.init()
screen = pygame.display.set_mode((615, 615))
pygame.display.set_caption('五子棋')
screen.fill("#DD954F")
a = pygame.Surface((603, 603), flags=pygame.HWSURFACE)
a.fill(color='#121010')
b = pygame.Surface((585, 585), flags=pygame.HWSURFACE)
b.fill(color="#DD954F")
c = pygame.Surface((579, 579), flags=pygame.HWSURFACE)
c.fill(color='#121010')
d = pygame.Surface((576, 576), flags=pygame.HWSURFACE)
d.fill(color="#DD954F")
e = pygame.Surface((31, 31), flags=pygame.HWSURFACE)
e.fill(color="#DD954F")
screen.blit(a, (6.5, 6.5))
screen.blit(b, (15, 15))
screen.blit(c, (18, 18))
for j in range(18):
for i in range(18):
screen.blit(e, (20 + 32 * i, 20 + 32 * j))
alist = []
for j in range(19):
alistone = []
for i in range(19):
alistone.append(0)
alist.append(alistone)
pygame.draw.circle(screen, '#121010', [307.5, 307.5], 5)
pygame.draw.circle(screen, '#121010', [115.5, 307.5], 5)
pygame.draw.circle(screen, '#121010', [499.5, 307.5], 5)
pygame.draw.circle(screen, '#121010', [115.5, 499.5], 5)
pygame.draw.circle(screen, '#121010', [499.5, 499.5], 5)
pygame.draw.circle(screen, '#121010', [115.5, 115.5], 5)
pygame.draw.circle(screen, '#121010', [499.5, 115.5], 5)
pygame.draw.circle(screen, '#121010', [307.5, 499.5], 5)
pygame.draw.circle(screen, '#121010', [307.5, 115.5], 5)
pygame.display.flip()
可以看到,我們先畫了一個花哨的邊框,然後畫上其中的格子,順便定義了一個被0填滿的19*19的二維列表(在此處似乎很冗餘,但到後面,你會發現它異常有用!)。最後,九個平平無奇的點被畫上了棋盤。
至此,我們的五子棋初見雛形。但是,要使用它進行對弈,這還遠遠不夠。
wb = "black"
font1 = pygame.font.SysFont('stxingkai', 70)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
x, y = pygame.mouse.get_pos()
x = round((x - 19.5) / 32)
y = round((y - 19.5) / 32)
if x < 0:
x = 0
if x > 18:
x = 18
if y < 0:
y = 0
if y > 18:
y = 18
z = False
if alist[x][y] == 0:
eval(wb + "({},{})".format(x, y))
if wb == "black":
alist[x][y] = 1
wb1 = "黑棋"
wb = "white"
elif wb == "white":
alist[x][y] = 2
wb1 = "白棋"
wb = "black"
這裡,就是最核心的對弈程式。首先我們進入主迴圈,並獲取事件。在這裡,我們除了對按下關閉按鈕進行了幾乎每個pygame程式都會進行的處理外,還對按下滑鼠事件進行了處理。首先,我們獲取滑鼠點選的座標,通過計算來得到對應的格點(這裡對不在格點上的點選進行四捨五入,對棋盤之外的點選自動匹配與其最近的格點)。然後,根據此時的先手方和計算得的格點執行black/white函數,繪製所需的棋子。
xx = x
yy = y
while True:
if xx == 0:
break
elif alist[xx][yy] != alist[x][y]:
xx += 1
break
else:
xx -= 1
num = 0
while True:
if xx == 18:
break
elif alist[xx][yy] != alist[x][y]:
break
else:
xx += 1
num += 1
if num >= 5:
pygame.font.init()
text = font1.render("{}贏了".format(wb1), True, (0, 0, 0))
textRect = text.get_rect()
textRect.center = (307.5, 307.5)
screen.blit(text, textRect)
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
do()
xx = x
yy = y
while True:
if yy == 0:
break
elif alist[xx][yy] != alist[x][y]:
yy += 1
break
else:
yy -= 1
num = 0
while True:
if yy == 18:
break
elif alist[xx][yy] != alist[x][y]:
break
else:
yy += 1
num += 1
if num >= 5:
pygame.font.init()
text = font1.render("{}贏了".format(wb1), True, (0, 0, 0))
textRect = text.get_rect()
textRect.center = (307.5, 307.5)
screen.blit(text, textRect)
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
do()
xx = x
yy = y
while True:
if xx == 0:
break
elif yy == 0:
break
elif alist[xx][yy] != alist[x][y]:
xx += 1
yy += 1
break
else:
xx -= 1
yy -= 1
num = 0
while True:
if xx == 18:
break
elif yy == 18:
break
elif alist[xx][yy] != alist[x][y]:
break
else:
xx += 1
yy += 1
num += 1
if num >= 5:
pygame.font.init()
text = font1.render("{}贏了".format(wb1), True, (0, 0, 0))
textRect = text.get_rect()
textRect.center = (307.5, 307.5)
screen.blit(text, textRect)
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
do()
xx = x
yy = y
while True:
if xx == 0:
break
elif yy == 18:
break
elif alist[xx][yy] != alist[x][y]:
xx += 1
yy -= 1
break
else:
xx -= 1
yy += 1
num = 0
while True:
if xx == 18:
break
elif yy == 0:
break
elif alist[xx][yy] != alist[x][y]:
break
else:
xx += 1
yy -= 1
num += 1
if num >= 5:
pygame.font.init()
text = font1.render("{}贏了".format(wb1), True, (0, 0, 0))
textRect = text.get_rect()
textRect.center = (307.5, 307.5)
screen.blit(text, textRect)
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
do()
這是冗長的勝負判斷,具體內容我自己也難以解釋(這個程式編了有一段時間了)。主要思路,是向各個方向尋找同色棋子的連線,並判斷是否滿五個棋。當然,不得不承認,這一段確實不太高明,似乎有別人釋出過比我更好的方案,感興趣的可以上網找找。
do()
這是程式的收尾,也就是對do()函數的執行。至此,整個程式完全結束。
完整程式碼:
#coding:utf-8
import sys
import pygame
import random
def do():
def black(x, y):
a = 20
b = 20
c = 20
d = 0
for i in range(50):
pygame.draw.circle(screen, (a, b, c), [19.5 + 32 * x, 19.5 + 32 * y], (10/(d-5)+10)*1.6)
a += 1
b += 1
c += 1
d += 0.08
pygame.display.update()
def white(x, y):
a = 170
b = 170
c = 170
d = 0
for i in range(50):
pygame.draw.circle(screen, (a, b, c), [19.5 + 32 * x, 19.5 + 32 * y], (10/(d-5)+10)*1.6)
a += 1
b += 1
c += 1
d += 0.08
pygame.display.update()
pygame.init()
screen = pygame.display.set_mode((615, 615))
pygame.display.set_caption('五子棋')
screen.fill("#DD954F")
a = pygame.Surface((603, 603), flags=pygame.HWSURFACE)
a.fill(color='#121010')
b = pygame.Surface((585, 585), flags=pygame.HWSURFACE)
b.fill(color="#DD954F")
c = pygame.Surface((579, 579), flags=pygame.HWSURFACE)
c.fill(color='#121010')
d = pygame.Surface((576, 576), flags=pygame.HWSURFACE)
d.fill(color="#DD954F")
e = pygame.Surface((31, 31), flags=pygame.HWSURFACE)
e.fill(color="#DD954F")
screen.blit(a, (6.5, 6.5))
screen.blit(b, (15, 15))
screen.blit(c, (18, 18))
for j in range(18):
for i in range(18):
screen.blit(e, (20 + 32 * i, 20 + 32 * j))
alist = []
for j in range(19):
alistone = []
for i in range(19):
alistone.append(0)
alist.append(alistone)
pygame.draw.circle(screen, '#121010', [307.5, 307.5], 5)
pygame.draw.circle(screen, '#121010', [115.5, 307.5], 5)
pygame.draw.circle(screen, '#121010', [499.5, 307.5], 5)
pygame.draw.circle(screen, '#121010', [115.5, 499.5], 5)
pygame.draw.circle(screen, '#121010', [499.5, 499.5], 5)
pygame.draw.circle(screen, '#121010', [115.5, 115.5], 5)
pygame.draw.circle(screen, '#121010', [499.5, 115.5], 5)
pygame.draw.circle(screen, '#121010', [307.5, 499.5], 5)
pygame.draw.circle(screen, '#121010', [307.5, 115.5], 5)
pygame.display.flip()
wb = "black"
font1 = pygame.font.SysFont('stxingkai', 70)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
x, y = pygame.mouse.get_pos()
x = round((x - 19.5) / 32)
y = round((y - 19.5) / 32)
if x < 0:
x = 0
if x > 18:
x = 18
if y < 0:
y = 0
if y > 18:
y = 18
z = False
if alist[x][y] == 0:
eval(wb + "({},{})".format(x, y))
if wb == "black":
alist[x][y] = 1
wb1 = "黑棋"
wb = "white"
elif wb == "white":
alist[x][y] = 2
wb1 = "白棋"
wb = "black"
xx = x
yy = y
while True:
if xx == 0:
break
elif alist[xx][yy] != alist[x][y]:
xx += 1
break
else:
xx -= 1
num = 0
while True:
if xx == 18:
break
elif alist[xx][yy] != alist[x][y]:
break
else:
xx += 1
num += 1
if num >= 5:
pygame.font.init()
text = font1.render("{}贏了".format(wb1), True, (0, 0, 0))
textRect = text.get_rect()
textRect.center = (307.5, 307.5)
screen.blit(text, textRect)
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
do()
xx = x
yy = y
while True:
if yy == 0:
break
elif alist[xx][yy] != alist[x][y]:
yy += 1
break
else:
yy -= 1
num = 0
while True:
if yy == 18:
break
elif alist[xx][yy] != alist[x][y]:
break
else:
yy += 1
num += 1
if num >= 5:
pygame.font.init()
text = font1.render("{}贏了".format(wb1), True, (0, 0, 0))
textRect = text.get_rect()
textRect.center = (307.5, 307.5)
screen.blit(text, textRect)
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
do()
xx = x
yy = y
while True:
if xx == 0:
break
elif yy == 0:
break
elif alist[xx][yy] != alist[x][y]:
xx += 1
yy += 1
break
else:
xx -= 1
yy -= 1
num = 0
while True:
if xx == 18:
break
elif yy == 18:
break
elif alist[xx][yy] != alist[x][y]:
break
else:
xx += 1
yy += 1
num += 1
if num >= 5:
pygame.font.init()
text = font1.render("{}贏了".format(wb1), True, (0, 0, 0))
textRect = text.get_rect()
textRect.center = (307.5, 307.5)
screen.blit(text, textRect)
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
do()
xx = x
yy = y
while True:
if xx == 0:
break
elif yy == 18:
break
elif alist[xx][yy] != alist[x][y]:
xx += 1
yy -= 1
break
else:
xx -= 1
yy += 1
num = 0
while True:
if xx == 18:
break
elif yy == 0:
break
elif alist[xx][yy] != alist[x][y]:
break
else:
xx += 1
yy -= 1
num += 1
if num >= 5:
pygame.font.init()
text = font1.render("{}贏了".format(wb1), True, (0, 0, 0))
textRect = text.get_rect()
textRect.center = (307.5, 307.5)
screen.blit(text, textRect)
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
do()
do()