帮我写一个python代码的贪吃蛇游戏
时间: 2024-03-02 18:22:09 浏览: 31
好的,以下是一个简单的Snake游戏的代码,你可以按照自己的需求进行修改。
```
import pygame
import random
# 初始化 pygame
pygame.init()
# 设置游戏窗口大小
WIDTH, HEIGHT = 500, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("贪吃蛇游戏")
# 游戏参数设置
CELL_SIZE = 20 # 圆圈大小
GRID_SQUARES = WIDTH // CELL_SIZE # 圆圈数量
FPS = 10 # 帧率
# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
# 定义圆圈类
class Cell:
def __init__(self, x, y):
self.x = x
self.y = y
def draw(self, win):
pygame.draw.circle(win, WHITE, (self.x+CELL_SIZE//2, self.y+CELL_SIZE//2), CELL_SIZE//2-1)
# 定义食物类
class Food:
def __init__(self):
self.x = random.randint(0, GRID_SQUARES-1) * CELL_SIZE
self.y = random.randint(0, GRID_SQUARES-1) * CELL_SIZE
def draw(self, win):
pygame.draw.circle(win, RED, (self.x+CELL_SIZE//2, self.y+CELL_SIZE//2), CELL_SIZE//2-1)
# 定义贪吃蛇类
class Snake:
def __init__(self):
self.cells = [Cell(CELL_SIZE*3, CELL_SIZE*3), Cell(CELL_SIZE*2, CELL_SIZE*3), Cell(CELL_SIZE, CELL_SIZE*3)]
self.direction = "right"
def draw(self, win):
for cell in self.cells:
cell.draw(win)
def move(self):
# 计算新的头部位置
if self.direction == "right":
new_head = Cell(self.cells[0].x+CELL_SIZE, self.cells[0].y)
elif self.direction == "left":
new_head = Cell(self.cells[0].x-CELL_SIZE, self.cells[0].y)
elif self.direction == "up":
new_head = Cell(self.cells[0].x, self.cells[0].y-CELL_SIZE)
elif self.direction == "down":
new_head = Cell(self.cells[0].x, self.cells[0].y+CELL_SIZE)
# 将新的头部添加到列表最前面
self.cells.insert(0, new_head)
# 如果吃到食物,则重新生成一个食物
if self.cells[0].x == food.x and self.cells[0].y == food.y:
global score
score += 10
food.__init__()
# 删除列表最后一个元素
self.cells.pop()
# 判断是否与自身相撞
for cell in self.cells[1:]:
if cell.x == self.cells[0].x and cell.y == self.cells[0].y:
game_over()
# 判断是否撞到墙壁
if self.cells[0].x < 0 or self.cells[0].x >= WIDTH or self.cells[0].y < 0 or self.cells[0].y >= HEIGHT:
game_over()
# 游戏结束
def game_over():
print("Game Over!")
pygame.quit()
quit()
# 显示分数
def show_score(win, score):
font = pygame.font.SysFont("arial", 20)
text = font.render("Score: "+str(score), True, WHITE)
win.blit(text, (WIDTH-100, 10))
# 初始化游戏
def game_init():
global snake, food, score
snake = Snake()
food = Food()
score = 0
# 游戏主体循环
def game_loop():
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT and snake.direction != "left":
snake.direction = "right"
elif event.key == pygame.K_LEFT and snake.direction != "right":
snake.direction = "left"
elif event.key == pygame.K_UP and snake.direction != "down":
snake.direction = "up"
elif event.key == pygame.K_DOWN and snake.direction != "up":
snake.direction = "down"
WIN.fill(BLACK)
snake.move()
snake.draw(WIN)
food.draw(WIN)
show_score(WIN, score)
pygame.display.update()
clock.tick(FPS)
# 初始化游戏
game_init()
# 运行游戏主体循环
game_loop()
```
那么,这段代码是否符合您的需求呢?
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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_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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)