在好例子网,分享、交流、成长!
您当前所在位置:首页Python 开发实例Python语言基础 → XXOO游戏(井字棋)

XXOO游戏(井字棋)

Python语言基础

下载此实例
  • 开发语言:Python
  • 实例大小:6.76KB
  • 下载次数:2
  • 浏览次数:59
  • 发布时间:2025-02-19
  • 实例类别:Python语言基础
  • 发 布 人:情不自禁到的
  • 文件格式:.py
  • 所需积分:10
 相关标签: 井字棋游戏 井字棋 游戏

实例介绍

【实例简介】简单的xxoo游戏

【实例截图】

from clipboard

【核心代码】

import pygame

import sys
import random

# 初始化 Pygame
pygame.init()

# 常量定义
WIDTH, HEIGHT = 300, 400  # 增加高度以容纳按钮
LINE_WIDTH = 5
BOARD_ROWS, BOARD_COLS = 3, 3
SQUARE_SIZE = WIDTH // BOARD_COLS
CIRCLE_RADIUS = SQUARE_SIZE // 3
CIRCLE_WIDTH = 5
CROSS_WIDTH = 5
SPACE = SQUARE_SIZE // 4

# 颜色定义
BG_COLOR = (28, 170, 156)
LINE_COLOR = (23, 145, 135)
CIRCLE_COLOR = (239, 231, 200)
CROSS_COLOR = (66, 66, 66)
BUTTON_COLOR = (52, 152, 219)
TEXT_COLOR = (255, 255, 255)

# 初始化屏幕
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("井字棋")
screen.fill(BG_COLOR)

# 初始化字体

font_path = "C:/Windows/Fonts/simhei.ttf"
font = pygame.font.Font(font_path, 16)  # 使用默认字体

# 初始化棋盘
board = [[None for _ in range(BOARD_COLS)] for _ in range(BOARD_ROWS)]

# 画棋盘线
def draw_lines():
    # 水平线
    pygame.draw.line(screen, LINE_COLOR, (0, SQUARE_SIZE), (WIDTH, SQUARE_SIZE), LINE_WIDTH)
    pygame.draw.line(screen, LINE_COLOR, (0, 2 * SQUARE_SIZE), (WIDTH, 2 * SQUARE_SIZE), LINE_WIDTH)
    # 垂直线
    pygame.draw.line(screen, LINE_COLOR, (SQUARE_SIZE, 0), (SQUARE_SIZE, HEIGHT - 100), LINE_WIDTH)
    pygame.draw.line(screen, LINE_COLOR, (2 * SQUARE_SIZE, 0), (2 * SQUARE_SIZE, HEIGHT - 100), LINE_WIDTH)

# 画棋子
def draw_figures():
    for row in range(BOARD_ROWS):
        for col in range(BOARD_COLS):
            if board[row][col] == "O":
                pygame.draw.circle(screen, CIRCLE_COLOR, 
                                 (int(col * SQUARE_SIZE SQUARE_SIZE // 2), 
                                  int(row * SQUARE_SIZE SQUARE_SIZE // 2)), 
                                 CIRCLE_RADIUS, CIRCLE_WIDTH)
            elif board[row][col] == "X":
                pygame.draw.line(screen, CROSS_COLOR, 
                                 (col * SQUARE_SIZE SPACE, row * SQUARE_SIZE SQUARE_SIZE - SPACE),
                                 (col * SQUARE_SIZE SQUARE_SIZE - SPACE, row * SQUARE_SIZE SPACE), CROSS_WIDTH)
                pygame.draw.line(screen, CROSS_COLOR, 
                                 (col * SQUARE_SIZE SPACE, row * SQUARE_SIZE SPACE),
                                 (col * SQUARE_SIZE SQUARE_SIZE - SPACE, row * SQUARE_SIZE SQUARE_SIZE - SPACE), CROSS_WIDTH)

# 检查是否有玩家获胜
def check_winner(player):
    # 检查行
    for row in range(BOARD_ROWS):
        if all(board[row][col] == player for col in range(BOARD_COLS)):
            return True
    # 检查列
    for col in range(BOARD_COLS):
        if all(board[row][col] == player for row in range(BOARD_ROWS)):
            return True
    # 检查对角线
    if all(board[i][i] == player for i in range(BOARD_ROWS)):
        return True
    if all(board[i][BOARD_COLS - i - 1] == player for i in range(BOARD_ROWS)):
        return True
    return False

# 检查是否平局
def is_board_full():
    return all(board[row][col] is not None for row in range(BOARD_ROWS) for col in range(BOARD_COLS))

# 重置棋盘
def reset_board():
    for row in range(BOARD_ROWS):
        for col in range(BOARD_COLS):
            board[row][col] = None

# 简单AI逻辑(随机选择空位)
def ai_move():
    empty_positions = [(row, col) for row in range(BOARD_ROWS) for col in range(BOARD_COLS) if board[row][col] is None]
    if empty_positions:
        row, col = random.choice(empty_positions)
        board[row][col] = "O"

# 画按钮
def draw_button(text, x, y, width, height):
    pygame.draw.rect(screen, BUTTON_COLOR, (x, y, width, height))
    text_surface = font.render(text, True, TEXT_COLOR)
    text_rect = text_surface.get_rect(center=(x width // 2, y height // 2))
    screen.blit(text_surface, text_rect)

# 显示获胜提示
def show_winner_message(winner):
    if winner == "平局":
        message = "平局!"
    else:
        message = f"玩家 {winner} 获胜!"
    text_surface = font.render(message, True, TEXT_COLOR)
    text_rect = text_surface.get_rect(center=(WIDTH // 2, HEIGHT - 50))
    screen.blit(text_surface, text_rect)

# 主游戏循环
def main():
    current_player = "X"
    game_over = False
    ai_mode = False
    winner = None

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

            if event.type == pygame.MOUSEBUTTONDOWN:
                mouseX, mouseY = event.pos

                # 检查是否点击了按钮
                if HEIGHT - 90 <= mouseY <= HEIGHT - 40:
                    if 10 <= mouseX <= 140:  # 新游戏按钮
                        reset_board()
                        game_over = False
                        winner = None
                        current_player = "X"
                    elif 160 <= mouseX <= 290:  # AI对战按钮
                        ai_mode = not ai_mode
                        reset_board()
                        game_over = False
                        winner = None
                        current_player = "X"

                # 检查是否点击了棋盘
                if not game_over and mouseY < HEIGHT - 100:
                    clicked_row = mouseY // SQUARE_SIZE
                    clicked_col = mouseX // SQUARE_SIZE

                    if board[clicked_row][clicked_col] is None:
                        board[clicked_row][clicked_col] = current_player

                        if check_winner(current_player):
                            winner = current_player
                            game_over = True
                        elif is_board_full():
                            winner = "平局"
                            game_over = True

                        current_player = "O" if current_player == "X" else "X"

                        # AI回合
                        if ai_mode and not game_over and current_player == "O":
                            ai_move()
                            if check_winner("O"):
                                winner = "O"
                                game_over = True
                            elif is_board_full():
                                winner = "平局"
                                game_over = True
                            current_player = "X"

        # 绘制界面
        screen.fill(BG_COLOR)
        draw_lines()
        draw_figures()

        # 绘制按钮
        draw_button("新游戏", 10, HEIGHT - 90, 130, 50)
        draw_button("AI对战" if not ai_mode else "双人对战", 160, HEIGHT - 90, 130, 50)

        # 显示获胜提示
        if game_over:
            show_winner_message(winner)

        pygame.display.update()

if __name__ == "__main__":
    main()


实例下载地址

XXOO游戏(井字棋)

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警