五子棋游戏

发布于:2023-12-03 ⋅ 阅读:(69) ⋅ 点赞:(0)
import pygame #导入pygame模块
pygame.init()#初始化
screen = pygame.display.set_mode((750,750))#设置游戏屏幕大小
running = True#建立一个事件
while running:#事件运行
    for event in pygame.event.get():
        if event.type == pygame.QUIT:#当点击事件后退出
            running = False  #事件关闭
    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条线,画出中央的小点

二,创建列表,计算出x和y的当前坐标的位置,且存储二维列表的值,画出圆形白色棋子

import pygame #导入pygame模块
pygame.init()#初始化
screen = pygame.display.set_mode((750,750))#设置游戏屏幕大小
map = [0]*15#创建一个列表
for i in range(15):#列表循环来存储数据
    map[i] = [0]*15#二维列表来存储数据
running = True#建立一个事件
while running:#事件运行
    for event in pygame.event.get():
        if event.type == pygame.QUIT:#当点击事件后退出
            running = False  #事件关闭
        elif event.type == pygame.MOUSEBUTTONDOWN:#鼠标相应点击事件
            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表示把它的数值存储起来
    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()#退出游戏

三.黑白棋子交替出现

import pygame #导入pygame模块
pygame.init()#初始化
screen = pygame.display.set_mode((750,750))#设置游戏屏幕大小
map = [0]*15#创建一个列表
for i in range(15):#列表循环来存储数据
    map[i] = [0]*15#二维列表来存储数据
player = 1#创建玩家1
running = True#建立一个事件
while running:#事件运行
    for event in pygame.event.get():
        if event.type == pygame.QUIT:#当点击事件后退出
            running = False  #事件关闭
        elif event.type == pygame.MOUSEBUTTONDOWN:#鼠标相应点击事件
            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表示把它的数值存储起来
            map[row][col] = player#
            if player == 1:#当点击事件后
                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)  #当时玩家2时棋盘上画出黑色棋子
    pygame.display.update()#刷新,变色用的
pygame.quit()#退出游戏

四,提示已经被其他棋子占领了,不能重复占领

五。创建检测五子相连的函数  def  check

六.,实现水平向左和向右方向的五子连线的判断胜负

import pygame #导入pygame模块
pygame.init()#初始化
screen = pygame.display.set_mode((750,750))#设置游戏屏幕大小
map = [0]*15#创建一个列表
for i in range(15):#列表循环来存储数据
    map[i] = [0]*15#二维列表来存储数据
player = 1#创建玩家1
running = True
def check(row,col):#创建check函数检测五子连线
    #判断左右方向是否五子连线
    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:#当点击事件后退出
            running = False  #事件关闭
        elif event.type == pygame.MOUSEBUTTONDOWN:#鼠标相应点击事件
            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表示把它的数值存储起来
                map[row][col] = player#
                if(check(row,col)):#如果检测到五子连线了那么打印赢了
                    print("有人赢了")
                else:
                    if player == 1:#当点击事件后
                        player = 2 #2是黑子
                    else:
                        player = 1#下一次是白子
            else:
                print("当前位置已经被占领了")
    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)  #当时玩家2时棋盘上画出黑色棋子
    pygame.display.update()#刷新,变色用的
pygame.quit()#退出游戏

检查左右方向的水平方向五子棋连接的五个

检测上下方向五子相连

检测左下 右上方向五子相连

检测左上右下的五子相连

import pygame #导入pygame模块
pygame.init()#初始化
screen = pygame.display.set_mode((750,750))#设置游戏屏幕大小
map = [0]*15#创建一个列表
for i in range(15):#列表循环来存储数据
    map[i] = [0]*15#二维列表来存储数据
player = 1#创建玩家1

running = True
def check(row,col):#创建check函数检测五子连线
    #判断左右方向是否五子连线
    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:#当点击事件后退出
            running = False  #事件关闭
        elif event.type == pygame.MOUSEBUTTONDOWN:#鼠标相应点击事件
            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表示把它的数值存储起来
                map[row][col] = player#
                if(check(row,col)):#如果检测到五子连线了那么打印赢了
                    print("有人赢了")
                else:
                    if player == 1:#当点击事件后
                        player = 2 #2是黑子
                    else:
                        player = 1#下一次是白子
            else:
                print("当前位置已经被占领了")
    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)  #当时玩家2时棋盘上画出黑色棋子
    pygame.display.update()#刷新,变色用的
pygame.quit()#退出游戏

添加变量winner赋值给player,提示谁赢了,且提示3秒退出游戏

bug问题1,不是连续的五个放在一起棋子,不会判断赢输,

修改条件score 》=5

2.优化把所有的数值设置为变量,

pygame.display.set_caption("这是五子棋游戏")
border_left =25
border_right =725
border_top = 25
border_bottom = 725
width = 50
height = 50

3.ttf字体下载网站;https://www.hanyi.com.cn/

下载下来和五子棋放在同一个目录下面

4.把棋盘下面的按钮功能用class button来实现

class Button:#定义按钮的类
    def __init__(self,x,y,width,height,text,color,click_color,text_color):#初始化
        self.text = text#定义内容
        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)#双人对战按钮的颜色
        text_rect = text_surface.get_rect(center = self.rect.center)#双人对战按钮的位置坐标
        screen.blit(text_surface,text_rect)##双人对战按钮的颜色
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 #导入pygame模块
pygame.init()#初始化
screen = pygame.display.set_mode((490,590))#设置游戏屏幕大小
#screen = pygame.display.set_mode((750,850))#设置游戏屏幕大小
pygame.display.set_caption("这是五子棋游戏")
border_left =25
border_right =445
border_top = 25
border_bottom = 445
width = 30
height = 30


map = [0]*15#创建一个列表
for i in range(15):#列表循环来存储数据
    map[i] = [0]*15#二维列表来存储数据
player = 1#创建玩家1
winner = 0#创建一个变量,看看是谁赢了
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#定义内容
        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)#双人对战按钮的颜色
        text_rect = text_surface.get_rect(center = self.rect.center)#双人对战按钮的位置坐标
        screen.blit(text_surface,text_rect)##双人对战按钮的颜色
def check(row,col):#创建check函数检测五子连线

    #判断左右方向是否五子连线
    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:#当点击事件后退出
            running = False  #事件关闭
        elif event.type == pygame.MOUSEBUTTONDOWN:#鼠标相应点击事件
            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表示把它的数值存储起来
                    map[row][col] = player#
                    if(check(row,col)):#如果检测到五子连线了那么打印赢了
                        #print("有人赢了")
                        winner = player#把赢赋值给winner
                    else:
                        if player == 1:#当点击事件后
                            player = 2 #2是黑子
                        else:
                            player = 1#下一次是白子
                else:
                    print("当前位置已经被占领了")
    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("双人对战",True,(200,200,200))#双人对战按钮的颜色
    #text_position = (50,750)#双人对战按钮的位置坐标
    #screen.blit(text_surface,text_position)##双人对战按钮的颜色
    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)  #当时玩家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#程序关闭
pygame.quit()#退出游戏

本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到