Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import turtle
- import random
- class App:
- pass
- def go_up():
- if app.direction !="down":
- app.direction = "up"
- def go_down():
- if app.direction !="up":
- app.direction = "down"
- def go_left():
- if app.direction !="right":
- app.direction = "left"
- def go_right():
- if app.direction !="left":
- app.direction = "right"
- def start_game():
- # Don't start again if already started
- if app.started:
- return
- else:
- app.started = True
- app.alive = True
- app.direction = "stop"
- new_food()
- app.score = 0
- update_score()
- new_snake()
- app.banner.clear()
- app.press_x.clear()
- wn.ontimer(move, app.frequency)
- def update_score():
- app.scoreboard.clear()
- app.scoreboard.write(f"Score: {app.score}\nPress z to turn crazy mode on/off", align="center", font="Consolas")
- def end_game():
- app.alive = False
- print('End game')
- app.started = False
- you_died()
- def check_hit_food():
- if app.segments[0].distance(app.food) < 20:
- new_food()
- app.score += 1
- update_score()
- add_segment()
- def replace_head():
- app.segments.insert(0, app.segments.pop())
- def move():
- if app.direction == "up":
- x = app.segments[0].xcor()
- y = app.segments[0].ycor()
- if y >= 335:
- end_game()
- else:
- if not app.crazy_mode:
- app.segments[-1].setx(x)
- app.segments[-1].sety(y + app.pixel_jump)
- replace_head()
- check_hit_food()
- if app.direction == "down":
- x = app.segments[0].xcor()
- y = app.segments[0].ycor()
- if y <= -325:
- end_game()
- else:
- if not app.crazy_mode:
- app.segments[-1].setx(x)
- app.segments[-1].sety(y - app.pixel_jump)
- replace_head()
- check_hit_food()
- if app.direction == "left":
- x = app.segments[0].xcor()
- y = app.segments[0].ycor()
- if x <= -335:
- end_game()
- else:
- app.segments[-1].setx(x - app.pixel_jump)
- if not app.crazy_mode:
- app.segments[-1].sety(y)
- replace_head()
- check_hit_food()
- if app.direction == "right":
- x = app.segments[0].xcor()
- y = app.segments[0].ycor()
- if x >= 325:
- end_game()
- else:
- app.segments[-1].setx(x + app.pixel_jump)
- if not app.crazy_mode:
- app.segments[-1].sety(y)
- replace_head()
- check_hit_food()
- if app.alive:
- wn.ontimer(move, app.frequency)
- def new_food():
- if app.food:
- app.food.hideturtle()
- app.food = None
- app.food = turtle.Turtle()
- x = random.randint(-330, 330)
- y = random.randint(-330, 330)
- wn.tracer(0, 0)
- app.food.speed("fastest")
- app.food.hideturtle()
- app.food.penup()
- app.food.setposition(x, y)
- app.food.pendown()
- app.food.showturtle()
- wn.update()
- wn.tracer(1, 0)
- app.food.shape("square")
- app.food.color("black")
- app.food.penup()
- def you_died():
- app.banner.speed(0)
- app.banner.shape("square")
- app.banner.color("white")
- app.banner.penup()
- app.banner.hideturtle()
- app.banner.goto(0, 0)
- app.banner.write("You Died!\nPress 'x' to restart.", align="center", font=("Consolas", 50, "normal"))
- app = App()
- app.crazy_mode = False
- app.segments = []
- app.score = 0
- app.highscore = 0
- app.time = 0
- app.started = False
- app.alive = True
- app.frequency = 50 # milliseconds
- app.pixel_jump = 10 # pixels
- app.food = None
- app.banner = turtle.Turtle()
- app.banner.hideturtle()
- wn = turtle.Screen()
- wn.colormode(255)
- wn.title("Snake game")
- wn.bgcolor("#782E2E")
- wn.setup(height=700, width=700)
- def toggle_crazy_mode():
- app.crazy_mode = not app.crazy_mode
- def new_snake():
- while app.segments:
- segment = app.segments[0]
- segment.clear()
- segment.hideturtle()
- del app.segments[0]
- app.segments = []
- head = turtle.Turtle()
- head.speed(0)
- head.shape("square")
- head.color("#22B14A")
- head.penup()
- head.goto(0,0)
- app.segments = [head]
- app.direction = "stop"
- def create_scoreboard():
- app.scoreboard = turtle.Turtle()
- app.scoreboard.speed(0)
- app.scoreboard.shape("square")
- app.scoreboard.color("white")
- app.scoreboard.penup()
- app.scoreboard.hideturtle()
- app.scoreboard.goto(0,260)
- update_score()
- def create_start_game_banner():
- app.press_x = turtle.Turtle()
- app.press_x.hideturtle()
- app.press_x.speed(0)
- app.press_x.shape("square")
- app.press_x.color("white")
- app.press_x.penup()
- app.press_x.hideturtle()
- app.press_x.goto(0, 0)
- app.press_x.write("Press 'x' to start.", align="center", font=("Consolas", 50, "normal"))
- wn.listen()
- wn.onkeypress(go_up, "w")
- wn.onkeypress(go_down, "s")
- wn.onkeypress(go_left, "a")
- wn.onkeypress(go_right, "d")
- wn.onkeypress(go_up, "Up")
- wn.onkeypress(go_down, "Down")
- wn.onkeypress(go_left, "Left")
- wn.onkeypress(go_right, "Right")
- wn.onkeypress(start_game, "x")
- wn.onkeypress(toggle_crazy_mode, "z")
- create_scoreboard()
- create_start_game_banner()
- def add_segment():
- new_segment = turtle.Turtle()
- new_segment.speed(0)
- new_segment.shape("square")
- new_segment.color("#22B14A")
- new_segment.penup()
- app.segments.append(new_segment)
- if app.direction == 'up':
- x = app.segments[-2].xcor()
- y = app.segments[-2].ycor() - 20
- new_segment.goto(x, y)
- elif app.direction == 'down':
- x = app.segments[-2].xcor()
- y = app.segments[-2].ycor() + 20
- new_segment.goto(x, y)
- elif app.direction == 'left':
- x = app.segments[-2].xcor() + 20
- y = app.segments[-2].ycor()
- new_segment.goto(x, y)
- elif app.direction == 'right':
- x = app.segments[-2].xcor() - 20
- y = app.segments[-2].ycor()
- new_segment.goto(x, y)
- wn.mainloop()
- turtle.done()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement