制作五子棋

import pygame,sys#导入模块
pygame.init()#游戏初始化
pygame.mixer.init()
pygame.font.init()#汉字初始化
screen = pygame.display.set_mode((650,450))#创建游戏窗口
pygame.display.set_caption("五子棋")#设置游戏窗口的标题
pos=[]#记录棋盘上每一个点的坐标
x=10#记录点的x坐标
y=10#记录点的y坐标
p=[]#记录点的地图位置
n=0#判断棋子的颜色
color=[]#记录棋子的颜色
map1=[]#判断哪些项有点
i=0#判断胜负查看每一个点的循环变量
for a in range(16):#表示棋盘的行的点
    for a in range(16):#表示棋盘列的点
        pos.append((x,y))#将点的位置用元祖的方式记录到列表
        map1.append(0)#添加元素0代表点,1代表白棋,2代表黑棋
        x+=30#记录下一个点
    x=10#记录下一行的第一个
    y+=30#记录下一行
pygame.mixer.music.load("5a41_37f5_ac53_b2f6f7c0ea87d23a393503caa67a2c5f.mp3")
while True:#游戏循环
    if pygame.mixer.music.get_busy()==False:
        pygame.mixer.music.play()
    x=10#重设x坐标
    y=10#重设y坐标
    screen.fill((139,69,19))#设置为棋盘的颜色
    for event in pygame.event.get():#遍历所有按键
        if event.type == pygame.QUIT:#如果按下关闭键
            pygame.quit()#关闭窗口
            sys.exit()#关闭程序
        if event.type==pygame.MOUSEBUTTONDOWN:#判断是否点击鼠标
            for b in range(len(pos)):#遍历所有的点的位置
                #设置一个虚拟的圆,判断是否鼠标点击每个圆中
                if pos[b][0]-15<pygame.mouse.get_pos()[0] and pos[b][0]+15>pygame.mouse.get_pos()[0]:
                    if pos[b][1]-15<pygame.mouse.get_pos()[1] and pos[b][1]+15>pygame.mouse.get_pos()[1]:
                        if n==0:
                            n+=1
                            p.append(b)#添加点的项
                        if b!=p[-1]:
                            n+=1
                            p.append(b)#添加点的项
                        if n%2==0:#判断奇偶数
                            color.append((255,255,255))#将棋子颜色添加到列表
                            map1[b]=1
                        else:
                            color.append((0,0,0))
                            map1[b]=2
                        break
    for a in range(14):#表示棋盘行的点
        for a in range(15):#表示棋盘列的点
            pygame.draw.rect(screen,(0,0,0),(x,y,30,30),1)#绘制矩形
            x+=30#绘制下一个圆
        x=10#绘制下一行的第一个的圆
        y+=30#绘制下一行的圆
    pygame.draw.circle(screen,(255,0,0),(pygame.mouse.get_pos()[0]+5,pygame.mouse.get_pos()[1]+5),15,1)
    i+=1
    #绘制一个跟随鼠标的圆
    for c in range(len(p)):#判断有多少个点,将这些点都绘制出来
        pygame.draw.circle(screen,color[c],pos[p[c]],15,0)#绘制圆形
    screen.blit(pygame.font.SysFont("simhei",25).render("棋子数量:   "+str(n),0,(255,255,0)),(466,10))
    if i==250:#看循环是否结束
        i=0#重新检测
    #判断横
    if map1[i]==1 and map1[i+1]==1 and map1[i+2]==1 and map1[i+3]==1 and map1[i+4]==1:
        print("白棋赢")
        break
    if map1[i]==2 and map1[i+1]==2 and map1[i+2]==2 and map1[i+3]==2 and map1[i+4]==2:
        print("黑棋赢")
        break
    #判断竖
    if map1[i]==1 and map1[i+16]==1 and map1[i+32]==1 and map1[i+48]==1 and map1[i+64]==1:
        print("白棋赢")
        break
    if map1[i]==2 and map1[i+16]==2 and map1[i+32]==2 and map1[i+48]==2 and map1[i+64]==2:
        print("黑棋赢")
        break
    #判断反斜
    if map1[i]==1 and map1[i+16+1]==1 and map1[i+32+2]==1 and map1[i+48+3]==1 and map1[i+64+4]==1:
        print("白棋赢")
        break
    if map1[i]==2 and map1[i+16+1]==2 and map1[i+32+2]==2 and map1[i+48+3]==2 and map1[i+64+4]==2:
        print("黑棋赢")
        break
    #判断斜
    if map1[i]==1 and map1[i+16-1]==1 and map1[i+32-2]==1 and map1[i+48-3]==1 and map1[i+64-4]==1:
        print("白棋赢")
        break
    if map1[i]==2 and map1[i+16-1]==2 and map1[i+32-2]==2 and map1[i+48-3]==2 and map1[i+64-4]==2:
        print("黑棋赢")
        break
    #显示棋子数量,因为一共256个交叉点,所以空三格
    pygame.display.flip()#刷新游戏

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是使用Dev-C++制作五子棋的示例代码: ```cpp #include <iostream> #include <conio.h> #include <windows.h> using namespace std; const int BOARD_SIZE = 15; const int CELL_SIZE = 40; const int BOARD_OFFSET = 20; const int WINDOW_WIDTH = BOARD_SIZE * CELL_SIZE + BOARD_OFFSET * 2; const int WINDOW_HEIGHT = BOARD_SIZE * CELL_SIZE + BOARD_OFFSET * 2; enum class Player { NONE, BLACK, WHITE }; Player board[BOARD_SIZE][BOARD_SIZE]; Player currentPlayer; void initBoard() { for (int i = 0; i < BOARD_SIZE; i++) { for (int j = 0; j < BOARD_SIZE; j++) { board[i][j] = Player::NONE; } } } void drawBoard() { system("cls"); for (int i = 0; i < BOARD_SIZE; i++) { for (int j = 0; j < BOARD_SIZE; j++) { int x = BOARD_OFFSET + j * CELL_SIZE; int y = BOARD_OFFSET + i * CELL_SIZE; if (board[i][j] == Player::NONE) { cout << "."; } else if (board[i][j] == Player::BLACK) { cout << "X"; } else if (board[i][j] == Player::WHITE) { cout << "O"; } cout << " "; } cout << endl; } } bool isValidMove(int row, int col) { return row >= 0 && row < BOARD_SIZE && col >= 0 && col < BOARD_SIZE && board[row][col] == Player::NONE; } bool isWinningMove(int row, int col) { Player player = board[row][col]; int count = 1; // Check horizontally for (int i = col - 1; i >= 0 && board[row][i] == player; i--) { count++; } for (int i = col + 1; i < BOARD_SIZE && board[row][i] == player; i++) { count++; } if (count >= 5) { return true; } // Check vertically count = 1; for (int i = row - 1; i >= 0 && board[i][col] == player; i--) { count++; } for (int i = row + 1; i < BOARD_SIZE && board[i][col] == player; i++) { count++; } if (count >= 5) { return true; } // Check diagonally (top-left to bottom-right) count = 1; for (int i = row - 1, j = col - 1; i >= 0 && j >= 0 && board[i][j] == player; i--, j--) { count++; } for (int i = row + 1, j = col + 1; i < BOARD_SIZE && j < BOARD_SIZE && board[i][j] == player; i++, j++) { count++; } if (count >= 5) { return true; } // Check diagonally (top-right to bottom-left) count = 1; for (int i = row - 1, j = col + 1; i >= 0 && j < BOARD_SIZE && board[i][j] == player; i--, j++) { count++; } for (int i = row + 1, j = col - 1; i < BOARD_SIZE && j >= 0 && board[i][j] == player; i++, j--) { count++; } if (count >= 5) { return true; } return false; } void makeMove(int row, int col) { board[row][col] = currentPlayer; currentPlayer = (currentPlayer == Player::BLACK) ? Player::WHITE : Player::BLACK; } int main() { initBoard(); currentPlayer = Player::BLACK; while (true) { drawBoard(); cout << "Player " << ((currentPlayer == Player::BLACK) ? "X" : "O") << "'s turn. Enter row and column: "; int row, col; cin >> row >> col; if (isValidMove(row, col)) { makeMove(row, col); if (isWinningMove(row, col)) { drawBoard(); cout << "Player " << ((currentPlayer == Player::BLACK) ? "X" : "O") << " wins!" << endl; break; } } else { cout << "Invalid move. Try again." << endl; } } return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值