使用Pygame实现井字棋

时间: 2023-07-11 13:06:41 浏览: 53
好的,我理解了你的要求,我可以给你分享一下使用 Pygame 实现井字棋的代码。 ``` import pygame pygame.init() # 设置游戏窗口 WINDOW_SIZE = (300, 300) screen = pygame.display.set_mode(WINDOW_SIZE) pygame.display.set_caption("Tic Tac Toe") # 定义颜色 BLACK = (0, 0, 0) WHITE = (255, 255, 255) RED = (255, 0, 0) # 定义游戏状态 game_over = False winner = None # 创建游戏区域 board = [[None] * 3, [None] * 3, [None] * 3] # 设置字体 font = pygame.font.SysFont(None, 30) # 绘制游戏区域 def draw_board(): for row in range(3): for col in range(3): pygame.draw.rect(screen, WHITE, [(col * 100) + 10, (row * 100) + 10, 80, 80]) if board[row][col] == "X": text = font.render("X", True, BLACK) screen.blit(text, ((col * 100) + 35, (row * 100) + 35)) elif board[row][col] == "O": text = font.render("O", True, BLACK) screen.blit(text, ((col * 100) + 35, (row * 100) + 35)) # 检查游戏是否结束 def check_game_over(): global game_over, winner # 检查行 for row in range(3): if board[row][0] == board[row][1] == board[row][2] and board[row][0] is not None: winner = board[row][0] game_over = True # 检查列 for col in range(3): if board[0][col] == board[1][col] == board[2][col] and board[0][col] is not None: winner = board[0][col] game_over = True # 检查对角线 if board[0][0] == board[1][1] == board[2][2] and board[0][0] is not None: winner = board[0][0] game_over = True if board[0][2] == board[1][1] == board[2][0] and board[0][2] is not None: winner = board[0][2] game_over = True # 检查是否平局 if all([all(row) for row in board]) and winner is None: game_over = True # 主循环 while not game_over: for event in pygame.event.get(): if event.type == pygame.QUIT: game_over = True if winner is None: if event.type == pygame.MOUSEBUTTONDOWN: # 获取鼠标点击位置 xpos, ypos = pygame.mouse.get_pos() row = ypos // 100 col = xpos // 100 # 检查是否为合法位置 if board[row][col] is None: # 设置该位置为当前玩家的符号 board[row][col] = "X" # 切换到另一个玩家 if winner is None: draw_board() check_game_over() pygame.display.update() pygame.time.wait(500) if winner is None: open_spots = [(r, c) for r in range(3) for c in range(3) if board[r][c] is None] if len(open_spots) > 0: row, col = open_spots[0] board[row][col] = "O" draw_board() pygame.display.update() # 显示胜者 if winner == "X": text = font.render("You win!", True, RED) elif winner == "O": text = font.render("Computer wins!", True, RED) else: text = font.render("Tie game!", True, RED) screen.blit(text, (80, 140)) pygame.display.update() # 等待用户关闭窗口 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() ``` 希望这个代码对你有帮助!

相关推荐

最新推荐

recommend-type

python pygame实现五子棋小游戏

主要为大家详细介绍了python pygame实现五子棋小游戏,代码注释很详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

pygame游戏之旅 调用按钮实现游戏开始功能

主要为大家详细介绍了pygame游戏之旅的第12篇,教大家调用按钮实现游戏开始功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

python飞机大战pygame碰撞检测实现方法分析

主要介绍了python飞机大战pygame碰撞检测实现方法,结合实例形式分析了Python使用pygame实现飞机大战游戏中碰撞检测的原理与相关操作技巧,需要的朋友可以参考下
recommend-type

使用Python第三方库pygame写个贪吃蛇小游戏

今天看到几个关于pygame模块的博客和视频,感觉非常有趣,这里照猫画虎写了一个贪吃蛇小游戏,目前还有待完善,但是基本游戏功能已经实现,下面是代码: # 导入模块 import pygame import random # 初始化 pygame....
recommend-type

麦肯锡-年月―中国xx集团战略咨询项目建议书.ppt

麦肯锡-年月―中国xx集团战略咨询项目建议书.ppt
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。