Simple Python Program Code for Snake Game

 import pygame

import time

import random

 

pygame.init()

 

white = (255, 255, 255)

yellow = (255, 255, 102)

black = (0, 0, 0)

red = (213, 50, 80)

green = (0, 255, 0)

blue = (50, 153, 213)

 

dis_width = 600

dis_height = 400

 

dis = pygame.display.set_mode((dis_width, dis_height))

pygame.display.set_caption('Snake Game by @ChatGPT')

 

clock = pygame.time.Clock()

 

snake_block = 10

snake_speed = 15

 

font_style = pygame.font.SysFont(None, 30)

 

 

def message(msg, color):

    mesg = font_style.render(msg, True, color)

    dis.blit(mesg, [dis_width / 6, dis_height / 3])

 

 

def gameLoop():  

    game_over = False

    game_close = False

 

    x1 = dis_width / 2

    y1 = dis_height / 2

 

    x1_change = 0       

    y1_change = 0

 

    foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0

    foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0

 

    while not game_over:

 

        while game_close == True:

            dis.fill(blue)

            message("You Lost! Press Q-Quit or C-Play Again", red)

            pygame.display.update()

            

            for event in pygame.event.get():

                if event.type == pygame.KEYDOWN:

                    if event.key == pygame.K_q:

                        game_over = True

                        game_close = False

                    if event.key == pygame.K_c:

                        gameLoop()

 

        for event in pygame.event.get():

            if event.type == pygame.QUIT:

                game_over = True

            if event.type == pygame.KEYDOWN:

                if event.key == pygame.K_LEFT:

                    x1_change = -snake_block

                    y1_change = 0

                elif event.key == pygame.K_RIGHT:

                    x1_change = snake_block

                    y1_change = 0

                elif event.key == pygame.K_UP:

                    y1_change = -snake_block

                    x1_change = 0

                elif event.key == pygame.K_DOWN:

                    y1_change = snake_block

                    x1_change = 0

 

        if x1 >= dis_width or x1 < 0 or y1 >= dis_height or y1 < 0:

            game_close = True

 

        x1 += x1_change

        y1 += y1_change

        dis.fill(blue)

        pygame.draw.rect(dis, green, [foodx, foody, snake_block, snake_block])

        pygame.draw.rect(dis, black, [x1, y1, snake_block, snake_block])

        pygame.display.update()

        

        if x1 == foodx and y1 == foody:

            print("Yummy!!")

        clock.tick(snake_speed)

 

    pygame.quit()

    quit()

 

 

gameLoop()


Explanation of the above Python code for a basic snake game using the Pygame library:

  1. First, we import the necessary libraries - pygame, time, and random. We also initialize the Pygame library using the pygame.init() method.

  2. We define some color constants using RGB values, and set the dimensions of the game window using the dis_width and dis_height variables.

  3. We create the game window using the pygame.display.set_mode() method and set the caption using the pygame.display.set_caption() method.

  4. We initialize the game clock using the pygame.time.Clock() method, which will be used to control the game's frame rate.

  5. We set the size of the snake and its movement speed using the snake_block and snake_speed variables.

  6. We create a font style using the pygame.font.SysFont() method to display messages on the game screen.

  7. We define a message() function to display messages on the game screen, which takes the message to be displayed and its color as input parameters.

  8. We define the main game loop using the gameLoop() function.

  9. We set the initial state of the game - game_over and game_close variables are set to False.

  10. We set the initial position of the snake using the x1 and y1 variables, and initialize the snake's movement direction using the x1_change and y1_change variables.

  11. We randomly generate the position of the food using the random.randrange() method and store it in foodx and foody variables.

  12. We enter the main game loop where we keep track of the game state and handle user input events.

  13. We check if the game is over or not, and display appropriate messages on the game screen.

  14. We handle user input events using the pygame.event.get() method, which returns a list of all the events that have occurred since the last time it was called.

  15. We move the snake based on the user input and update its position using the x1_change and y1_change variables.

  16. We check if the snake has collided with the wall, and if so, set the game_close variable to True.

  17. We draw the snake and food on the game screen using the pygame.draw.rect() method.

  18. We check if the snake has eaten the food, and if so, generate a new food position and increase the length of the snake.

  19. We control the frame rate of the game using the clock.tick() method.

  20. Finally, we quit the Pygame library using the pygame.quit() method and exit the Python program using the quit() function.

Comments

Popular posts from this blog

Iris Flowers Classification ML Project with Code

Project 01: Python Code for Music Recommendation with Explanation