python 下面八个块显示鼠标追踪位置turtle.canvas 重力四子棋 turtle 代码
时间: 2023-10-08 14:11:54 浏览: 148
1. 显示鼠标追踪位置:
```python
import turtle
def track_mouse(x, y):
turtle.goto(x, y)
turtle.onscreenclick(track_mouse)
turtle.mainloop()
```
2. turtle.canvas:
```python
import turtle
turtle.getcanvas().postscript(file="canvas.eps")
turtle.mainloop()
```
3. 重力:
```python
import turtle
GRAVITY = 0.1
class Ball(turtle.Turtle):
def __init__(self, x, y, dx, dy, radius, color):
super().__init__(shape="circle")
self.penup()
self.setposition(x, y)
self.dx = dx
self.dy = dy
self.radius = radius
self.color(color)
def move(self):
self.dy -= GRAVITY
self.sety(self.ycor() + self.dy)
self.setx(self.xcor() + self.dx)
class Game:
def __init__(self):
self.window = turtle.Screen()
self.window.setup(800, 600)
self.window.bgcolor("white")
self.balls = []
def add_ball(self, x, y, dx, dy, radius, color):
ball = Ball(x, y, dx, dy, radius, color)
self.balls.append(ball)
def start(self):
while True:
for ball in self.balls:
ball.move()
if ball.ycor() - ball.radius < -300:
ball.dy = -ball.dy
if ball.xcor() + ball.radius > 400 or ball.xcor() - ball.radius < -400:
ball.dx = -ball.dx
self.window.update()
game = Game()
game.add_ball(0, 200, 0, 0, 30, "red")
game.add_ball(0, 0, 5, 0, 20, "blue")
game.add_ball(0, -200, -5, 0, 10, "green")
game.start()
```
4. 四子棋:
```python
import turtle
class Board:
def __init__(self):
self.board = [[None for _ in range(7)] for _ in range(6)]
def is_full(self):
for row in self.board:
for cell in row:
if cell is None:
return False
return True
def is_winner(self, player):
# Check rows
for row in self.board:
count = 0
for cell in row:
if cell == player:
count += 1
if count == 4:
return True
else:
count = 0
# Check columns
for j in range(7):
count = 0
for i in range(6):
if self.board[i][j] == player:
count += 1
if count == 4:
return True
else:
count = 0
# Check diagonals
for i in range(3):
for j in range(4):
if (self.board[i][j] == player and self.board[i+1][j+1] == player and
self.board[i+2][j+2] == player and self.board[i+3][j+3] == player):
return True
for i in range(3):
for j in range(3, 7):
if (self.board[i][j] == player and self.board[i+1][j-1] == player and
self.board[i+2][j-2] == player and self.board[i+3][j-3] == player):
return True
return False
def play(self, player, column):
for i in range(5, -1, -1):
if self.board[i][column] is None:
self.board[i][column] = player
return True
return False
class Game:
def __init__(self):
self.window = turtle.Screen()
self.window.setup(700, 600)
self.window.bgcolor("white")
self.board = Board()
self.turn = "red"
self.draw_board()
self.window.onclick(self.play)
def draw_board(self):
pen = turtle.Turtle()
pen.hideturtle()
pen.speed(0)
pen.penup()
pen.setposition(-300, -250)
pen.pendown()
pen.pensize(3)
for _ in range(2):
pen.forward(600)
pen.left(90)
pen.forward(500)
pen.left(90)
pen.penup()
pen.setposition(-280, -225)
for column in range(7):
pen.write(column, align="center", font=("Arial", 16, "normal"))
pen.forward(80)
pen.hideturtle()
def play(self, x, y):
column = int((x + 300) // 80)
if self.board.play(self.turn, column):
self.draw_piece(column)
if self.board.is_winner(self.turn):
self.window.onclick(None)
turtle.color(self.turn)
turtle.write(f"{self.turn.upper()} wins!", align="center", font=("Arial", 32, "bold"))
elif self.board.is_full():
self.window.onclick(None)
turtle.write("Draw!", align="center", font=("Arial", 32, "bold"))
else:
self.turn = "yellow" if self.turn == "red" else "red"
def draw_piece(self, column):
pen = turtle.Turtle()
pen.hideturtle()
pen.penup()
pen.color(self.turn)
row = max([i for i, cell in enumerate(self.board.board) if cell[column] is None])
pen.setposition(-240 + column * 80, -200 + row * 80)
pen.pendown()
pen.begin_fill()
pen.circle(30)
pen.end_fill()
game = Game()
turtle.mainloop()
```
5. turtle 代码:
```python
import turtle
pen = turtle.Turtle()
pen.speed(0)
pen.hideturtle()
def draw_triangle(x, y, size, color):
pen.penup()
pen.setposition(x, y)
pen.pendown()
pen.color(color)
pen.begin_fill()
for _ in range(3):
pen.forward(size)
pen.left(120)
pen.end_fill()
draw_triangle(-200, 0, 100, "red")
draw_triangle(0, 0, 100, "green")
draw_triangle(200, 0, 100, "blue")
turtle.mainloop()
```
阅读全文