diff --git a/game_functions.py b/game_functions.py index 6ac91f0..52e1c99 100644 --- a/game_functions.py +++ b/game_functions.py @@ -59,83 +59,94 @@ def show_end(settings, screen): pygame.time.wait(1500) settings.game_mode = 0 + def draw_grid(settings, screen): - #绘制横向线 - for x in range(0, settings.width, settings.cell_size): - pygame.draw.line(screen,(40,40,40),(x,0),(x,settings.height)) - #绘制竖向线 - for y in range(0, settings.height, settings.cell_size): - pygame.draw.line(screen,(40,40,40),(0, y),(settings.width,y)) - + # 绘制横向线 + for x in range(0, settings.width, settings.cell_size): + pygame.draw.line(screen, (40, 40, 40), (x, 0), (x, settings.height)) + # 绘制竖向线 + for y in range(0, settings.height, settings.cell_size): + pygame.draw.line(screen, (40, 40, 40), (0, y), (settings.width, y)) + + def update_screen(settings, screen): - screen.fill(settings.bg_color) - draw_grid(settings, screen) + screen.fill(settings.bg_color) + draw_grid(settings, screen) + +# 绘制蛇 + -#绘制蛇 def draw_snake(ai_settings, screen, snake): - #头部用白色 - x = snake.coords[0]['x'] * ai_settings.cell_size - y = snake.coords[0]['y'] * ai_settings.cell_size - snake_head_rect = pygame.Rect(x, y, ai_settings.cell_size, ai_settings.cell_size) - pygame.draw.rect(screen,(255,255,255), snake_head_rect) - #蛇身内部用浅绿,外框用深绿 - for coord in snake.coords[1: -1]: - x = coord['x'] * ai_settings.cell_size - y = coord['y'] * ai_settings.cell_size - snake_part_rect = pygame.Rect(x, y, ai_settings.cell_size, ai_settings.cell_size) - pygame.draw.rect(screen,(0,155,0), snake_part_rect) - snake_part_inner_rect = pygame.Rect(x + 4, y + 4, ai_settings.cell_size - 8, ai_settings.cell_size - 8) - pygame.draw.rect(screen,(0,255,0), snake_part_inner_rect) - #蛇尾用浅绿 - coord = snake.coords[-1] - x = coord['x'] * ai_settings.cell_size - y = coord['y'] * ai_settings.cell_size - snake_tail_rect = pygame.Rect(x, y, ai_settings.cell_size, ai_settings.cell_size) - pygame.draw.rect(screen,(0,255,0), snake_tail_rect) - -#绘制游戏界面 + # 头部用白色 + x = snake.coords[0]['x'] * ai_settings.cell_size + y = snake.coords[0]['y'] * ai_settings.cell_size + snake_head_rect = pygame.Rect( + x, y, ai_settings.cell_size, ai_settings.cell_size) + pygame.draw.rect(screen, (255, 255, 255), snake_head_rect) + # 蛇身内部用浅绿,外框用深绿 + for coord in snake.coords[1: -1]: + x = coord['x'] * ai_settings.cell_size + y = coord['y'] * ai_settings.cell_size + snake_part_rect = pygame.Rect( + x, y, ai_settings.cell_size, ai_settings.cell_size) + pygame.draw.rect(screen, (0, 155, 0), snake_part_rect) + snake_part_inner_rect = pygame.Rect( + x + 4, y + 4, ai_settings.cell_size - 8, ai_settings.cell_size - 8) + pygame.draw.rect(screen, (0, 255, 0), snake_part_inner_rect) + # 蛇尾用浅绿 + coord = snake.coords[-1] + x = coord['x'] * ai_settings.cell_size + y = coord['y'] * ai_settings.cell_size + snake_tail_rect = pygame.Rect( + x, y, ai_settings.cell_size, ai_settings.cell_size) + pygame.draw.rect(screen, (0, 255, 0), snake_tail_rect) + +# 绘制游戏界面 + + def update_screen(settings, screen, snake): - screen.fill(settings.bg_color) - draw_grid(settings, screen) - #移动蛇 - del snake.coords[-1]#加蛇头 - snake.update()#去蛇尾 - if not is_game_over(settings, snake): - settings.game_mode = -1 - else: - draw_snake(settings, screen, snake) - pygame.display.flip() - #暂停一下 - settings.my_clock.tick(settings.clock_frq) + screen.fill(settings.bg_color) + draw_grid(settings, screen) + # 移动蛇 + del snake.coords[-1] # 加蛇头 + snake.update() # 去蛇尾 + if not is_game_over(settings, snake): + settings.game_mode = -1 + else: + draw_snake(settings, screen, snake) + pygame.display.flip() + # 暂停一下 + settings.my_clock.tick(settings.clock_frq) -def is_game_over(settings,snake): - #碰到左右墙壁 - if (snake.coords[snake.head_index]['x'] == -1 or snake.coords[snake.head_index]['x'] == settings.cell_w): - return False - #碰到上下墙壁 - if(snake.coords[snake.head_index]['y'] == -1 or snake.coords[snake.head_index]['y'] == settings.cell_h): - return False - #碰到自己 - if(snake.coords[snake.head_index] in snake.coords[1:]): - return False - return True - -#检测游戏过程中的按键 +def is_game_over(settings, snake): + # 碰到左右墙壁 + if (snake.coords[snake.head_index]['x'] == -1 or snake.coords[snake.head_index]['x'] == settings.cell_w): + return False + # 碰到上下墙壁 + if (snake.coords[snake.head_index]['y'] == -1 or snake.coords[snake.head_index]['y'] == settings.cell_h): + return False + # 碰到自己 + if (snake.coords[snake.head_index] in snake.coords[1:]): + return False + return True + + +# 检测游戏过程中的按键 def check_play_events(snake): - for event in pygame.event.get(): - if event.type == pygame.QUIT: - pygame.quit() - sys.exit() - elif event.type == pygame.KEYDOWN: - if event.key == pygame.K_ESCAPE: - pygame.quit() - sys.exit() - elif event.key == pygame.K_LEFT and not snake.direction == 'right': - snake.direction = 'left' - elif event.key == pygame.K_RIGHT and not snake.direction == 'left': - snake.direction = 'right' - elif event.key == pygame.K_UP and not snake.direction == 'down': - snake.direction = 'up' - elif event.key == pygame.K_DOWN and not snake.direction == 'up': - snake.direction = 'down' \ No newline at end of file + for event in pygame.event.get(): + if event.type == pygame.QUIT: + pygame.quit() + sys.exit() + elif event.type == pygame.KEYDOWN: + if event.key == pygame.K_ESCAPE: + pygame.quit() + sys.exit() + elif event.key == pygame.K_LEFT and not snake.direction == 'right': + snake.direction = 'left' + elif event.key == pygame.K_RIGHT and not snake.direction == 'left': + snake.direction = 'right' + elif event.key == pygame.K_UP and not snake.direction == 'down': + snake.direction = 'up' + elif event.key == pygame.K_DOWN and not snake.direction == 'up': + snake.direction = 'down' diff --git a/main.py b/main.py index 4b622f4..cb87385 100644 --- a/main.py +++ b/main.py @@ -2,29 +2,32 @@ import pygame from settings import Settings import game_functions as gf from snake import Snake - + settings = Settings() -#设置屏幕 +# 设置屏幕 screen = pygame.display.set_mode((settings.width, settings.height)) -#添加游戏标题 +# 添加游戏标题 pygame.display.set_caption('贪吃蛇') -#创建蛇 +# 创建蛇 snake = Snake(settings) - + + def run_game1(): - gf.check_play_events(snake) - gf.update_screen(settings, screen, snake) - + gf.check_play_events(snake) + gf.update_screen(settings, screen, snake) + + def run_game(): - while True: - if settings.game_mode == 0: - gf.show_start(settings,screen) - elif settings.game_mode == -1: - gf.show_end(settings,screen) - snake.reset(settings) - elif settings.game_mode == 1: - run_game1() - + while True: + if settings.game_mode == 0: + gf.show_start(settings, screen) + elif settings.game_mode == -1: + gf.show_end(settings, screen) + snake.reset(settings) + elif settings.game_mode == 1: + run_game1() + + if __name__ == "__main__": - pygame.init() - run_game() \ No newline at end of file + pygame.init() + run_game() diff --git a/settings.py b/settings.py index e08e948..cd2df09 100644 --- a/settings.py +++ b/settings.py @@ -1,6 +1,7 @@ import pygame import sys + class Settings: def __init__(self): self.bg_color = (0, 0, 0) # 背景颜色 @@ -20,4 +21,4 @@ class Settings: if sys.platform.startswith("linux"): self.font_name = pygame.font.match_font('wenquanyizenhei') else: - self.font_name = pygame.font.match_font('simhei') \ No newline at end of file + self.font_name = pygame.font.match_font('simhei') diff --git a/snake.py b/snake.py index 0e0a2c6..422113c 100644 --- a/snake.py +++ b/snake.py @@ -1,38 +1,39 @@ import random - + + class Snake(): - def __init__(self,settings): - self.reset(settings) - - #蛇的初始化 - def reset(self,settings): - #蛇头的坐标 - self.start_x = random.randint(5, settings.cell_w - 6) - self.start_y = random.randint(5, settings.cell_h - 6) - self.head_index = 0 - #蛇的初始运动方向 - self.direction = 'right' - #蛇的初始坐标字典,初始蛇为蛇头位置及其左边的两个格子 - self.coords = [{'x' : self.start_x, 'y': self.start_y}, - {'x' : self.start_x - 1, 'y': self.start_y}, - {'x' : self.start_x - 2, 'y': self.start_y}] - #更新蛇,每次蛇移动一步,因此相当于每次移动在蛇的坐标字典中再插入一个新蛇头 - #按常识,每次移动后,不仅仅是蛇头向前移动一个,更重要的是蛇尾也要向前移,但是在该函数中先不处理蛇尾 - #因为如果蛇吃了食物,此时蛇尾其实并没有移动 - def update(self): - newHead = {} - #根据移动方向确定蛇头 - if self.direction == 'up': - newHead = {'x' : self.coords[self.head_index]['x'], - 'y' : self.coords[self.head_index]['y'] - 1} - elif self.direction == 'down': - newHead = {'x' : self.coords[self.head_index]['x'], - 'y' : self.coords[self.head_index]['y'] + 1} - elif self.direction == 'left': - newHead = {'x' : self.coords[self.head_index]['x'] - 1, - 'y' : self.coords[self.head_index]['y']} - elif self.direction == 'right': - newHead = {'x' : self.coords[self.head_index]['x'] + 1, - 'y' : self.coords[self.head_index]['y']} - self.coords.insert(0,newHead) - + def __init__(self, settings): + self.reset(settings) + + # 蛇的初始化 + def reset(self, settings): + # 蛇头的坐标 + self.start_x = random.randint(5, settings.cell_w - 6) + self.start_y = random.randint(5, settings.cell_h - 6) + self.head_index = 0 + # 蛇的初始运动方向 + self.direction = 'right' + # 蛇的初始坐标字典,初始蛇为蛇头位置及其左边的两个格子 + self.coords = [{'x': self.start_x, 'y': self.start_y}, + {'x': self.start_x - 1, 'y': self.start_y}, + {'x': self.start_x - 2, 'y': self.start_y}] + # 更新蛇,每次蛇移动一步,因此相当于每次移动在蛇的坐标字典中再插入一个新蛇头 + # 按常识,每次移动后,不仅仅是蛇头向前移动一个,更重要的是蛇尾也要向前移,但是在该函数中先不处理蛇尾 + # 因为如果蛇吃了食物,此时蛇尾其实并没有移动 + + def update(self): + newHead = {} + # 根据移动方向确定蛇头 + if self.direction == 'up': + newHead = {'x': self.coords[self.head_index]['x'], + 'y': self.coords[self.head_index]['y'] - 1} + elif self.direction == 'down': + newHead = {'x': self.coords[self.head_index]['x'], + 'y': self.coords[self.head_index]['y'] + 1} + elif self.direction == 'left': + newHead = {'x': self.coords[self.head_index]['x'] - 1, + 'y': self.coords[self.head_index]['y']} + elif self.direction == 'right': + newHead = {'x': self.coords[self.head_index]['x'] + 1, + 'y': self.coords[self.head_index]['y']} + self.coords.insert(0, newHead)