|
| 1 | +""" |
| 2 | +This is the main file that runs the Snake game. |
| 3 | +It handles screen setup, dynamic boundaries, UI controls (buttons), |
| 4 | +game state management, and the main game loop. |
| 5 | +""" |
| 6 | +from turtle import Screen, Turtle |
| 7 | +from snake import Snake |
| 8 | +from food import Food |
| 9 | +from scoreboard import Scoreboard |
| 10 | +from wall import Wall |
| 11 | +import colors |
| 12 | + |
| 13 | +# --- CONSTANTS --- |
| 14 | +MOVE_DELAY_MS = 100 # Game speed in milliseconds |
| 15 | + |
| 16 | +# --- GAME STATE --- |
| 17 | +game_state = "start" # Possible states: "start", "playing", "paused", "game_over" |
| 18 | + |
| 19 | +# --- SCREEN SETUP --- |
| 20 | +screen = Screen() |
| 21 | +screen.setup(width=0.9, height=0.9) # Set up a nearly fullscreen window |
| 22 | +screen.bgcolor(colors.BG_COLOR) |
| 23 | +screen.title("Interactive Snake Game") |
| 24 | +screen.tracer(0) |
| 25 | + |
| 26 | +# --- DYNAMIC GAME BOUNDARIES --- |
| 27 | +WIDTH = screen.window_width() |
| 28 | +HEIGHT = screen.window_height() |
| 29 | +# These boundaries are calculated to be inside the visible wall with a safe margin |
| 30 | +LEFT_WALL = -WIDTH / 2 + 25 |
| 31 | +RIGHT_WALL = WIDTH / 2 - 25 |
| 32 | +TOP_WALL = HEIGHT / 2 - 85 |
| 33 | +BOTTOM_WALL = -HEIGHT / 2 + 25 |
| 34 | + |
| 35 | +# --- GAME OBJECTS --- |
| 36 | +wall = Wall() |
| 37 | +snake = Snake() |
| 38 | +food = Food() |
| 39 | +# Initial food placement is now handled after boundaries are calculated |
| 40 | +food.refresh(LEFT_WALL, RIGHT_WALL, BOTTOM_WALL, TOP_WALL) |
| 41 | +scoreboard = Scoreboard() |
| 42 | + |
| 43 | +# --- UI CONTROLS (BUTTONS) --- |
| 44 | +buttons = {} # Dictionary to hold button turtles and their properties |
| 45 | + |
| 46 | +def create_button(name, x, y, width=120, height=40): |
| 47 | + """Creates a turtle-based button with a label.""" |
| 48 | + if name in buttons and buttons[name]['turtle'] is not None: |
| 49 | + buttons[name]['turtle'].clear() |
| 50 | + |
| 51 | + button_turtle = Turtle() |
| 52 | + button_turtle.hideturtle() |
| 53 | + button_turtle.penup() |
| 54 | + button_turtle.speed("fastest") |
| 55 | + |
| 56 | + button_turtle.goto(x - width/2, y - height/2) |
| 57 | + button_turtle.color(colors.BUTTON_BORDER_COLOR, colors.BUTTON_BG_COLOR) |
| 58 | + button_turtle.begin_fill() |
| 59 | + for _ in range(2): |
| 60 | + button_turtle.forward(width) |
| 61 | + button_turtle.left(90) |
| 62 | + button_turtle.forward(height) |
| 63 | + button_turtle.left(90) |
| 64 | + button_turtle.end_fill() |
| 65 | + |
| 66 | + button_turtle.goto(x, y - 12) |
| 67 | + button_turtle.color(colors.BUTTON_TEXT_COLOR) |
| 68 | + button_turtle.write(name, align="center", font=("Lucida Sans", 14, "bold")) |
| 69 | + |
| 70 | + buttons[name] = {'turtle': button_turtle, 'x': x, 'y': y, 'w': width, 'h': height, 'visible': True} |
| 71 | + |
| 72 | +def hide_button(name): |
| 73 | + """Hides a button by clearing its turtle.""" |
| 74 | + if name in buttons and buttons[name]['visible']: |
| 75 | + buttons[name]['turtle'].clear() |
| 76 | + buttons[name]['visible'] = False |
| 77 | + |
| 78 | +def manage_buttons(): |
| 79 | + """Shows or hides buttons based on the current game state.""" |
| 80 | + all_buttons = ["Play", "Pause", "Resume", "Restart"] |
| 81 | + for btn_name in all_buttons: |
| 82 | + hide_button(btn_name) |
| 83 | + |
| 84 | + btn_x = WIDTH / 2 - 100 |
| 85 | + btn_y = HEIGHT / 2 - 45 |
| 86 | + |
| 87 | + if game_state == "start": |
| 88 | + create_button("Play", 0, -100) |
| 89 | + elif game_state == "playing": |
| 90 | + create_button("Pause", btn_x, btn_y) |
| 91 | + elif game_state == "paused": |
| 92 | + create_button("Resume", btn_x, btn_y) |
| 93 | + elif game_state == "game_over": |
| 94 | + create_button("Restart", btn_x, btn_y) |
| 95 | + |
| 96 | +# --- GAME LOGIC & STATE TRANSITIONS --- |
| 97 | +def start_game(): |
| 98 | + global game_state |
| 99 | + if game_state == "start": |
| 100 | + game_state = "playing" |
| 101 | + scoreboard.update_scoreboard() |
| 102 | + |
| 103 | +def toggle_pause_resume(): |
| 104 | + global game_state |
| 105 | + if game_state == "playing": |
| 106 | + game_state = "paused" |
| 107 | + scoreboard.display_pause() |
| 108 | + elif game_state == "paused": |
| 109 | + game_state = "playing" |
| 110 | + scoreboard.update_scoreboard() |
| 111 | + |
| 112 | +def restart_game(): |
| 113 | + global game_state |
| 114 | + if game_state == "game_over": |
| 115 | + game_state = "playing" |
| 116 | + snake.reset() |
| 117 | + food.refresh(LEFT_WALL, RIGHT_WALL, BOTTOM_WALL, TOP_WALL) |
| 118 | + scoreboard.reset() |
| 119 | + |
| 120 | +def is_click_on_button(name, x, y): |
| 121 | + """Checks if a click (x, y) is within the bounds of a visible button.""" |
| 122 | + if name in buttons and buttons[name]['visible']: |
| 123 | + btn = buttons[name] |
| 124 | + return (btn['x'] - btn['w']/2 < x < btn['x'] + btn['w']/2 and |
| 125 | + btn['y'] - btn['h']/2 < y < btn['y'] + btn['h']/2) |
| 126 | + return False |
| 127 | + |
| 128 | +def handle_click(x, y): |
| 129 | + """Main click handler to delegate actions based on button clicks.""" |
| 130 | + if game_state == "start" and is_click_on_button("Play", x, y): |
| 131 | + start_game() |
| 132 | + elif game_state == "playing" and is_click_on_button("Pause", x, y): |
| 133 | + toggle_pause_resume() |
| 134 | + elif game_state == "paused" and is_click_on_button("Resume", x, y): |
| 135 | + toggle_pause_resume() |
| 136 | + elif game_state == "game_over" and is_click_on_button("Restart", x, y): |
| 137 | + restart_game() |
| 138 | + |
| 139 | +# --- KEYBOARD HANDLERS --- |
| 140 | +def handle_snake_up(): |
| 141 | + if game_state in ["start", "playing"]: |
| 142 | + start_game() |
| 143 | + snake.up() |
| 144 | +def handle_snake_down(): |
| 145 | + if game_state in ["start", "playing"]: |
| 146 | + start_game() |
| 147 | + snake.down() |
| 148 | +def handle_snake_left(): |
| 149 | + if game_state in ["start", "playing"]: |
| 150 | + start_game() |
| 151 | + snake.left() |
| 152 | +def handle_snake_right(): |
| 153 | + if game_state in ["start", "playing"]: |
| 154 | + start_game() |
| 155 | + snake.right() |
| 156 | + |
| 157 | +# --- KEY & MOUSE BINDINGS --- |
| 158 | +screen.listen() |
| 159 | +screen.onkey(handle_snake_up, "Up") |
| 160 | +screen.onkey(handle_snake_down, "Down") |
| 161 | +screen.onkey(handle_snake_left, "Left") |
| 162 | +screen.onkey(handle_snake_right, "Right") |
| 163 | +screen.onkey(toggle_pause_resume, "space") |
| 164 | +screen.onkey(restart_game, "r") |
| 165 | +screen.onkey(restart_game, "R") |
| 166 | +screen.onclick(handle_click) |
| 167 | + |
| 168 | +# --- MAIN GAME LOOP --- |
| 169 | +def game_loop(): |
| 170 | + global game_state |
| 171 | + if game_state == "playing": |
| 172 | + snake.move() |
| 173 | + # Collision with food |
| 174 | + if snake.head.distance(food) < 20: |
| 175 | + food.refresh(LEFT_WALL, RIGHT_WALL, BOTTOM_WALL, TOP_WALL) |
| 176 | + snake.extend() |
| 177 | + scoreboard.increase_score() |
| 178 | + # Collision with wall |
| 179 | + if not (LEFT_WALL < snake.head.xcor() < RIGHT_WALL and BOTTOM_WALL < snake.head.ycor() < TOP_WALL): |
| 180 | + game_state = "game_over" |
| 181 | + scoreboard.game_over() |
| 182 | + # Collision with tail |
| 183 | + for segment in snake.segments[1:]: |
| 184 | + if snake.head.distance(segment) < 10: |
| 185 | + game_state = "game_over" |
| 186 | + scoreboard.game_over() |
| 187 | + manage_buttons() |
| 188 | + screen.update() |
| 189 | + screen.ontimer(game_loop, MOVE_DELAY_MS) |
| 190 | + |
| 191 | +# --- INITIALIZE GAME --- |
| 192 | +scoreboard.display_start_message() |
| 193 | +game_loop() |
| 194 | +screen.exitonclick() |
| 195 | + |
0 commit comments