用python写贪吃蛇小游戏
时间: 2023-09-25 21:06:53 浏览: 128
100行Python代码的贪吃蛇小游戏源码.zip
5星 · 资源好评率100%
### 回答1:
好的,以下是用 Python 写的一个简单的贪吃蛇小游戏。
```python
import pygame, random
# 初始化 Pygame
pygame.init()
# 游戏窗口的宽度和高度
WINDOW_WIDTH = 600
WINDOW_HEIGHT = 600
# 定义颜色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
# 设置游戏窗口
game_display = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption('贪吃蛇')
# 设置时钟
clock = pygame.time.Clock()
# 蛇的大小和速度
BLOCK_SIZE = 10
SNAKE_SPEED = 20
# 字体
font = pygame.font.SysFont(None, 25)
# 定义函数,用于显示消息
def message_to_screen(msg, color):
screen_text = font.render(msg, True, color)
game_display.blit(screen_text, [WINDOW_WIDTH / 6, WINDOW_HEIGHT / 3])
# 定义函数,用于显示蛇
def snake(block_size, snake_list):
for block in snake_list:
pygame.draw.rect(game_display, GREEN, [block[0], block[1], block_size, block_size])
# 定义游戏主函数
def game_loop():
game_exit = False
game_over = False
# 蛇的初始位置和长度
lead_x = WINDOW_WIDTH / 2
lead_y = WINDOW_HEIGHT / 2
lead_x_change = 0
lead_y_change = 0
snake_list = []
snake_length = 1
# 随机生成食物的位置
food_x = round(random.randrange(0, WINDOW_WIDTH - BLOCK_SIZE) / 10.0) * 10.0
food_y = round(random.randrange(0, WINDOW_HEIGHT - BLOCK_SIZE) / 10.0) * 10.0
while not game_exit:
# 处理游戏结束的情况
while game_over == True:
game_display.fill(WHITE)
message_to_screen("游戏结束,按 Q 退出,按 C 重新开始", RED)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_exit = True
game_over = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
game_exit = True
game_over = False
elif event.key == pygame.K_c:
game_loop()
# 处理游戏中的事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_exit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
lead_x_change = -BLOCK_SIZE
lead_y_change = 0
elif event.key == pygame.K_RIGHT:
lead_x_change = BLOCK_SIZE
lead_y_change = 0
elif event.key == pygame.K_UP:
lead_y_change = -BLOCK_SIZE
lead_x_change = 0
elif event.key == pygame.K_DOWN:
lead_y_change = BLOCK_SIZE
lead_x_change = 0
# 处理边界碰撞和身体碰撞的情况
if lead_x >= WINDOW_WIDTH or lead_x < 0 or lead_y >= WINDOW_HEIGHT or lead_y < 0:
game_over = True
for block in snake_list[1:]:
if block == snake_head:
game_over = True
# 更新蛇的位置和长度
lead_x += lead_x_change
lead_y += lead_y_change
snake_head = [lead_x, lead_y]
snake_list.append(snake_head)
if len(snake_list) > snake_length:
del snake_list[0]
# 处理吃到食物的情况
if lead_x == food_x and lead_y == food_y:
food_x = round(random.randrange(0, WINDOW_WIDTH - BLOCK_SIZE) / 10.0) * 10.0
food_y = round(random.randrange(0, WINDOW_HEIGHT - BLOCK_SIZE) / 10.0) * 10.0
snake_length += 1
# 填充游戏窗口
game_display.fill(WHITE)
# 绘制食物和蛇
pygame.draw.rect(game_display, RED, [food_x, food_y, BLOCK_SIZE, BLOCK_SIZE])
snake(BLOCK_SIZE, snake_list)
# 更新游戏窗口
pygame.display.update()
# 设置游戏速度
clock.tick(SNAKE_SPEED)
# 退出 Pygame
pygame.quit()
quit()
# 开始游戏
game_loop()
```
希望对你有帮助!
### 回答2:
贪吃蛇是一款非常经典的游戏,下面是使用Python编写的简单贪吃蛇游戏的代码示例。
```python
import pygame
import random
# 初始化 pygame
pygame.init()
# 设置游戏窗口大小
screen_width, screen_height = 640, 480
screen = pygame.display.set_mode((screen_width, screen_height))
# 定义颜色
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
# 定义蛇的初始位置和大小
snake_x, snake_y = 50, 50
snake_width, snake_height = 10, 10
# 定义蛇的移动速度
snake_speed = 10
snake_x_change, snake_y_change = 0, 0
# 定义食物的初始位置和大小
food_x = random.randint(0, screen_width - 10)
food_y = random.randint(0, screen_height - 10)
food_width, food_height = 10, 10
# 定义得分
score = 0
# 游戏循环
running = True
while running:
screen.fill(BLACK) # 清空屏幕
for event in pygame.event.get():
if event.type == pygame.QUIT: # 如果点击关闭按钮
running = False
if event.type == pygame.KEYDOWN: # 如果按下键盘按键
if event.key == pygame.K_UP:
snake_y_change = -snake_speed
snake_x_change = 0
elif event.key == pygame.K_DOWN:
snake_y_change = snake_speed
snake_x_change = 0
elif event.key == pygame.K_LEFT:
snake_x_change = -snake_speed
snake_y_change = 0
elif event.key == pygame.K_RIGHT:
snake_x_change = snake_speed
snake_y_change = 0
# 移动蛇的位置
snake_x += snake_x_change
snake_y += snake_y_change
# 画出蛇和食物
pygame.draw.rect(screen, GREEN, (snake_x, snake_y, snake_width, snake_height))
pygame.draw.rect(screen, GREEN, (food_x, food_y, food_width, food_height))
# 检测蛇是否吃到食物
if snake_x == food_x and snake_y == food_y:
score += 1
food_x = random.randint(0, screen_width - 10)
food_y = random.randint(0, screen_height - 10)
pygame.display.update() # 更新屏幕
# 退出游戏
pygame.quit()
```
以上就是一个简单的贪吃蛇游戏的代码示例。玩家可以通过方向键控制蛇的移动方向,当蛇吃到食物时,得分会增加。游戏会不断循环,直到玩家关闭游戏窗口。希望这个示例对你有所帮助!
### 回答3:
贪吃蛇是经典的小游戏,可以用Python来实现。以下是基本的代码示例:
```python
import pygame
import random
# 游戏窗口的大小
width, height = 800, 600
# 定义颜色
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
# 初始化pygame和游戏窗口
pygame.init()
window = pygame.display.set_mode((width, height))
pygame.display.set_caption("贪吃蛇小游戏")
# 定义蛇的初始位置和大小
snake_size = 20
snake_x = 100
snake_y = 50
# 定义蛇移动的速度
snake_speed = 10
snake_x_change = 0
snake_y_change = 0
# 定义食物的随机位置
food_x = round(random.randrange(0, width - snake_size) / 10.0) * 10.0
food_y = round(random.randrange(0, height - snake_size) / 10.0) * 10.0
# 游戏是否结束的标志
game_over = False
# 游戏主循环
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_x_change = -snake_size
snake_y_change = 0
elif event.key == pygame.K_RIGHT:
snake_x_change = snake_size
snake_y_change = 0
elif event.key == pygame.K_UP:
snake_y_change = -snake_size
snake_x_change = 0
elif event.key == pygame.K_DOWN:
snake_y_change = snake_size
snake_x_change = 0
# 蛇的移动
snake_x += snake_x_change
snake_y += snake_y_change
# 检测蛇是否碰到边界
if snake_x >= width or snake_x < 0 or snake_y >= height or snake_y < 0:
game_over = True
# 渲染游戏窗口
window.fill(black)
pygame.draw.rect(window, white, [food_x, food_y, snake_size, snake_size])
pygame.draw.rect(window, red, [snake_x, snake_y, snake_size, snake_size])
pygame.display.update()
# 检测蛇是否吃到食物
if snake_x == food_x and snake_y == food_y:
food_x = round(random.randrange(0, width - snake_size) / 10.0) * 10.0
food_y = round(random.randrange(0, height - snake_size) / 10.0) * 10.0
# 控制游戏的速度
pygame.time.Clock().tick(snake_speed)
# 退出pygame
pygame.quit()
```
以上是用Python写贪吃蛇小游戏的代码示例,通过循环检测事件、移动蛇、检测边界和食物等,来实现一个基本的贪吃蛇游戏。游戏窗口使用pygame库来创建和渲染,控制蛇的移动和速度,判断蛇是否吃到食物,并在游戏结束后退出。这个代码示例只是贪吃蛇游戏的基本框架,你可以根据具体需求进行进一步的功能扩展和美化。
阅读全文