一、绪论
贪吃蛇游戏。
已实现功能:
1、上下左右移动;
2、吃食物,随机生成食物;
3、碰撞检测,判断是否游戏结束。
二、代码分享
1、main.py
import pygame
import sys
import food as c_food
import snake as c_snakedef 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活着,1diebody_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 = 0elif event.key == pygame.K_DOWN and move_dir != 0:move_dir = 1elif event.key == pygame.K_LEFT and move_dir != 3:move_dir = 2elif event.key == pygame.K_RIGHT and move_dir != 2:move_dir = 3elif 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, 0whether_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 pygameclass snake(object):def __init__(self):self.snake_color = pygame.Color(255, 255, 255)passdef move_step(self, not_body_list, body_list, food_list, move_dir, grid_num):whether_eat = 0whether_die = 0head = body_list[0].copy()if move_dir == 0:head[1] -= 1elif move_dir == 1:head[1] += 1elif move_dir == 2:head[0] -= 1elif move_dir == 3:head[0] += 1whether_die = self.hit_die(body_list, head, grid_num)if whether_die == 1:return not_body_list, body_list, whether_eat, whether_diewhether_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_diedef eat_food(self, food_list, head):# whether eat foodif head == food_list:return 1else:return 0def 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_listdef 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 wallif head[0] <= 0 or head[0] > grid_num[0]:return 1if head[1] <= 0 or head[1] > grid_num[1]:return 1# hit itselfif head in body_list:return 1return 0
3、food.py
import pygame
import randomclass food(object):def __init__(self):self.food_color = pygame.Color(255, 0, 0)passdef 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 = 0return whether_eat, food_listdef 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)