Skip to content

added snake game#3203

Open
vivek2004-sec wants to merge 1 commit into
geekcomputers:masterfrom
vivek2004-sec:dice_roller
Open

added snake game#3203
vivek2004-sec wants to merge 1 commit into
geekcomputers:masterfrom
vivek2004-sec:dice_roller

Conversation

@vivek2004-sec

Copy link
Copy Markdown
Contributor

Description

This PR adds a classic Snake game implementation using tkinter. The player controls a snake using the arrow keys, guiding it around a canvas to eat randomly-placed food. Each time the snake eats food, it grows longer and the score increases; the game ends if the snake collides with the canvas walls or its own body.
Core components:

Snake class — initializes the snake's starting body segments (as coordinates and canvas rectangles)
Food class — spawns food at a random grid-aligned position on the canvas
next_turn() — the main game loop: moves the snake each tick based on current direction, checks for food collision (grow + increment score) or normal movement (remove tail segment), checks for game-ending collisions, and re-schedules itself via window.after()
change_direction() — handles arrow key input, with a safeguard preventing the snake from reversing directly into itself (e.g., can't go left while moving right)
check_collisions() — detects wall collisions and self-collisions
game_over() — clears the canvas and displays a "GAME OVER" message
The game window is also centered on the screen at launch using screen/window dimension calculations.
Problem it solves: Adds a complete, playable, classic arcade game to the repository as an example of tkinter canvas manipulation, game loops using window.after(), and simple 2D collision detection — useful as a learning reference for GUI-based game development in Python.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update
  • Code cleanup/refactoring

Related Issues

Closes #(issue number)

Testing

Please describe the tests you've run to verify your changes:

  • Manual testing
  • Added automated tests
  • Existing tests pass

Checklist

  • My code follows the project's code style
  • I have commented my code, particularly in hard-to-understand areas
  • I have updated the README.md if needed
  • My changes generate no new warnings
  • I have tested my code locally
  • New and existing tests pass with my changes

Screenshots (if applicable)

Add screenshots or demos of your changes if relevant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant