Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added The-Epic-Snake-Game w/ Pictures and Instructions #47

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added src/The-Epic-Snake-Game/Layout.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions src/The-Epic-Snake-Game/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
### The-Epic-Snake-Game

![Files-in-repo](https://img.shields.io/github/directory-file-count/KrishGaur1354/Personal-Python-Projects/qrcode-generator)


---


![Personal-Python-Projects](https://socialify.git.ci/KrishGaur1354/Personal-Python-Projects/image?font=Source%20Code%20Pro&language=1&name=1&owner=1&pattern=Circuit%20Board&theme=Dark)


---
### Game-Layout
<img src="https://github.com/KrishGaur1354/Personal-Python-Projects/blob/main/The-Epic-Snake-Game/RUNTIME.png">

---

### Connect with me
<a href="https://twitter.com/ThatOneKrish">
<img width="30px" src="https://www.vectorlogo.zone/logos/twitter/twitter-official.svg" />
</a>&ensp;
<a href="https://www.instagram.com/ThatOneKrish/">
<img width="30px" src="https://www.vectorlogo.zone/logos/instagram/instagram-icon.svg" />
</a>&ensp;
Binary file added src/The-Epic-Snake-Game/RUNTIME.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/The-Epic-Snake-Game/TheSnakeGameDemo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
161 changes: 161 additions & 0 deletions src/The-Epic-Snake-Game/thesnake.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
#imports
import turtle
import time
import random

delay = 0.1

score = 0
high_score = 0

scrn = turtle.Screen()
scrn.title("Snake Game")
scrn.bgcolor('white')
scrn.setup(width=720, height=1080)
scrn.tracer(0)

#snake head
head = turtle.Turtle()
head.speed(0)
head.shape("square")
head.color("black")
head.penup()
head.goto(0,0)
head.direction = "stop"

#snake berry
berry= turtle.Turtle()
berry.speed(0)
berry.shape("circle")
berry.color("red")
berry.penup()
berry.goto(0,100)

segments = []

#scoreboards
scre = turtle.Turtle()
scre.speed(0)
scre.shape("square")
scre.color("red")
scre.penup()
scre.hideturtle()
scre.goto(0,260)
scre.write("Your Score: 0 High score: 0", align = "center", font=("ds-digital", 24, "normal"))

#Functions
def go_up():
if head.direction != "down":
head.direction = "up"
def go_down():
if head.direction != "up":
head.direction = "down"
def go_left():
if head.direction != "right":
head.direction = "left"
def go_right():
if head.direction != "left":
head.direction = "right"
def move():
if head.direction == "up":
y = head.ycor()
head.sety(y+20)
if head.direction == "down":
y = head.ycor()
head.sety(y-20)
if head.direction == "left":
x = head.xcor()
head.setx(x-20)
if head.direction == "right":
x = head.xcor()
head.setx(x+20)

#Keyboard bindings
scrn.listen()
scrn.onkeypress(go_up, "w")
scrn.onkeypress(go_down, "s")
scrn.onkeypress(go_left, "a")
scrn.onkeypress(go_right, "d")

#MainLoop
while True:
scrn.update()

#check collision with border area
if head.xcor()>290 or head.xcor()<-290 or head.ycor()>290 or head.ycor()<-290:
time.sleep(1)
head.goto(0,0)
head.direction = "stop"

#hide the segments of body
for segment in segments:
segment.goto(1000,1000) #out of range
#clear the segments
segments.clear()

#reset score
score = 0

#reset delay
delay = 0.1

scre.clear()
scre.write("score: {} High score: {}".format(score, high_score), align="center", font=("ds-digital", 24, "normal"))

#check collision with berry
if head.distance(berry) <20:
# move the berry to random place
x = random.randint(-290,290)
y = random.randint(-290,290)
berry.goto(x,y)

#add a new segment to the head
new_segment = turtle.Turtle()
new_segment.speed(0)
new_segment.shape("square")
new_segment.color("black")
new_segment.penup()
segments.append(new_segment)

#shorten the delay
delay -= 0.001
#increase the score
score += 10

if score > high_score:
high_score = score
scre.clear()
scre.write("score: {} High score: {}".format(score,high_score), align="center", font=("ds-digital", 24, "normal"))

#move the segments in reverse order
for index in range(len(segments)-1,0,-1):
x = segments[index-1].xcor()
y = segments[index-1].ycor()
segments[index].goto(x,y)
#move segment 0 to head
if len(segments)>0:
x = head.xcor()
y = head.ycor()
segments[0].goto(x,y)

move()

#check for collision with body
for segment in segments:
if segment.distance(head)<20:
time.sleep(1)
head.goto(0,0)
head.direction = "stop"

#hide segments
for segment in segments:
segment.goto(1000,1000)
segments.clear()
score = 0
delay = 0.1

#update the score
scre.clear()
scre.write("score: {} High score: {}".format(score,high_score), align="center", font=("ds-digital", 24, "normal"))
time.sleep(delay)
scrn.mainloop()