Skip to content

Commit 3f54bb5

Browse files
authored
Merge pull request #2 from HARSHV10/HARSHV10-patch-2
hey added a snake game hoping for a merge
2 parents d914852 + b8383c8 commit 3f54bb5

File tree

11 files changed

+200
-0
lines changed

11 files changed

+200
-0
lines changed

snakegame/snake_game/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# snake_game
2+
classic snake game "eat more to grow more"
3+
a basic snake game
Binary file not shown.
Binary file not shown.
Binary file not shown.

snakegame/snake_game/check.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from turtle import Turtle , Screen
2+
import time
3+
4+
screen= Screen()
5+
6+
screen.setup(width = 600,height=600)
7+
screen.bgcolor("black")
8+
screen.title("my game")
9+
screen.tracer(0)
10+
# ye humne body bana li
11+
body = [(0,0),(-20,0),(-40,0)]
12+
segment=[]
13+
for parts in body:
14+
moti = Turtle()
15+
moti.color("white")
16+
moti.shape("square")
17+
moti.penup()
18+
moti.goto(parts)
19+
segment.append(moti)
20+
21+
22+
game_on = True
23+
while game_on :
24+
screen.update()
25+
time.sleep(0.1)
26+
# ab snake ko turn karane ki liye hum ise ek tarike se link karenge iske co_ordinate se
27+
for seg in range (len(segment)-1,0,-1):
28+
new_xpos= segment[seg-1].xcor()
29+
new_ypos= segment[seg-1].ycor()
30+
segment[seg].goto(new_xpos,new_ypos)
31+
# ab hum first part ko control kar rahe hai aur pura snake control ho raha hai
32+
segment[0].forward(20)
33+
segment[0].left(90)
34+
35+
36+
37+
38+
screen.exitonclick()

snakegame/snake_game/data

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
5

snakegame/snake_game/food.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from turtle import Turtle
2+
import random
3+
# ab hum turtle class inherit karenge
4+
class Food(Turtle):
5+
6+
def __init__(self):
7+
super().__init__()
8+
self.shape("circle")
9+
self.color("blue")
10+
self.newfood()
11+
self.penup()
12+
self.shapesize(stretch_len=0.5,stretch_wid=0.5)
13+
self.speed("fastest")
14+
def newfood(self):
15+
self.penup()
16+
xcor= random.randint(-280,280)
17+
ycor= random.randint(-280,280)
18+
self.goto(xcor,ycor)

snakegame/snake_game/highscore.txt

Whitespace-only changes.

snakegame/snake_game/main.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from turtle import Turtle , Screen
2+
import time
3+
from snake import Snake
4+
from food import Food
5+
from scorecard import Score
6+
screen= Screen()
7+
score=Score()
8+
9+
screen.setup(width = 600,height=600)
10+
screen.bgcolor("black")
11+
screen.title("my game")
12+
screen.tracer(0)
13+
14+
snake =Snake()
15+
food =Food()
16+
screen.listen()
17+
screen.onkey(snake.up,"Up")
18+
screen.onkey(snake.down,"Down")
19+
screen.onkey(snake.left,"Left")
20+
screen.onkey(snake.right,"Right")
21+
22+
game_on = True
23+
while game_on :
24+
screen.update()
25+
time.sleep(0.1)
26+
snake.move()
27+
#now to detect the collision
28+
if snake.head.distance(food)<15:
29+
food.newfood()
30+
snake.extend()
31+
score.currentscore()
32+
# collision with the tail
33+
if snake.head.xcor()>280 or snake.head.xcor()<-280 or snake.head.ycor()>280 or snake.head.ycor()<-280:
34+
score.renew()
35+
snake.new()
36+
37+
38+
# collision with the tail
39+
for i in snake.segment[1:]:
40+
if i == snake.head:
41+
pass
42+
elif snake.head.distance(i)<10:
43+
score.renew()
44+
snake.new()
45+
46+
47+
48+
49+
screen.exitonclick()

snakegame/snake_game/scorecard.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from turtle import Turtle
2+
ALIGNMENT="center"
3+
FONT=('joker', 24, 'normal')
4+
5+
class Score(Turtle):
6+
def __init__(self):
7+
8+
self.scoreval =0
9+
10+
with open("data", mode="r") as file:
11+
self.highscore = int(file.read())
12+
13+
14+
# file= open("data")
15+
# data=file.read()
16+
# self.data=int(data)
17+
super().__init__()
18+
self.color("white")
19+
self.penup()
20+
self.goto(x=0, y=250)
21+
self.hideturtle()
22+
self.updatescore()
23+
def updatescore(self):
24+
self.clear()
25+
self.write(f"SCORE:{self.scoreval} HIGH SCORE:{self.highscore}", align=ALIGNMENT, font=FONT)
26+
def currentscore(self):
27+
self.scoreval+=1
28+
self.clear()
29+
self.updatescore()
30+
def renew(self):
31+
if self.scoreval>self.highscore:
32+
self.highscore = self.scoreval
33+
with open("data", mode="w") as change:
34+
change.write(str(self.scoreval))
35+
self.scoreval=0
36+
self.updatescore()

snakegame/snake_game/snake.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#ab hum snake ki class banaenge jo humar acoe tidy kar degi
2+
from turtle import Turtle
3+
BODY = [(0, 0), (-20, 0), (-40, 0)]
4+
STEP = 20
5+
UP = 90
6+
DOWN = 270
7+
LEFT = 180
8+
RIGHT = 0
9+
SHAPE="square"
10+
COLOR="white"
11+
class Snake :
12+
def __init__(self):
13+
self.segment=[]
14+
self.createsnake()
15+
self.head = self.segment[0]
16+
17+
def createsnake(self):
18+
for parts in BODY:
19+
self.addseg(parts)
20+
def new(self):
21+
for i in self.segment:
22+
i.goto(700,700)
23+
self.segment.clear()
24+
self.createsnake()
25+
self.head = self.segment[0]
26+
def addseg(self,parts):
27+
# is wale function se segment add ho rahe hai
28+
moti = Turtle()
29+
moti.color(COLOR)
30+
moti.shape(SHAPE)
31+
moti.penup()
32+
moti.goto(parts)
33+
self.segment.append(moti)
34+
def extend(self):
35+
self.addseg(self.segment[-1].position())
36+
def move(self):
37+
for seg in range(len(self.segment) - 1, 0, -1):
38+
new_xpos = self.segment[seg - 1].xcor()
39+
new_ypos = self.segment[seg - 1].ycor()
40+
self.segment[seg].goto(new_xpos, new_ypos)
41+
self.head.forward(STEP)
42+
def up(self):
43+
if self.head.heading() != DOWN:
44+
self.head.setheading(UP)
45+
def down(self):
46+
if self.head.heading() != UP:
47+
self.head.setheading(DOWN)
48+
def left(self):
49+
if self.head.heading() != RIGHT:
50+
self.head.setheading(LEFT)
51+
def right(self):
52+
if self.head.heading() != LEFT:
53+
self.head.setheading(RIGHT)
54+
55+

0 commit comments

Comments
 (0)