import pygame #導(dǎo)入pygame模塊
pygame.init()#初始化
screen = pygame.display.set_mode((750,750))#設(shè)置游戲屏幕大小
running = True#建立一個事件
while running:#事件運行
for event in pygame.event.get():
if event.type == pygame.QUIT:#當(dāng)點擊事件后退出
running = False #事件關(guān)閉
screen.fill([125,95,24])#給屏幕畫上顏色
for x in range(15):#畫出15條豎線
pygame.draw.line(screen, (255, 0, 0),[25+50*x,25],[25+50*x,725],2)
for y in range(15):#畫出15條橫線
pygame.draw.line(screen, (255, 0, 0),[25,25+50*y],[725,25+50*y],2)
pygame.draw.circle(screen, (255, 0, 0),[25+50*7,25+50*7],8)#棋盤中間畫出一個中心點
pygame.display.update()#刷新,變色用的
pygame.quit()#退出游戲
一。畫出棋盤x和y? 橫和豎個15條線,畫出中央的小點
二,創(chuàng)建列表,計算出x和y的當(dāng)前坐標的位置,且存儲二維列表的值,畫出圓形白色棋子
import pygame #導(dǎo)入pygame模塊
pygame.init()#初始化
screen = pygame.display.set_mode((750,750))#設(shè)置游戲屏幕大小
map = [0]*15#創(chuàng)建一個列表
for i in range(15):#列表循環(huán)來存儲數(shù)據(jù)
map[i] = [0]*15#二維列表來存儲數(shù)據(jù)
running = True#建立一個事件
while running:#事件運行
for event in pygame.event.get():
if event.type == pygame.QUIT:#當(dāng)點擊事件后退出
running = False #事件關(guān)閉
elif event.type == pygame.MOUSEBUTTONDOWN:#鼠標相應(yīng)點擊事件
x,y = pygame.mouse.get_pos()#獲取x,y的位置
col = round((x - 25)/50)#計算出x的位置
row = round((y - 25)/50)#計算出y的位置
print(row+1,col+1)#打印出x與y的位置
map[row][col] = 1#等于1表示把它的數(shù)值存儲起來
screen.fill([125,95,24])#給屏幕畫上顏色
for x in range(15):#畫出15條豎線
pygame.draw.line(screen, (255, 0, 0),[25+50*x,25],[25+50*x,725],2)
for y in range(15):#畫出15條橫線
pygame.draw.line(screen, (255, 0, 0),[25,25+50*y],[725,25+50*y],2)
x,y = pygame.mouse.get_pos()#獲取鼠標所在位置
x = round((x - 25)/50)*50 + 25#找到x和y的交叉點
y = round((y - 25)/50)*50 + 25#找到x和y的交叉點
pygame.draw.rect(screen,(255, 0, 0),[x-25,y-25,50,50],2)#畫一個正方形框框,表示可以落子
pygame.draw.circle(screen, (255, 0, 0),[25+50*7,25+50*7],8)#棋盤中間畫出一個中心點
for row in range(15):#棋盤上畫出白色棋子
for col in range(15):#棋盤上畫出白色棋
if map[row][col] == 1:#點擊后在棋盤上畫出白色棋
pygame.draw.circle(screen,(255, 255, 255),[col*50+25,row*50+25],25)#棋盤上畫出白色棋的位置和大小半徑
pygame.display.update()#刷新,變色用的
pygame.quit()#退出游戲
三.黑白棋子交替出現(xiàn)
import pygame #導(dǎo)入pygame模塊
pygame.init()#初始化
screen = pygame.display.set_mode((750,750))#設(shè)置游戲屏幕大小
map = [0]*15#創(chuàng)建一個列表
for i in range(15):#列表循環(huán)來存儲數(shù)據(jù)
map[i] = [0]*15#二維列表來存儲數(shù)據(jù)
player = 1#創(chuàng)建玩家1
running = True#建立一個事件
while running:#事件運行
for event in pygame.event.get():
if event.type == pygame.QUIT:#當(dāng)點擊事件后退出
running = False #事件關(guān)閉
elif event.type == pygame.MOUSEBUTTONDOWN:#鼠標相應(yīng)點擊事件
x,y = pygame.mouse.get_pos()#獲取x,y的位置
col = round((x - 25)/50)#計算出x的位置
row = round((y - 25)/50)#計算出y的位置
print(row+1,col+1)#打印出x與y的位置
# map[row][col] = 1#等于1表示把它的數(shù)值存儲起來
map[row][col] = player#
if player == 1:#當(dāng)點擊事件后
player = 2 #2是黑子
else:
player = 1#下一次是白子
screen.fill([125,95,24])#給屏幕畫上顏色
for x in range(15):#畫出15條豎線
pygame.draw.line(screen, (255, 0, 0),[25+50*x,25],[25+50*x,725],2)
for y in range(15):#畫出15條橫線
pygame.draw.line(screen, (255, 0, 0),[25,25+50*y],[725,25+50*y],2)
x,y = pygame.mouse.get_pos()#獲取鼠標所在位置
x = round((x - 25)/50)*50 + 25#找到x和y的交叉點
y = round((y - 25)/50)*50 + 25#找到x和y的交叉點
pygame.draw.rect(screen,(255, 0, 0),[x-25,y-25,50,50],2)#畫一個正方形框框,表示可以落子
pygame.draw.circle(screen, (255, 0, 0),[25+50*7,25+50*7],8)#棋盤中間畫出一個中心點
for row in range(15):#棋盤上畫出白色棋子
for col in range(15):#棋盤上畫出白色棋
if map[row][col] == 1:#點擊后在棋盤上畫出白色棋
pygame.draw.circle(screen,(255, 255, 255),[col*50+25,row*50+25],25)#棋盤上畫出白色棋的位置和大小半徑
if map[row][col] == 2: # 點擊后在棋盤上畫出白色棋
pygame.draw.circle(screen, (000, 000, 000), [col * 50 + 25, row * 50 + 25], 25) #當(dāng)時玩家2時棋盤上畫出黑色棋子
pygame.display.update()#刷新,變色用的
pygame.quit()#退出游戲
四,提示已經(jīng)被其他棋子占領(lǐng)了,不能重復(fù)占領(lǐng)
五。創(chuàng)建檢測五子相連的函數(shù)? def? check
六.,實現(xiàn)水平向左和向右方向的五子連線的判斷勝負
import pygame #導(dǎo)入pygame模塊
pygame.init()#初始化
screen = pygame.display.set_mode((750,750))#設(shè)置游戲屏幕大小
map = [0]*15#創(chuàng)建一個列表
for i in range(15):#列表循環(huán)來存儲數(shù)據(jù)
map[i] = [0]*15#二維列表來存儲數(shù)據(jù)
player = 1#創(chuàng)建玩家1
running = True
def check(row,col):#創(chuàng)建check函數(shù)檢測五子連線
#判斷左右方向是否五子連線
score = 1
for i in range(4):
if map[row][col+i] == map[row][col+i+1]:
score = score + 1
else:
break
for i in range(4):
if map[row][col-i] == map[row][col-i-1]:
score = score + 1
else:
break
if score == 5:
return True
while running:#事件運行
for event in pygame.event.get():
if event.type == pygame.QUIT:#當(dāng)點擊事件后退出
running = False #事件關(guān)閉
elif event.type == pygame.MOUSEBUTTONDOWN:#鼠標相應(yīng)點擊事件
x,y = pygame.mouse.get_pos()#獲取x,y的位置
col = round((x - 25)/50)#計算出x的位置
row = round((y - 25)/50)#計算出y的位置
if map[row][col]== 0:
print(row+1,col+1)#打印出x與y的位置
# map[row][col] = 1#等于1表示把它的數(shù)值存儲起來
map[row][col] = player#
if(check(row,col)):#如果檢測到五子連線了那么打印贏了
print("有人贏了")
else:
if player == 1:#當(dāng)點擊事件后
player = 2 #2是黑子
else:
player = 1#下一次是白子
else:
print("當(dāng)前位置已經(jīng)被占領(lǐng)了")
screen.fill([125,95,24])#給屏幕畫上顏色
for x in range(15):#畫出15條豎線
pygame.draw.line(screen, (255, 0, 0),[25+50*x,25],[25+50*x,725],2)
for y in range(15):#畫出15條橫線
pygame.draw.line(screen, (255, 0, 0),[25,25+50*y],[725,25+50*y],2)
x,y = pygame.mouse.get_pos()#獲取鼠標所在位置
x = round((x - 25)/50)*50 + 25#找到x和y的交叉點
y = round((y - 25)/50)*50 + 25#找到x和y的交叉點
pygame.draw.rect(screen,(255, 0, 0),[x-25,y-25,50,50],2)#畫一個正方形框框,表示可以落子
pygame.draw.circle(screen, (255, 0, 0),[25+50*7,25+50*7],8)#棋盤中間畫出一個中心點
for row in range(15):#棋盤上畫出白色棋子
for col in range(15):#棋盤上畫出白色棋
if map[row][col] == 1:#點擊后在棋盤上畫出白色棋
pygame.draw.circle(screen,(255, 255, 255),[col*50+25,row*50+25],25)#棋盤上畫出白色棋的位置和大小半徑
if map[row][col] == 2: # 點擊后在棋盤上畫出白色棋
pygame.draw.circle(screen, (000, 000, 000), [col * 50 + 25, row * 50 + 25], 25) #當(dāng)時玩家2時棋盤上畫出黑色棋子
pygame.display.update()#刷新,變色用的
pygame.quit()#退出游戲
檢查左右方向的水平方向五子棋連接的五個
檢測上下方向五子相連
檢測左下 右上方向五子相連
檢測左上右下的五子相連
import pygame #導(dǎo)入pygame模塊
pygame.init()#初始化
screen = pygame.display.set_mode((750,750))#設(shè)置游戲屏幕大小
map = [0]*15#創(chuàng)建一個列表
for i in range(15):#列表循環(huán)來存儲數(shù)據(jù)
map[i] = [0]*15#二維列表來存儲數(shù)據(jù)
player = 1#創(chuàng)建玩家1
running = True
def check(row,col):#創(chuàng)建check函數(shù)檢測五子連線
#判斷左右方向是否五子連線
score = 1
for i in range(4):
if map[row][col+i] == map[row][col+i+1]:
score = score + 1
else:
break
for i in range(4):
if map[row][col-i] == map[row][col-i-1]:
score = score + 1
else:
break
if score == 5:
return True
# 判斷上下方向是否五子連線
score = 1
for i in range(4):
if map[row+i][col] == map[row+i+1][col]:
score = score + 1
else:
break
for i in range(4):
if map[row-i][col] == map[row-i-1][col]:
score = score + 1
else:
break
if score == 5:
return True
# 判斷左下右上方向是否五子連線(左下行和列都減小,右上行和列都變大)
score = 1
for i in range(4):
if map[row+i][col+i] == map[row+i+1][col+i+1]:
score = score + 1
else:
break
for i in range(4):
if map[row-i][col-i] == map[row-i-1][col-i-1]:
score = score + 1
else:
break
if score == 5:
return True
# 判斷左上右下方向是否五子連線(左上行增加列減小,右下行變大,列變小)
score = 1
for i in range(4):
if map[row - i][col + i] == map[row - i - 1][col + i + 1]:
score = score + 1
else:
break
for i in range(4):
if map[row + i][col - i] == map[row + i + 1][col - i - 1]:
score = score + 1
else:
break
if score == 5:
return True
while running:#事件運行
for event in pygame.event.get():
if event.type == pygame.QUIT:#當(dāng)點擊事件后退出
running = False #事件關(guān)閉
elif event.type == pygame.MOUSEBUTTONDOWN:#鼠標相應(yīng)點擊事件
x,y = pygame.mouse.get_pos()#獲取x,y的位置
col = round((x - 25)/50)#計算出x的位置
row = round((y - 25)/50)#計算出y的位置
if map[row][col]== 0:
print(row+1,col+1)#打印出x與y的位置
# map[row][col] = 1#等于1表示把它的數(shù)值存儲起來
map[row][col] = player#
if(check(row,col)):#如果檢測到五子連線了那么打印贏了
print("有人贏了")
else:
if player == 1:#當(dāng)點擊事件后
player = 2 #2是黑子
else:
player = 1#下一次是白子
else:
print("當(dāng)前位置已經(jīng)被占領(lǐng)了")
screen.fill([125,95,24])#給屏幕畫上顏色
for x in range(15):#畫出15條豎線
pygame.draw.line(screen, (255, 0, 0),[25+50*x,25],[25+50*x,725],2)
for y in range(15):#畫出15條橫線
pygame.draw.line(screen, (255, 0, 0),[25,25+50*y],[725,25+50*y],2)
x,y = pygame.mouse.get_pos()#獲取鼠標所在位置
x = round((x - 25)/50)*50 + 25#找到x和y的交叉點
y = round((y - 25)/50)*50 + 25#找到x和y的交叉點
pygame.draw.rect(screen,(255, 0, 0),[x-25,y-25,50,50],2)#畫一個正方形框框,表示可以落子
pygame.draw.circle(screen, (255, 0, 0),[25+50*7,25+50*7],8)#棋盤中間畫出一個中心點
for row in range(15):#棋盤上畫出白色棋子
for col in range(15):#棋盤上畫出白色棋
if map[row][col] == 1:#點擊后在棋盤上畫出白色棋
pygame.draw.circle(screen,(255, 255, 255),[col*50+25,row*50+25],25)#棋盤上畫出白色棋的位置和大小半徑
if map[row][col] == 2: # 點擊后在棋盤上畫出白色棋
pygame.draw.circle(screen, (000, 000, 000), [col * 50 + 25, row * 50 + 25], 25) #當(dāng)時玩家2時棋盤上畫出黑色棋子
pygame.display.update()#刷新,變色用的
pygame.quit()#退出游戲
添加變量winner賦值給player,提示誰贏了,且提示3秒退出游戲
bug問題1,不是連續(xù)的五個放在一起棋子,不會判斷贏輸,
修改條件score 》=5
2.優(yōu)化把所有的數(shù)值設(shè)置為變量,
pygame.display.set_caption("這是五子棋游戲")
border_left =25
border_right =725
border_top = 25
border_bottom = 725
width = 50
height = 50
3.ttf字體下載網(wǎng)站;https://www.hanyi.com.cn/
下載下來和五子棋放在同一個目錄下面
4.把棋盤下面的按鈕功能用class button來實現(xiàn)
class Button:#定義按鈕的類
def __init__(self,x,y,width,height,text,color,click_color,text_color):#初始化
self.text = text#定義內(nèi)容
self.color = color#定義顏色
self.click_color = click_color#定義點擊后的顏色
self.text_color = text_color#定義文本顏色
self.rect = pygame.Rect(x,y,width,height)#定義按鈕的大小
self.clicked = False#不點擊
def draw(self,screen):#定義方法生成出按鈕
if self.clicked:
pygame.draw.rect(screen,self.click_color,self.rect)#畫出按鈕
else:
pygame.draw.rect(screen,self.color,self.rect)
text_surface = font.render(self.text,True,self.text_color)#雙人對戰(zhàn)按鈕的顏色
text_rect = text_surface.get_rect(center = self.rect.center)#雙人對戰(zhàn)按鈕的位置坐標
screen.blit(text_surface,text_rect)##雙人對戰(zhàn)按鈕的顏色
button = Button(30,470,100,30,"雙人模式",(153,51,250),(221,160,221),(255,255,255))#畫一個雙人模式按鈕
button.draw(screen)#畫一個雙人模式按鈕
button = Button(200, 470, 100, 30, "AI模式", (153, 51, 250), (221, 160, 221), (255, 255, 255)) # 畫一個機器人模式按鈕
button.draw(screen) # 畫一個機器人式按鈕
5.坐標出了棋盤范圍
在以下兩處添加限制x的左右和y軸的上下范圍后正常
import pygame #導(dǎo)入pygame模塊
pygame.init()#初始化
screen = pygame.display.set_mode((490,590))#設(shè)置游戲屏幕大小
#screen = pygame.display.set_mode((750,850))#設(shè)置游戲屏幕大小
pygame.display.set_caption("這是五子棋游戲")#游戲的名字
border_left =25#屏幕左邊界
border_right =445#屏幕右邊界
border_top = 25#屏幕上邊界
border_bottom = 445#屏幕下邊界
width = 30#棋盤小格子的寬
height = 30#棋盤小格子的高
map = [0]*15#創(chuàng)建一個列表
for i in range(15):#列表循環(huán)來存儲數(shù)據(jù)
map[i] = [0]*15#二維列表來存儲數(shù)據(jù)
player = 1#創(chuàng)建玩家1
winner = 0#創(chuàng)建一個變量,看看是誰贏了
running = True
#font = pygame.font.Font(None,30)#使用默認字體英文字體
font = pygame.font.Font("font.ttf", 24)#使用漢語字體大小24
class Button:#定義棋盤上胡按鈕的類
def __init__(self,x,y,width,height,text,color,click_color,text_color):#初始化定義類的屬性
self.text = text#定義按鈕文本內(nèi)容
self.color = color#定義按鈕胡顏色
self.click_color = click_color#定義點擊后的顏色
self.text_color = text_color#定義文本顏色
self.rect = pygame.Rect(x,y,width,height)#定義畫矩形按鈕的大小
self.clicked = False#不點擊
def draw(self,screen):#定義方法生成出按鈕
if self.clicked:
pygame.draw.rect(screen,self.click_color,self.rect)#畫出按鈕
else:
pygame.draw.rect(screen,self.color,self.rect)
text_surface = font.render(self.text,True,self.text_color)#雙人對戰(zhàn)按鈕的顏色和文本
text_rect = text_surface.get_rect(center = self.rect.center)#雙人對戰(zhàn)按鈕的位置坐標居中
screen.blit(text_surface,text_rect)##繪畫雙人對戰(zhàn)按鈕
def check(row,col):#創(chuàng)建check函數(shù)檢測五子連線
#判斷左右方向是否五子連線
score = 1
for i in range(4):
if map[row][col+i] == map[row][col+i+1]:
score = score + 1
else:
break
for i in range(4):
if map[row][col-i] == map[row][col-i-1]:
score = score + 1
else:
break
if score == 5:
return True
# 判斷上下方向是否五子連線
score = 1
for i in range(4):
if map[row+i][col] == map[row+i+1][col]:
score = score + 1
else:
break
for i in range(4):
if map[row-i][col] == map[row-i-1][col]:
score = score + 1
else:
break
if score == 5:
return True
# 判斷左下右上方向是否五子連線(左下行和列都減小,右上行和列都變大)
score = 1
for i in range(4):
if map[row+i][col+i] == map[row+i+1][col+i+1]:
score = score + 1
else:
break
for i in range(4):
if map[row-i][col-i] == map[row-i-1][col-i-1]:
score = score + 1
else:
break
if score == 5:
return True
# 判斷左上右下方向是否五子連線(左上行增加列減小,右下行變大,列變小)
score = 1
for i in range(4):
if map[row - i][col + i] == map[row - i - 1][col + i + 1]:
score = score + 1
else:
break
for i in range(4):
if map[row + i][col - i] == map[row + i + 1][col - i - 1]:
score = score + 1
else:
break
if score == 5:
return True
while running:#事件運行
for event in pygame.event.get():
if event.type == pygame.QUIT:#當(dāng)點擊事件后退出
running = False #事件關(guān)閉
elif event.type == pygame.MOUSEBUTTONDOWN:#鼠標相應(yīng)點擊事件
x,y = pygame.mouse.get_pos()#獲取x,y的位置
if x >= border_left and x <= border_right and y >= border_top and y <= border_bottom: # 限制x的左右邊
col = round((x - 15)/30)#計算出x的位置
row = round((y - 15)/30)#計算出y的位置
if map[row][col]== 0:
print(row+1,col+1)#打印出x與y的位置
# map[row][col] = 1#等于1表示把它的數(shù)值存儲起來
map[row][col] = player#
if(check(row,col)):#如果檢測到五子連線了那么打印贏了
#print("有人贏了")
winner = player#把贏賦值給winner
else:
if player == 1:#當(dāng)點擊事件后
player = 2 #2是黑子
else:
player = 1#下一次是白子
else:
print("當(dāng)前位置已經(jīng)被占領(lǐng)了")
screen.fill([125,95,24])#給屏幕畫上顏色
for x in range(15):#畫出15條豎線
pygame.draw.line(screen, (255, 0, 0),[border_left+width*x,border_top],[border_left+width*x,border_bottom],2)
for y in range(15):#畫出15條橫線
pygame.draw.line(screen, (255, 0, 0),[border_left,border_top+height*y],[border_right,border_top+height*y],2)
x,y = pygame.mouse.get_pos()#獲取鼠標所在位置
if x >=border_left and x<=border_right and y>=border_top and y<=border_bottom:#限制x的左右邊緣和y的上下邊緣
x = round((x - border_left)/width)*width + border_left#找到x和y的交叉點
y = round((y - border_top)/width)*width + border_top#找到x和y的交叉點
pygame.draw.rect(screen,(255, 0, 0),[x-15,y-15,30,30],2)#畫一個正方形框框,表示可以落子
#pygame.draw.rect(screen,(153, 51, 250),[50,750,100,50],2)#畫一個正方形框框,表示按鈕
#text_surface = font.render("雙人對戰(zhàn)",True,(200,200,200))#雙人對戰(zhàn)按鈕的顏色
#text_position = (50,750)#雙人對戰(zhàn)按鈕的位置坐標
#screen.blit(text_surface,text_position)##雙人對戰(zhàn)按鈕的顏色
pygame.draw.circle(screen, (255, 0, 0),[25+30*7,25+30*7],8)#棋盤中間畫出一個中心點
button = Button(30,470,100,30,"雙人模式",(153,51,250),(221,160,221),(255,255,255))#畫一個雙人模式按鈕,調(diào)用類Button實例化對象button(x坐標,Y坐標,文本內(nèi)容,文本顏色)
button.draw(screen)#畫一個雙人模式按鈕,調(diào)用方法draw
button = Button(200, 470, 100, 30, "AI模式", (153, 51, 250), (221, 160, 221), (255, 255, 255)) # 畫一個機器人模式按鈕
button.draw(screen) # 畫一個機器人式按鈕
for row in range(15):#棋盤上畫出白色棋子
for col in range(15):#棋盤上畫出白色棋
if map[row][col] == 1:#點擊后在棋盤上畫出白色棋
pygame.draw.circle(screen,(255, 255, 255),[col*width+border_left,row*height+border_top],15)#棋盤上畫出白色棋的位置和大小半徑
if map[row][col] == 2: # 點擊后在棋盤上畫出白色棋
pygame.draw.circle(screen, (000, 000, 000), [col * width + border_left, row * height +border_top ], 15) #當(dāng)時玩家2時棋盤上畫出黑色棋子
pygame.display.update() # 刷新,變色用的
if(winner !=0):
if winner ==1:
text ="白子贏了,倒計時3秒退出"
color = (255,255,255,255,)
else:
text = '黑子贏了,倒計時3秒退出'
#text = 'black won,countdown to 3 seconds更改'
color = (0,0,0,0)
#font = pygame.font.Font(None,30)#使用默認字體英文字體
font = pygame.font.Font("font.ttf", 20)#使用漢語字體提示
text_surface = font.render(text,True,color)#字體的表面顏色
text_position = (60,60)#字體的坐標
screen.blit(text_surface,text_position)#開始繪畫字體文本
pygame.display.update() # 刷新,變色用的
pygame.time.wait(3000)#顯示字體等待3秒
running = False#程序關(guān)閉
pygame.quit()#退出游戲
5,當(dāng)在第15格也就是邊界時會報錯
添加try? except? ? ?break后就可以,比對坐標超過16行了,也不報錯了,程序繼續(xù)運行
到這里這個游戲基本完成了
import pygame #導(dǎo)入pygame模塊
pygame.init()#初始化
screen = pygame.display.set_mode((490,590))#設(shè)置游戲屏幕大小
#screen = pygame.display.set_mode((750,850))#設(shè)置游戲屏幕大小
pygame.display.set_caption("這是五子棋游戲")
border_left =25
border_right =445
border_top = 25
border_bottom = 445
width = 30
height = 30
map = [0]*15#創(chuàng)建一個列表
for i in range(15):#列表循環(huán)來存儲數(shù)據(jù)
map[i] = [0]*15#二維列表來存儲數(shù)據(jù)
player = 1#創(chuàng)建玩家1
winner = 0#創(chuàng)建一個變量,看看是誰贏了
running = True
#font = pygame.font.Font(None,30)#使用默認字體英文字體
font = pygame.font.Font("font.ttf", 24)#使用漢語字體大小24
#font = pygame.font.Font("font.ttf",70)
class Button:#定義按鈕的類
def __init__(self,x,y,width,height,text,color,click_color,text_color):#初始化
self.text = text#定義內(nèi)容
self.color = color#定義顏色
self.click_color = click_color#定義點擊后的顏色
self.text_color = text_color#定義文本顏色
self.rect = pygame.Rect(x,y,width,height)#定義按鈕的大小
self.clicked = False#不點擊
def draw(self,screen):#定義方法生成出按鈕
if self.clicked:
pygame.draw.rect(screen,self.click_color,self.rect)#畫出按鈕
else:
pygame.draw.rect(screen,self.color,self.rect)
text_surface = font.render(self.text,True,self.text_color)#雙人對戰(zhàn)按鈕的顏色
text_rect = text_surface.get_rect(center = self.rect.center)#雙人對戰(zhàn)按鈕的位置坐標
screen.blit(text_surface,text_rect)##雙人對戰(zhàn)按鈕的顏色
def check(row,col):#創(chuàng)建check函數(shù)檢測五子連線
#判斷左右方向是否五子連線
score = 1
for i in range(4):
try:
if map[row][col+i] == map[row][col+i+1]:
score = score + 1
else:
break
except:
break
for i in range(4):
try:
if map[row][col-i] == map[row][col-i-1]:
score = score + 1
else:
break
except:
break
if score == 5:
return True
# 判斷上下方向是否五子連線
score = 1
for i in range(4):
try:
if map[row+i][col] == map[row+i+1][col]:
score = score + 1
else:
break
except:
break
for i in range(4):
try:
if map[row-i][col] == map[row-i-1][col]:
score = score + 1
else:
break
except:
break
if score == 5:
return True
# 判斷左下右上方向是否五子連線(左下行和列都減小,右上行和列都變大)
score = 1
for i in range(4):
try:
if map[row+i][col+i] == map[row+i+1][col+i+1]:
score = score + 1
else:
break
except:
break
for i in range(4):
try:
if map[row-i][col-i] == map[row-i-1][col-i-1]:
score = score + 1
else:
break
except:
break
if score == 5:
return True
# 判斷左上右下方向是否五子連線(左上行增加列減小,右下行變大,列變小)
score = 1
for i in range(4):
try:
if map[row - i][col + i] == map[row - i - 1][col + i + 1]:
score = score + 1
else:
break
except:
break
for i in range(4):
try:
if map[row + i][col - i] == map[row + i + 1][col - i - 1]:
score = score + 1
else:
break
except:
break
if score == 5:
return True
while running:#事件運行
for event in pygame.event.get():
if event.type == pygame.QUIT:#當(dāng)點擊事件后退出
running = False #事件關(guān)閉
elif event.type == pygame.MOUSEBUTTONDOWN:#鼠標相應(yīng)點擊事件
x,y = pygame.mouse.get_pos()#獲取x,y的位置
if x >= border_left and x <= border_right and y >= border_top and y <= border_bottom: # 限制x的左右邊
col = round((x - 15)/30)#計算出x的位置
row = round((y - 15)/30)#計算出y的位置
if map[row][col]== 0:
print(row+1,col+1)#打印出x與y的位置
# map[row][col] = 1#等于1表示把它的數(shù)值存儲起來
map[row][col] = player#
if(check(row,col)):#如果檢測到五子連線了那么打印贏了
#print("有人贏了")
winner = player#把贏賦值給winner
else:
if player == 1:#當(dāng)點擊事件后
player = 2 #2是黑子
else:
player = 1#下一次是白子
else:
print("當(dāng)前位置已經(jīng)被占領(lǐng)了")
screen.fill([125,95,24])#給屏幕畫上顏色
for x in range(15):#畫出15條豎線
pygame.draw.line(screen, (255, 0, 0),[border_left+width*x,border_top],[border_left+width*x,border_bottom],2)
for y in range(15):#畫出15條橫線
pygame.draw.line(screen, (255, 0, 0),[border_left,border_top+height*y],[border_right,border_top+height*y],2)
x,y = pygame.mouse.get_pos()#獲取鼠標所在位置
if x >=border_left and x<=border_right and y>=border_top and y<=border_bottom:#限制x的左右邊緣和y的上下邊緣
x = round((x - border_left)/width)*width + border_left#找到x和y的交叉點
y = round((y - border_top)/width)*width + border_top#找到x和y的交叉點
pygame.draw.rect(screen,(255, 0, 0),[x-15,y-15,30,30],2)#畫一個正方形框框,表示可以落子
#pygame.draw.rect(screen,(153, 51, 250),[50,750,100,50],2)#畫一個正方形框框,表示按鈕
#text_surface = font.render("雙人對戰(zhàn)",True,(200,200,200))#雙人對戰(zhàn)按鈕的顏色
#text_position = (50,750)#雙人對戰(zhàn)按鈕的位置坐標
#screen.blit(text_surface,text_position)##雙人對戰(zhàn)按鈕的顏色
pygame.draw.circle(screen, (255, 0, 0),[25+30*7,25+30*7],8)#棋盤中間畫出一個中心點
button = Button(30,470,100,30,"雙人模式",(153,51,250),(221,160,221),(255,255,255))#畫一個雙人模式按鈕
button.draw(screen)#畫一個雙人模式按鈕
button = Button(200, 470, 100, 30, "AI模式", (153, 51, 250), (221, 160, 221), (255, 255, 255)) # 畫一個機器人模式按鈕
button.draw(screen) # 畫一個機器人式按鈕
for row in range(15):#棋盤上畫出白色棋子
for col in range(15):#棋盤上畫出白色棋
if map[row][col] == 1:#點擊后在棋盤上畫出白色棋
pygame.draw.circle(screen,(255, 255, 255),[col*width+border_left,row*height+border_top],15)#棋盤上畫出白色棋的位置和大小半徑
if map[row][col] == 2: # 點擊后在棋盤上畫出白色棋
pygame.draw.circle(screen, (000, 000, 000), [col * width + border_left, row * height +border_top ], 15) #當(dāng)時玩家2時棋盤上畫出黑色棋子
pygame.display.update() # 刷新,變色用的
if(winner !=0):
if winner ==1:
text ="白子贏了,倒計時3秒后退出"
color = (255,255,255,255,)
else:
text = '黑子贏了,倒計時3秒后退出'
#text = 'black won,countdown to 3 seconds更改'
color = (0,0,0,0)
#font = pygame.font.Font(None,30)#使用默認字體英文字體
font = pygame.font.Font("font.ttf", 20)#使用漢語字體提示
text_surface = font.render(text,True,color)#字體的顏色
text_position = (60,60)#字體的坐標
screen.blit(text_surface,text_position)
pygame.display.update() # 刷新,變色用的
pygame.time.wait(3000)#顯示字體等待3秒
running = False#程序關(guān)閉
pygame.quit()#退出游戲
創(chuàng)建類把游戲分成2大塊,創(chuàng)建2個類分別是按鈕類和游戲類,游戲類里面定義三個方法1,開始方法,2檢查方法。3是鼠標點擊方法
import pygame #導(dǎo)入pygame模塊
pygame.init()#初始化
screen = pygame.display.set_mode((490,590))#設(shè)置游戲屏幕大小
#screen = pygame.display.set_mode((750,850))#設(shè)置游戲屏幕大小
pygame.display.set_caption("這是五子棋游戲")
border_left =25
border_right =445
border_top = 25
border_bottom = 445
width = 30
height = 30
running = True
#font = pygame.font.Font(None,30)#使用默認字體英文字體
font = pygame.font.Font("font.ttf", 24)#使用漢語字體大小24
#font = pygame.font.Font("font.ttf",70)
class Button:#定義按鈕的類
def __init__(self,x,y,width,height,text,color,click_color,text_color):#初始化
self.text = text#定義內(nèi)容
self.color = color#定義顏色
self.click_color = click_color#定義點擊后的顏色
self.text_color = text_color#定義文本顏色
self.rect = pygame.Rect(x,y,width,height)#定義按鈕的大小
self.clicked = False#不點擊
def draw(self,screen):#定義方法生成出按鈕
if self.clicked:
pygame.draw.rect(screen,self.click_color,self.rect)#畫出按鈕
else:
pygame.draw.rect(screen,self.color,self.rect)
text_surface = font.render(self.text,True,self.text_color)#雙人對戰(zhàn)按鈕的顏色
text_rect = text_surface.get_rect(center = self.rect.center)#雙人對戰(zhàn)按鈕的位置坐標
screen.blit(text_surface,text_rect)##雙人對戰(zhàn)按鈕的顏色cla
class Game:
def __init__(self):
self.player = 1 # 創(chuàng)建玩家1
self.winner = 0 # 創(chuàng)建一個變量,看看是誰贏了
self.map = [0] * 15 # 創(chuàng)建一個列表
for i in range(15): # 列表循環(huán)來存儲數(shù)據(jù)
self.map[i] = [0] * 15 # 二維列表來存儲數(shù)據(jù)
def start(self):
screen.fill([125, 95, 24]) # 給屏幕畫上顏色
for x in range(15): # 畫出15條豎線
pygame.draw.line(screen, (255, 0, 0), [border_left + width * x, border_top],
[border_left + width * x, border_bottom], 2)
for y in range(15): # 畫出15條橫線
pygame.draw.line(screen, (255, 0, 0), [border_left, border_top + height * y],
[border_right, border_top + height * y], 2)
x, y = pygame.mouse.get_pos() # 獲取鼠標所在位置
if x >= border_left and x <= border_right and y >= border_top and y <= border_bottom: # 限制x的左右邊緣和y的上下邊緣
x = round((x - border_left) / width) * width + border_left # 找到x和y的交叉點
y = round((y - border_top) / width) * width + border_top # 找到x和y的交叉點
pygame.draw.rect(screen, (255, 0, 0), [x - 15, y - 15, 30, 30], 2) # 畫一個正方形框框,表示可以落子
# pygame.draw.rect(screen,(153, 51, 250),[50,750,100,50],2)#畫一個正方形框框,表示按鈕
# text_surface = font.render("雙人對戰(zhàn)",True,(200,200,200))#雙人對戰(zhàn)按鈕的顏色
# text_position = (50,750)#雙人對戰(zhàn)按鈕的位置坐標
# screen.blit(text_surface,text_position)##雙人對戰(zhàn)按鈕的顏色
pygame.draw.circle(screen, (255, 0, 0), [25 + 30 * 7, 25 + 30 * 7], 8) # 棋盤中間畫出一個中心點
#button = Button(30, 470, 100, 30, "雙人模式", (153, 51, 250), (221, 160, 221), (255, 255, 255)) # 畫一個雙人模式按鈕
button.draw(screen) # 畫一個雙人模式按鈕
#button_ai = Button(200, 470, 100, 30, "AI模式", (153, 51, 250), (221, 160, 221), (255, 255, 255)) # 畫一個機器人模式按鈕
button_ai.draw(screen) # 畫一個機器人式按鈕
for row in range(15): # 棋盤上畫出白色棋子
for col in range(15): # 棋盤上畫出白色棋
if self.map[row][col] == 1: # 點擊后在棋盤上畫出白色棋
pygame.draw.circle(screen, (255, 255, 255), [col * width + border_left, row * height + border_top],
15) # 棋盤上畫出白色棋的位置和大小半徑
if self.map[row][col] == 2: # 點擊后在棋盤上畫出白色棋
pygame.draw.circle(screen, (000, 000, 000), [col * width + border_left, row * height + border_top],
15) # 當(dāng)時玩家2時棋盤上畫出黑色棋子
if (self.winner != 0):
if self.winner == 1:
text = "白子贏了,倒計時3秒后退出"
color = (255, 255, 255, 255,)
else:
text = '黑子贏了,倒計時3秒后退出'
# text = 'black won,countdown to 3 seconds更改'
color = (0, 0, 0, 0)
# font = pygame.font.Font(None,30)#使用默認字體英文字體
font = pygame.font.Font("font.ttf", 20) # 使用漢語字體提示
text_surface = font.render(text, True, color) # 字體的顏色
text_position = (60, 60) # 字體的坐標
screen.blit(text_surface, text_position)
pygame.display.update() # 刷新,變色用的
pygame.time.wait(3000) # 顯示字體等待3秒
running = False # 程序關(guān)閉
def check(self,row, col): # 創(chuàng)建check函數(shù)檢測五子連線
# 判斷左右方向是否五子連線
score = 1
for i in range(4):
try:
if self.map[row][col + i] == self.map[row][col + i + 1]:
score = score + 1
else:
break
except:
break
for i in range(4):
try:
if self.map[row][col - i] == self.map[row][col - i - 1]:
score = score + 1
else:
break
except:
break
if score == 5:
return True
# 判斷上下方向是否五子連線
score = 1
for i in range(4):
try:
if self.map[row + i][col] == self.map[row + i + 1][col]:
score = score + 1
else:
break
except:
break
for i in range(4):
try:
if self.map[row - i][col] == self.map[row - i - 1][col]:
score = score + 1
else:
break
except:
break
if score == 5:
return True
# 判斷左下右上方向是否五子連線(左下行和列都減小,右上行和列都變大)
score = 1
for i in range(4):
try:
if self.map[row + i][col + i] == self.map[row + i + 1][col + i + 1]:
score = score + 1
else:
break
except:
break
for i in range(4):
try:
if self.map[row - i][col - i] == self.map[row - i - 1][col - i - 1]:
score = score + 1
else:
break
except:
break
if score == 5:
return True
# 判斷左上右下方向是否五子連線(左上行增加列減小,右下行變大,列變小)
score = 1
for i in range(4):
try:
if self.map[row - i][col + i] == self.map[row - i - 1][col + i + 1]:
score = score + 1
else:
break
except:
break
for i in range(4):
try:
if self.map[row + i][col - i] == self.map[row + i + 1][col - i - 1]:
score = score + 1
else:
break
except:
break
if score == 5:
return True
def mouseClick(self,x,y):
if x >= border_left and x <= border_right and y >= border_top and y <= border_bottom: # 限制x的左右邊
col = round((x - 15) / 30) # 計算出x的位置
row = round((y - 15) / 30) # 計算出y的位置
if self.map[row][col] == 0:
print(row + 1, col + 1) # 打印出x與y的位置
# self.map[row][col] = 1#等于1表示把它的數(shù)值存儲起來
self.map[row][col] = self.player #
if (self.check(row, col)): # 如果檢測到五子連線了那么打印贏了
# print("有人贏了")
self.winner = self.player # 把贏賦值給winner
else:
if self.player == 1: # 當(dāng)點擊事件后
self.player = 2 # 2是黑子
else:
self.player = 1 # 下一次是白子
else:
print("當(dāng)前位置已經(jīng)被占領(lǐng)了")
button = Button(30, 470, 100, 30, "雙人模式", (153, 51, 250), (221, 160, 221), (255, 255, 255)) # 畫一個雙人模式按鈕
button_ai = Button(200, 470, 100, 30, "AI模式", (153, 51, 250), (221, 160, 221), (255, 255, 255)) # 畫一個機器人模式按鈕
game = Game()
while running:#事件運行
for event in pygame.event.get():
if event.type == pygame.QUIT:#當(dāng)點擊事件后退出
running = False #事件關(guān)閉
elif event.type == pygame.MOUSEBUTTONDOWN:#鼠標相應(yīng)點擊事件
x,y = pygame.mouse.get_pos()#獲取x,y的位置
game.mouseClick(x,y)
game.start()
pygame.display.update() # 刷新,變色用的
pygame.quit()#退出游戲
實現(xiàn)了點“ 雙人模式”開始玩游戲,沒有點擊雙人“雙人模式前”不能在棋盤上落子看到棋子
import pygame #導(dǎo)入pygame模塊
pygame.init()#初始化
screen = pygame.display.set_mode((490,590))#設(shè)置游戲屏幕大小
#screen = pygame.display.set_mode((750,850))#設(shè)置游戲屏幕大小
pygame.display.set_caption("這是五子棋游戲")
border_left =25
border_right =445
border_top = 25
border_bottom = 445
width = 30
height = 30
#running = True
#font = pygame.font.Font(None,30)#使用默認字體英文字體
font = pygame.font.Font("font.ttf", 24)#使用漢語字體大小24
#font = pygame.font.Font("font.ttf",70)
class Button:#定義按鈕的類
def __init__(self,x,y,width,height,text,color,click_color,text_color):#初始化
self.text = text#定義內(nèi)容
self.color = color#定義顏色
self.click_color = click_color#定義點擊后的顏色
self.text_color = text_color#定義文本顏色
self.rect = pygame.Rect(x,y,width,height)#定義按鈕的大小
self.clicked = False#不點擊
def draw(self,screen):#定義方法生成出按鈕
if self.clicked:
pygame.draw.rect(screen,self.click_color,self.rect)#畫出按鈕
else:
pygame.draw.rect(screen,self.color,self.rect)
text_surface = font.render(self.text,True,self.text_color)#雙人對戰(zhàn)按鈕的顏色
text_rect = text_surface.get_rect(center = self.rect.center)#雙人對戰(zhàn)按鈕的位置坐標
screen.blit(text_surface,text_rect)##雙人對戰(zhàn)按鈕的顏色cla
def handle_event(self,event,game):
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
if self.rect.collidepoint(event.pos):
if(self.clicked):
self.clicked = False
print("游戲開始了,別點了")
else:
self.clicked = True
game.started = True
print("點擊,已經(jīng)開始")
class Game:
def __init__(self):
self.started = False#游戲默認是不能玩的
self.player = 1 # 創(chuàng)建玩家1
self.winner = 0 # 創(chuàng)建一個變量,看看是誰贏了
self.map = [0] * 15 # 創(chuàng)建一個列表
for i in range(15): # 列表循環(huán)來存儲數(shù)據(jù)
self.map[i] = [0] * 15 # 二維列表來存儲數(shù)據(jù)
def start(self):
screen.fill([125, 95, 24]) # 給屏幕畫上顏色
for x in range(15): # 畫出15條豎線
pygame.draw.line(screen, (255, 0, 0), [border_left + width * x, border_top],
[border_left + width * x, border_bottom], 2)
for y in range(15): # 畫出15條橫線
pygame.draw.line(screen, (255, 0, 0), [border_left, border_top + height * y],
[border_right, border_top + height * y], 2)
x, y = pygame.mouse.get_pos() # 獲取鼠標所在位置
if x >= border_left and x <= border_right and y >= border_top and y <= border_bottom: # 限制x的左右邊緣和y的上下邊緣
x = round((x - border_left) / width) * width + border_left # 找到x和y的交叉點
y = round((y - border_top) / width) * width + border_top # 找到x和y的交叉點
pygame.draw.rect(screen, (255, 0, 0), [x - 15, y - 15, 30, 30], 2) # 畫一個正方形框框,表示可以落子
# pygame.draw.rect(screen,(153, 51, 250),[50,750,100,50],2)#畫一個正方形框框,表示按鈕
# text_surface = font.render("雙人對戰(zhàn)",True,(200,200,200))#雙人對戰(zhàn)按鈕的顏色
# text_position = (50,750)#雙人對戰(zhàn)按鈕的位置坐標
# screen.blit(text_surface,text_position)##雙人對戰(zhàn)按鈕的顏色
pygame.draw.circle(screen, (255, 0, 0), [25 + 30 * 7, 25 + 30 * 7], 8) # 棋盤中間畫出一個中心點
#button = Button(30, 470, 100, 30, "雙人模式", (153, 51, 250), (221, 160, 221), (255, 255, 255)) # 畫一個雙人模式按鈕
button.draw(screen) # 畫一個雙人模式按鈕
#button_ai = Button(200, 470, 100, 30, "AI模式", (153, 51, 250), (221, 160, 221), (255, 255, 255)) # 畫一個機器人模式按鈕
button_ai.draw(screen) # 畫一個機器人式按鈕
for row in range(15): # 棋盤上畫出白色棋子
for col in range(15): # 棋盤上畫出白色棋
if self.map[row][col] == 1: # 點擊后在棋盤上畫出白色棋
pygame.draw.circle(screen, (255, 255, 255), [col * width + border_left, row * height + border_top],
15) # 棋盤上畫出白色棋的位置和大小半徑
if self.map[row][col] == 2: # 點擊后在棋盤上畫出白色棋
pygame.draw.circle(screen, (000, 000, 000), [col * width + border_left, row * height + border_top],
15) # 當(dāng)時玩家2時棋盤上畫出黑色棋子
if (self.winner != 0):
if self.winner == 1:
text = "白子贏了,倒計時3秒后退出"
color = (255, 255, 255, 255,)
else:
text = '黑子贏了,倒計時3秒后退出'
# text = 'black won,countdown to 3 seconds更改'
color = (0, 0, 0, 0)
# font = pygame.font.Font(None,30)#使用默認字體英文字體
font = pygame.font.Font("font.ttf", 20) # 使用漢語字體提示
text_surface = font.render(text, True, color) # 字體的顏色
text_position = (60, 60) # 字體的坐標
screen.blit(text_surface, text_position)
pygame.display.update() # 刷新,變色用的
pygame.time.wait(3000) # 顯示字體等待3秒
#running = False # 程序關(guān)閉
button.clicked = False#贏了后點擊“雙人模式”繼續(xù)游戲
def check(self,row, col): # 創(chuàng)建check函數(shù)檢測五子連線
# 判斷左右方向是否五子連線
score = 1
for i in range(4):
try:
if self.map[row][col + i] == self.map[row][col + i + 1]:
score = score + 1
else:
break
except:
break
for i in range(4):
try:
if self.map[row][col - i] == self.map[row][col - i - 1]:
score = score + 1
else:
break
except:
break
if score == 5:
return True
# 判斷上下方向是否五子連線
score = 1
for i in range(4):
try:
if self.map[row + i][col] == self.map[row + i + 1][col]:
score = score + 1
else:
break
except:
break
for i in range(4):
try:
if self.map[row - i][col] == self.map[row - i - 1][col]:
score = score + 1
else:
break
except:
break
if score == 5:
return True
# 判斷左下右上方向是否五子連線(左下行和列都減小,右上行和列都變大)
score = 1
for i in range(4):
try:
if self.map[row + i][col + i] == self.map[row + i + 1][col + i + 1]:
score = score + 1
else:
break
except:
break
for i in range(4):
try:
if self.map[row - i][col - i] == self.map[row - i - 1][col - i - 1]:
score = score + 1
else:
break
except:
break
if score == 5:
return True
# 判斷左上右下方向是否五子連線(左上行增加列減小,右下行變大,列變小)
score = 1
for i in range(4):
try:
if self.map[row - i][col + i] == self.map[row - i - 1][col + i + 1]:
score = score + 1
else:
break
except:
break
for i in range(4):
try:
if self.map[row + i][col - i] == self.map[row + i + 1][col - i - 1]:
score = score + 1
else:
break
except:
break
if score == 5:
return True
def mouse_Click(self,x,y):
if x >= border_left and x <= border_right and y >= border_top and y <= border_bottom: # 限制x的左右邊
if self.started:#點擊“雙人模式”后可以玩游戲
col = round((x - 15) / 30) # 計算出x的位置
row = round((y - 15) / 30) # 計算出y的位置
if self.map[row][col] == 0:
print(row + 1, col + 1) # 打印出x與y的位置
# self.map[row][col] = 1#等于1表示把它的數(shù)值存儲起來
self.map[row][col] = self.player #
if (self.check(row, col)): # 如果檢測到五子連線了那么打印贏了
# print("有人贏了")
self.winner = self.player # 把贏賦值給winner
else:
if self.player == 1: # 當(dāng)點擊事件后
self.player = 2 # 2是黑子
else:
self.player = 1 # 下一次是白子
else:
print("當(dāng)前位置已經(jīng)被占領(lǐng)了")
else:
print("請點擊了雙人模式 后才能玩游戲了")
button = Button(30, 470, 100, 30, "雙人模式", (153, 51, 250), (221, 160, 221), (255, 255, 255)) # 畫一個雙人模式按鈕
button_ai = Button(200, 470, 100, 30, "AI模式", (153, 51, 250), (221, 160, 221), (255, 255, 255)) # 畫一個機器人模式按鈕
game = Game()
while True:#事件運行
for event in pygame.event.get():
button.handle_event(event,game)
if event.type == pygame.QUIT:#當(dāng)點擊事件后退出
running = False #事件關(guān)閉
pygame.quit() # 退出游戲
exit()#系統(tǒng)級別的退出
elif event.type == pygame.MOUSEBUTTONDOWN:#鼠標相應(yīng)點擊事件
x,y = pygame.mouse.get_pos()#獲取x,y的位置
game.mouse_Click(x,y)
game.start()
pygame.display.update() # 刷新,變色用的
實現(xiàn)下完棋后,棋盤清空,等待三秒可以點擊“雙人模式”再次打開游戲文章來源:http://www.zghlxwxcb.cn/news/detail-752824.html
完整代碼如下:在pycharm當(dāng)中運行,python3,7文章來源地址http://www.zghlxwxcb.cn/news/detail-752824.html
import pygame #導(dǎo)入pygame模塊
pygame.init()#初始化
screen = pygame.display.set_mode((490,590))#設(shè)置游戲屏幕大小
#screen = pygame.display.set_mode((750,850))#設(shè)置游戲屏幕大小
pygame.display.set_caption("這是五子棋游戲")
border_left =25
border_right =445
border_top = 25
border_bottom = 445
width = 30
height = 30
#running = True
#font = pygame.font.Font(None,30)#使用默認字體英文字體
font = pygame.font.Font("font.ttf", 24)#使用漢語字體大小24
#font = pygame.font.Font("font.ttf",70)
class Button:#定義按鈕的類
def __init__(self,x,y,width,height,text,color,click_color,text_color):#初始化
self.text = text#定義內(nèi)容
self.color = color#定義顏色
self.click_color = click_color#定義點擊后的顏色
self.text_color = text_color#定義文本顏色
self.rect = pygame.Rect(x,y,width,height)#定義按鈕的大小
self.clicked = False#不點擊
def draw(self,screen):#定義方法生成出按鈕
if self.clicked:
pygame.draw.rect(screen,self.click_color,self.rect)#畫出按鈕
else:
pygame.draw.rect(screen,self.color,self.rect)
text_surface = font.render(self.text,True,self.text_color)#雙人對戰(zhàn)按鈕的顏色
text_rect = text_surface.get_rect(center = self.rect.center)#雙人對戰(zhàn)按鈕的位置坐標
screen.blit(text_surface,text_rect)##雙人對戰(zhàn)按鈕的顏色cla
def handle_event(self,event,game):
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
if self.rect.collidepoint(event.pos):
if(self.clicked):
self.clicked = False
print("游戲開始了,別點了")
else:
if game.winner == 0:#游戲開始沒有人玩的狀態(tài)
self.clicked = True
game.started = True
print("點擊,已經(jīng)開始")
else:
self.clicked = True
#game.started = False # 游戲默認是不能玩的
game.started = True # 游戲默認是不能玩的
game.player = 1 # 玩家1
game.winner = 0 # 創(chuàng)建一個變量,看看是誰贏了
game.map = [0] * 15 # 創(chuàng)建一個列表
for i in range(15): # 列表循環(huán)來存儲數(shù)據(jù)
game.map[i] = [0] * 15 # 二維列表來存儲數(shù)據(jù)
print("點 雙人模式 重新開始游戲")
class Game:
def __init__(self):
self.started = False#游戲默認是不能玩的
self.player = 1 # 創(chuàng)建玩家1
self.winner = 0 # 創(chuàng)建一個變量,看看是誰贏了
self.map = [0] * 15 # 創(chuàng)建一個列表
for i in range(15): # 列表循環(huán)來存儲數(shù)據(jù)
self.map[i] = [0] * 15 # 二維列表來存儲數(shù)據(jù)
def start(self):
screen.fill([125, 95, 24]) # 給屏幕畫上顏色
for x in range(15): # 畫出15條豎線
pygame.draw.line(screen, (255, 0, 0), [border_left + width * x, border_top],
[border_left + width * x, border_bottom], 2)
for y in range(15): # 畫出15條橫線
pygame.draw.line(screen, (255, 0, 0), [border_left, border_top + height * y],
[border_right, border_top + height * y], 2)
x, y = pygame.mouse.get_pos() # 獲取鼠標所在位置
if x >= border_left and x <= border_right and y >= border_top and y <= border_bottom: # 限制x的左右邊緣和y的上下邊緣
x = round((x - border_left) / width) * width + border_left # 找到x和y的交叉點
y = round((y - border_top) / width) * width + border_top # 找到x和y的交叉點
pygame.draw.rect(screen, (255, 0, 0), [x - 15, y - 15, 30, 30], 2) # 畫一個正方形框框,表示可以落子
# pygame.draw.rect(screen,(153, 51, 250),[50,750,100,50],2)#畫一個正方形框框,表示按鈕
# text_surface = font.render("雙人對戰(zhàn)",True,(200,200,200))#雙人對戰(zhàn)按鈕的顏色
# text_position = (50,750)#雙人對戰(zhàn)按鈕的位置坐標
# screen.blit(text_surface,text_position)##雙人對戰(zhàn)按鈕的顏色
pygame.draw.circle(screen, (255, 0, 0), [25 + 30 * 7, 25 + 30 * 7], 8) # 棋盤中間畫出一個中心點
#button = Button(30, 470, 100, 30, "雙人模式", (153, 51, 250), (221, 160, 221), (255, 255, 255)) # 畫一個雙人模式按鈕
button.draw(screen) # 畫一個雙人模式按鈕
#button_ai = Button(200, 470, 100, 30, "AI模式", (153, 51, 250), (221, 160, 221), (255, 255, 255)) # 畫一個機器人模式按鈕
button_ai.draw(screen) # 畫一個機器人式按鈕
for row in range(15): # 棋盤上畫出白色棋子
for col in range(15): # 棋盤上畫出白色棋
if self.map[row][col] == 1: # 點擊后在棋盤上畫出白色棋
pygame.draw.circle(screen, (255, 255, 255), [col * width + border_left, row * height + border_top],
15) # 棋盤上畫出白色棋的位置和大小半徑
if self.map[row][col] == 2: # 點擊后在棋盤上畫出白色棋
pygame.draw.circle(screen, (000, 000, 000), [col * width + border_left, row * height + border_top],
15) # 當(dāng)時玩家2時棋盤上畫出黑色棋子
if (self.winner != 0):
if self.winner == 1:
text = "白子贏了,倒計時3秒后退出"
color = (255, 255, 255, 255,)
else:
text = '黑子贏了,倒計時3秒后退出'
# text = 'black won,countdown to 3 seconds更改'
color = (0, 0, 0, 0)
# font = pygame.font.Font(None,30)#使用默認字體英文字體
font = pygame.font.Font("font.ttf", 20) # 使用漢語字體提示
text_surface = font.render(text, True, color) # 字體的顏色
text_position = (60, 60) # 字體的坐標
screen.blit(text_surface, text_position)
pygame.display.update() # 刷新,變色用的
pygame.time.wait(3000) # 顯示字體等待3秒
#running = False # 程序關(guān)閉
button.clicked = False#贏了后點擊“雙人模式”繼續(xù)游戲
def check(self,row, col): # 創(chuàng)建check函數(shù)檢測五子連線
# 判斷左右方向是否五子連線
score = 1
for i in range(4):
try:
if self.map[row][col + i] == self.map[row][col + i + 1]:
score = score + 1
else:
break
except:
break
for i in range(4):
try:
if self.map[row][col - i] == self.map[row][col - i - 1]:
score = score + 1
else:
break
except:
break
if score == 5:
return True
# 判斷上下方向是否五子連線
score = 1
for i in range(4):
try:
if self.map[row + i][col] == self.map[row + i + 1][col]:
score = score + 1
else:
break
except:
break
for i in range(4):
try:
if self.map[row - i][col] == self.map[row - i - 1][col]:
score = score + 1
else:
break
except:
break
if score == 5:
return True
# 判斷左下右上方向是否五子連線(左下行和列都減小,右上行和列都變大)
score = 1
for i in range(4):
try:
if self.map[row + i][col + i] == self.map[row + i + 1][col + i + 1]:
score = score + 1
else:
break
except:
break
for i in range(4):
try:
if self.map[row - i][col - i] == self.map[row - i - 1][col - i - 1]:
score = score + 1
else:
break
except:
break
if score == 5:
return True
# 判斷左上右下方向是否五子連線(左上行增加列減小,右下行變大,列變小)
score = 1
for i in range(4):
try:
if self.map[row - i][col + i] == self.map[row - i - 1][col + i + 1]:
score = score + 1
else:
break
except:
break
for i in range(4):
try:
if self.map[row + i][col - i] == self.map[row + i + 1][col - i - 1]:
score = score + 1
else:
break
except:
break
if score == 5:
return True
def mouse_Click(self,x,y):
if x >= border_left and x <= border_right and y >= border_top and y <= border_bottom: # 限制x的左右邊
if self.started:#點擊“雙人模式”后可以玩游戲
col = round((x - 15) / 30) # 計算出x的位置
row = round((y - 15) / 30) # 計算出y的位置
if self.map[row][col] == 0:
print(row + 1, col + 1) # 打印出x與y的位置
# self.map[row][col] = 1#等于1表示把它的數(shù)值存儲起來
self.map[row][col] = self.player #
if (self.check(row, col)): # 如果檢測到五子連線了那么打印贏了
# print("有人贏了")
self.winner = self.player # 把贏賦值給winner
else:
if self.player == 1: # 當(dāng)點擊事件后
self.player = 2 # 2是黑子
else:
self.player = 1 # 下一次是白子
else:
print("當(dāng)前位置已經(jīng)被占領(lǐng)了")
else:
print("請點擊了雙人模式 后才能玩游戲了")
button = Button(30, 470, 100, 30, "雙人模式", (153, 51, 250), (221, 160, 221), (255, 255, 255)) # 畫一個雙人模式按鈕
button_ai = Button(200, 470, 100, 30, "AI模式", (153, 51, 250), (221, 160, 221), (255, 255, 255)) # 畫一個機器人模式按鈕
game = Game()
while True:#事件運行
for event in pygame.event.get():
button.handle_event(event,game)
if event.type == pygame.QUIT:#當(dāng)點擊事件后退出
running = False #事件關(guān)閉
pygame.quit() # 退出游戲
exit()#系統(tǒng)級別的退出
elif event.type == pygame.MOUSEBUTTONDOWN:#鼠標相應(yīng)點擊事件
x,y = pygame.mouse.get_pos()#獲取x,y的位置
game.mouse_Click(x,y)
game.start()
pygame.display.update() # 刷新,變色用的
到了這里,關(guān)于五子棋游戲的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!