Advertisement
harisha

Snake

Feb 28th, 2022
957
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.03 KB | None | 0 0
  1. import pygame
  2. import time
  3. import random
  4.  
  5. pygame.init()
  6.  
  7. white = (255, 255, 255)
  8. black = (0, 0, 0)
  9. green = (0, 255, 0)
  10. blue = (50, 153, 213)
  11.  
  12. dis_width = 600
  13. dis_height = 400
  14.  
  15. dis = pygame.display.set_mode((dis_width, dis_height))
  16.  
  17.  
  18. clock = pygame.time.Clock()
  19.  
  20. snake_block = 10
  21.  
  22.  
  23.  
  24.  
  25. def our_snake(snake_block, snake_list):
  26.     for x in snake_list:
  27.         pygame.draw.rect(dis, white, [x[0], x[1], snake_block, snake_block])
  28.  
  29.  
  30. def message(msg, color):
  31.     mesg = font_style.render(msg, True, color)
  32.     dis.blit(mesg, [dis_width / 6, dis_height / 3])
  33.  
  34.  
  35. def gameLoop():
  36.     game_over = False
  37.     game_close = False
  38.  
  39.     x1 = dis_width / 2
  40.     y1 = dis_height / 2
  41.  
  42.     x1_change = 0
  43.     y1_change = 0
  44.  
  45.     snake_List = []
  46.     Length_of_snake = 1
  47.  
  48.     foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0
  49.     foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0
  50.  
  51.     while not game_over:
  52.  
  53.         while game_close == True:
  54.             dis.fill(blue)
  55.            
  56.  
  57.             pygame.display.update()
  58.  
  59.             for event in pygame.event.get():
  60.                 if event.type == pygame.KEYDOWN:
  61.                     if event.key == pygame.K_q:
  62.                         game_over = True
  63.                         game_close = False
  64.                     if event.key == pygame.K_c:
  65.                         gameLoop()
  66.  
  67.         for event in pygame.event.get():
  68.             if event.type == pygame.QUIT:
  69.                 game_over = True
  70.             if event.type == pygame.KEYDOWN:
  71.                 if event.key == pygame.K_LEFT:
  72.                     x1_change = -snake_block
  73.                     y1_change = 0
  74.                 elif event.key == pygame.K_RIGHT:
  75.                     x1_change = snake_block
  76.                     y1_change = 0
  77.                 elif event.key == pygame.K_UP:
  78.                     y1_change = -snake_block
  79.                     x1_change = 0
  80.                 elif event.key == pygame.K_DOWN:
  81.                     y1_change = snake_block
  82.                     x1_change = 0
  83.  
  84.         if x1 >= dis_width or x1 < 0 or y1 >= dis_height or y1 < 0:
  85.             game_close = True
  86.         x1 += x1_change
  87.         y1 += y1_change
  88.         dis.fill(black)
  89.         pygame.draw.rect(dis, green, [foodx, foody, snake_block, snake_block])
  90.         snake_Head = []
  91.         snake_Head.append(x1)
  92.         snake_Head.append(y1)
  93.         snake_List.append(snake_Head)
  94.         if len(snake_List) > Length_of_snake:
  95.             del snake_List[0]
  96.  
  97.         for x in snake_List[:-1]:
  98.             if x == snake_Head:
  99.                 game_close = True
  100.  
  101.         our_snake(snake_block, snake_List)
  102.  
  103.  
  104.         pygame.display.update()
  105.  
  106.         if x1 == foodx and y1 == foody:
  107.             foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0
  108.             foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0
  109.             Length_of_snake += 1
  110.  
  111.         clock.tick(10)
  112.  
  113.     pygame.quit()
  114.     quit()
  115.  
  116.  
  117. gameLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement