import pygame import random # Check for modules of pygame pygame.init() # Defining RGB color white = (255, 255, 255) red = (255, 0, 0) black = (0, 0, 0) # Creating Screen screen_width = 900 screen_height = 600 gameWindow = pygame.display.set_mode((screen_height, screen_height)) pygame.display.set_caption('Snake Game') # this is used to pygameDisplay pygame.display.update() # Game Specific Variables clock = pygame.time.Clock() exit_game = False game_over = False snake_x = 45 snake_y = 55 snake_size = 11 score = 0 food_size = 10 init_vel = 5 velocity_x = 0 velocity_y = 0 fps = 60 food_x = random.randint(20, int(screen_width / 2)) food_y = random.randint(20, int(screen_height / 2)) while not exit_game: for event in pygame.event.get(): if event.type == pygame.QUIT: exit_game = True if event.type == pygame.KEYDOWN: if event.key == pygame.K_RIGHT: velocity_x = init_vel velocity_y = 0 if event.key == pygame.K_LEFT: velocity_x = - init_vel velocity_y = 0 if event.key == pygame.K_UP: velocity_y = - init_vel velocity_x = 0 if event.key == pygame.K_DOWN: velocity_y = init_vel velocity_x = 0 if abs(snake_x - food_x) < 6 and abs(snake_y - food_y) < 6: score += 1 print('SCORE: ', score * 10) food_x = random.randint(20, int(screen_width / 2)) food_y = random.randint(20, int(screen_height / 2)) snake_x = snake_x + velocity_x snake_y = snake_y + velocity_y gameWindow.fill(white) pygame.draw.rect(gameWindow, black, [snake_x, snake_y, snake_size, snake_size]) pygame.draw.rect(gameWindow, red, [food_x, food_y, food_size, food_size]) pygame.display.update() clock.tick(fps) pygame.quit() quit()