My First Q Assist Game Development Project

Its been a while since I blogged but the carrot of a nice T-Shirt with Q Developer CLI on it has made me jump into action.

I’d recently amplified a post on linked in for

https://community.aws/content/2xIoduO0xhkhUApQpVUIqBFGmAc/build-games-with-amazon-q-cli-and-score-a-t-shirt?trk=b085178b-f0cb-447b-b32d-bd0641720467&sc_channel=el

To address the things expected in this blog by AWS 😀

Your chosen game and why you picked it

I chose Snakes because it was the first thing that sprung to mind as something simple to describe 🙂 Also. I’m old.


Effective prompting techniques you discovered

I didn’t discover any effective prompting techniques during this process as I’d already discovered these skills historically 🙂 I made sure I included the pygames component and described the nature of how the game should work in as much details as I felt nessecary for success.


How AI handled classic programming challenges

It did the reasoning and made a single py file. Very very quickly.


Examples of development automation that saved you time

I really achieved this with just one prompt! 😀


Code examples of interesting AI-generated solutions

See the generated code below


Screenshots or gameplay footage of your final creation

See screen shot below 🙂

My process for success:
Having followed the instructions this has been my experience:;

  • I already had WSL installed with Ubuntu
  • I made sure I did a sudo apt-get update, sudo apt-get upgrade to update things as it had been a while
  • I tried to install Q CLI the way they had in the post but switched to these instructions instead: https://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/command-line-installing.html
  • I ran the command “python3 -m pip install -U pygame –user” to install pygames
  • Then I made a ~/dev/game folder and changed into it
  • Ran q
  • used /context .
  • and then entered my wonderful game prompt
using pygame write a game that has a snake that is moved around the screen by the arrow keys. the screen is bounded so hitting the outside walls will cause the snake to die and the game be over. there are random dots sread out on the screen that makes when the head of the snake consumes one it will make the snake grow longer and longer the more it eats. if the snake hits itself when turning the game is also over.

This worked way too easy.

The code Q CLI generated is as follows:

  • 1: import pygame
  • 2: import random
  • 3: import sys
  • 4:
  • 5: # Initialize pygame
  • 6: pygame.init()
  • 7:
  • 8: # Constants
  • 9: WIDTH, HEIGHT = 600, 600
  • 10: GRID_SIZE = 20
  • 11: GRID_WIDTH = WIDTH // GRID_SIZE
  • 12: GRID_HEIGHT = HEIGHT // GRID_SIZE
  • 13: FPS = 10
  • 14:
  • 15: # Colors
  • 16: BLACK = (0, 0, 0)
  • 17: WHITE = (255, 255, 255)
  • 18: GREEN = (0, 255, 0)
  • 19: RED = (255, 0, 0)
  • 20: BLUE = (0, 0, 255)
  • 21:
  • 22: # Directions
  • 23: UP = (0, -1)
  • 24: DOWN = (0, 1)
  • 25: LEFT = (-1, 0)
  • 26: RIGHT = (1, 0)
  • 27:
  • 28: class Snake:
  • 29: def init(self):
  • 30: self.reset()
  • 31:
  • 32: def reset(self):
  • 33: self.length = 1
  • 34: self.positions = [((GRID_WIDTH // 2), (GRID_HEIGHT // 2))]
  • 35: self.direction = random.choice([UP, DOWN, LEFT, RIGHT])
  • 36: self.color = GREEN
  • 37: self.score = 0
  • 38:
  • 39: def get_head_position(self):
  • 40: return self.positions[0]
  • 41:
  • 42: def change_direction(self, direction):
  • 43: if (direction[0] * -1, direction[1] * -1) == self.direction:
  • 44: return # Prevent 180-degree turns
  • 45: self.direction = direction
  • 46:
  • 47: def move(self):
  • 48: head = self.get_head_position()
  • 49: x, y = self.direction
  • 50: new_x = (head[0] + x) % GRID_WIDTH
  • 51: new_y = (head[1] + y) % GRID_HEIGHT
  • 52:
  • 53: # Check for wall collision
  • 54: if (head[0] + x < 0 or head[0] + x >= GRID_WIDTH or
  • 55: head[1] + y < 0 or head[1] + y >= GRID_HEIGHT):
  • 56: return True # Game over due to wall collision
  • 57:
  • 58: # Check for self collision
  • 59: if (new_x, new_y) in self.positions[1:]:
  • 60: return True # Game over due to self collision
  • 61:
  • 62: self.positions.insert(0, (new_x, new_y))
  • 63: if len(self.positions) > self.length:
  • 64: self.positions.pop()
  • 65:
  • 66: return False # Game continues
  • 67:
  • 68: def draw(self, surface):
  • 69: for i, p in enumerate(self.positions):
  • 70: rect = pygame.Rect(p[0] * GRID_SIZE, p[1] * GRID_SIZE, GRID_SIZE, GRID_SIZE)
  • 71: pygame.draw.rect(surface, self.color, rect)
  • 72: pygame.draw.rect(surface, BLACK, rect, 1)
  • 73:
  • 74: # Draw eyes on the head
  • 75: if i == 0:
  • 76: # Adjust eye position based on direction
  • 77: eye_size = GRID_SIZE // 5
  • 78: offset = GRID_SIZE // 3
  • 79:
  • 80: # Left eye
  • 81: left_eye_x = p[0] * GRID_SIZE + offset
  • 82: left_eye_y = p[1] * GRID_SIZE + offset
  • 83: pygame.draw.circle(surface, BLACK, (left_eye_x, left_eye_y), eye_size)
  • 84:
  • 85: # Right eye
  • 86: right_eye_x = p[0] * GRID_SIZE + GRID_SIZE – offset
  • 87: right_eye_y = p[1] * GRID_SIZE + offset
  • 88: pygame.draw.circle(surface, BLACK, (right_eye_x, right_eye_y), eye_size)
  • 89:
  • 90: class Food:
  • 91: def init(self):
  • 92: self.position = (0, 0)
  • 93: self.color = RED
  • 94: self.randomize_position()
  • 95:
  • 96: def randomize_position(self):
  • 97: self.position = (random.randint(0, GRID_WIDTH – 1),
  • 98: random.randint(0, GRID_HEIGHT – 1))
  • 99:
  • 100: def draw(self, surface):
  • 101: rect = pygame.Rect(self.position[0] * GRID_SIZE, self.position[1] * GRID_SIZE,
  • 102: GRID_SIZE, GRID_SIZE)
  • 103: pygame.draw.rect(surface, self.color, rect)
  • 104: pygame.draw.rect(surface, BLACK, rect, 1)
  • 105:
  • 106: def draw_grid(surface):
  • 107: for y in range(0, HEIGHT, GRID_SIZE):
  • 108: for x in range(0, WIDTH, GRID_SIZE):
  • 109: rect = pygame.Rect(x, y, GRID_SIZE, GRID_SIZE)
  • 110: pygame.draw.rect(surface, BLACK, rect, 1)
  • 111:
  • 112: def main():
  • 113: screen = pygame.display.set_mode((WIDTH, HEIGHT))
  • 114: pygame.display.set_caption(‘Snake Game’)
  • 115: clock = pygame.time.Clock()
  • 116: font = pygame.font.SysFont(‘Arial’, 25)
  • 117:
  • 118: snake = Snake()
  • 119: food = Food()
  • 120:
  • 121: game_over = False
  • 122:
  • 123: while True:
  • 124: for event in pygame.event.get():
  • 125: if event.type == pygame.QUIT:
  • 126: pygame.quit()
  • 127: sys.exit()
  • 128: elif event.type == pygame.KEYDOWN:
  • 129: if game_over:
  • 130: if event.key == pygame.K_SPACE:
  • 131: snake.reset()
  • 132: food.randomize_position()
  • 133: game_over = False
  • 134: else:
  • 135: if event.key == pygame.K_UP:
  • 136: snake.change_direction(UP)
  • 137: elif event.key == pygame.K_DOWN:
  • 138: snake.change_direction(DOWN)
  • 139: elif event.key == pygame.K_LEFT:
  • 140: snake.change_direction(LEFT)
  • 141: elif event.key == pygame.K_RIGHT:
  • 142: snake.change_direction(RIGHT)
  • 143:
  • 144: if not game_over:
  • 145: # Move snake
  • 146: game_over = snake.move()
  • 147:
  • 148: # Check if snake ate food
  • 149: if snake.get_head_position() == food.position:
  • 150: snake.length += 1
  • 151: snake.score += 10
  • 152: food.randomize_position()
  • 153:
  • 154: # Make sure food doesn’t appear on snake
  • 155: while food.position in snake.positions:
  • 156: food.randomize_position()
  • 157:
  • 158: # Draw everything
  • 159: screen.fill(WHITE)
  • 160: draw_grid(screen)
  • 161: snake.draw(screen)
  • 162: food.draw(screen)
  • 163:
  • 164: # Display score
  • 165: score_text = font.render(f’Score: {snake.score}’, True, BLUE)
  • 166: screen.blit(score_text, (5, 5))
  • 167:
  • 168: if game_over:
  • 169: game_over_text = font.render(‘Game Over! Press SPACE to restart’, True, BLUE)
  • 170: text_rect = game_over_text.get_rect(center=(WIDTH//2, HEIGHT//2))
  • 171: screen.blit(game_over_text, text_rect)
  • 172:
  • 173: pygame.display.update()
  • 174: clock.tick(FPS)
  • 175:
  • 176: if name == “main“:
  • 177: main()

How cool is this! ?

I’ll let ya know if I manage to get a T-Shirt out this 😀 Thanks AWS if so! 🙂

Cheers!