Skip to content

Commit b1a780b

Browse files
Merge pull request #1602 from Akanksha-202/script
Added the snake game
2 parents c77a97d + 05be6b4 commit b1a780b

File tree

2 files changed

+216
-0
lines changed

2 files changed

+216
-0
lines changed

TheSnakeGame/Readme.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# The Snake Game
2+
3+
The Snake Game is a classic arcade game where the player controls a snake, guiding it to eat food and grow longer. The objective is to achieve the highest score possible without colliding with the snake's own body or the boundaries of the game.
4+
5+
## Game Description
6+
In this version of the Snake Game, you control the snake using the arrow keys on your keyboard. The snake moves in four directions: up, down, left, and right. The snake's body consists of green squares, and the food is represented by a red oval. Each time the snake eats the food, it grows longer, and your score increases. The game ends when the snake collides with itself or hits the game boundaries.
7+
8+
## Setup Instructions
9+
To run the Snake Game, follow these steps:
10+
11+
1. Make sure you have Python installed on your system (Python 3.7 or above).
12+
2. Install the required libraries by running the following command in your terminal:
13+
```
14+
pip install tkinter
15+
```
16+
3. Save the game code provided into a file named `snake_game.py`.
17+
4. Open a terminal or command prompt and navigate to the directory where the `snake_game.py` file is saved.
18+
5. Run the following command to start the game:
19+
```
20+
python snake_game.py
21+
```
22+
6. The game window will appear, and you can start playing by using the arrow keys to control the snake's direction.
23+
7. Avoid colliding with the snake's own body or the boundaries of the game.
24+
8. Whenever the game ends, a "GAME OVER" message will be displayed, and you can restart the game by clicking the "Restart" button.
25+
26+
## Customization Options
27+
You can customize various aspects of the game by modifying the following constants in the code:
28+
29+
- `GAME_WIDTH`: Width of the game window in pixels.
30+
- `GAME_HEIGHT`: Height of the game window in pixels.
31+
- `SPEED`: Speed of the snake's movement (lower values make the game faster).
32+
- `SPACE_SIZE`: Size of each square space in pixels.
33+
- `BODY_PARTS`: Initial length of the snake.
34+
- `SNAKE_COLOUR`: Color of the snake's body (in hexadecimal format).
35+
- `FOOD_COLOUR`: Color of the food (in hexadecimal format).
36+
- `BACKGROUND_COLOUR`: Color of the game window background (in hexadecimal format).
37+
38+
Feel free to modify these constants to customize the game according to your preferences.
39+
40+
Enjoy playing The Snake Game!
41+
42+
## Output
43+
44+
![Screenshot](https://github.com/avinashkranjan/Amazing-Python-Scripts/raw/scipt/path/to/Screenshot%202023-05-25%20122921.png)
45+
46+
## Author(s)
47+
48+
[Akanksha Jha](https://github.com/Akanksha-202)

TheSnakeGame/snake.py

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
from tkinter import *
2+
import random
3+
4+
GAME_WIDTH = 600
5+
GAME_HEIGHT = 600
6+
SPEED = 100
7+
SPACE_SIZE = 20
8+
BODY_PARTS = 3
9+
SNAKE_COLOUR = "#00FF00"
10+
FOOD_COLOUR = "#FF0000"
11+
BACKGROUND_COLOUR = "#000000"
12+
13+
class Snake:
14+
15+
def __init__(self):
16+
self.body_size = BODY_PARTS
17+
self.coordinates = []
18+
self.squares =[]
19+
20+
for i in range (0, BODY_PARTS):
21+
self.coordinates.append([0,0])
22+
23+
for x,y in self.coordinates:
24+
square = canvas.create_rectangle(x, y, x + SPACE_SIZE , y + SPACE_SIZE , fill=SNAKE_COLOUR, tag = "snake")
25+
self.squares.append(square)
26+
27+
28+
class Food:
29+
30+
def __init__(self):
31+
32+
x= random.randint(0,(GAME_WIDTH/SPACE_SIZE)-1)* SPACE_SIZE
33+
y= random.randint(0,(GAME_HEIGHT/SPACE_SIZE)-1)* SPACE_SIZE
34+
35+
self.coordinates = [x,y]
36+
37+
canvas.create_oval(x, y, x+ SPACE_SIZE, y+SPACE_SIZE, fill=FOOD_COLOUR, tag="food")
38+
39+
40+
41+
def next_turn(snake, food):
42+
43+
x, y = snake.coordinates[0]
44+
45+
if direction == 'up':
46+
y-=SPACE_SIZE
47+
48+
elif direction == 'down':
49+
y+=SPACE_SIZE
50+
51+
elif direction == 'left':
52+
x-=SPACE_SIZE
53+
54+
elif direction == 'right':
55+
x+=SPACE_SIZE
56+
57+
58+
snake.coordinates.insert(0, (x,y))
59+
60+
square = canvas.create_rectangle(x,y , x + SPACE_SIZE , y + SPACE_SIZE, fill=SNAKE_COLOUR)
61+
62+
snake.squares.insert(0,square)
63+
64+
if x==food.coordinates[0] and y == food.coordinates[1]:
65+
66+
global score
67+
68+
score += 1
69+
70+
label.config(text="Score: {}".format(score))
71+
72+
canvas.delete("food")
73+
74+
food = Food()
75+
76+
else:
77+
del snake.coordinates[-1]
78+
79+
canvas.delete(snake.squares[-1])
80+
81+
del snake.squares[-1]
82+
83+
if check_collisons(snake):
84+
game_over()
85+
86+
else:
87+
window.after(SPEED,next_turn,snake,food)
88+
89+
90+
def change_direction(new_direction):
91+
92+
global direction
93+
94+
if new_direction =='left':
95+
if direction!= 'right':
96+
direction = new_direction
97+
98+
elif new_direction == 'right':
99+
if direction!= 'left':
100+
direction = new_direction
101+
102+
if new_direction =='up':
103+
if direction!= 'down':
104+
direction = new_direction
105+
106+
if new_direction =='down':
107+
if direction!= 'up':
108+
direction = new_direction
109+
110+
111+
def check_collisons(snake):
112+
x , y = snake.coordinates[0]
113+
114+
if x < 0 or x >= GAME_WIDTH:
115+
return True
116+
elif y < 0 or y >= GAME_HEIGHT:
117+
return True
118+
119+
for body_part in snake.coordinates[1:]:
120+
if x == body_part[0] and y == body_part[1]:
121+
return True
122+
123+
return False
124+
125+
def game_over():
126+
# canvas.delete(ALL)
127+
canvas.create_text(canvas.winfo_width()/2, canvas.winfo_height()/2, font=('consolas',70),text="GAME OVER", fill="red", tag = "gameover")
128+
129+
130+
reset_button = Button(text = "Restart", font=('consolas',20),command = reset)
131+
canvas.create_window(300,400,window=reset_button)
132+
133+
def reset():
134+
score = 0
135+
label.config(text="Score:{}".format(score))
136+
canvas.delete(ALL)
137+
138+
snake = Snake()
139+
food = Food()
140+
next_turn(snake,food)
141+
142+
143+
144+
window = Tk()
145+
window.title("Snake Game")
146+
window.resizable(False,False)
147+
148+
score = 0
149+
direction = 'down'
150+
151+
label = Label(window, text="Score:{}".format(score),font=('consolas',40))
152+
label.pack()
153+
154+
canvas = Canvas(window, bg= BACKGROUND_COLOUR,height=GAME_HEIGHT,width=GAME_WIDTH )
155+
canvas.pack()
156+
157+
window.bind('<Left>',lambda event: change_direction('left'))
158+
window.bind('<Right>',lambda event: change_direction('right'))
159+
window.bind('<Up>',lambda event: change_direction('up'))
160+
window.bind('<Down>',lambda event: change_direction('down'))
161+
162+
snake = Snake()
163+
164+
food =Food()
165+
166+
next_turn(snake,food)
167+
168+
window.mainloop()

0 commit comments

Comments
 (0)