Select your cookie preferences

We use essential cookies and similar tools that are necessary to provide our site and services. We use performance cookies to collect anonymous statistics, so we can understand how customers use our site and make improvements. Essential cookies cannot be deactivated, but you can choose “Customize” or “Decline” to decline performance cookies.

If you agree, AWS and approved third parties will also use cookies to provide useful site features, remember your preferences, and display relevant content, including relevant advertising. To accept or decline all non-essential cookies, choose “Accept” or “Decline.” To make more detailed choices, choose “Customize.”

AWS Logo
Menu
AWS Game Hackathon - Cloud Tetris

AWS Game Hackathon - Cloud Tetris

This is the requirements for the implementation game from AWS Devpost Hackathon

Published Jan 13, 2025

The implementation

I started the generation of this tetris game with a basic idea: create and implement a game from scratch getting help from generative AI using Amazon Q.

What is Anazon Q?

Amazon Q offers user-based plans, so you get features, pricing, and options tailored to how you use the product. Amazon Q can adapt its interactions to each individual user based on the existing identities, roles, and permissions of your business. AWS never uses customers’ content from Amazon Q to train the underlying models. In other words, your company information remains secure and private.
So this was my main idea (speaking in code)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import pygame
import random

# Initialize pygame
pygame.init()

# Screen dimensions
SCREEN_WIDTH, SCREEN_HEIGHT = 300, 600
GRID_SIZE = 30 # Size of each grid square
GRID_WIDTH, GRID_HEIGHT = SCREEN_WIDTH // GRID_SIZE, SCREEN_HEIGHT // GRID_SIZE

# Colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GRAY = (128, 128, 128)
COLORS = [
(0, 255, 255), # Cyan
(255, 0, 255), # Purple
(255, 255, 0), # Yellow
(0, 255, 0), # Green
(255, 0, 0), # Red
(0, 0, 255), # Blue
(255, 165, 0), # Orange
]

# Tetromino shapes
SHAPES = [
[[1, 1, 1, 1]], # I
[[1, 1], [1, 1]], # O
[[0, 1, 0], [1, 1, 1]], # T
[[1, 0, 0], [1, 1, 1]], # L
[[0, 0, 1], [1, 1, 1]], # J
[[0, 1, 1], [1, 1, 0]], # S
[[1, 1, 0], [0, 1, 1]], # Z
]

# Initialize screen
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Tetris")

# Clock for controlling the game loop
clock = pygame.time.Clock()

class Tetromino:
def __init__(self, shape, color):
self.shape = shape
self.color = color
self.x = GRID_WIDTH // 2 - len(shape[0]) // 2
self.y = 0

def rotate(self):
self.shape = [list(row) for row in zip(*self.shape[::-1])]

def check_collision(grid, tetromino, offset_x=0, offset_y=0):
for y, row in enumerate(tetromino.shape):
for x, cell in enumerate(row):
if cell:
new_x = tetromino.x + x + offset_x
new_y = tetromino.y + y + offset_y
if new_x < 0 or new_x >= GRID_WIDTH or new_y >= GRID_HEIGHT or grid[new_y][new_x]:
return True
return False

def merge_grid(grid, tetromino):
for y, row in enumerate(tetromino.shape):
for x, cell in enumerate(row):
if cell:
grid[tetromino.y + y][tetromino.x + x] = tetromino.color

def clear_lines(grid):
full_rows = [i for i, row in enumerate(grid) if all(row)]
for i in full_rows:
del grid[i]
grid.insert(0, [0] * GRID_WIDTH)
return len(full_rows)

def draw_grid(grid):
for y in range(GRID_HEIGHT):
for x in range(GRID_WIDTH):
if grid[y][x]:
pygame.draw.rect(screen, grid[y][x], (x * GRID_SIZE, y * GRID_SIZE, GRID_SIZE, GRID_SIZE))
pygame.draw.rect(screen, GRAY, (x * GRID_SIZE, y * GRID_SIZE, GRID_SIZE, GRID_SIZE), 1)

def main():
grid = [[0] * GRID_WIDTH for _ in range(GRID_HEIGHT)]
current_tetromino = Tetromino(random.choice(SHAPES), random.choice(COLORS))
next_tetromino = Tetromino(random.choice(SHAPES), random.choice(COLORS))
drop_timer = 0
game_over = False
score = 0

while not game_over:
screen.fill(BLACK)
draw_grid(grid)

for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
if not check_collision(grid, current_tetromino, offset_x=-1):
current_tetromino.x -= 1
elif event.key == pygame.K_RIGHT:
if not check_collision(grid, current_tetromino, offset_x=1):
current_tetromino.x += 1
elif event.key == pygame.K_DOWN:
if not check_collision(grid, current_tetromino, offset_y=1):
current_tetromino.y += 1
elif event.key == pygame.K_UP:
current_tetromino.rotate()
if check_collision(grid, current_tetromino):
current_tetromino.rotate()
current_tetromino.rotate()
current_tetromino.rotate()

# Automatic drop
drop_timer += clock.get_rawtime()
if drop_timer > 500: # Drop speed in milliseconds
drop_timer = 0
if not check_collision(grid, current_tetromino, offset_y=1):
current_tetromino.y += 1
else:
merge_grid(grid, current_tetromino)
cleared_lines = clear_lines(grid)
score += cleared_lines * 100
current_tetromino = next_tetromino
next_tetromino = Tetromino(random.choice(SHAPES), random.choice(COLORS))
if check_collision(grid, current_tetromino):
game_over = True

# Draw current tetromino
for y, row in enumerate(current_tetromino.shape):
for x, cell in enumerate(row):
if cell:
pygame.draw.rect(screen, current_tetromino.color,
((current_tetromino.x + x) * GRID_SIZE,
(current_tetromino.y + y) * GRID_SIZE,
GRID_SIZE, GRID_SIZE))

pygame.display.flip()
clock.tick(30)

pygame.quit()

if __name__ == "__main__":
main()

Code improvements

I started by asking Amazon Q, How could I improve my code implementation?
Image not found
Working with generative AI is a doulble-sword edge, and Amazon Q is not an exception, as with avery LLM model, you need and must verify the responses that the AI is providing you, AI is not the absolute truth.
Image not found
Then I continued making improvements in my code structure with the help of Amazon Q in VSCode, and also undestanding and verifying with other sources the suggestions that the AI was giving me.
Image not found
Find above how the API for the game starts, and this way Amazon Q helped me to build my game implemantation using latest trends technologies like FastAPI, and other AWS services.
 

Comments

Log in to comment