写一个贪吃蛇pyhton代码
时间: 2023-08-31 09:28:35 浏览: 145
贪吃蛇python代码
### 回答1:
我可以给你一些简单的Python代码来实现贪吃蛇:import pygame
import sys
import random# 初始化游戏
pygame.init()# 设置窗口
game_width = 800
game_height = 600
game_window = pygame.display.set_mode((game_width, game_height))
pygame.display.set_caption("贪吃蛇")# 颜色
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)# 定义蛇的起始位置
snake_x = 400
snake_y = 300# 定义蛇的大小
snake_size = 10# 蛇的移动方向
direction = "right"# 定义食物的位置
food_x = random.randint(0, game_width-snake_size)
food_y = random.randint(0, game_height-snake_size)# 设置循环条件
game_over = False# 开始游戏循环
while not game_over:
# 获取按键事件
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:
direction = "left"
if event.key == pygame.K_RIGHT:
direction = "right"
if event.key == pygame.K_UP:
direction = "up"
if event.key == pygame.K_DOWN:
direction = "down" # 更新蛇的位置
if direction == "right":
snake_x += 10
if direction == "left":
snake_x -= 10
if direction == "up":
snake_y -= 10
if direction == "down":
snake_y += 10 # 填充游戏窗口
game_window.fill(white)
# 画蛇
pygame.draw.rect(game_window, green, [snake_x, snake_y, snake_size, snake_size])
# 画食物
pygame.draw.rect(game_window, red, [food_x, food_y, snake_size, snake_size])
# 刷新游戏窗口
pygame.display.update()# 退出游戏
pygame.quit()
sys.exit()
### 回答2:
贪吃蛇是一款经典的游戏,下面是一个基于Python的贪吃蛇代码:
```python
import pygame
import random
# 初始化游戏
pygame.init()
# 设置游戏窗口大小
window_width = 600
window_height = 400
# 创建游戏窗口
window = pygame.display.set_mode((window_width, window_height))
pygame.display.set_caption("贪吃蛇")
# 定义游戏相关参数
snake_size = 20
snake_speed = 10
# 定义颜色
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
# 定义贪吃蛇的初始位置
snake_x = window_width / 2
snake_y = window_height / 2
# 定义贪吃蛇的移动方向
snake_dx = 0
snake_dy = 0
# 定义食物的初始位置
food_x = round(random.randrange(0, window_width - snake_size) / 20) * 20
food_y = round(random.randrange(0, window_height - snake_size) / 20) * 20
# 定义得分
score = 0
# 控制游戏循环退出的变量
game_over = False
# 控制游戏帧率
clock = pygame.time.Clock()
# 定义贪吃蛇身体
snake_body = []
snake_body_length = 1
# 游戏循环
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
snake_dx = -snake_size
snake_dy = 0
elif event.key == pygame.K_RIGHT:
snake_dx = snake_size
snake_dy = 0
elif event.key == pygame.K_UP:
snake_dx = 0
snake_dy = -snake_size
elif event.key == pygame.K_DOWN:
snake_dx = 0
snake_dy = snake_size
# 更新贪吃蛇头部位置
snake_x += snake_dx
snake_y += snake_dy
# 判断是否吃到食物
if snake_x == food_x and snake_y == food_y:
food_x = round(random.randrange(0, window_width - snake_size) / 20) * 20
food_y = round(random.randrange(0, window_height - snake_size) / 20) * 20
score += 1
snake_body_length += 1
# 更新贪吃蛇身体
snake_head = []
snake_head.append(snake_x)
snake_head.append(snake_y)
snake_body.append(snake_head)
if len(snake_body) > snake_body_length:
del snake_body[0]
# 判断是否游戏结束
if snake_x < 0 or snake_x >= window_width or snake_y < 0 or snake_y >= window_height:
game_over = True
print("游戏结束,得分:%d" % score)
for item in snake_body[:-1]:
if item == snake_head:
game_over = True
print("游戏结束,得分:%d" % score)
# 填充窗口背景色
window.fill(black)
# 绘制贪吃蛇和食物
for item in snake_body:
pygame.draw.rect(window, white, [item[0], item[1], snake_size, snake_size])
pygame.draw.rect(window, red, [food_x, food_y, snake_size, snake_size])
# 更新窗口显示
pygame.display.update()
# 控制游戏帧率
clock.tick(snake_speed)
# 退出游戏
pygame.quit()
```
这段代码实现了一个简单的贪吃蛇游戏,玩家通过键盘上下左右箭头控制蛇头移动,吃到食物后得分增加,当蛇头撞墙或者撞到自己身体时游戏结束,最终会有一个得分的输出。游戏使用Pygame库来实现窗口和图形的显示,通过不断更新贪吃蛇的位置来实现蛇的移动,并判断游戏是否结束。
### 回答3:
下面是一个简单的 Python 贪吃蛇代码:
```python
import pygame
import random
# 初始化游戏
pygame.init()
# 游戏窗口大小
width = 800
height = 600
# 颜色
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
# 初始化游戏窗口
game_display = pygame.display.set_mode((width, height))
pygame.display.set_caption('贪吃蛇')
# 游戏速度(贪吃蛇移动的速度)
clock = pygame.time.Clock()
snake_block = 20
snake_speed = 20
font_style = pygame.font.SysFont(None, 50)
score_font = pygame.font.SysFont(None, 35)
def our_snake(snake_block, snake_list):
for x in snake_list:
pygame.draw.rect(game_display, black, [x[0], x[1], snake_block, snake_block])
def message(msg, color):
mesg = font_style.render(msg, True, color)
game_display.blit(mesg, [width / 6, height / 3])
# 游戏主循环
def game_loop():
game_over = False
game_close = False
# 初始位置
x1 = width/2
y1 = height/2
# 移动方向
x1_change = 0
y1_change = 0
# 初始长度
snake_List = []
Length_of_snake = 1
# 随机生成食物位置
foodx = round(random.randrange(0, width - snake_block) / 20.0) * 20.0
foody = round(random.randrange(0, height - snake_block) / 20.0) * 20.0
while not game_over:
while game_close == True:
game_display.fill(white)
message("You lost! Press Q-Quit or C-Play Again", red)
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:
x1_change = -snake_block
y1_change = 0
elif event.key == pygame.K_RIGHT:
x1_change = snake_block
y1_change = 0
elif event.key == pygame.K_UP:
y1_change = -snake_block
x1_change = 0
elif event.key == pygame.K_DOWN:
y1_change = snake_block
x1_change = 0
if x1 >= width or x1 < 0 or y1 >= height or y1 < 0:
game_close = True
x1 += x1_change
y1 += y1_change
game_display.fill(white)
pygame.draw.rect(game_display, red, [foodx, foody, snake_block, snake_block])
snake_Head = []
snake_Head.append(x1)
snake_Head.append(y1)
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_close = True
our_snake(snake_block, snake_List)
pygame.display.update()
if x1 == foodx and y1 == foody:
foodx = round(random.randrange(0, width - snake_block) / 20.0) * 20.0
foody = round(random.randrange(0, height - snake_block) / 20.0) * 20.0
Length_of_snake += 1
clock.tick(snake_speed)
pygame.quit()
# 运行游戏
game_loop()
```
这段代码使用了 Python 中的 Pygame 模块创建了一个贪吃蛇游戏。在游戏中,玩家通过控制蛇的移动方向,将食物吃掉,并尽量避免撞到自己的身体或者游戏窗口的边界。游戏使用矩形来表示蛇的身体和食物,通过监听按键事件,改变蛇头的坐标实现蛇的移动。当蛇头与食物的坐标重合时,代表蛇吃到了食物,蛇的长度增加,食物会在窗口内的随机位置重新生成。当蛇头撞到自己的身体或者游戏窗口的边界时,游戏结束。
阅读全文