pythonr入门学习记录之井字棋小游戏

Python初学者的井字棋小游戏实现
这篇博客记录了作者作为Python初学者,从Java转到Python后尝试编写井字棋小游戏的过程。作者提到虽然Python的基础语法较易掌握,但避免用Java习惯思考仍需努力。博客中分享了游戏的代码实现,并表示期待通过优化减少代码行数。

引言:

刚学python好几天了,从java到python,基础学起来确实比较容易,语法掌握,基本概念上都比较容易入脑,

唯一比较郁闷的是老想着用java的语法去学python代码,这点还需要后面慢慢掌握吧,相信学多种语言的你们也有这种经历吧。

start:开始上代码了,希望有更好的逻辑思维来写,自己也是用最笨拙的思路去写的,如果有可以优化的代码请各位大神指教

#!/user/bin/python
# -*- coding: utf-8 -*-
import os
import sys
#棋盘模块
def model(dictionary,serial=False):
     if serial:
         print('-(初版)井字棋游戏,输入棋号进行对战,')
         print('对应棋号为第一行:a1-a2-a3',end=',')
         print('对应棋号为第二行:b1-b2-b3',end=',')
         print('对应棋号为第三行:c1-c2-c3')
     print(dictionary['a1'] + ' | '+ dictionary['a2'] +' | '+ dictionary['a3'] +' | ')
     print('- +-  +-  +-')
     print(dictionary['b1'] + ' | ' + dictionary['b2'] + ' | ' + dictionary['b3'] + ' | ')
     print('- +-  +-  +-')
     print(dictionary['c1'] + ' | ' + dictionary['c2'] + ' | ' + dictionary['c3'] + ' | ')
#主模块
def main():
    dict
### 使用 `pgzrun` 实现井字棋游戏 #### 游戏基本规则 井字棋(Tic-Tac-Toe)是一个由两名玩家轮流在一个3×3网格上放置自己的标记的游戏。第一个成功将自己的三个标记连成一条线(横、竖或斜)的玩家获胜[^1]。 #### 安装 Pygame Zero (pgzrun) 为了使用 `pgzrun` 创建井字棋游戏,首先需要安装 Pygame Zero 库。可以通过 pip 来完成这一操作: ```bash pip install pygame-zero ``` #### 初始化项目结构 创建一个新的 Python 文件来编写代码,并确保文件扩展名为 `.py` 或者直接通过命令行运行脚本。 #### 编写基础框架 下面是一段简单的代码片段用于初始化窗口并设置一些全局变量: ```python import pgzrun TITLE = "Tic Tac Toe" WIDTH = 300 HEIGHT = 300 CELL_SIZE = WIDTH // 3 board = [[None]*3 for _ in range(3)] current_player = 'X' def draw(): screen.fill((255, 255, 255)) # 绘制线条 for i in range(1, 3): screen.draw.line((i * CELL_SIZE, 0), (i * CELL_SIZE, HEIGHT), color='black') screen.draw.line((0, i * CELL_SIZE), (WIDTH, i * CELL_SIZE), color='black') pgzrun.go() ``` 这段代码设置了屏幕尺寸以及定义了一个二维列表表示盘状态。还指定了当前玩家为'X'。 #### 添加交互逻辑 为了让用户能够点击格子来进行游戏操作,需增加鼠标事件处理函数,在其中判断位置并将相应的位置填充为当前玩家符: ```python def on_mouse_down(pos): global current_player col = pos[0] // CELL_SIZE row = pos[1] // CELL_SIZE if board[row][col] is None: board[row][col] = current_player # 切换玩家 current_player = 'O' if current_player == 'X' else 'X' check_winner() def update(): pass # 可在此处加入更多实时更新逻辑 def check_winner(): lines = [ [(0, 0), (0, 1), (0, 2)], # 行 [(1, 0), (1, 1), (1, 2)], [(2, 0), (2, 1), (2, 2)], [(0, 0), (1, 0), (2, 0)], # 列 [(0, 1), (1, 1), (2, 1)], [(0, 2), (1, 2), (2, 2)], [(0, 0), (1, 1), (2, 2)], # 对角线 [(0, 2), (1, 1), (2, 0)] ] winner = None for line in lines: symbols = set([board[y][x] for y,x in line]) if len(symbols) == 1 and not None in symbols: winner = list(symbols)[0] if winner: print(f"Player {winner} wins!") reset_game() def reset_game(): global board, current_player board = [[None]*3 for _ in range(3)] current_player = 'X' ``` 此部分实现了当有玩家赢得比赛时打印胜利消息的功能,并重置游戏以便重新开始新一局。 #### 显示子图像 最后一步是在绘制界面的时候显示已经下过的子: ```python def draw(): screen.fill((255, 255, 255)) for r in range(3): for c in range(3): symbol = board[r][c] rect = Rect(c*CELL_SIZE, r*CELL_SIZE, CELL_SIZE, CELL_SIZE) if symbol == 'X': screen.draw.text('X', center=rect.center, fontsize=80, color="blue") elif symbol == 'O': screen.draw.text('O', center=rect.center, fontsize=80, color="red") # Draw grid lines... for i in range(1, 3): screen.draw.line((i * CELL_SIZE, 0), (i * CELL_SIZE, HEIGHT), color='black') screen.draw.line((0, i * CELL_SIZE), (WIDTH, i * CELL_SIZE), color='black') ``` 这样就完成了整个基于 `pgzrun` 的井字棋小游戏开发过程。
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值