【WPS 每天Ai一下】打卡第三天
WPS 的强大 省心省力全靠它✅✅✅
今天是用wps ai 写有效小代码的一天💫💫
快速且标准就完成了五子棋的小游戏😁😁
感兴趣的小伙伴可以试玩一下:
以下是一个简单的Python 代码,实现了五子棋小游戏的基本功能:
```python
import turtle
# 定义棋盘大小和格子大小
BOARD_WIDTH = 15
BOARD_HEIGHT = 15
BLOCK_SIZE = 30
# 定义颜色
BLACK = "#000000"
WHITE = "#ffffff"
RED = "#ff0000"
# 定义五子棋的规则
def check_win(color, x, y):
"""
判断当前位置是否为胜利
"""
# 判断横向胜利
for i in range(-2, 3):
if x + i < 0 or x + i >= BOARD_WIDTH:
continue
if y + i < 0 or y + i >= BOARD_HEIGHT:
continue
if turtle.board[x + i][y + i] == color:
return True
# 判断纵向胜利
for i in range(-2, 3):
if i + y < 0 or i + y >= BOARD_HEIGHT:
continue
if i + x < 0 or i + x >= BOARD_WIDTH:
continue
if turtle.board[x + i][y + i] == color:
return True
# 判断对角线胜利
for i in range(-2, 3):
if i + y < 0 or i + y >= BOARD_HEIGHT:
continue
if i + x < 0 or i + x >= BOARD_WIDTH:
continue
if turtle.board[x + i][y + i] == color:
return True
for i in range(-2, 3):
if i + y < 0 or i + y >= BOARD_HEIGHT:
continue
if i + x < 0 or i + x >= BOARD_WIDTH:
continue
if turtle.board[x + i][y - i] == color:
return True
return False
# 初始化棋盘和画布
turtle.board = [[None for _ in range(BOARD_WIDTH)] for _ in range(BOARD_HEIGHT)]
turtle.canvas = turtle.Screen()
turtle.canvas.setup(BOARD_WIDTH * BLOCK_SIZE, BOARD_HEIGHT * BLOCK_SIZE)
turtle.delay = 0
turtle.penup()
turtle.hideturtle()
turtle.speed(10)
turtle.pensize(2)
turtle.color(RED)
turtle.penup()
turtle.goto(-BOARD_WIDTH // 2, BOARD_HEIGHT // 2)
turtle.pendown()
for i in range(BOARD_WIDTH):
turtle.penup()
turtle.goto(-BOARD_WIDTH // 2, i - BOARD_WIDTH // 2)
turtle.pendown()
for j in range(BOARD_HEIGHT):
turtle.penup()
turtle.goto(j - BOARD_HEIGHT // 2, BOARD_HEIGHT // 2)
turtle.pendown()
turtle.dot(BLOCK_SIZE)
turtle.penup()
turtle.penup()
turtle.goto(-BOARD_WIDTH // 2, BOARD_HEIGHT - BOARD_WIDTH // 2)
turtle.pendown()
for j in range(BOARD_HEIGHT):
turtle.penup()
turtle.goto(j - BOARD_HEIGHT // 2, -BOARD_HEIGHT // 2)
turtle.pendown()
for i in range(BOARD_WIDTH):
turtle.penup()
turtle.goto(i - BOARD_WIDTH // 2, BOARD_WIDTH // 2)
turtle.pendown()
turtle.dot(BLOCK_SIZE)
turtle.penup()
turtle.penup()
turtle.goto(-BOARD_WIDTH + BOARD_WIDTH // 2, BOARD_HEIGHT - BOARD_WIDTH // 2)
turtle.pendown()
```