Skip to content

Commit 9ded490

Browse files
committed
Added Snake game to play
1 parent b7be4bc commit 9ded490

2 files changed

Lines changed: 185 additions & 0 deletions

File tree

scripts/SnakeGame/SnakeGame.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
## Snake Game
2+
GUI based Snake game, where you can play it directly without installing any dependencies.
3+
4+
## How to install?
5+
`1.` Download the file rock_paper_scissor.py
6+
`2.` Open CMD (Command Prompt)
7+
`3.` Run python rock_paper_scissor.py
8+
`4.` Follow the instructions and enjoy!
9+
10+
Enjoy and play with your friends

scripts/SnakeGame/SnakeGame.py

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
import turtle
2+
import time
3+
import random
4+
5+
delay = 0.1
6+
score = 0
7+
high_score = 0
8+
9+
#Window Screen
10+
wn = turtle.Screen()
11+
wn.title("SNAKE GAME")
12+
wn.bgcolor("black")
13+
14+
wn.setup(width=600,height=600)
15+
wn.tracer(0)
16+
17+
#Head of Snake
18+
head = turtle.Turtle()
19+
head.shape("square")
20+
head.color("green")
21+
head.penup()
22+
head.goto(0, 0)
23+
head.direction = "stop"
24+
25+
#Food in the game
26+
food = turtle.Turtle()
27+
food.speed(0)
28+
food.shape("circle")
29+
food.color("red")
30+
food.penup()
31+
food.goto(0, 100)
32+
33+
#Score
34+
pen = turtle.Turtle()
35+
pen.speed(0)
36+
pen.shape("turtle")
37+
pen.color("white")
38+
pen.penup()
39+
pen.hideturtle()
40+
pen.goto(0, 250)
41+
pen.write("Score : 0 High Score : 0", align="center",
42+
font=("Times New Roman", 24, "bold"))
43+
44+
45+
#Assigning key values
46+
def goup():
47+
if head.direction != "down":
48+
head.direction = "up"
49+
50+
def godown():
51+
if head.direction != "up":
52+
head.direction = "down"
53+
54+
def goright():
55+
if head.direction != "left":
56+
head.direction = "right"
57+
58+
def goleft():
59+
if head.direction != "right":
60+
head.direction = "left"
61+
62+
def move():
63+
if head.direction == "up":
64+
y = head.ycor()
65+
head.sety(y+20)
66+
67+
if head.direction == "down":
68+
y = head.ycor()
69+
head.sety(y-20)
70+
71+
if head.direction == "right":
72+
x = head.xcor()
73+
head.setx(x+20)
74+
75+
if head.direction == "left":
76+
x = head.xcor()
77+
head.setx(x-20)
78+
79+
wn.listen()
80+
wn.onkeypress(goup, "Up")
81+
wn.onkeypress(godown, "Down")
82+
wn.onkeypress(goleft, "Left")
83+
wn.onkeypress(goright, "Right")
84+
85+
86+
#Main Loop
87+
segments = []
88+
89+
while True:
90+
wn.update()
91+
#for collisions with border
92+
if head.xcor() > 290 or head.xcor() < -290 or head.ycor() > 290 or head.ycor() < -290:
93+
time.sleep(1)
94+
head.goto(0, 0)
95+
head.direction = "stop"
96+
97+
#hiding segments of snake
98+
for segment in segments:
99+
segment.goto(1000,1000)
100+
#clearing the segments
101+
segments.clear()
102+
103+
#reset score
104+
score = 0
105+
106+
#reset delay
107+
delay = 0.1
108+
109+
pen.clear()
110+
pen.write("Score : {} High Score : {} ".format(
111+
score, high_score), align="center", font=("Times New Roman", 24, "bold"))
112+
113+
#checking collision with food
114+
if head.distance(food) < 20:
115+
x = random.randint(-270, 270)
116+
y = random.randint(-270, 270)
117+
food.goto(x, y)
118+
d = ["red","yellow","blue"]
119+
colors = random.choice(d)
120+
food.color(colors)
121+
e = ["circle","square","triangle"]
122+
shapes = random.choice(e)
123+
food.shape(shapes)
124+
125+
126+
#adding new segment
127+
new_segment = turtle.Turtle()
128+
new_segment.speed(0)
129+
new_segment.color("green")
130+
new_segment.shape("square")
131+
new_segment.penup()
132+
segments.append(new_segment)
133+
134+
delay -= 0.001
135+
score += 10
136+
137+
if score>high_score:
138+
high_score = score
139+
pen.clear()
140+
pen.write("Score : {} High Score : {} ".format(
141+
score, high_score), align="center", font=("Times New Roman", 24, "bold"))
142+
143+
#moving segments in reverse order
144+
for i in range(len(segments)-1,0,-1):
145+
x = segments[i-1].xcor()
146+
y = segments[i-1].ycor()
147+
segments[i].goto(x,y)
148+
if len(segments) > 0:
149+
x = head.xcor()
150+
y = head.ycor()
151+
segments[0].goto(x, y)
152+
153+
move()
154+
155+
#Checking collisions with body
156+
for segment in segments:
157+
if segment.distance(head) < 20:
158+
time.sleep(1)
159+
head.goto(0,0)
160+
head.direction = "stop"
161+
162+
#hide segments
163+
for segment in segments:
164+
segment.goto(1000,1000)
165+
segment.clear()
166+
167+
score = 0
168+
delay = 0.1
169+
pen.clear()
170+
pen.write("Score : {} High Score : {} ".format(
171+
score, high_score), align="center", font=("Times New Roman", 24, "bold"))
172+
time.sleep(delay)
173+
174+
turtle.done()
175+

0 commit comments

Comments
 (0)