制作一个贪吃蛇的程序要完整的制作代码
由于 Python 是一种非常适合编写游戏的语言,因此我们可以使用 Python 编写一个贪吃蛇游戏的程序。下面是完整的 Python 代码:
import pygame
import random
# 初始化 Pygame
pygame.init()
# 游戏屏幕尺寸
SCREEN_WIDTH = 640
SCREEN_HEIGHT = 480
# 贪吃蛇单元格尺寸
CELL_SIZE = 20
# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
# 创建游戏屏幕
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
# 设置游戏标题
pygame.display.set_caption('贪吃蛇')
# 定义贪吃蛇类
class Snake:
def __init__(self):
self.body = [(160, 200), (140, 200), (120, 200)]
self.direction = 'right'
# 贪吃蛇移动
def move(self):
if self.direction == 'up':
new_head = (self.body[0][0], self.body[0][1] - CELL_SIZE)
elif self.direction == 'down':
new_head = (self.body[0][0], self.body[0][1] + CELL_SIZE)
elif self.direction == 'left':
new_head = (self.body[0][0] - CELL_SIZE, self.body[0][1])
elif self.direction == 'right':
new_head = (self.body[0][0] + CELL_SIZE, self.body[0][1])
self.body.insert(0, new_head)
self.body.pop()
# 贪吃蛇改变方向
def change_direction(self, direction):
if direction == 'up' and self.direction != 'down':
self.direction = 'up'
elif direction == 'down' and self.direction != 'up':
self.direction = 'down'
elif direction == 'left' and self.direction != 'right':
self.direction = 'left'
elif direction == 'right' and self.direction != 'left':
self.direction = 'right'
# 贪吃蛇增长
def grow(self):
tail = self.body[-1]
if self.direction == 'up':
new_tail = (tail[0], tail[1] + CELL_SIZE)
elif self.direction == 'down':
new_tail = (tail[0], tail[1] - CELL_SIZE)
elif self.direction == 'left':
new_tail = (tail[0] + CELL_SIZE, tail[1])
elif self.direction == 'right':
new_tail = (tail[0] - CELL_SIZE, tail[1])
self.body.append(new_tail)
# 定义食物类
class Food:
def __init__(self):
self.position = (0, 0)
self.color = RED
self.randomize_position()
# 随机生成食物位置
def randomize_position(self):
x = random.randint(0, (SCREEN_WIDTH - CELL_SIZE) // CELL_SIZE) * CELL_SIZE
y = random.randint(0, (SCREEN_HEIGHT - CELL_SIZE) // CELL_SIZE) * CELL_SIZE
self.position = (x, y)
# 绘制食物
def draw(self):
pygame.draw.rect(screen, self.color, (self.position[0], self.position[1], CELL_SIZE, CELL_SIZE))
# 初始化贪吃蛇和食物
snake = Snake()
food = Food()
# 定义游戏循环
running = True
clock = pygame.time.Clock()
while running:
clock.tick(10) # 控制游戏帧率
# 处理游戏事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
snake.change_direction('up')
elif event.key == pygame.K_DOWN:
snake.change_direction('down')
elif event.key == pygame.K_LEFT:
snake.change_direction('left')
elif event.key == pygame.K_RIGHT:
snake.change_direction('right')
# 绘制游戏背景
screen.fill(WHITE)
# 绘制贪吃蛇
for cell in snake.body:
pygame.draw.rect(screen, BLACK, (cell[0], cell[1], CELL_SIZE, CELL_SIZE))
# 移动贪吃蛇
snake.move()
# 检查贪吃蛇是否与食物碰撞
if snake.body[0] == food.position:
snake.grow()
food.randomize_position()
# 绘制食物
food.draw()
# 判断游戏是否结束
if snake.body[0][0] < 0 or snake.body[0][0] >= SCREEN_WIDTH or snake.body[0][1] < 0 or snake.body[0][1] >= SCREEN_HEIGHT:
running = False
for cell in snake.body[1:]:
if snake.body[0] == cell:
running = False
# 更新游戏屏幕
pygame.display.update()
# 退出 Pygame
pygame.quit()
这段代码使用了 Pygame 库来绘制游戏界面,同时定义了贪吃蛇和食物类来管理游戏中的元素。在游戏循环中,我们不断地更新游戏状态并绘制游戏界面,直到游戏结束为止。
原文地址: https://www.cveoy.top/t/topic/XM3 著作权归作者所有。请勿转载和采集!