Advertisement
harisha

Python end

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