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:
First, we import the necessary libraries -
pygame
,time
, andrandom
. We also initialize the Pygame library using thepygame.init()
method.We define some color constants using RGB values, and set the dimensions of the game window using the
dis_width
anddis_height
variables.We create the game window using the
pygame.display.set_mode()
method and set the caption using thepygame.display.set_caption()
method.We initialize the game clock using the
pygame.time.Clock()
method, which will be used to control the game's frame rate.We set the size of the snake and its movement speed using the
snake_block
andsnake_speed
variables.We create a font style using the
pygame.font.SysFont()
method to display messages on the game screen.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.We define the main game loop using the
gameLoop()
function.We set the initial state of the game -
game_over
andgame_close
variables are set toFalse
.We set the initial position of the snake using the
x1
andy1
variables, and initialize the snake's movement direction using thex1_change
andy1_change
variables.We randomly generate the position of the food using the
random.randrange()
method and store it infoodx
andfoody
variables.We enter the main game loop where we keep track of the game state and handle user input events.
We check if the game is over or not, and display appropriate messages on the game screen.
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.We move the snake based on the user input and update its position using the
x1_change
andy1_change
variables.We check if the snake has collided with the wall, and if so, set the
game_close
variable toTrue
.We draw the snake and food on the game screen using the
pygame.draw.rect()
method.We check if the snake has eaten the food, and if so, generate a new food position and increase the length of the snake.
We control the frame rate of the game using the
clock.tick()
method.Finally, we quit the Pygame library using the
pygame.quit()
method and exit the Python program using thequit()
function.
Comments
Post a Comment