贪吃蛇 python代码
时间: 2023-06-27 07:05:51 浏览: 70
贪吃蛇是一个经典的游戏,可以用 Python 编写代码实现。这里是一个简单的贪吃蛇游戏的 Python 代码示例:
```
import turtle
import time
# 设置画布大小
turtle.setup(width=600, height=600)
# 创建一个 Turtle 实例
snake = turtle.Turtle()
snake.speed(10)
snake.penup()
# 设置食物的位置
food = turtle.Turtle()
food.speed(0)
food.color("red")
food.penup()
food.goto(0, 100)
food.pendown()
food.begin_fill()
food.circle(10)
food.end_fill()
# 定义蛇的移动方向
directions = ["down", "right", "up", "left"]
# 开始游戏
while True:
snake.forward(20)
# 判断是否吃到食物
if snake.distance(food) < 20:
food.goto(0, 0)
time.sleep(0.2)
turtle.done()
```
希望这个代码示例对你有所帮助。
相关问题
贪吃蛇python代码
以下是一个简单的贪吃蛇Python代码示例:
```
import pygame
import random
# 初始化pygame
pygame.init()
# 设置游戏窗口大小和标题
width, height = 640, 480
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("贪吃蛇")
# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
# 定义游戏参数
block_size = 10
clock = pygame.time.Clock()
font = pygame.font.SysFont(None, 25)
# 绘制文字
def draw_text(msg, color, x, y):
screen_text = font.render(msg, True, color)
screen.blit(screen_text, [x, y])
# 绘制蛇
def draw_snake(snake_list):
for x, y in snake_list:
pygame.draw.rect(screen, GREEN, [x, y, block_size, block_size])
# 游戏循环
def game_loop():
game_over = False
game_close = False
x, y = width / 2, height / 2
x_change, y_change = 0, 0
snake_list = []
snake_length = 1
foodx, foody = round(random.randrange(0, width - block_size) / 10.0) * 10.0, round(
random.randrange(0, height - block_size) / 10.0) * 10.0
while not game_over:
while game_close:
screen.fill(BLACK)
draw_text("游戏结束,按Q退出或按C重新开始", RED, width / 6, height / 3)
draw_text("得分:" + str(snake_length - 1), WHITE, width / 3, height / 2)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
game_over = True
game_close = False
if event.key == pygame.K_c:
game_loop()
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -block_size
y_change = 0
elif event.key == pygame.K_RIGHT:
x_change = block_size
y_change = 0
elif event.key == pygame.K_UP:
y_change = -block_size
x_change = 0
elif event.key == pygame.K_DOWN:
y_change = block_size
x_change = 0
if x >= width or x < 0 or y >= height or y < 0:
game_close = True
x += x_change
y += y_change
screen.fill(BLACK)
pygame.draw.rect(screen, BLUE, [foodx, foody, block_size, block_size])
snake_head = [x, y]
snake_list.append(snake_head)
if len(snake_list) > snake_length:
del snake_list[0]
for segment in snake_list[:-1]:
if segment == snake_head:
game_close = True
draw_snake(snake_list)
draw_text("得分:" + str(snake_length - 1), WHITE, 5, 5)
pygame.display.update()
if x == foodx and y == foody:
foodx, foody = round(random.randrange(0, width - block_size) / 10.0) * 10.0, round(
random.randrange(0, height - block_size) / 10.0) * 10.0
snake_length += 1
clock.tick(15)
pygame.quit()
quit()
# 运行游戏循环
game_loop()
```
这只是一个简单的贪吃蛇游戏代码示例,你可以从这个基础上添加更多的功能和改进,让游戏更加有趣。
贪吃蛇Python代码
以下是一个简单的贪吃蛇Python代码:
```python
import pygame
import random
# 游戏参数
WIDTH = 500
HEIGHT = 500
GRID_SIZE = 20
# 颜色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
# 初始化游戏
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
# 定义贪吃蛇类
class Snake:
def __init__(self):
self.body = [(WIDTH/2, HEIGHT/2)]
self.direction = random.choice(["UP", "DOWN", "LEFT", "RIGHT"])
def move(self):
x, y = self.body[0]
if self.direction == "UP":
y -= GRID_SIZE
elif self.direction == "DOWN":
y += GRID_SIZE
elif self.direction == "LEFT":
x -= GRID_SIZE
elif self.direction == "RIGHT":
x += GRID_SIZE
self.body.insert(0, (x, y))
self.body.pop()
def change_direction(self, direction):
if direction == "UP" and self.direction != "DOWN":
self.direction = "UP"
elif direction == "DOWN" and self.direction != "UP":
self.direction = "DOWN"
elif direction == "LEFT" and self.direction != "RIGHT":
self.direction = "LEFT"
elif direction == "RIGHT" and self.direction != "LEFT":
self.direction = "RIGHT"
def grow(self):
x, y = self.body[0]
if self.direction == "UP":
y -= GRID_SIZE
elif self.direction == "DOWN":
y += GRID_SIZE
elif self.direction == "LEFT":
x -= GRID_SIZE
elif self.direction == "RIGHT":
x += GRID_SIZE
self.body.insert(0, (x, y))
def draw(self):
for x, y in self.body:
pygame.draw.rect(screen, GREEN, (x, y, GRID_SIZE, GRID_SIZE))
def check_collision(self):
x, y = self.body[0]
if x < 0 or x >= WIDTH or y < 0 or y >= HEIGHT:
return True
for i in range(1, len(self.body)):
if self.body[i] == self.body[0]:
return True
return False
# 定义食物类
class Food:
def __init__(self):
self.x = random.randrange(0, WIDTH, GRID_SIZE)
self.y = random.randrange(0, HEIGHT, GRID_SIZE)
def draw(self):
pygame.draw.rect(screen, RED, (self.x, self.y, GRID_SIZE, GRID_SIZE))
# 创建贪吃蛇和食物
snake = Snake()
food = Food()
# 游戏循环
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
snake.change_direction("UP")
elif event.key == pygame.K_DOWN:
snake.change_direction("DOWN")
elif event.key == pygame.K_LEFT:
snake.change_direction("LEFT")
elif event.key == pygame.K_RIGHT:
snake.change_direction("RIGHT")
# 移动贪吃蛇
snake.move()
# 检查贪吃蛇是否碰撞
if snake.check_collision():
pygame.quit()
sys.exit()
# 检查贪吃蛇是否吃到食物
if snake.body[0] == (food.x, food.y):
snake.grow()
food = Food()
# 绘制游戏界面
screen.fill(BLACK)
snake.draw()
food.draw()
pygame.display.update()
# 控制游戏速度
clock.tick(10)
```