Why I Built This Game
As part of the Amazon Q CLI Game Challenge, I decided to build a simple but fun Snake Game using Python and Pygame.
The goal was to explore how Amazon Q (AI assistant from AWS) can support game development and help us learn coding with ease.
I chose Snake because itβs a classic game that is both nostalgic and a great starting point for understanding game loops, input handling, and collision logic.
What I Learned
During this challenge, I learned:
How to use Amazon Q to generate Python game code using prompts
How Pygame handles game rendering, movement, and user input
The importance of breaking a project into smaller pieces (game loop, food generation, collision detection)
How to run Python code using virtual environments and install external libraries like Pygame
Even though I had some installation issues with Q CLI, I was able to use the web version of Q to get the code and build this game successfully!
Code Sample
snake_game.py
Snake game code generated by Amazon Q CLI
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)
width = 600
height = 400
dis = pygame.display.set_mode((width, height))
pygame.display.set_caption('Snake Game by Bala π')
clock = pygame.time.Clock()
snake_block = 10
snake_speed = 15
font_style = pygame.font.SysFont(None, 30)
score_font = pygame.font.SysFont(None, 25)
def message(msg, color):
mesg = font_style.render(msg, True, color)
dis.blit(mesg, [width / 6, height / 3])
def draw_snake(snake_block, snake_list):
for x in snake_list:
pygame.draw.rect(dis, black, [x[0], x[1], snake_block, snake_block])
def gameLoop():
game_over = False
game_close = False
x1 = width / 2
y1 = height / 2
x1_change = 0
y1_change = 0
snake_list = []
length_of_snake = 1
foodx = round(random.randrange(0, width - snake_block) / 10.0) * 10.0
foody = round(random.randrange(0, height - snake_block) / 10.0) * 10.0
while not game_over:
while game_close:
dis.fill(blue)
message("Game Over! Press Q to Quit or C to 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 >= width or x1 < 0 or y1 >= 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])
snake_head = [x1, y1]
snake_list.append(snake_head)
if len(snake_list) > length_of_snake:
del snake_list[0]
for segment in snake_list[:-1]:
if segment == snake_head:
game_close = True
draw_snake(snake_block, snake_list)
pygame.display.update()
if x1 == foodx and y1 == foody:
foodx = round(random.randrange(0, width - snake_block) / 10.0) * 10.0
foody = round(random.randrange(0, height - snake_block) / 10.0) * 10.0
length_of_snake += 1
clock.tick(snake_speed)
pygame.quit()
quit()
gameLoop()
Snake game code generated by Amazon Q CLI
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)
width = 600
height = 400
dis = pygame.display.set_mode((width, height))
pygame.display.set_caption('Snake Game by Bala π')
clock = pygame.time.Clock()
snake_block = 10
snake_speed = 15
font_style = pygame.font.SysFont(None, 30)
score_font = pygame.font.SysFont(None, 25)
def message(msg, color):
mesg = font_style.render(msg, True, color)
dis.blit(mesg, [width / 6, height / 3])
def draw_snake(snake_block, snake_list):
for x in snake_list:
pygame.draw.rect(dis, black, [x[0], x[1], snake_block, snake_block])
def gameLoop():
game_over = False
game_close = False
x1 = width / 2
y1 = height / 2
x1_change = 0
y1_change = 0
snake_list = []
length_of_snake = 1
foodx = round(random.randrange(0, width - snake_block) / 10.0) * 10.0
foody = round(random.randrange(0, height - snake_block) / 10.0) * 10.0
while not game_over:
while game_close:
dis.fill(blue)
message("Game Over! Press Q to Quit or C to 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 >= width or x1 < 0 or y1 >= 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])
snake_head = [x1, y1]
snake_list.append(snake_head)
if len(snake_list) > length_of_snake:
del snake_list[0]
for segment in snake_list[:-1]:
if segment == snake_head:
game_close = True
draw_snake(snake_block, snake_list)
pygame.display.update()
if x1 == foodx and y1 == foody:
foodx = round(random.randrange(0, width - snake_block) / 10.0) * 10.0
foody = round(random.randrange(0, height - snake_block) / 10.0) * 10.0
length_of_snake += 1
clock.tick(snake_speed)
pygame.quit()
quit()
gameLoop()
GitHub Repo:
https://github.com/balamanikandanb191/Snake_Game_Amazon-Q-CLI_Challenge
Thank you AWS and Amazon Q for this fun opportunity. I enjoyed the challenge and learned how AI can help build creative projects.
I'm excited to keep exploring what else I can build with Amazon Q CLI!
Top comments (0)