Python实现贪吃蛇
时间: 2023-11-05 20:57:13 浏览: 40
好的,以下是一个简单的 Python 实现贪吃蛇的代码,你可以尝试一下:
```python
import pygame
import random
# 初始化 Pygame
pygame.init()
# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
# 设置屏幕大小
width = 640
height = 480
size = (width, height)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Snake Game")
# 设置时钟
clock = pygame.time.Clock()
# 定义方格大小
block_size = 10
# 定义字体
font = pygame.font.SysFont(None, 25)
# 定义蛇的初始位置和大小
def snake(block_size, snakeList):
for element in snakeList:
pygame.draw.rect(screen, BLACK, [element[0], element[1], block_size, block_size])
# 定义消息显示函数
def message_to_screen(msg, color):
screen_text = font.render(msg, True, color)
screen.blit(screen_text, [width / 2, height / 2])
# 定义游戏循环
def gameLoop():
gameExit = False
gameOver = False
# 定义蛇的初始位置和大小
lead_x = width / 2
lead_y = height / 2
lead_x_change = 0
lead_y_change = 0
snakeList = []
snakeLength = 1
# 定义食物的随机位置
randAppleX = round(random.randrange(0, width - block_size) / 10.0) * 10.0
randAppleY = round(random.randrange(0, height - block_size) / 10.0) * 10.0
while not gameExit:
while gameOver == True:
screen.fill(WHITE)
message_to_screen("Game over, press C to play again or Q to quit", RED)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
gameExit = True
gameOver = False
if event.key == pygame.K_c:
gameLoop()
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = 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 >= width or lead_x < 0 or lead_y >= height or lead_y < 0:
gameOver = True
# 更新蛇的位置
lead_x += lead_x_change
lead_y += lead_y_change
# 绘制背景
screen.fill(WHITE)
# 绘制食物
pygame.draw.rect(screen, RED, [randAppleX, randAppleY, block_size, block_size])
# 绘制蛇
snakeHead = []
snakeHead.append(lead_x)
snakeHead.append(lead_y)
snakeList.append(snakeHead)
if len(snakeList) > snakeLength:
del snakeList[0]
for eachSegment in snakeList[:-1]:
if eachSegment == snakeHead:
gameOver = True
snake(block_size, snakeList)
pygame.display.update()
# 食物被吃掉
if lead_x == randAppleX and lead_y == randAppleY:
randAppleX = round(random.randrange(0, width - block_size) / 10.0) * 10.0
randAppleY = round(random.randrange(0, height - block_size) / 10.0) * 10.0
snakeLength += 1
# 设置帧率
clock.tick(15)
# 退出 Pygame
pygame.quit()
# 开始游戏循环
gameLoop()
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)