(四)、python程序--贪吃蛇游戏

一、绪论

贪吃蛇游戏。

已实现功能:

1、上下左右移动;

2、吃食物,随机生成食物;

3、碰撞检测,判断是否游戏结束。

二、代码分享

1、main.py

import pygame
import sys
import food as c_food
import snake as c_snake


def game_over():
    pygame.quit()
    sys.exit()


def game_start():
    window_title = 'Snake-AI'
    window_size = (640, 480)
    grid_num = (16, 12)                  # 64列,48行
    grid_size = (40, 40)

    pygame.init()
    pg_clock = pygame.time.Clock()

    main_window = pygame.display.set_mode(window_size)
    pygame.display.set_caption(window_title)

    FOOD = c_food.food()
    SNAKE = c_snake.snake()

    move_dir = 0                                # 0,1,2,3    上下左右
    whether_eat = 0                             # 0未吃,1吃了
    whether_die = 0                             # 0活着,1die

    body_list, not_body_list = SNAKE.init_body(grid_num)
    whether_eat, food_list = FOOD.make_food(1, not_body_list, [])

    while True:

        for event in pygame.event.get():

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP and move_dir != 1:
                    move_dir = 0

                elif event.key == pygame.K_DOWN and move_dir != 0:
                    move_dir = 1

                elif event.key == pygame.K_LEFT and move_dir != 3:
                    move_dir = 2

                elif event.key == pygame.K_RIGHT and move_dir != 2:
                    move_dir = 3

            elif event.type == pygame.QUIT:
                game_over()

        not_body_list, body_list, whether_eat, whether_die = SNAKE.move_step(not_body_list, body_list, food_list, move_dir, grid_num)

        if whether_die == 1:
            body_list, not_body_list = SNAKE.init_body(grid_num)
            whether_eat, food_list = FOOD.make_food(1, not_body_list, [])
            whether_eat, whether_die = 0, 0

        whether_eat, food_list = FOOD.make_food(whether_eat, not_body_list, food_list)

        main_window.fill((0, 0, 0))

        FOOD.draw_food(food_list, main_window, grid_size)
        SNAKE.draw_body(body_list, main_window, grid_size)

        pygame.display.update()
        pg_clock.tick(5)


if __name__ == '__main__':
    game_start()

2、snake.py

import pygame


class snake(object):

    def __init__(self):
        self.snake_color = pygame.Color(255, 255, 255)
        pass


    def move_step(self, not_body_list, body_list, food_list, move_dir, grid_num):
        whether_eat = 0
        whether_die = 0
        head = body_list[0].copy()
        if move_dir == 0:
            head[1] -= 1
        elif move_dir == 1:
            head[1] += 1
        elif move_dir == 2:
            head[0] -= 1
        elif move_dir == 3:
            head[0] += 1

        whether_die = self.hit_die(body_list, head, grid_num)
        if whether_die == 1:
            return not_body_list, body_list, whether_eat, whether_die

        whether_eat = self.eat_food(food_list, head)
        body_list.insert(0, head)
        not_body_list.remove(head)
        if whether_eat == 0:
            not_body_list.append(body_list[-1])
            body_list.pop()
        return not_body_list, body_list, whether_eat, whether_die


    def eat_food(self, food_list, head):
        # whether eat food
        if head == food_list:
            return 1
        else:
            return 0


    def init_body(self, grid_num):
        body_list = [[int(grid_num[0]/2), int(grid_num[1]/2)]]  # 蛇的身体头在前,列行
        not_body_list = [[i + 1, j + 1] for i in range(grid_num[0]) for j in range(grid_num[1])]
        not_body_list.remove(body_list[0])
        return body_list, not_body_list


    def draw_body(self, body_list, window, grid_size):
        for index in body_list:
            x = (index[0] - 1) * grid_size[0]
            y = (index[1] - 1) * grid_size[1]
            rec = (x, y, grid_size[0]-1, grid_size[1]-1)
            pygame.draw.rect(window, self.snake_color, rec)


    def hit_die(self, body_list, head, grid_num):
        # hit wall
        if head[0] <= 0 or head[0] > grid_num[0]:
            return 1
        if head[1] <= 0 or head[1] > grid_num[1]:
            return 1
        # hit itself
        if head in body_list:
            return 1
        return 0

3、food.py

import pygame
import random


class food(object):

    def __init__(self):
        self.food_color = pygame.Color(255, 0, 0)
        pass


    def make_food(self, whether_eat, not_body_list, food_list):
        if whether_eat == 1:
            position = random.randrange(1, len(not_body_list))
            food_list = not_body_list[position]
            whether_eat = 0
        return whether_eat, food_list


    def draw_food(self, food_list, window, grid_size):
        x = (food_list[0] - 1) * grid_size[0]
        y = (food_list[1] - 1) * grid_size[1]
        rec = (x, y, grid_size[0], grid_size[1])
        pygame.draw.rect(window, self.food_color, rec)

相关推荐

  1. python制作贪吃游戏

    2024-07-12 02:06:03       54 阅读
  2. 贪吃游戏

    2024-07-12 02:06:03       51 阅读
  3. 贪吃游戏

    2024-07-12 02:06:03       44 阅读
  4. 贪吃游戏

    2024-07-12 02:06:03       43 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-12 02:06:03       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-12 02:06:03       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-12 02:06:03       58 阅读
  4. Python语言-面向对象

    2024-07-12 02:06:03       69 阅读

热门阅读

  1. 北京大学教育评论

    2024-07-12 02:06:03       24 阅读
  2. leetcode秋招冲刺 (专题16--18)

    2024-07-12 02:06:03       21 阅读
  3. 日常的网络杂记

    2024-07-12 02:06:03       19 阅读
  4. 设计模式之单例模式

    2024-07-12 02:06:03       23 阅读
  5. 软件架构之测评方法

    2024-07-12 02:06:03       16 阅读
  6. Webpack打包生产环境进行优化处理

    2024-07-12 02:06:03       21 阅读
  7. 【深度学习】关于模型加速

    2024-07-12 02:06:03       23 阅读
  8. k8s 部署RuoYi-Vue-Plus之mysql搭建

    2024-07-12 02:06:03       23 阅读
  9. 大数据面试题之Hudi(1)

    2024-07-12 02:06:03       19 阅读
  10. ES6 Iterator 与 for...of 循环(五)

    2024-07-12 02:06:03       24 阅读
  11. 对素数的一种新理解

    2024-07-12 02:06:03       22 阅读
  12. 力扣 454四数相加

    2024-07-12 02:06:03       21 阅读
  13. 十大排序算法(慢慢更新)

    2024-07-12 02:06:03       23 阅读
  14. 简谈设计模式之建造者模式

    2024-07-12 02:06:03       18 阅读