Guest User

Untitled

a guest
Jul 22nd, 2020
25,604
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import pygame
  2. import random
  3.  
  4. # initialize pygame
  5. pygame.init()
  6.  
  7. # create display & run update
  8. width = 640
  9. height = 480
  10. display = pygame.display.set_mode((width, height))
  11.  
  12. pygame.display.update()
  13. pygame.display.set_caption("Snakee game by Howdy")
  14.  
  15. # define colors
  16. colors = {
  17.     "snake_head": (0, 255, 0),
  18.     "snake_tail": (0, 200, 0),
  19.     "apple": (255, 0, 0)
  20. }
  21.  
  22. # snake position with offsets
  23. snake_pos = {
  24.     "x": width/2-10,
  25.     "y": height/2-10,
  26.     "x_change": 0,
  27.     "y_change": 0
  28. }
  29.  
  30. # snake el size
  31. snake_size = (10, 10)
  32.  
  33. # current snake movement speed
  34. snake_speed = 10
  35.  
  36. # snake tails
  37. snake_tails = []
  38.  
  39. snake_pos["x_change"] = -snake_speed
  40. for i in range(75):
  41.     snake_tails.append([snake_pos["x"] + 10*i, snake_pos["y"]])
  42.  
  43. # food
  44. food_pos = {
  45.     "x": round(random.randrange(0, width - snake_size[0]) / 10) * 10,
  46.     "y": round(random.randrange(0, height - snake_size[1]) / 10) * 10,
  47. }
  48.  
  49. food_size = (10, 10)
  50. food_eaten = 0
  51.  
  52. # start loop
  53. game_end = False
  54. clock = pygame.time.Clock()
  55.  
  56. while not game_end:
  57.     # game loop
  58.     for event in pygame.event.get():
  59.         if event.type == pygame.QUIT:
  60.             game_end = True
  61.  
  62.         elif event.type == pygame.KEYDOWN:
  63.             if event.key == pygame.K_LEFT and snake_pos["x_change"] == 0:
  64.                 # move left
  65.                 snake_pos["x_change"] = -snake_speed
  66.                 snake_pos["y_change"] = 0
  67.  
  68.             elif event.key == pygame.K_RIGHT and snake_pos["x_change"] == 0:
  69.                 # move right
  70.                 snake_pos["x_change"] = snake_speed
  71.                 snake_pos["y_change"] = 0
  72.  
  73.             elif event.key == pygame.K_UP and snake_pos["y_change"] == 0:
  74.                 # move up
  75.                 snake_pos["x_change"] = 0
  76.                 snake_pos["y_change"] = -snake_speed
  77.  
  78.             elif event.key == pygame.K_DOWN and snake_pos["y_change"] == 0:
  79.                 # move down
  80.                 snake_pos["x_change"] = 0
  81.                 snake_pos["y_change"] = snake_speed
  82.  
  83.     # clear screen
  84.     display.fill((0,0,0))
  85.  
  86.     # move snake tails
  87.     ltx = snake_pos["x"]
  88.     lty = snake_pos["y"]
  89.  
  90.     for i,v in enumerate(snake_tails):
  91.         _ltx = snake_tails[i][0]
  92.         _lty = snake_tails[i][1]
  93.  
  94.         snake_tails[i][0] = ltx
  95.         snake_tails[i][1] = lty
  96.  
  97.         ltx = _ltx
  98.         lty = _lty
  99.  
  100.     # draw snake tails
  101.     for t in snake_tails:
  102.         pygame.draw.rect(display, colors["snake_tail"], [
  103.             t[0],
  104.             t[1],
  105.             snake_size[0],
  106.             snake_size[1]])
  107.  
  108.     # draw snake
  109.     snake_pos["x"] += snake_pos["x_change"]
  110.     snake_pos["y"] += snake_pos["y_change"]
  111.  
  112.     # teleport snake, if required
  113.     if(snake_pos["x"] < -snake_size[0]):
  114.         snake_pos["x"] = width
  115.  
  116.     elif(snake_pos["x"] > width):
  117.         snake_pos["x"] = 0
  118.  
  119.     elif(snake_pos["y"] < -snake_size[1]):
  120.         snake_pos["y"] = height
  121.  
  122.     elif(snake_pos["y"] > height):
  123.         snake_pos["y"] = 0
  124.  
  125.     pygame.draw.rect(display, colors["snake_head"], [
  126.         snake_pos["x"],
  127.         snake_pos["y"],
  128.         snake_size[0],
  129.         snake_size[1]])
  130.  
  131.     # draw food
  132.     pygame.draw.rect(display, colors["apple"], [
  133.         food_pos["x"],
  134.         food_pos["y"],
  135.         food_size[0],
  136.         food_size[1]])
  137.  
  138.     # detect collision with food
  139.     if(snake_pos["x"] == food_pos["x"]
  140.         and snake_pos["y"] == food_pos["y"]):
  141.         food_eaten += 1
  142.         snake_tails.append([food_pos["x"], food_pos["y"]])
  143.  
  144.         food_pos = {
  145.             "x": round(random.randrange(0, width - snake_size[0]) / 10) * 10,
  146.             "y": round(random.randrange(0, height - snake_size[1]) / 10) * 10,
  147.         }
  148.  
  149.     # detect collision with tail
  150.     for i,v in enumerate(snake_tails):
  151.         if(snake_pos["x"]+snake_pos["x_change"] == snake_tails[i][0]
  152.             and snake_pos["y"]+snake_pos["y_change"] == snake_tails[i][1]):
  153.             snake_tails = snake_tails[:i]
  154.             break
  155.  
  156.     pygame.display.update()
  157.    
  158.     # set FPS
  159.     clock.tick(30)
  160.    
  161.  
  162. # close app, if required
  163. pygame.quit()
  164. quit()
RAW Paste Data