├── README.md ├── LICENCE └── snake_game.py /README.md: -------------------------------------------------------------------------------- 1 | # Snake Game 2 | 3 | # Python Command 4 | 5 | # Hazrat Ali 6 | 7 | # Software Engineer & MERN Stack Developer 8 | 9 | 1. # pip install pygame 10 | 2. # pip install --user pygame 11 | 3. # pip install pygame --pre 12 | 4. # pip install pyautogui 13 | 5. # pip install-opencv-contrib-python 14 | 6. # py -m pip list 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /LICENCE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Hazrat Ali 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 | -------------------------------------------------------------------------------- /snake_game.py: -------------------------------------------------------------------------------- 1 | # Using Programming Language Python 2 | # Hazrat Ali 3 | # University Of Scholars. 4 | 5 | import turtle 6 | import time 7 | import random 8 | 9 | delay = 0.1 10 | 11 | # Score 12 | score = 0 13 | high_score = 0 14 | 15 | # Set up the screen 16 | wn = turtle.Screen() 17 | wn.title("Snake Game by @ Hazrat Ali") 18 | wn.bgcolor("Green") 19 | wn.setup(width=600, height=600) 20 | wn.tracer(0) # Turns off the screen updates 21 | 22 | # Snake head 23 | head = turtle.Turtle() 24 | head.speed(0) 25 | head.shape("circle") 26 | head.color("pink") 27 | head.penup() 28 | head.goto(0,0) 29 | head.direction = "stop" 30 | 31 | # Snake food 32 | food = turtle.Turtle() 33 | food.speed(0) 34 | food.shape("circle") 35 | food.color("red") 36 | food.penup() 37 | food.goto(0,100) 38 | 39 | segments = [] 40 | 41 | # Pen 42 | pen = turtle.Turtle() 43 | pen.speed(0) 44 | pen.shape("square") 45 | pen.color("white") 46 | pen.penup() 47 | pen.hideturtle() 48 | pen.goto(0, 260) 49 | pen.write("Score: 0 High Score: 0", align="center", font=("Courier", 24, "normal")) 50 | 51 | # Functions 52 | def go_up(): 53 | if head.direction != "down": 54 | head.direction = "up" 55 | 56 | def go_down(): 57 | if head.direction != "up": 58 | head.direction = "down" 59 | 60 | def go_left(): 61 | if head.direction != "right": 62 | head.direction = "left" 63 | 64 | def go_right(): 65 | if head.direction != "left": 66 | head.direction = "right" 67 | 68 | def move(): 69 | if head.direction == "up": 70 | y = head.ycor() 71 | head.sety(y + 20) 72 | 73 | if head.direction == "down": 74 | y = head.ycor() 75 | head.sety(y - 20) 76 | 77 | if head.direction == "left": 78 | x = head.xcor() 79 | head.setx(x - 20) 80 | 81 | if head.direction == "right": 82 | x = head.xcor() 83 | head.setx(x + 20) 84 | 85 | # Keyboard bindings 86 | wn.listen() 87 | wn.onkeypress(go_up, "w") 88 | wn.onkeypress(go_down, "s") 89 | wn.onkeypress(go_left, "a") 90 | wn.onkeypress(go_right, "d") 91 | 92 | # Main game loop 93 | while True: 94 | wn.update() 95 | 96 | # Check for a collision with the border 97 | if head.xcor()>290 or head.xcor()<-290 or head.ycor()>290 or head.ycor()<-290: 98 | time.sleep(1) 99 | head.goto(0,0) 100 | head.direction = "stop" 101 | 102 | # Hide the segments 103 | for segment in segments: 104 | segment.goto(1000, 1000) 105 | 106 | # Clear the segments list 107 | segments.clear() 108 | 109 | # Reset the score 110 | score = 0 111 | 112 | # Reset the delay 113 | delay = 0.1 114 | 115 | pen.clear() 116 | pen.write("Score: {} High Score: {}".format(score, high_score), align="center", font=("Courier", 24, "normal")) 117 | 118 | 119 | # Check for a collision with the food 120 | if head.distance(food) < 20: 121 | # Move the food to a random spot 122 | x = random.randint(-290, 290) 123 | y = random.randint(-290, 290) 124 | food.goto(x,y) 125 | 126 | # Add a segment 127 | new_segment = turtle.Turtle() 128 | new_segment.speed(0) 129 | new_segment.shape("circle") 130 | new_segment.color("grey") 131 | new_segment.penup() 132 | segments.append(new_segment) 133 | 134 | # Shorten the delay 135 | delay -= 0.001 136 | 137 | # Increase the score 138 | score += 10 139 | 140 | if score > high_score: 141 | high_score = score 142 | 143 | pen.clear() 144 | pen.write("Score: {} High Score: {}".format(score, high_score), align="center", font=("Courier", 24, "normal")) 145 | 146 | # Move the end segments first in reverse order 147 | for index in range(len(segments)-1, 0, -1): 148 | x = segments[index-1].xcor() 149 | y = segments[index-1].ycor() 150 | segments[index].goto(x, y) 151 | 152 | # Move segment 0 to where the head is 153 | if len(segments) > 0: 154 | x = head.xcor() 155 | y = head.ycor() 156 | segments[0].goto(x,y) 157 | 158 | move() 159 | 160 | # Check for head collision with the body segments 161 | for segment in segments: 162 | if segment.distance(head) < 20: 163 | time.sleep(1) 164 | head.goto(0,0) 165 | head.direction = "stop" 166 | 167 | # Hide the segments 168 | for segment in segments: 169 | segment.goto(1000, 1000) 170 | 171 | # Clear the segments list 172 | segments.clear() 173 | 174 | # Reset the score 175 | score = 0 176 | 177 | # Reset the delay 178 | delay = 0.1 179 | 180 | # Update the score display 181 | pen.clear() 182 | pen.write("Score: {} High Score: {}".format(score, high_score), align="center", font=("Courier", 24, "normal")) 183 | 184 | time.sleep(delay) 185 | 186 | wn.mainloop() 187 | --------------------------------------------------------------------------------