├── LICENSE ├── README.md └── Snake Game.py /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 Abbireddy Venkata Chandu 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | **Snake Game in Python** 3 | 4 | This project implements a classic snake game using Python. It offers a fun way to learn or practice fundamental programming concepts like game loops, object-oriented programming, and user interaction. 5 | 6 | **Features** 7 | 8 | * **Basic snake mechanics:** Control the snake's movement using arrow keys (up, down, left, right) to grow it by eating food. 9 | * **Random food generation:** Food appears at random locations on the game screen, encouraging strategic movements. 10 | * **Scorekeeping:** Your score increases with each food item eaten. 11 | * **Game over detection:** The game ends if the snake collides with itself, the walls, or reaches a maximum length. 12 | * **Optional features (consider implementing based on your preferences):** 13 | * Adjustable difficulty levels: Increase game speed or change the rate of food generation for a more challenging experience. 14 | * Power-ups: Introduce temporary effects like speed boosts or changes in snake direction to add variety. 15 | * Visual enhancements: Customize the game window size, colors, or include background music/sound effects. 16 | 17 | **Requirements** 18 | 19 | * Python 3.x (download from [https://www.python.org/downloads/](https://www.google.com/url?sa=E&source=gmail&q=https://www.python.org/downloads/)) 20 | * Pygame library (install using `pip install pygame`) 21 | 22 | **Instructions** 23 | 24 | 1. **Clone or download** this repository. 25 | 2. **Open a terminal** (Command Prompt on Windows) and navigate to the project directory. 26 | 3. **Run the game:** Type `python snake_game.py` (replace with the actual filename if different). 27 | 28 | **Controls** 29 | 30 | * Arrow keys (up, down, left, right): Control the snake's movement. 31 | 32 | **Gameplay** 33 | 34 | * The snake starts small and grows longer by eating food (represented by a square). 35 | * Avoid hitting the walls or your own body, or the game will end. 36 | * Your score increases with each eaten food item. 37 | 38 | **Additional Notes** 39 | 40 | * This is a basic implementation of the snake game. Feel free to extend it by adding more features or customizing the gameplay mechanics. 41 | * For beginners, consider starting with the basic code and gradually add complexity. Many online tutorials can guide you through the process. 42 | 43 | **Contributing** 44 | 45 | We welcome pull requests and suggestions for improvement. Feel free to fork the repository and submit your changes. 46 | 47 | **License** 48 | 49 | This project is licensed under the MIT License (see LICENSE file for details). 50 | 51 | This improved README.md incorporates the following enhancements: 52 | 53 | * **Clear and concise structure:** Provides an easy-to-follow overview of the game, features, requirements, instructions, controls, gameplay, additional notes, contribution guidelines, and licensing. 54 | * **Optional features:** Suggests additional elements to enhance the game experience, while leaving the implementation up to the developer's choice. 55 | * **Flexibility for customization:** Encourages users to personalize the game based on their preferences. 56 | * **Beginner-friendly:** Offers a starting point for beginners with suggestions for learning resources. 57 | * **Contribution guidelines:** Makes it easy for others to contribute to the project. 58 | * **Comprehensive details:** Covers all essential aspects of the game and its development. 59 | -------------------------------------------------------------------------------- /Snake Game.py: -------------------------------------------------------------------------------- 1 | # Sanke Game 2 | import turtle 3 | import time 4 | import random 5 | 6 | delay = 0.1 7 | 8 | # Score 9 | score = 0 10 | high_score = 0 11 | 12 | wn = turtle.Screen() 13 | wn.title("Snake Game ") 14 | wn.bgcolor("green") 15 | wn.setup(width = 600, height = 600) 16 | wn.tracer(0) # Turns off the screen updates 17 | 18 | # Snake Head 19 | 20 | head = turtle.Turtle() 21 | head.speed(0) 22 | head.shape("square") 23 | head.color("black") 24 | head.penup() 25 | head.goto(0,0) 26 | head.direction = "stop" 27 | 28 | #Snake Food 29 | 30 | food = turtle.Turtle() 31 | food.speed(0) 32 | food.shape("circle") 33 | food.color("red") 34 | food.penup() 35 | food.goto(0,100) 36 | 37 | segments = [] 38 | 39 | # Pen 40 | pen = turtle.Turtle() 41 | pen.speed(0) 42 | pen.shape("square") 43 | pen.color("white") 44 | pen.hideturtle() 45 | pen.goto(0,260) 46 | pen.write("Score : 0 High Score : 0", align = "center", font = ("courier" , 24 , "normal")) 47 | 48 | 49 | 50 | 51 | # Functions 52 | 53 | def go_up(): 54 | if head.direction != "dowm": 55 | head.direction = "up" 56 | 57 | def go_down(): 58 | if head.direction != "up": 59 | head.direction = "down" 60 | 61 | def go_left(): 62 | if head.direction != "right": 63 | head.direction = "left" 64 | 65 | def go_right(): 66 | if head.direction != "left": 67 | head.direction = "right" 68 | 69 | def move(): 70 | if head.direction == "up": 71 | y = head.ycor() 72 | head.sety(y+20) 73 | if head.direction == "down": 74 | y = head.ycor() 75 | head.sety(y-20) 76 | if head.direction == "left": 77 | x = head.xcor() 78 | head.setx(x-20) 79 | if head.direction == "right": 80 | x = head.xcor() 81 | head.setx(x+20) 82 | 83 | # Keyboard blindings 84 | wn.listen() 85 | wn.onkeypress(go_up,"w") 86 | wn.onkeypress(go_down,"s") 87 | wn.onkeypress(go_left,"a") 88 | wn.onkeypress(go_right,"d") 89 | 90 | # Main game loop 91 | while True: 92 | wn.update() 93 | 94 | # Check for a collison with the border 95 | if head.xcor()>290 or head.xcor()<-290 or head.ycor()>290 or head.ycor()<-290: 96 | time.sleep(1) 97 | head.goto(0,0) 98 | head.direction = "stop" 99 | 100 | # Hide the segment 101 | for segment in segments: 102 | segment.goto(1000,1000) 103 | 104 | # Clear the segements list 105 | segments.clear() 106 | 107 | # Reset the score 108 | score = 0 109 | 110 | # Reset the Delay 111 | delay = 0.1 112 | 113 | 114 | 115 | pen.clear() 116 | pen.write("score : {} High Score: {}".format(score,high_score), align="center" , font = ("courier" , 24,"normal")) 117 | 118 | 119 | 120 | 121 | 122 | # Check for a coolision with the food 123 | 124 | if head.distance(food) < 20: 125 | #Move the food to a random spot 126 | x = random.randint(-290,290) 127 | y = random.randint(-290,290) 128 | food.goto(x,y) 129 | 130 | # Add a segment 131 | new_segment = turtle.Turtle() 132 | new_segment.speed(0) 133 | new_segment.shape("square") 134 | new_segment.color("grey") 135 | new_segment.penup() 136 | segments.append(new_segment) 137 | 138 | # Shorten the Delay 139 | delay -= 0.001 140 | 141 | # Increase the score 142 | score += 10 143 | 144 | if score > high_score : 145 | high_score = score 146 | 147 | pen.clear() 148 | pen.write("score : {} High Score: {}".format(score,high_score),align="center" , font = ("courier" , 24,"normal")) 149 | 150 | 151 | 152 | # Move the end segments first in reverse order 153 | for index in range(len(segments)-1,0,-1): 154 | x = segments[index-1].xcor() 155 | y = segments[index-1].ycor() 156 | segments[index].goto(x,y) 157 | 158 | # Move segments 0 to where the head is 159 | if len(segments) > 0: 160 | 161 | x = head.xcor() 162 | y= head.ycor() 163 | segments[0].goto(x,y) 164 | 165 | 166 | 167 | move() 168 | 169 | # Check for head collision with the body segments 170 | for segment in segments: 171 | if segment.distance(head) < 20: 172 | time.sleep(1) 173 | head.goto(0,0) 174 | head.direction = "stop" 175 | 176 | # Hide the segment 177 | for segment in segments: 178 | segment.goto(1000,1000) 179 | 180 | # Clear the segements list 181 | segments.clear() 182 | 183 | # Reset the Score 184 | score = 0 185 | 186 | # Reset the Delay 187 | delay = 0.1 188 | 189 | 190 | # Update the score Display 191 | pen.clear() 192 | pen.write("score : {} High Score: {}".format(score,high_score),align="center" , font = ("courier" , 24,"normal")) 193 | 194 | 195 | 196 | 197 | 198 | time.sleep(delay) 199 | 200 | 201 | 202 | 203 | wn.mainloop() 204 | 205 | --------------------------------------------------------------------------------