Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import turtle
- import time
- import random
- #variables
- width = 600
- height = 600
- delay = 0.1
- ran1 = -290
- ran2 = 290
- Color = ["red","grey","blue","purple","yellow","pink","green","brown"]
- pen = turtle.Turtle()
- pen.speed(0)
- pen.shape("square")
- pen.color("blue")
- pen.penup()
- pen.hideturtle()
- pen.goto(0,260)
- pen.write("Score: 0 High Score: 0",align = "center",font = ("Courier",24,"normal"))
- #clases
- class setup_screen:
- def __init__(self,t,c,w,h):
- self.wn = turtle.Screen()
- self.wn.title(t)
- self.wn.bgcolor(c)
- self.wn.setup(w,h)
- self.wn.tracer(0)
- def Listen(self):
- self.wn.listen()
- self.wn.onkeypress(snake.go_up,"w")
- self.wn.onkeypress(snake.go_down,"s")
- self.wn.onkeypress(snake.go_right,"d")
- self.wn.onkeypress(snake.go_left,"a")
- class Snake(object):
- def __init__(self,Shape,Color):
- self.head = turtle.Turtle()
- self.head.speed(0)
- self.head.shape(Shape)
- self.head.color(Color)
- self.head.penup()#doesn't draw
- self.head.goto(0,0)#align center
- self.head.direction = "stop"
- self.segment = []
- self.score = 0
- self.high_score = 0
- def move(self):
- if self.head.direction == "up":
- self.y = self.head.ycor()
- self.head.sety(self.y + 20)
- if self.head.direction == "down":
- self.y = self.head.ycor()
- self.head.sety(self.y - 20)
- if self.head.direction == "right":
- self.x = self.head.xcor()
- self.head.setx(self.x + 20)
- if self.head.direction == "left":
- self.x = self.head.xcor()
- self.head.setx(self.x - 20)
- def go_up(self):
- if self.head.direction != "down":
- self.head.direction = "up"
- else:
- go_up()
- def go_down(self):
- if self.head.direction != "up":
- self.head.direction = "down"
- else :
- go_down()
- def go_right(self):
- if self.head.direction != "left":
- self.head.direction = "right"
- else:
- go_right()
- def go_left(self):
- if self.head.direction != "right":
- self.head.direction = "left"
- else:
- go_left()
- def Grow(self):
- if self.head.distance(FOOD.food) < 20 :
- self.x = random.randint(-289,289)
- self.y = random.randint(-289,289)
- if self.x % 2 == 1:
- self.x = self.x - 1
- if self.y % 2 == 1:
- self.y = self.y - 1
- FOOD.food.goto(self.x,self.y)
- self.score = self.score + 10
- if self.score > self.high_score :
- self.high_score = self.score
- pen.clear()
- pen.write("Score: {} High Score: {}".format(self.score,self.high_score),align = "center",font = ("Courier",24,"normal"))
- self.new_segment = turtle.Turtle()
- self.new_segment.speed(0)
- self.new_segment.shape("square")
- self.new_segment.color(Color[random.randint(0,len(Color) - 1)])
- self.new_segment.penup()
- self.segment.append(self.new_segment)
- for index in range(len(self.segment) - 1,0,-1):
- self.x = self.segment[index - 1].xcor()
- self.y = self.segment[index - 1].ycor()
- self.segment[index].goto(self.x,self.y)
- if len(self.segment) > 0:
- self.x = self.head.xcor()
- self.y = self.head.ycor()
- self.segment[0].goto(self.x,self.y)
- def GameOver(self):
- if self.head.xcor() > 300 or self.head.ycor() > 300 or self.head.ycor() <-300 or self.head.xcor() <-300:
- time.sleep(1)
- self.head.goto(0,0)
- self.head.direction = "stop"
- for s in self.segment :
- s.goto(1000,1000)
- self.segment.clear()
- self.score = 0
- pen.clear()
- pen.write("Score: {} High Score: {}".format(self.score,self.high_score),align = "center",font = ("Courier",24,"normal"))
- for s in self.segment:
- if s.distance(self.head) < 20:
- time.sleep(1)
- self.head.goto(0,0)
- self.head.direction = "stop"
- for d in self.segment :
- d.goto(1000,1000)
- self.segment.clear()
- self.score = 0
- pen.clear()
- pen.write("Score: {} High Score: {}".format(self.score,self.high_score),align = "center",font = ("Courier",24,"normal"))
- class Food(object):
- def __init__(self,Shape,Color):
- self.food = turtle.Turtle()
- self.food.speed(0)
- self.food.shape(Shape)
- self.food.color(Color)
- self.food.penup()
- self.food.goto(0,100)
- FOOD = Food("square","red")
- snake = Snake("square","white")
- screen = setup_screen("Snake Game","black",width,height)
- screen.Listen()
- while True:
- screen.wn.update()
- snake.Grow()
- snake.move()
- snake.GameOver()
- if snake.head.distance(FOOD.food) < 20:
- delay -= 0.005
- if snake.score == 0:
- delay = 0.1
- time.sleep(delay)
- wn.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment