
From Concept to Playable in seconds: Creating the Greedy Snake Game with Amazon Bedrock
This blog post demonstrates creating a Greedy Snake game from scratch using Amazon Bedrock and a text prompt, highlighting the speed and capabilities of large language models on Amazon Bedrock like Llama 3.1 in transforming ideas into runnable code. It also covers prompt engineering best practices.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import pygame
import sys
import random
# Initialize Pygame
pygame.init()
# Set up some constants
WIDTH, HEIGHT = 800, 600
BLOCK_SIZE = 20
SPEED = 10
# Set up some colors
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
# Set up the display
screen = pygame.display.set_mode((WIDTH, HEIGHT))
# Set up the font
font = pygame.font.Font(None, 36)
# Set up the snake and food
snake = [(200, 200), (220, 200), (240, 200)]
food = (400, 300)
# Set up the direction
direction = 'RIGHT'
# Game loop
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP and direction != 'DOWN':
direction = 'UP'
elif event.key == pygame.K_DOWN and direction != 'UP':
direction = 'DOWN'
elif event.key == pygame.K_LEFT and direction != 'RIGHT':
direction = 'LEFT'
elif event.key == pygame.K_RIGHT and direction != 'LEFT':
direction = 'RIGHT'
# Move the snake
head = snake[-1]
if direction == 'UP':
new_head = (head[0], head[1] - BLOCK_SIZE)
elif direction == 'DOWN':
new_head = (head[0], head[1] + BLOCK_SIZE)
elif direction == 'LEFT':
new_head = (head[0] - BLOCK_SIZE, head[1])
elif direction == 'RIGHT':
new_head = (head[0] + BLOCK_SIZE, head[1])
snake.append(new_head)
# Check if the snake has eaten the food
if snake[-1] == food:
food = (random.randint(0, WIDTH - BLOCK_SIZE) // BLOCK_SIZE * BLOCK_SIZE,
random.randint(0, HEIGHT - BLOCK_SIZE) // BLOCK_SIZE * BLOCK_SIZE)
else:
snake.pop(0)
# Check if the snake has hit the edge or itself
if (snake[-1][0] < 0 or snake[-1][0] >= WIDTH or
snake[-1][1] < 0 or snake[-1][1] >= HEIGHT or
snake[-1] in snake[:-1]):
pygame.quit()
sys.exit()
# Draw everything
screen.fill(WHITE)
for pos in snake:
pygame.draw.rect(screen, GREEN, pygame.Rect(pos[0], pos[1], BLOCK_SIZE, BLOCK_SIZE))
pygame.draw.rect(screen, RED, pygame.Rect(food[0], food[1], BLOCK_SIZE, BLOCK_SIZE))
text = font.render(f'Score: {len(snake)}', True, (0, 0, 0))
screen.blit(text, (10, 10))
# Update the display
pygame.display.flip()
# Cap the frame rate
pygame.time.delay(1000 // SPEED)
1
2
3
4
5
6
7
8
**Strengths:**
1. **Clear and concise**: Your prompt is well-structured and easy to understand.
2. **Specific directives**: You explicitly state the desired output format (Python script) and the level of expertise (very skilled Python expert).
3. **Context and few-shot example prompts**: You provide a clear task description (Greedy Snake Game) and specify the expected output (high-quality Python script).
4. **Desired size of the response**: You ask for a "short" script.
5. **Specific response format**: You use the ```python``` block to indicate the expected format.
6. **Error handling**: You instruct the model to check its work and list errors or write "CHECKED: NO ERRORS" if none are found.
1
2
3
4
5
6
1. **Weaknesses:**
1. **Ambiguity**: While your prompt is generally clear, the phrase "something a very skilled python expert would write" might be subjective and open to interpretation.
2. **Negative formulations**: You use "NEVER write anything before the ```python``` block," which could be rephrased as "Only write code within the ```python``` block."
3. **Lack of constraints**: You don't specify any constraints on the script's length, complexity, or specific requirements (e.g., game features, difficulty level).
4. **No disclaimers**: You don't mention any sensitive domains or potential issues with the game's content.
1
2
3
4
5
6
**Suggestions:**
1. Consider rephrasing "something a very skilled python expert would write" to a more objective requirement, such as "follow best practices for Python coding."
2. Replace "NEVER write anything before the ```python``` block" with a more positive instruction, like "Only write code within the ```python``` block."
3. Add constraints, such as "The script should not exceed 200 lines of code" or "The game should have a minimum of 3 levels."
4. Consider adding a disclaimer, like "Please ensure the game's content is suitable for all ages and does not contain any offensive material."
Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.