diff --git a/贪吃蛇.py b/贪吃蛇.py new file mode 100644 index 0000000..87282f0 --- /dev/null +++ b/贪吃蛇.py @@ -0,0 +1,168 @@ +import pygame +import random +import copy + +# 蛇 初始位置刷新点 +snake_list = [[10, 10]] + +# 移动 初始向下 定义四个移动方向 +move_up = False +move_down = True +move_left = False +move_right = False +# 食物位置刷新点 +food_point = [random.randint(10, 490), random.randint(10, 490)] + +# 游戏状态 +is_game_over = False +is_game_started = False + +# 游戏计时器 +game_duration = 60 # 游戏时长,单位为秒 +start_time = 0 # 游戏开始时间 + +"""初始化游戏""" +pygame.init() +# fps刷新 +clock = pygame.time.Clock() +# 设置屏幕大小 +screen = pygame.display.set_mode((500, 500)) +# 游戏名称 +pygame.display.set_caption('贪吃蛇小游戏') + +# 游戏开始界面 +font = pygame.font.SysFont(None, 36) +start_text = font.render("Press Enter to Start Game", True, (255, 255, 255)) +start_text_rect = start_text.get_rect(center=(250, 250)) + +# 结束界面文本 +game_over_font = pygame.font.SysFont(None, 48) +game_over_text = game_over_font.render("Game Over", True, (255, 0, 0)) +game_over_text_rect = game_over_text.get_rect(center=(250, 250)) + +# 倒计时文本 +countdown_font = pygame.font.SysFont(None, 48) + +"""进入游戏,玩游戏""" +# 设置游戏开关 +running = True +while running: + # 设置fps + clock.tick(20) + + if is_game_started and not is_game_over: + # 绘制屏幕颜色 + screen.fill([255, 255, 255]) + # 用键盘实现移动 + for event in pygame.event.get(): + # 判断是否按键盘 + if event.type == pygame.KEYDOWN: + if event.key == pygame.K_UP: + move_up = True + move_down = False + move_left = False + move_right = False + if event.key == pygame.K_DOWN: + move_up = False + move_down = True + move_left = False + move_right = False + if event.key == pygame.K_LEFT: + move_up = False + move_down = False + move_left = True + move_right = False + if event.key == pygame.K_RIGHT: + move_up = False + move_down = False + move_left = False + move_right = True + # 绘制食物 + food_rect = pygame.draw.circle(screen, [255, 0, 0], food_point, 15, 0) + + # 绘制蛇 + snake_rect = [] + for snake_point in snake_list: + snake_rect.append(pygame.draw.circle(screen, [255, 0, 0], snake_point, 5, 0)) + #检测蛇吃果子 + if food_rect.collidepoint(snake_point): + #蛇吃果子 + snake_list.append(food_point) + #重新绘制食物 + food_point = [random.randint(10, 490), random.randint(10, 490)] + food_rect =pygame.draw.circle(screen, [255, 0, 0], food_point, 15, 0) + break + """游戏进行""" + snake_len = len(snake_list) - 1 + + # 移动身子 + while snake_len > 0: + snake_list[snake_len] = copy.deepcopy(snake_list[snake_len - 1]) + snake_len -= 1 + + # 移动蛇头 + if move_up: + snake_list[snake_len][1] -= 10 + if snake_list[snake_len][1] < 0: + snake_list[snake_len][1] = 500 + if move_down: + snake_list[snake_len][1] += 10 + if snake_list[snake_len][1] > 500: + snake_list[snake_len][1] = 0 + if move_left: + snake_list[snake_len][0] -= 10 + if snake_list[snake_len][0] < 0: + snake_list[snake_len][0] = 500 + if move_right: + snake_list[snake_len][0] += 10 + if snake_list[snake_len][0] > 500: + snake_list[snake_len][0] = 0 + #蛇不能撞自己 + head_rect =snake_rect[0] + count = len(snake_rect) + while count > 1: + if head_rect.colliderect(snake_rect[count-1]): + running =False + count -= 1 + # 计算剩余时间 + elapsed_time = pygame.time.get_ticks() - start_time + remaining_time = game_duration - int(elapsed_time / 1000) + + # 渲染剩余时间到屏幕 + countdown_text = countdown_font.render(f"Time: {remaining_time}", True, (0, 0, 0)) + screen.blit(countdown_text, (10, 10)) + + # 检查游戏时间是否超过一分钟 + if remaining_time <= 0: + is_game_over = True + + # 实现绘制内容展示 + pygame.display.update() + elif is_game_over: + # 游戏结束界面 + screen.fill((0, 0, 0)) + screen.blit(game_over_text, game_over_text_rect) + pygame.display.update() + else: + # 游戏开始界面 + screen.fill((0, 0, 0)) + screen.blit(start_text, start_text_rect) + pygame.display.update() + + for event in pygame.event.get(): + if event.type == pygame.QUIT: + running = False + if event.type == pygame.KEYDOWN: + if event.key == pygame.K_RETURN: + if is_game_over: + # 重置游戏状态 + is_game_over = False + is_game_started = False + snake_list = [[10, 10]] + food_point = [random.randint(10, 490), random.randint(10, 490)] + start_time = pygame.time.get_ticks() # 重置游戏计时器 + else: + is_game_started = True + start_time = pygame.time.get_ticks() # 重置游戏计时器 + +pygame.quit() \ No newline at end of file