贪吃蛇 python代码
时间: 2023-09-11 08:05:05 浏览: 92
### 回答1:
贪吃蛇是一个经典的游戏,可以用 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()
```
希望这个代码示例对你有所帮助。
### 回答2:
贪吃蛇是一款经典的游戏,也是很多人学习编程的开始之一。以下是一个基本的贪吃蛇Python代码:
```python
import pygame
import time
import random
# 设置窗口的尺寸及背景颜色
window_width = 400
window_height = 300
window_background = (0, 0, 0)
# 设置蛇的尺寸和移动速度
snake_block = 10
snake_speed = 15
# 初始化Pygame
pygame.init()
# 创建窗口并设置窗口标题
window = pygame.display.set_mode((window_width, window_height))
pygame.display.set_caption('贪吃蛇')
# 设置游戏时钟
clock = pygame.time.Clock()
# 定义蛇的颜色
snake_color = (0, 255, 0)
# 定义字体
font_style = pygame.font.SysFont(None, 30)
score_font = pygame.font.SysFont(None, 25)
# 显示得分
def show_score(score):
score_text = score_font.render("得分:" + str(score), True, (255, 255, 255))
window.blit(score_text, [0, 0])
# 绘制蛇
def draw_snake(snake_block, snake_list):
for x in snake_list:
pygame.draw.rect(window, snake_color, [x[0], x[1], snake_block, snake_block])
# 游戏主循环
def game_loop():
game_over = False
game_end = False
snake_x = window_width / 2
snake_y = window_height / 2
snake_x_change = 0
snake_y_change = 0
snake_list = []
length_of_snake = 1
food_x = round(random.randrange(0, window_width - snake_block) / snake_block) * snake_block
food_y = round(random.randrange(0, window_height - snake_block) / snake_block) * snake_block
while not game_over:
while game_end == True:
window.fill(window_background)
game_over_text = font_style.render("游戏已结束! 按Q键退出, 按C键重新开始", True, (255, 255, 255))
window.blit(game_over_text, [window_width/6, window_height/3])
show_score(length_of_snake - 1)
pygame.display.flip()
# 处理重新开始和退出
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
game_over = True
game_end = 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:
snake_x_change = -snake_block
snake_y_change = 0
elif event.key == pygame.K_RIGHT:
snake_x_change = snake_block
snake_y_change = 0
elif event.key == pygame.K_UP:
snake_y_change = -snake_block
snake_x_change = 0
elif event.key == pygame.K_DOWN:
snake_y_change = snake_block
snake_x_change = 0
# 判断蛇是否碰到了边界
if snake_x >= window_width or snake_x < 0 or snake_y >= window_height or snake_y < 0:
game_end = True
# 更新蛇的位置
snake_x += snake_x_change
snake_y += snake_y_change
window.fill(window_background)
pygame.draw.rect(window, (255, 0, 0), [food_x, food_y, snake_block, snake_block])
snake_head = []
snake_head.append(snake_x)
snake_head.append(snake_y)
snake_list.append(snake_head)
if len(snake_list) > length_of_snake:
del snake_list[0]
# 判断蛇是否吃到了食物
for x in snake_list[:-1]:
if x == snake_head:
game_end = True
# 绘制蛇和得分
draw_snake(snake_block, snake_list)
show_score(length_of_snake - 1)
pygame.display.update()
# 判断蛇是否吃到了食物
if snake_x == food_x and snake_y == food_y:
food_x = round(random.randrange(0, window_width - snake_block) / snake_block) * snake_block
food_y = round(random.randrange(0, window_height - snake_block) / snake_block) * snake_block
length_of_snake += 1
# 限制蛇的移动速度
clock.tick(snake_speed)
# 退出游戏
pygame.quit()
# 启动游戏循环
game_loop()
```
以上就是一个基本的贪吃蛇Python代码,代码中注释部分有详细的解释和说明,希望能对您有帮助。
### 回答3:
贪吃蛇是一款经典的游戏,玩家需要控制一条蛇在游戏界面上吃食物并不断成长。下面是一个简单的Python代码实现,用于展示贪吃蛇游戏的基本逻辑:
```python
import pygame
import random
# 游戏界面的大小
width = 640
height = 480
cell_size = 20
# 初始化游戏界面
pygame.init()
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Greedy Snake Game")
# 蛇的初始位置和长度
snake_head = [width//2, height//2]
snake_body = [[width//2, height//2], [width//2-cell_size, height//2], [width//2-2*cell_size, height//2]]
change_direction = ''
# 食物的初始位置
food_position = [random.randrange(1, (width-cell_size)//cell_size) * cell_size,
random.randrange(1, (height-cell_size)//cell_size) * cell_size]
# 控制蛇的移动方向
def change_dir():
global change_direction
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
change_direction = 'UP'
elif event.key == pygame.K_DOWN:
change_direction = 'DOWN'
elif event.key == pygame.K_LEFT:
change_direction = 'LEFT'
elif event.key == pygame.K_RIGHT:
change_direction = 'RIGHT'
# 更新蛇的位置和长度
def move_snake():
global snake_head, snake_body, food_position
if change_direction == 'UP':
snake_head[1] -= cell_size
elif change_direction == 'DOWN':
snake_head[1] += cell_size
elif change_direction == 'LEFT':
snake_head[0] -= cell_size
elif change_direction == 'RIGHT':
snake_head[0] += cell_size
snake_body.insert(0, list(snake_head))
if snake_head == food_position:
food_position = [random.randrange(1, (width-cell_size)//cell_size) * cell_size,
random.randrange(1, (height-cell_size)//cell_size) * cell_size]
else:
snake_body.pop()
# 绘制游戏界面
def draw_screen():
screen.fill((0, 0, 0))
for position in snake_body:
pygame.draw.rect(screen, (0, 255, 0), (position[0], position[1], cell_size, cell_size))
pygame.draw.rect(screen, (255, 0, 0), (food_position[0], food_position[1], cell_size, cell_size))
pygame.display.update()
# 判断游戏结束
def is_game_over():
if snake_head[0] < 0 or snake_head[0] >= width or snake_head[1] < 0 or snake_head[1] >= height:
return True
for segment in snake_body[1:]:
if segment == snake_head:
return True
return False
# 游戏循环
running = True
while running:
change_dir()
move_snake()
if is_game_over():
running = False
draw_screen()
pygame.time.Clock().tick(10)
pygame.quit()
```
以上是简单的贪吃蛇游戏的Python代码实现,运行程序后可以通过 ↑、↓、←、→ 控制蛇的移动方向,游戏界面由黑色背景、蛇身绿色方块和红色食物方块组成。蛇的长度会随着吃食物而增长,当蛇撞到边界或自身身体时游戏结束。这只是一个简单的示例代码,可以根据需求进行功能的扩展和优化。
阅读全文