用python写贪吃蛇游戏
时间: 2023-10-02 11:04:44 浏览: 39
好的,我可以帮您解答这个问题。以下是基本的代码实现方式:
```python
import pygame
import random
# 初始化 Pygame
pygame.init()
# 设置窗口和游戏标题
screen_width = 600
screen_height = 400
game_display = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('贪吃蛇')
# 设置颜色
black = (0, 0, 0)
white = (255, 255, 255)
red = (213, 50, 80)
green = (0, 255, 0)
blue = (50, 153, 213)
# 设置蛇的大小和速度
block_size = 10
snake_speed = 15
# 定义字体
font_style = pygame.font.SysFont(None, 30)
# 定义函数,用于在屏幕上绘制蛇
def draw_snake(block_size, snake_list):
for x in snake_list:
pygame.draw.rect(game_display, green, [x[0], x[1], block_size, block_size])
# 定义函数,用于在屏幕上显示消息
def message(msg, color):
message_text = font_style.render(msg, True, color)
game_display.blit(message_text, [screen_width / 6, screen_height / 3])
# 定义游戏主循环函数
def game_loop():
game_over = False
game_close = False
# 初始化蛇的位置和方向
x1 = screen_width / 2
y1 = screen_height / 2
x1_change = 0
y1_change = 0
# 初始化蛇的身体
snake_List = []
Length_of_snake = 1
# 随机生成食物的位置
foodx = round(random.randrange(0, screen_width - block_size) / 10.0) * 10.0
foody = round(random.randrange(0, screen_height - block_size) / 10.0) * 10.0
# 开始游戏循环
while not game_over:
while game_close == True:
game_display.fill(black)
message("你输了!按 Q 退出,按 C 重新开始", 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 = -block_size
y1_change = 0
elif event.key == pygame.K_RIGHT:
x1_change = block_size
y1_change = 0
elif event.key == pygame.K_UP:
y1_change = -block_size
x1_change = 0
elif event.key == pygame.K_DOWN:
y1_change = block_size
x1_change = 0
# 当蛇的位置碰到边界,游戏失败
if x1 >= screen_width or x1 < 0 or y1 >= screen_height or y1 < 0:
game_close = True
# 更改蛇的位置
x1 += x1_change
y1 += y1_change
# 绘制游戏屏幕
game_display.fill(black)
pygame.draw.rect(game_display, red, [foodx, foody, block_size, block_size])
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
draw_snake(block_size, snake_List)
pygame.display.update()
# 当蛇吃到食物,则增加蛇的长度
if x1 == foodx and y1 == foody:
foodx = round(random.randrange(0, screen_width - block_size) / 10.0) * 10.0
foody = round(random.randrange(0, screen_height - block_size) / 10.0) * 10.0
Length_of_snake += 1
# 设置游戏速度
clock.tick(snake_speed)
# 关闭 Pygame
pygame.quit()
# 调用游戏主循环函数
game_loop()
```
希望这个代码对您有所帮助!
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)