├── data.text ├── README.md ├── food.py ├── scoreboard.py ├── main.py └── snake.py /data.text: -------------------------------------------------------------------------------- 1 | 10 -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Snake-Game 2 | the old snake game that we all loved made by python turtle 3 | -------------------------------------------------------------------------------- /food.py: -------------------------------------------------------------------------------- 1 | from turtle import Turtle 2 | import random 3 | class Food(Turtle): 4 | def __init__(self): 5 | super().__init__() 6 | self.shape("circle") 7 | self.penup() 8 | self.color("blue") 9 | self.shapesize(stretch_len=0.5,stretch_wid=0.5) 10 | self.speed("fastest") 11 | self.goto(x=random.randint(-270,270),y=random.randint(-270,270)) 12 | def spawn(self): 13 | new_x=random.randint(-270,270) 14 | new_y=random.randint(-270,270) 15 | self.goto(new_x,new_y) -------------------------------------------------------------------------------- /scoreboard.py: -------------------------------------------------------------------------------- 1 | from turtle import Turtle 2 | import random 3 | class Scoreboard(Turtle): 4 | def __init__(self): 5 | super().__init__() 6 | self.penup() 7 | self.hideturtle() 8 | self.speed("fastest") 9 | self.color("white") 10 | self.score=0 11 | with open("Day20/data.text") as file: 12 | self.high_score=int(file.read()) 13 | self.goto(x=0,y=250) 14 | self.write_f() 15 | 16 | def inscrease(self): 17 | self.clear() 18 | self.score+=1 19 | self.write_f() 20 | def write_f(self): 21 | self.write(f"Score : {self.score} High score : {self.high_score}", align='center', font=('Arial', 20, 'normal')) 22 | def reset(self): 23 | if self.score > self.high_score: 24 | with open("Day20/data.text","w") as file: 25 | file.write(f"{self.score}") 26 | self.high_score=self.score 27 | self.score=0 28 | self.clear() 29 | self.write_f() 30 | # def game_over(self): 31 | # self.goto(x=0, y=0) 32 | # self.write(f"Game Over", align='center',font=('Arial', 30,"normal")) -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | from turtle import * 2 | import random 3 | from time import sleep 4 | from snake import Snake 5 | from food import Food 6 | from scoreboard import Scoreboard 7 | window=Screen() 8 | window.setup(width=600,height=600) 9 | window.bgcolor("black") 10 | window.listen() 11 | window.tracer(0) 12 | timmy=Snake() 13 | ball=Food() 14 | table=Scoreboard() 15 | window.onkey(timmy.move_forward,"w") 16 | window.onkey(timmy.move_left,"a") 17 | window.onkey(timmy.move_right,"d") 18 | window.onkey(timmy.move_backward,"s") 19 | game_on=True 20 | while game_on : 21 | window.update() 22 | sleep(0.1) 23 | if timmy.header.distance(ball) <15: 24 | ball.spawn() 25 | table.inscrease() 26 | timmy.extend() 27 | if timmy.header.xcor() >= 280 or timmy.header.ycor() >= 280 or timmy.header.ycor() <= -280 or timmy.header.xcor() <= -280: 28 | table.reset() 29 | timmy.reset() 30 | 31 | for i in timmy.segments[1:]: 32 | if timmy.header.distance(i) < 12 : 33 | table.reset() 34 | timmy.reset() 35 | 36 | timmy.move() 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | window.mainloop() 50 | -------------------------------------------------------------------------------- /snake.py: -------------------------------------------------------------------------------- 1 | from turtle import Turtle 2 | import random 3 | from time import sleep 4 | cord=[(0,0),(-20,0),(-40,0)] 5 | snake=[] 6 | 7 | 8 | 9 | class Snake(Turtle): 10 | def __init__(self): 11 | self.segments=[] 12 | self.create_snake() 13 | self.header=self.segments[0] 14 | 15 | 16 | def create_snake(self): 17 | for i in cord: 18 | self.add_segment(i) 19 | def add_segment(self,position): 20 | tim=Turtle(shape="square") 21 | tim.penup() 22 | tim.color("white") 23 | tim.goto(position) 24 | self.segments.append(tim) 25 | def extend(self): 26 | self.add_segment(self.segments[-1].position()) 27 | 28 | def move(self): 29 | for i in range(len(self.segments)-1,0,-1): 30 | x=self.segments[i-1].xcor() 31 | y=self.segments[i-1].ycor() 32 | self.segments[i].goto(x,y) 33 | self.header.forward(20) 34 | def move_forward(self): 35 | if self.header.heading() != 270 : 36 | self.header.setheading(90) 37 | def move_backward(self): 38 | if self.header.heading() != 90 : 39 | self.header.setheading(270) 40 | def move_left(self): 41 | if self.header.heading() != 0 : 42 | self.header.setheading(180) 43 | def move_right(self): 44 | if self.header.heading() != 180 : 45 | self.header.setheading(0) 46 | def reset(self): 47 | for i in self.segments: 48 | i.goto(1000,1000) 49 | self.segments.clear() 50 | self.create_snake() 51 | self.header=self.segments[0] --------------------------------------------------------------------------------