编写五子棋的完整python代码_五子棋游戏源码(python实现)

本文介绍了一款使用Python编写的五子棋游戏,包括游戏界面的绘制、棋盘逻辑以及AI对战功能。玩家可以与AI进行对弈,游戏支持棋盘的网格线绘制、棋子放置、胜负判断等功能。通过AI_drop方法,AI能够智能选择落子位置,增加游戏难度。
摘要由CSDN通过智能技术生成

【实例简介】

【实例截图】

290b380013f3205196052f079a0517a8.png

【核心代码】

"""五子棋之人机对战"""

import sys

import random

import pygame

from pygame.locals import *

import pygame.gfxdraw

from checkerboard import Checkerboard, BLACK_CHESSMAN, WHITE_CHESSMAN, offset, Point

SIZE = 30 # 棋盘每个点时间的间隔

Line_Points = 19 # 棋盘每行/每列点数

Outer_Width = 20 # 棋盘外宽度

Border_Width = 4 # 边框宽度

Inside_Width = 4 # 边框跟实际的棋盘之间的间隔

Border_Length = SIZE * (Line_Points - 1) Inside_Width * 2 Border_Width # 边框线的长度

Start_X = Start_Y = Outer_Width int(Border_Width / 2) Inside_Width # 网格线起点(左上角)坐标

SCREEN_HEIGHT = SIZE * (Line_Points - 1) Outer_Width * 2 Border_Width Inside_Width * 2 # 游戏屏幕的高

SCREEN_WIDTH = SCREEN_HEIGHT 200 # 游戏屏幕的宽

Stone_Radius = SIZE // 2 - 3 # 棋子半径

Stone_Radius2 = SIZE // 2 3

Checkerboard_Color = (0xE3, 0x92, 0x65) # 棋盘颜色

BLACK_COLOR = (0, 0, 0)

WHITE_COLOR = (255, 255, 255)

RED_COLOR = (200, 30, 30)

BLUE_COLOR = (30, 30, 200)

RIGHT_INFO_POS_X = SCREEN_HEIGHT Stone_Radius2 * 2 10

def print_text(screen, font, x, y, text, fcolor=(255, 255, 255)):

imgText = font.render(text, True, fcolor)

screen.blit(imgText, (x, y))

def main():

pygame.init()

screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

pygame.display.set_caption('五子棋')

font1 = pygame.font.SysFont('SimHei', 32)

font2 = pygame.font.SysFont('SimHei', 72)

fwidth, fheight = font2.size('黑方获胜')

checkerboard = Checkerboard(Line_Points)

cur_runner = BLACK_CHESSMAN

winner = None

computer = AI(Line_Points, WHITE_CHESSMAN)

black_win_count = 0

white_win_count = 0

while True:

for event in pygame.event.get():

if event.type == QUIT:

sys.exit()

elif event.type == KEYDOWN:

if event.key == K_RETURN:

if winner is not None:

winner = None

cur_runner = BLACK_CHESSMAN

checkerboard = Checkerboard(Line_Points)

computer = AI(Line_Points, WHITE_CHESSMAN)

elif event.type == MOUSEBUTTONDOWN:

if winner is None:

pressed_array = pygame

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值