Advertisement
sawczakl

snake_crazy

Mar 9th, 2023 (edited)
928
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.19 KB | Gaming | 0 0
  1. import turtle
  2. import random
  3.  
  4. class App:
  5.     pass
  6.  
  7. def go_up():
  8.     if app.direction !="down":
  9.         app.direction = "up"
  10.  
  11. def go_down():
  12.     if app.direction !="up":
  13.         app.direction = "down"
  14.  
  15. def go_left():
  16.     if app.direction !="right":
  17.         app.direction = "left"
  18.  
  19. def go_right():
  20.     if app.direction !="left":
  21.         app.direction = "right"
  22.  
  23. def start_game():
  24.     # Don't start again if already started
  25.     if app.started:
  26.         return
  27.  
  28.     else:
  29.         app.started = True
  30.         app.alive = True
  31.         app.direction = "stop"
  32.         new_food()
  33.         app.score = 0
  34.         update_score()
  35.         new_snake()
  36.         app.banner.clear()
  37.         app.press_x.clear()
  38.         wn.ontimer(move, app.frequency)
  39.  
  40. def update_score():
  41.     app.scoreboard.clear()
  42.     app.scoreboard.write(f"Score: {app.score}\nPress z to turn crazy mode on/off", align="center", font="Consolas")
  43.  
  44. def end_game():
  45.     app.alive = False
  46.     print('End game')
  47.     app.started = False
  48.     you_died()
  49.  
  50. def check_hit_food():
  51.     if app.segments[0].distance(app.food) < 20:
  52.         new_food()
  53.         app.score += 1
  54.         update_score()
  55.         add_segment()
  56.  
  57. def replace_head():
  58.     app.segments.insert(0, app.segments.pop())
  59.  
  60. def move():
  61.  
  62.     if app.direction == "up":
  63.         x = app.segments[0].xcor()
  64.         y = app.segments[0].ycor()
  65.         if y >= 335:
  66.             end_game()
  67.         else:
  68.             if not app.crazy_mode:
  69.                 app.segments[-1].setx(x)
  70.  
  71.             app.segments[-1].sety(y + app.pixel_jump)
  72.             replace_head()
  73.             check_hit_food()
  74.  
  75.     if app.direction == "down":
  76.         x = app.segments[0].xcor()
  77.         y = app.segments[0].ycor()
  78.         if y <= -325:
  79.             end_game()
  80.         else:
  81.             if not app.crazy_mode:
  82.                 app.segments[-1].setx(x)
  83.             app.segments[-1].sety(y - app.pixel_jump)
  84.             replace_head()
  85.             check_hit_food()
  86.  
  87.     if app.direction == "left":
  88.         x = app.segments[0].xcor()
  89.         y = app.segments[0].ycor()
  90.         if x <= -335:
  91.             end_game()
  92.         else:
  93.             app.segments[-1].setx(x - app.pixel_jump)
  94.             if not app.crazy_mode:
  95.                 app.segments[-1].sety(y)
  96.             replace_head()
  97.             check_hit_food()
  98.  
  99.     if app.direction == "right":
  100.         x = app.segments[0].xcor()
  101.         y = app.segments[0].ycor()
  102.         if x >= 325:
  103.             end_game()
  104.         else:
  105.             app.segments[-1].setx(x + app.pixel_jump)
  106.             if not app.crazy_mode:
  107.                 app.segments[-1].sety(y)
  108.             replace_head()
  109.             check_hit_food()
  110.  
  111.     if app.alive:
  112.         wn.ontimer(move, app.frequency)
  113.  
  114. def new_food():
  115.  
  116.     if app.food:
  117.        
  118.         app.food.hideturtle()
  119.         app.food = None
  120.  
  121.     app.food = turtle.Turtle()
  122.  
  123.     x = random.randint(-330, 330)
  124.     y = random.randint(-330, 330)
  125.     wn.tracer(0, 0)
  126.     app.food.speed("fastest")
  127.     app.food.hideturtle()
  128.     app.food.penup()
  129.     app.food.setposition(x, y)
  130.     app.food.pendown()
  131.     app.food.showturtle()
  132.     wn.update()
  133.     wn.tracer(1, 0)
  134.  
  135.     app.food.shape("square")
  136.     app.food.color("black")
  137.     app.food.penup()
  138.  
  139. def you_died():
  140.     app.banner.speed(0)
  141.     app.banner.shape("square")
  142.     app.banner.color("white")
  143.     app.banner.penup()
  144.     app.banner.hideturtle()
  145.     app.banner.goto(0, 0)
  146.     app.banner.write("You Died!\nPress 'x' to restart.", align="center", font=("Consolas", 50, "normal"))
  147.  
  148. app = App()
  149. app.crazy_mode = False
  150. app.segments = []
  151. app.score = 0
  152. app.highscore = 0
  153. app.time = 0
  154. app.started = False
  155. app.alive = True
  156. app.frequency = 50   # milliseconds
  157. app.pixel_jump = 10   # pixels
  158. app.food = None
  159.  
  160. app.banner = turtle.Turtle()
  161. app.banner.hideturtle()
  162.  
  163. wn = turtle.Screen()
  164. wn.colormode(255)
  165. wn.title("Snake game")
  166. wn.bgcolor("#782E2E")
  167. wn.setup(height=700, width=700)
  168.  
  169. def toggle_crazy_mode():
  170.     app.crazy_mode = not app.crazy_mode
  171.  
  172. def new_snake():
  173.  
  174.     while app.segments:
  175.         segment = app.segments[0]
  176.  
  177.         segment.clear()
  178.         segment.hideturtle()
  179.  
  180.         del app.segments[0]
  181.  
  182.     app.segments = []
  183.  
  184.     head = turtle.Turtle()
  185.     head.speed(0)
  186.     head.shape("square")
  187.     head.color("#22B14A")
  188.     head.penup()
  189.     head.goto(0,0)
  190.  
  191.     app.segments = [head]
  192.  
  193. app.direction = "stop"
  194.  
  195. def create_scoreboard():
  196.     app.scoreboard = turtle.Turtle()
  197.     app.scoreboard.speed(0)
  198.     app.scoreboard.shape("square")
  199.     app.scoreboard.color("white")
  200.     app.scoreboard.penup()
  201.     app.scoreboard.hideturtle()
  202.     app.scoreboard.goto(0,260)
  203.     update_score()
  204.  
  205. def create_start_game_banner():
  206.     app.press_x = turtle.Turtle()
  207.     app.press_x.hideturtle()
  208.     app.press_x.speed(0)
  209.     app.press_x.shape("square")
  210.     app.press_x.color("white")
  211.     app.press_x.penup()
  212.     app.press_x.hideturtle()
  213.     app.press_x.goto(0, 0)
  214.     app.press_x.write("Press 'x' to start.", align="center", font=("Consolas", 50, "normal"))
  215.  
  216. wn.listen()
  217.  
  218. wn.onkeypress(go_up, "w")
  219. wn.onkeypress(go_down, "s")
  220. wn.onkeypress(go_left, "a")
  221. wn.onkeypress(go_right, "d")
  222.  
  223. wn.onkeypress(go_up, "Up")
  224. wn.onkeypress(go_down, "Down")
  225. wn.onkeypress(go_left, "Left")
  226. wn.onkeypress(go_right, "Right")
  227.  
  228. wn.onkeypress(start_game, "x")
  229. wn.onkeypress(toggle_crazy_mode, "z")
  230.  
  231. create_scoreboard()
  232. create_start_game_banner()
  233.  
  234. def add_segment():
  235.  
  236.     new_segment = turtle.Turtle()
  237.     new_segment.speed(0)
  238.     new_segment.shape("square")
  239.     new_segment.color("#22B14A")
  240.     new_segment.penup()
  241.     app.segments.append(new_segment)
  242.  
  243.     if app.direction == 'up':
  244.         x = app.segments[-2].xcor()
  245.         y = app.segments[-2].ycor() - 20
  246.         new_segment.goto(x, y)
  247.     elif app.direction == 'down':
  248.         x = app.segments[-2].xcor()
  249.         y = app.segments[-2].ycor() + 20
  250.         new_segment.goto(x, y)
  251.     elif app.direction == 'left':
  252.         x = app.segments[-2].xcor() + 20
  253.         y = app.segments[-2].ycor()
  254.         new_segment.goto(x, y)
  255.     elif app.direction == 'right':
  256.         x = app.segments[-2].xcor() - 20
  257.         y = app.segments[-2].ycor()
  258.         new_segment.goto(x, y)
  259.  
  260. wn.mainloop()
  261. turtle.done()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement