
From Concept to Playable in Seconds: Creating the Greedy Snake Game with Amazon Q Developer
Demonstrating AI's potential for rapid prototyping, this post details using Amazon Q Developer to generate a Greedy Snake game from prompts, including the AI's thought process, final Python code leveraging Pygame, and iterative improvements.
1
2
3
4
5
6
7
8
Write a short and high-quality python script for the following task, something a very skilled python expert would write. You are writing code for an experienced developer so only add comments for things that are non-obvious. Make sure to include any imports required.
NEVER write anything before the ```python``` block. After you are done generating the code and after the ```python``` block, check your work carefully to make sure there are no mistakes, errors, or inconsistencies.
If there are errors, list those errors in tags, then generate a new version with those errors fixed. If there are no errors, write "CHECKED: NO ERRORS" in tags.
Here is the task: write a greedy snake game.
Double check your work to ensure no errors or inconsistencies.
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import pygame
import random
# Initialize Pygame
pygame.init()
# Set up the game window
width, height = 640, 480
window = pygame.display.set_mode((width, height))
pygame.display.set_caption("Greedy Snake")
# Colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
# Snake properties
snake_block = 20
snake_speed = 15
# Initialize clock
clock = pygame.time.Clock()
# Font for score display
font = pygame.font.SysFont(None, 50)
def draw_snake(snake_list):
for block in snake_list:
pygame.draw.rect(window, GREEN, [block[0], block[1], snake_block, snake_block])
def game_loop():
game_over = False
game_close = False
# Starting position of the snake
x1 = width // 2
y1 = height // 2
# Initial movement
x1_change = 0
y1_change = 0
# Initialize snake
snake_list = []
length_of_snake = 1
# Generate first food
foodx = round(random.randrange(0, width - snake_block) / snake_block) * snake_block
foody = round(random.randrange(0, height - snake_block) / snake_block) * snake_block
while not game_over:
while game_close:
window.fill(BLACK)
message = font.render("You Lost! Press Q-Quit or C-Play Again", True, RED)
window.blit(message, [width // 6, height // 3])
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:
game_loop()
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
# Check if snake hits the boundaries
if x1 >= width or x1 < 0 or y1 >= height or y1 < 0:
game_close = True
x1 += x1_change
y1 += y1_change
window.fill(BLACK)
pygame.draw.rect(window, RED, [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]
# Check if snake hits itself
for segment in snake_list[:-1]:
if segment == snake_head:
game_close = True
draw_snake(snake_list)
# Display score
score = font.render(f"Score: {length_of_snake - 1}", True, WHITE)
window.blit(score, [0, 0])
pygame.display.update()
# Check if snake eats food
if x1 == foodx and y1 == foody:
foodx = round(random.randrange(0, width - snake_block) / snake_block) * snake_block
foody = round(random.randrange(0, height - snake_block) / snake_block) * snake_block
length_of_snake += 1
clock.tick(snake_speed)
pygame.quit()
quit()
if __name__ == "__main__":
game_loop()
1
The code works, but the speed of the snake is too fast. I'm just a beginner at the game, could you adjust the game's speed for me?
1
snake_speed = 8
1
“An illustration of a greedy snake slithering through a modern abstract circuit board design, with binary code and computer components in the background, representing the fusion of classic gaming and cutting-edge AI technology. The snake should be large, bold, and vibrant, with a sleek and dynamic appearance. The background should have a futuristic and digital aesthetic, with glowing neon colors and intricate circuitry patterns. The overall style should be a blend of retro gaming elements and contemporary AI/tech motifs.”
Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.