基本信息
源码名称:XXOO游戏(井字棋)
源码大小:6.76KB
文件格式:.py
开发语言:Python
更新时间:2025-02-19
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 10 元×
微信扫码支付:10 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
简单的xxoo游戏
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游戏
import pygame
import sysimport 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()