python turtle小游戏
时间: 2023-09-20 21:00:56 浏览: 54
Python turtle 是一个绘制图形的模块,可以用来编写小游戏。
下面是一个使用 Python turtle 实现的简单井字棋小游戏的示例代码:
```python
import turtle
# 初始化游戏界面
window = turtle.Screen()
window.title("井字棋小游戏")
window.setup(600, 600)
# 绘制网格线
pen = turtle.Turtle()
pen.speed(0)
pen.hideturtle()
pen.penup()
pen.goto(-300, 100)
pen.pendown()
pen.color("black")
pen.pensize(3)
for _ in range(2):
pen.forward(600)
pen.right(90)
pen.forward(200)
pen.right(90)
pen.penup()
pen.goto(-300, 0)
pen.pendown()
pen.forward(600)
pen.penup()
pen.goto(-300, -100)
pen.pendown()
pen.forward(600)
pen.penup()
pen.goto(-100, 200)
pen.right(90)
pen.pendown()
pen.forward(400)
pen.penup()
pen.goto(100, 200)
pen.pendown()
pen.forward(400)
# 定义玩家类
class Player:
def __init__(self, symbol):
self.symbol = symbol
# 定义游戏状态
class Game:
def __init__(self):
self.board = [[' ']*3 for _ in range(3)]
self.current_player = Player('X')
def draw(self, row, col):
if self.board[row][col] == ' ':
self.board[row][col] = self.current_player.symbol
self.draw_symbol(row, col)
if self.check_winner(row, col):
pen.penup()
pen.goto(-100, -250)
pen.write(f"玩家 {self.current_player.symbol} 获胜!", align="center", font=("Courier", 24, "normal"))
return True
self.switch_player()
return False
def draw_symbol(self, row, col):
pen.penup()
pen.goto(col * 200 - 200, -row * 200 + 100)
pen.pendown()
pen.write(self.current_player.symbol, align="center", font=("Courier", 96, "normal"))
def switch_player(self):
if self.current_player.symbol == 'X':
self.current_player.symbol = 'O'
else:
self.current_player.symbol = 'X'
def check_winner(self, row, col):
symbol = self.current_player.symbol
# 检查同行
if self.board[row][0] == symbol and self.board[row][1] == symbol and self.board[row][2] == symbol:
return True
# 检查同列
if self.board[0][col] == symbol and self.board[1][col] == symbol and self.board[2][col] == symbol:
return True
# 检查对角线
if row == col and self.board[0][0] == symbol and self.board[1][1] == symbol and self.board[2][2] == symbol:
return True
if row + col == 2 and self.board[0][2] == symbol and self.board[1][1] == symbol and self.board[2][0] == symbol:
return True
return False
# 创建游戏对象
game = Game()
# 注册点击事件处理函数
def click_handler(x, y):
if -300 <= x <= 300 and -300 <= y <= 300:
row = -int(y // 200 - 1)
col = int(x // 200)
game.draw(row, col)
# 绑定点击事件
turtle.onscreenclick(click_handler)
turtle.listen()
# 启动游戏主循环
turtle.mainloop()
```
这个小游戏使用 turtle 绘制了一个井字棋的游戏界面,并实现了玩家的交替下棋和判断胜负的功能。玩家可以通过点击界面上的格子来下棋,并显示结果。
希望以上示例代码能帮助您理解 Python turtle 的基本用法以及如何编写一个简单的小游戏。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)