Advertisement
Arcot

snake game

Sep 29th, 2022 (edited)
756
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.17 KB | None | 0 0
  1. import pygame
  2. import time
  3. import random
  4.  
  5. snake_speed = 15
  6. height = 720
  7. width = 480
  8.  
  9. BLACK = (0, 0, 0)
  10. WHITE = (255, 255, 255)
  11. RED = (255, 0, 0)
  12. GREEN = (0, 255, 0)
  13. BLUE = (0, 0, 255)
  14.  
  15. pygame.init()
  16. game_window = pygame.display.set_mode((width, height))
  17.  
  18. pygame.display.set_caption("Snake")
  19.  
  20. fps = pygame.time.Clock() #frames per second = fps
  21.  
  22. snake_position = [100, 50]
  23.  
  24. #defining first 4 blocks of snake body
  25. snake_body = [[100, 50],
  26.             [90, 50],
  27.             [80, 50],
  28.             [70, 50]
  29.             ]
  30.  
  31. fruit_position = [random.randrange(1, (width//10)) * 10,
  32.                  random.randrange(1, (height//10)) * 10]
  33.  
  34. fruit_spawn = True
  35.  
  36. direction = 'RIGHT'
  37.  
  38. change_to = direction
  39.  
  40. #initial score
  41. score = 0
  42.  
  43. #function to display score
  44. def show_score(choice, color, font, size):
  45.     score_font = pygame.font.SysFont(font, size)
  46.     score_surface = score_font.render('Score: ' + str(score), True, color)
  47.     #creating a rectangular object for the text
  48.     score_rect = score_surface.get_rect()
  49.     #displaying text
  50.     game_window.blit(score_surface, score_rect)
  51.  
  52. #game over function
  53. def game_over():
  54.     my_font = pygame.font.SysFont('times new roman', 50)
  55.     #defining surface on which to draw text
  56.     game_over_surface = my_font.render('Your score is:' + str(score), True, RED)
  57.     #creating a rectangular object for the text
  58.     game_over_rect = game_over_surface.get_rect()
  59.     #setting potision of the text
  60.     game_over_rect.midtop = (width/2, height/4)
  61.     game_window.blit(game_over_surface, game_over_rect)
  62.     pygame.display.flip()
  63.     #adding line to quit program after 2 seconds
  64.     time.sleep(2) #value is time in seconds
  65.     #deactivating pygame library
  66.     pygame.quit()
  67.     #qutting the program
  68.     quit()
  69.  
  70. #main
  71. while True:
  72.     for event in pygame.event.get():
  73.         if event.type == pygame.KEYDOWN:
  74.             if event.key == pygame.K_UP:
  75.                 change_to = 'UP'
  76.             if event.key == pygame.K_DOWN:
  77.                 change_to = 'DOWN'
  78.             if event.key == pygame.K_LEFT:
  79.                 change_to = 'LEFT'
  80.             if event.key == pygame.K_RIGHT:
  81.                 change_to == 'RIGHT'
  82.     #if two keys pressed simultaneously,  we dont want
  83.     #snake to move in two different directions
  84.  
  85.     if change_to == 'UP' and direction != 'DOWN':
  86.         direction = 'UP'
  87.     if change_to == 'DOWN' and direction != 'UP':
  88.         direction = 'DOWN'
  89.     if change_to == 'LEFT' and direction != 'RIGHT':
  90.         direction = 'LEFT'
  91.     if change_to == 'RIGHT' and direction != 'LEFT':
  92.         direction = 'RIGHT'
  93.    
  94.     #moving the snake
  95.     if direction == 'UP':
  96.         snake_position[1] -= 10
  97.     if direction == 'DOWN':
  98.         snake_position[1] += 10
  99.     if direction == 'LEFT':
  100.         snake_position[0] -= 10
  101.     if direction == 'RIGHT':
  102.         snake_position[0] += 10
  103.    
  104.     #snake body growing mechanism
  105.     #if fruit and snake collide then scores increase by 1
  106.     snake_body.insert(0, list(snake_position))
  107.     if snake_position[0] == fruit_position[0] and snake_position[1] == fruit_position[1]:
  108.         score += 1
  109.         fruit_spawn = False
  110.     else:
  111.         snake_body.pop()
  112.  
  113.     if not fruit_spawn:
  114.         fruit_position = [random.randrange(1, (width//10)) * 10,
  115.                         random.randrange(1, (height//10)) * 10]
  116.    
  117.     fruit_spawn = True
  118.     game_window.fill(BLACK)
  119.  
  120.     for pos in snake_body:
  121.         pygame.draw.rect(game_window, GREEN, pygame.Rect(pos[0], pos[1], 10, 10))
  122.    
  123.     pygame.draw.rect(game_window, WHITE, pygame.Rect(fruit_position[0], fruit_position[1], 10, 10))
  124.  
  125.     #game over condition
  126.     if snake_position[0] < 0 or snake_position[0] > width-10:
  127.         game_over()
  128.     if snake_position[1] < 0 or snake_position[1] > height-10:
  129.         game_over()
  130.  
  131.     #touching snake body
  132.     for block in snake_body[1:]:
  133.         if snake_position[0] == block[0] and snake_position[1] == block[1]:
  134.             game_over()
  135.    
  136.     #displaying score continuously
  137.     show_score(1, WHITE, 'times new roman', 20)
  138.    
  139.     #updating window
  140.     pygame.display.update()
  141.  
  142.     #refresh rate/frame rate per second
  143.     fps.tick(snake_speed)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement