用pygame做一个五子棋小游戏

本文介绍如何利用pygame和numpy库开发一个五子棋小游戏,包括安装库、初始化、判断胜利条件、落子判断、查找可落子位置以及主循环的实现。
摘要由CSDN通过智能技术生成

一、安装第三方库

在开始做五子棋之前,我们得先下载第三方库。这个项目要用到pygame和numpy这两个第三方库。
已经安装好这两个库的同学,可以跳过这一段。
打开cmd,输入:

pip install pygame -i “https://pypi.doubanio.com/simple”
pip install numpy -i “https://pypi.doubanio.com/simple”

等待一小会儿,就安装好了。

二、库和常量

创建文件FIR.py,先导入所有库,然后初始化pygame,然后创建窗口,再设置一些变量。代码如下:

import pygame
import sys
import numpy as np
from pygame.locals import QUIT, KEYDOWN

pygame.init()
screen = pygame.display.set_mode([670, 670])
bg = "Tan3"  # 背景颜色
black = [0, 0, 0]  # 黑色
white = [255, 255, 255]  # 白色
over_pos = []  # 保存下过的棋
flag = False  # 标记延时
tim = 0  # 鼠标左键延时

三、判断胜利

创建函数check_win(over_pos),用于判断胜利。这个函数里面,首先需要创建一个“地图”,然后扫描一遍棋盘,如果检测到五子连心就返回颜色、坐标。代码如下:

def check_win(over_pos):
    mp = np.zeros([15, 15], dtype=int)  # 创建一个15*15的所有元素为零的嵌套列表
    for val in over_pos:  # 遍历列表
        x = int((val[0][0] - 27) / 44)  # 获取到x坐标
        y = int((val[0][1] - 27) / 44)  # 获取到y坐标
        mp[y][x] = 1 if val[1] == black else 2  # 按照x、y坐标改变数值
    for i in range(15):  # i是y坐标
        pos1 = []
        pos2 = []
        for j in range(15):  # j是x坐标
            if mp[i][j] == 1:
                pos1.append([i, j])
            else:
                pos1 = []
            if mp[i][j] == 2:
                pos2.append([i, j])
            else:
                pos2 = []
            if len(pos1) == 5:  # 长度为5,五子连心
                return 1, pos1
            if len(pos2) == 5:
                return 2, pos2
    for j in range(15):
        pos1 = []
        pos2 = []
        for i in range(15):
            if mp[i][j] == 1:
                pos1.append([i, j])
            else:
                pos1 = []
            if mp[i][j] == 2:
                pos2.append([i, j])
            else:
                pos2 = []
            if len(pos1) == 5:
                return 1, pos1
            if len(pos2) == 5:
                return 2, pos2
    for i in range(15):
        for j in range(15):
            pos1 = []
            pos2 = []
            for k in range(15):
                if i + k >= 15 or j + k >= 15:
                    break
                if mp[i + k][j + k] == 1:
                    """
                    假设棋盘上:
                    1 2 0 0 0 0 0 0 0 0 0 0 0 0 0
                    0 1 2 0 0 0 0 0 0 0 0 0 0 0 0
                    0 0 1 2 0 0 0 0 0 0 0 0 0 0 0
                    0 0 0 1 2 0 0 0 0 0 0 0 0 0 0
                    0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
                    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
                    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
                    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
                    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
                    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
                    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
                    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
                    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
                    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
                    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
   					第一次循环,i = 0, j = 0, k = 0, break语句不会被执行。
   					i + k = 0, j + k = 0, mp[0][0] = 1, pos1会加上
   					[i 
好的,下面是一个简单的五子棋游戏的代码,使用 Pygame 库进行绘制和事件处理。 ```python import pygame from pygame.locals import * # 初始化 Pygame pygame.init() # 设置窗口大小 SCREEN_SIZE = (600, 600) screen = pygame.display.set_mode(SCREEN_SIZE) # 设置窗口标题 pygame.display.set_caption('五子棋') # 定义棋盘格子大小和棋子半径 GRID_SIZE = 40 RADIUS = GRID_SIZE // 2 - 2 # 定义棋盘大小和边距 BOARD_SIZE = 15 MARGIN = 40 # 定义棋子颜色 BLACK = (0, 0, 0) WHITE = (255, 255, 255) # 定义棋盘二维数组,表示棋子的位置 board = [[0 for x in range(BOARD_SIZE)] for y in range(BOARD_SIZE)] # 定义当前下棋方,1 表示黑方,-1 表示白方 current_player = 1 def draw_board(): # 绘制棋盘背景 screen.fill((200, 200, 200)) # 绘制棋盘网格 for i in range(BOARD_SIZE): x = MARGIN + i * GRID_SIZE pygame.draw.line(screen, BLACK, (x, MARGIN), (x, SCREEN_SIZE[1] - MARGIN)) pygame.draw.line(screen, BLACK, (MARGIN, x), (SCREEN_SIZE[0] - MARGIN, x)) # 绘制棋子 for i in range(BOARD_SIZE): for j in range(BOARD_SIZE): if board[i][j] == 1: pygame.draw.circle(screen, BLACK, (MARGIN + i * GRID_SIZE, MARGIN + j * GRID_SIZE), RADIUS) elif board[i][j] == -1: pygame.draw.circle(screen, WHITE, (MARGIN + i * GRID_SIZE, MARGIN + j * GRID_SIZE), RADIUS) def get_pos(x, y): # 将鼠标点击位置转换成棋盘坐标 i = (x - MARGIN) // GRID_SIZE j = (y - MARGIN) // GRID_SIZE return i, j def check_win(i, j): # 检查是否有五子连成一线,如果有则返回 True,否则返回 False directions = [(1, 0), (0, 1), (1, 1), (1, -1)] for dx, dy in directions: count = 1 x, y = i, j while count < 5: x, y = x + dx, y + dy if x < 0 or x >= BOARD_SIZE or y < 0 or y >= BOARD_SIZE or board[x][y] != current_player: break count += 1 x, y = i, j while count < 5: x, y = x - dx, y - dy if x < 0 or x >= BOARD_SIZE or y < 0 or y >= BOARD_SIZE or board[x][y] != current_player: break count += 1 if count >= 5: return True return False # 游戏循环 running = True while running: # 处理事件 for event in pygame.event.get(): if event.type == QUIT: running = False elif event.type == MOUSEBUTTONDOWN: # 处理鼠标点击事件 if current_player == 1: # 黑方下棋 i, j = get_pos(event.pos[0], event.pos[1]) if board[i][j] == 0: board[i][j] = current_player if check_win(i, j): print('黑方胜利') running = False current_player = -current_player else: # 白方下棋 i, j = get_pos(event.pos[0], event.pos[1]) if board[i][j] == 0: board[i][j] = current_player if check_win(i, j): print('白方胜利') running = False current_player = -current_player # 绘制界面 draw_board() pygame.display.update() # 退出 Pygame pygame.quit() ``` 这个代码运行后会显示一个窗口,你可以用鼠标点击棋盘上的空白格子来下棋。黑方先下,如果有五子连成一线则判定胜利。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值