Snake Game
Snake Game
February 1, 2024
snake_speed = 15
# Window size
window_x = 720
window_y = 480
# defining colors
pygame.init()
pygame.font.init()
black = pygame.Color(0, 0, 0)
white = pygame.Color(255, 255, 255)
red = pygame.Color(255, 0, 0)
green = pygame.Color(0, 255, 0)
blue = pygame.Color(0, 0, 255)
1
[3]: snake_position = [100, 50]
# displaying text
game_window.blit(score_surface, score_rect)
Game function
[5]: def game_over():
pygame.init()
pygame.font.init()
# creating font object my_font
my_font = pygame.font.SysFont('times new roman', 50)
2
# creating a text surface on which text
# will be drawn
game_over_surface = my_font.render('Your Score is : ' + str(score),␣
↪True, red)
Game function
[ ]: # Main Function
while True:
3
direction = 'UP'
if change_to == 'DOWN' and direction != 'UP':
direction = 'DOWN'
if change_to == 'LEFT' and direction != 'RIGHT':
direction = 'LEFT'
if change_to == 'RIGHT' and direction != 'LEFT':
direction = 'RIGHT'
score += 10
fruit_spawn = False
else:
snake_body.pop()
if not fruit_spawn:
fruit_position = [random.randrange(1, (window_x//10)) * 10,
random.randrange(1, (window_y//
↪10)) * 10]
fruit_spawn = True
game_window.fill(black)
4
if snake_position[1] < 0 or snake_position[1] > window_y-10:
game_over()
game_over()