Advertisement
rishabbansal21

Day-6

Jan 13th, 2021 (edited)
946
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.70 KB | None | 0 0
  1. import pygame
  2. import random
  3.  
  4. pygame.init()
  5. clock = pygame.time.Clock()
  6.  
  7. #SCREEN
  8. screen = pygame.display.set_mode((800,600))
  9.  
  10. #CAPTION
  11. pygame.display.set_caption("SNAKE GAME")
  12.  
  13. #SNAKE KI POSITIONS
  14. snake_pos = [[400,300],[420,300],[440,300]]
  15.  
  16. #APPLE KI POSITION
  17. #apple_pos = [100,100]
  18. apple_pos = [(random.randint(100,700)//20)*20, (random.randint(100,500)//20)*20]
  19.  
  20. #SCORE
  21. score = 0
  22.  
  23. #DIRECTIONS
  24. step = 20
  25. up = (0, -step)
  26. left = (-step,0)
  27. right = (step,0)
  28. down = (0,step)
  29.  
  30. direction = left
  31.  
  32. #FONT
  33. font = pygame.font.SysFont('Arial', 30)
  34.  
  35. timer = 0
  36. running = True
  37.  
  38. while running:
  39.     screen.fill((20,150,20))
  40.  
  41.     #EVENT FETCHER
  42.     for event in pygame.event.get():
  43.  
  44.         #QUIT
  45.         if event.type == pygame.QUIT:
  46.             running = False
  47.  
  48.         #KEYDOWN
  49.         if event.type == pygame.KEYDOWN:
  50.             if event.key == pygame.K_UP:
  51.                 print("UP")
  52.                 direction = up
  53.             if event.key == pygame.K_DOWN:
  54.                 print("DOWN")
  55.                 direction = down
  56.             if event.key == pygame.K_LEFT:
  57.                 print("LEFT")
  58.                 direction = left
  59.             if event.key == pygame.K_RIGHT:
  60.                 print("RIGHT")
  61.                 direction = right
  62.  
  63.  
  64.     #snake_pos = [[snake_pos[0][0]+direction[0], snake_pos[0][1] + direction[1]]]+snake_pos[:-1]
  65.  
  66.     timer += 1
  67.     if timer == 6:
  68.         snake_pos = [[snake_pos[0][0]+direction[0], snake_pos[0][1] + direction[1]]]+snake_pos[:-1]
  69.         timer = 0
  70.  
  71.  
  72.     #IF SNAKE EATS APPLE
  73.     if snake_pos[0] == apple_pos:
  74.         apple_pos = [(random.randint(100,700)//20)*20, (random.randint(100,500)//20)*20]
  75.         snake_pos.append(snake_pos[-1])
  76.         score += 1
  77.  
  78.  
  79.     #SNAKE DEATH
  80.     for i in range(1, len(snake_pos)):
  81.         if snake_pos[0] == snake_pos[i]:
  82.             print("DEAD!")
  83.             running = False
  84.  
  85.         #VERTICAL BOUNDARY
  86.         if 800 <= snake_pos[0][0] or snake_pos[0][0] <= 0:
  87.             print("DEAD!")
  88.             running = False
  89.        
  90.         #HORIZONTAL BOUNDARY
  91.         if 0 >= snake_pos[0][1] or snake_pos[0][1] >= 600:
  92.             print("DEAD!")
  93.             running = False
  94.  
  95.     #SNAKE PRINT
  96.     for x,y in snake_pos:
  97.         pygame.draw.circle(screen, (0,0,200), (x,y), 10)
  98.  
  99.     #APPLE PRINT
  100.     pygame.draw.circle(screen, (200,0,0), apple_pos, 10)
  101.  
  102.     #pygame.draw.circle(screen, (0,0,200), (400,300), 10)
  103.     #pygame.draw.circle(screen, (0,0,200), (420,300), 10)
  104.     #pygame.draw.circle(screen, (0,0,200), (440,300), 10)
  105.  
  106.     #SCORE PRINT
  107.     text = font.render( "SCORE: " + str(score), True, (255,255,255) )
  108.     screen.blit(text, (0,0))
  109.  
  110.     clock.tick(30)
  111.     pygame.display.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement