找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 1032|回复: 0

[代码与实例] pygame贪吃蛇源码

1

主题

1

帖子

1

积分

贫民

积分
1
古一 发表于 2022-5-16 10:38:49 | 显示全部楼层 |阅读模式
使用 python pygame 模块实现的初代贪吃蛇源码;可更容易学习
更多示例欢迎访问网站:https://python-abc.xyz/

  1. import sys
  2. import random
  3. import pygame
  4. from pygame.math import Vector2


  5. class Fruit:
  6.     """水果类"""
  7.     def __init__(self):
  8.         self.randomize()

  9.     def draw(self):
  10.         """绘制水果"""
  11.         rect = pygame.Rect(self.pos.x * cell_size, self.pos.y * cell_size, cell_size, cell_size)
  12.         pygame.draw.rect(screen, fruit_color, rect)

  13.     def randomize(self):
  14.         """随机位置"""
  15.         self.x = random.randint(0, cell_number - 1)
  16.         self.y = random.randint(0, cell_number - 1)
  17.         self.pos = Vector2(self.x, self.y)


  18. class Snake:
  19.     """贪吃蛇"""
  20.     def __init__(self):
  21.         self.body = [Vector2(5, 10), Vector2(4, 10), Vector2(3, 10)]
  22.         self.direction = Vector2(1, 0)

  23.     def draw(self):
  24.         """绘制贪吃蛇"""
  25.         for block in self.body:
  26.             x_pos = int(block.x * cell_size)
  27.             y_pos = int(block.y * cell_size)
  28.             rect = pygame.Rect(x_pos, y_pos, cell_size, cell_size)
  29.             pygame.draw.rect(screen, snake_color, rect)

  30.     def move(self):
  31.         """移动"""
  32.         self.eat()
  33.         self.body.pop()

  34.     def eat(self):
  35.         """吃到水果"""
  36.         self.body.insert(0, self.body[0] + self.direction)


  37. class Main:
  38.     def __init__(self):
  39.         self.snake = Snake()
  40.         self.fruit = Fruit()

  41.     def update(self):
  42.         self.snake.move()
  43.         self.check_eat()
  44.         self.check_fail()

  45.     def draw_elements(self):
  46.         self.fruit.draw()
  47.         self.snake.draw()

  48.     def check_eat(self):
  49.         """判断是否吃到了水果"""
  50.         if self.fruit.pos == self.snake.body[0]:
  51.             self.fruit.randomize()
  52.             self.snake.eat()

  53.     def check_fail(self):
  54.         """失败检测
  55.         1. 蛇头撞墙失败
  56.         2. 蛇头吃到自己失败
  57.         """
  58.         if not 0 <= self.snake.body[0].x < cell_number or not 0 <= self.snake.body[0].y < cell_number:
  59.             print('撞墙了')
  60.             self.game_over()

  61.         for block in self.snake.body[1:]:
  62.             if block == self.snake.body[0]:
  63.                 print('吃到自己了')
  64.                 self.game_over()

  65.     @staticmethod
  66.     def game_over():
  67.         pygame.quit()
  68.         sys.exit()


  69. screen_color = (175, 215, 70)
  70. fruit_color = (126, 166, 114)
  71. snake_color = (183, 111, 122)
  72. cell_size = 40
  73. cell_number = 20

  74. pygame.init()
  75. screen = pygame.display.set_mode((cell_number * cell_size, cell_number * cell_size))
  76. clock = pygame.time.Clock()

  77. SCREEN_UPDATE = pygame.USEREVENT
  78. pygame.time.set_timer(SCREEN_UPDATE, 200)

  79. main = Main()

  80. while True:
  81.     for event in pygame.event.get():
  82.         if event.type == pygame.QUIT:
  83.             main.game_over()
  84.         if event.type == SCREEN_UPDATE:
  85.             main.update()
  86.         if event.type == pygame.KEYDOWN:
  87.             if event.key == pygame.K_UP:
  88.                 if main.snake.direction.y != 1:
  89.                     main.snake.direction = Vector2(0, -1)
  90.             if event.key == pygame.K_RIGHT:
  91.                 if main.snake.direction.x != -1:
  92.                     main.snake.direction = Vector2(1, 0)
  93.             if event.key == pygame.K_DOWN:
  94.                 if main.snake.direction.y != -1:
  95.                     main.snake.direction = Vector2(0, 1)
  96.             if event.key == pygame.K_LEFT:
  97.                 if main.snake.direction.x != 1:
  98.                     main.snake.direction = Vector2(-1, 0)

  99.     screen.fill(screen_color)
  100.     main.draw_elements()
  101.     pygame.display.update()
  102.     clock.tick(60)
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表