用python写一个贪吃蛇
时间: 2024-01-06 09:06:20 浏览: 63
python写的贪吃蛇
好的,下面是一个简单的贪吃蛇游戏,使用 Python 和 Pygame 库编写:
```python
import pygame
import random
# 初始化 Pygame 库
pygame.init()
# 游戏窗口尺寸和标题
SCREEN_WIDTH = 600
SCREEN_HEIGHT = 600
SCREEN_TITLE = '贪吃蛇游戏'
# 定义颜色
BLACK_COLOR = (0, 0, 0)
WHITE_COLOR = (255, 255, 255)
RED_COLOR = (255, 0, 0)
GREEN_COLOR = (0, 255, 0)
BLUE_COLOR = (0, 0, 255)
# 创建游戏窗口
SCREEN = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption(SCREEN_TITLE)
# 定义贪吃蛇类
class Snake:
def __init__(self):
self.length = 1
self.positions = [(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2)]
self.direction = random.choice([UP, DOWN, LEFT, RIGHT])
self.color = GREEN_COLOR
self.score = 0
def get_head_position(self):
return self.positions[0]
def turn(self, point):
if self.length > 1 and (point[0]*-1,point[1]*-1) == self.direction:
return
else:
self.direction = point
def move(self):
cur = self.get_head_position()
x, y = self.direction
new = ((cur[0] + (x*GRID_SIZE)), (cur[1] + (y*GRID_SIZE)))
if new in self.positions[2:]:
self.reset()
else:
self.positions.insert(0, new)
if len(self.positions) > self.length:
self.positions.pop()
def reset(self):
self.length = 1
self.positions = [(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2)]
self.direction = random.choice([UP, DOWN, LEFT, RIGHT])
self.score = 0
def draw(self, surface):
for p in self.positions:
r = pygame.Rect((p[0], p[1]), (GRID_SIZE, GRID_SIZE))
pygame.draw.rect(surface, self.color, r)
pygame.draw.rect(surface, BLACK_COLOR, r, 1)
def handle_keys(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
self.turn(UP)
elif event.key == pygame.K_DOWN:
self.turn(DOWN)
elif event.key == pygame.K_LEFT:
self.turn(LEFT)
elif event.key == pygame.K_RIGHT:
self.turn(RIGHT)
# 定义食物类
class Food:
def __init__(self):
self.position = (0, 0)
self.color = RED_COLOR
self.randomize_position()
def randomize_position(self):
self.position = (random.randint(0, GRID_WIDTH-1) * GRID_SIZE, random.randint(0, GRID_HEIGHT-1) * GRID_SIZE)
def draw(self, surface):
r = pygame.Rect((self.position[0], self.position[1]), (GRID_SIZE, GRID_SIZE))
pygame.draw.rect(surface, self.color, r)
pygame.draw.rect(surface, BLACK_COLOR, r, 1)
# 定义游戏主函数
def main():
pygame.mixer.init()
pygame.mixer.music.load('bg_music.mp3')
pygame.mixer.music.play(-1, 0.0)
snake = Snake()
food = Food()
score_font = pygame.font.SysFont('arial', 26)
while True:
# 处理按键事件
snake.handle_keys()
# 移动贪吃蛇
snake.move()
# 判断是否吃到食物
if snake.get_head_position() == food.position:
snake.length += 1
snake.score += 1
food.randomize_position()
# 绘制游戏界面
SCREEN.fill(WHITE_COLOR)
snake.draw(SCREEN)
food.draw(SCREEN)
score_text = 'Score: ' + str(snake.score)
score_surface = score_font.render(score_text, True, BLACK_COLOR)
SCREEN.blit(score_surface, (SCREEN_WIDTH - score_surface.get_width() - 10, 10))
pygame.display.update()
# 判断是否撞墙
if snake.get_head_position()[0] < 0 or snake.get_head_position()[0] > SCREEN_WIDTH-GRID_SIZE:
snake.reset()
elif snake.get_head_position()[1] < 0 or snake.get_head_position()[1] > SCREEN_HEIGHT-GRID_SIZE:
snake.reset()
# 定义方向常量
UP = (0, -1)
DOWN = (0, 1)
LEFT = (-1, 0)
RIGHT = (1, 0)
# 定义格子大小和数量
GRID_SIZE = 20
GRID_WIDTH = SCREEN_WIDTH / GRID_SIZE
GRID_HEIGHT = SCREEN_HEIGHT / GRID_SIZE
if __name__ == '__main__':
main()
```
运行代码后,会弹出一个游戏窗口,按上、下、左、右键控制贪吃蛇移动,吃到食物后贪吃蛇会变长,撞墙或自己会死亡,每吃到一个食物得分加一。
阅读全文