Python 贪吃蛇游戏代码示例
当然可以!以下是使用Python编写的贪吃蛇游戏示例代码:
import pygame
import random
# 游戏窗口宽度和高度
window_width = 400
window_height = 400
cell_size = 20
# 定义颜色
black = pygame.Color(0, 0, 0)
white = pygame.Color(255, 255, 255)
red = pygame.Color(255, 0, 0)
green = pygame.Color(0, 255, 0)
blue = pygame.Color(0, 0, 255)
# 初始化游戏
pygame.init()
clock = pygame.time.Clock()
window = pygame.display.set_mode((window_width, window_height))
pygame.display.set_caption('贪吃蛇游戏')
# 蛇类
class Snake:
def __init__(self):
self.head = [160, 200] # 蛇头初始位置
self.body = [[160, 200], [140, 200], [120, 200]] # 蛇身初始位置
self.direction = 'RIGHT' # 初始向右移动的方向
def change_direction(self, new_direction):
if new_direction == 'RIGHT' and self.direction != 'LEFT':
self.direction = 'RIGHT'
if new_direction == 'LEFT' and self.direction != 'RIGHT':
self.direction = 'LEFT'
if new_direction == 'UP' and self.direction != 'DOWN':
self.direction = 'UP'
if new_direction == 'DOWN' and self.direction != 'UP':
self.direction = 'DOWN'
def move(self, food_position):
if self.direction == 'RIGHT':
self.head[0] += cell_size
if self.direction == 'LEFT':
self.head[0] -= cell_size
if self.direction == 'UP':
self.head[1] -= cell_size
if self.direction == 'DOWN':
self.head[1] += cell_size
self.body.insert(0, list(self.head))
if self.head == food_position:
return 1
else:
self.body.pop()
return 0
def check_collision(self):
if self.head[0] >= window_width or self.head[0] < 0 or self.head[1] >= window_height or self.head[1] < 0:
return 1
for block in self.body[1:]:
if self.head == block:
return 1
return 0
def draw(self, window):
for segment in self.body:
pygame.draw.rect(window, green, pygame.Rect(segment[0], segment[1], cell_size, cell_size))
# 食物类
class Food:
def __init__(self):
self.position = [random.randrange(1, (window_width // cell_size)) * cell_size,
random.randrange(1, (window_height // cell_size)) * cell_size]
def draw(self, window):
pygame.draw.rect(window, blue, pygame.Rect(self.position[0], self.position[1], cell_size, cell_size))
# 游戏主函数
def play_game():
snake = Snake()
food = Food()
score = 0
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:
snake.change_direction('RIGHT')
if event.key == pygame.K_LEFT:
snake.change_direction('LEFT')
if event.key == pygame.K_UP:
snake.change_direction('UP')
if event.key == pygame.K_DOWN:
snake.change_direction('DOWN')
food_collision = snake.move(food.position)
if food_collision == 1:
score += 1
food = Food()
window.fill(black)
snake.draw(window)
food.draw(window)
pygame.display.flip()
if snake.check_collision() == 1:
break
clock.tick(15)
return score
# 游戏运行
while True:
score = play_game()
print('游戏结束!得分:', score)
while True:
restart = input('再玩一次?(Y/N): ')
if restart.lower() == 'y':
break
elif restart.lower() == 'n':
pygame.quit()
quit()
else:
print('输入无效,请重新输入!')
请确保你已经安装了pygame库(可以使用pip install pygame进行安装)。运行以上代码后,你可以通过方向键控制蛇的移动,吃到食物后得分加一。游戏结束后,会显示得分,并询问是否再次游戏。
原文地址: https://www.cveoy.top/t/topic/bpFk 著作权归作者所有。请勿转载和采集!