対話するだけでゲームができた!?Amazon Q CLIでAIコーディング体験
背景
- 現在開催中の「Amazon Q CLI でゲームを作ろう」キャンペーンでは、2025 年 5 月 20 日から 6 月 20 日までにAmazon Q CLI でゲームを作って、その結果をブログ・SNSに投稿すると、Amazon QブランドのTシャツをゲットすることができます。
- AIコーディングアシスタントであるAmazon Q CLIを用いて、Pygameで簡単なゲームを開発してみましたので、事前準備や開発の流れを投稿します。
事前準備
開発に向けて、Amazon Q CLIやPyGameのインストールを進めていきます。
Amazon Q CLIのインストール
公式ドキュメントを参考にAmazon Q CLIをインストールします。
インストール完了後にターミナルでq chat
と入力すると、Amazon Q チャットが無事起動しました。
PyGameのインストール
公式ドキュメントを参考にPyGameをインストールします。
python -m pip install -U pygame --user
インストール後に以下コマンドを実行すると、サンプルゲームが無事に立ち上がりました。
python -m pygame.examples.aliens
Amazon Q CLIを用いたゲーム開発
Amazon Q CLI とのチャットセッションを開始し、チャット内のプロンプトだけでゲームを作成しましょう。
とのことですので、プロンプトだけでゲーム開発を試みてみましょう。
今回はボタンをクリックするとランダムにAWS認定資格をオススメするゲーム
を作成してみます。
まずは、Amazon Q チャット上で指示を出してみます。
あなたの仕事はユーザの指示に従ってpygameを用いてゲームを作成することです。
ボタンをクリックするとランダムにAWS認定資格をオススメするゲームを作成してください。
暫く待つと、Pythonファイルが生成されました。
python aws_cert_recommender.py
を実行してゲームを起動してみましたが、日本語の文字が正しく表示されませんでした。
正しく日本語が表示されるようにコード修正を依頼してみます。
新しいコードが生成されたので、再度python aws_cert_recommender.py
を実行して、ゲームを起動してみます。
日本語が正しく表示されていますね。
その上で、最後の細かい修正を依頼しました。
結果画面に表示される資格名が見切れてしまっているので修正してください
AWS認定資格の情報が最新ではないようです。以下のAWS認定資格一覧を参考に修正してください
AWS Certified Cloud Practitioner
AWS Certified AI Practitioner
AWS Certified SysOps Administrator - Associate
AWS Certified Developer - Associate
AWS Certified Solutions Architect - Associate
AWS Certified Data Engineer - Associate
AWS Certified Machine Learning Engineer - Associate
AWS Certified Solutions Architect - Professional
AWS Certified DevOps Engineer - Professional
AWS Certified Advanced Networking - Specialty
AWS Certified Security - Specialty
AWS Certified Machine Learning - Specialty
無事、指示通りに修正されていますね。
最終的に生成されたコードも貼っておきます。
コード
import pygame
import sys
import random
# Pygameの初期化
pygame.init()
# 画面設定
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("AWS認定資格レコメンダー")
# 色の定義
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
ORANGE = (255, 153, 0) # AWSオレンジ
BLUE = (35, 47, 62) # AWSブルー
LIGHT_BLUE = (70, 94, 124)
GRAY = (200, 200, 200)
# フォント設定
# 日本語フォントを使用
try:
# macOSの場合、ヒラギノフォントを使用
title_font = pygame.font.Font('/System/Library/Fonts/ヒラギノ角ゴシック W6.ttc', 48)
button_font = pygame.font.Font('/System/Library/Fonts/ヒラギノ角ゴシック W6.ttc', 24)
cert_font = pygame.font.Font('/System/Library/Fonts/ヒラギノ角ゴシック W6.ttc', 32)
desc_font = pygame.font.Font('/System/Library/Fonts/ヒラギノ角ゴシック W3.ttc', 18)
except:
try:
# 代替フォントを試す
title_font = pygame.font.Font('/System/Library/Fonts/AppleGothic.ttf', 48)
button_font = pygame.font.Font('/System/Library/Fonts/AppleGothic.ttf', 24)
cert_font = pygame.font.Font('/System/Library/Fonts/AppleGothic.ttf', 32)
desc_font = pygame.font.Font('/System/Library/Fonts/AppleGothic.ttf', 18)
except:
# フォールバック:SysFontを使用
print("警告: 日本語フォントが見つかりませんでした。テキストが正しく表示されない可能性があります。")
title_font = pygame.font.SysFont('Arial', 48, bold=True)
button_font = pygame.font.SysFont('Arial', 24)
cert_font = pygame.font.SysFont('Arial', 32)
desc_font = pygame.font.SysFont('Arial', 18)
# AWS認定資格リスト(名前、説明)
aws_certifications = [
("AWS Certified Cloud Practitioner", "クラウドの基礎知識を証明する入門レベルの認定資格"),
("AWS Certified AI Practitioner", "AIとMLの基礎知識を証明する入門レベルの認定資格"),
("AWS Certified SysOps Administrator - Associate", "AWSの運用管理スキルを証明する認定資格"),
("AWS Certified Developer - Associate", "AWSでのアプリケーション開発スキルを証明する認定資格"),
("AWS Certified Solutions Architect - Associate", "AWSでシステム設計ができることを証明する認定資格"),
("AWS Certified Data Engineer - Associate", "データエンジニアリングスキルを証明する認定資格"),
("AWS Certified Machine Learning Engineer - Associate", "機械学習エンジニアリングスキルを証明する認定資格"),
("AWS Certified Solutions Architect - Professional", "複雑なシステム設計の高度なスキルを証明する上級認定資格"),
("AWS Certified DevOps Engineer - Professional", "DevOpsの高度なスキルを証明する上級認定資格"),
("AWS Certified Advanced Networking - Specialty", "ネットワークの専門スキルを証明する認定資格"),
("AWS Certified Security - Specialty", "セキュリティの専門スキルを証明する認定資格"),
("AWS Certified Machine Learning - Specialty", "機械学習の専門スキルを証明する認定資格")
]
# 現在表示している認定資格
current_cert = None
# ボタンクラス
class Button:
def __init__(self, x, y, width, height, text, color, hover_color):
self.rect = pygame.Rect(x, y, width, height)
self.text = text
self.color = color
self.hover_color = hover_color
self.is_hovered = False
def draw(self, surface):
color = self.hover_color if self.is_hovered else self.color
pygame.draw.rect(surface, color, self.rect, border_radius=10)
pygame.draw.rect(surface, BLACK, self.rect, 2, border_radius=10)
# 日本語テキストのレンダリング
text_surf = button_font.render(self.text, True, WHITE)
text_rect = text_surf.get_rect(center=self.rect.center)
surface.blit(text_surf, text_rect)
def check_hover(self, pos):
self.is_hovered = self.rect.collidepoint(pos)
return self.is_hovered
def is_clicked(self, pos, click):
return self.rect.collidepoint(pos) and click
# メインボタンの作成
recommend_button = Button(WIDTH//2 - 150, HEIGHT - 100, 300, 60, "認定資格をオススメする", ORANGE, LIGHT_BLUE)
# ゲームのメインループ
def main():
global current_cert
clock = pygame.time.Clock()
running = True
while running:
mouse_pos = pygame.mouse.get_pos()
mouse_click = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
mouse_click = True
# ボタンのホバー状態を更新
recommend_button.check_hover(mouse_pos)
# ボタンがクリックされたらランダムに認定資格を選択
if recommend_button.is_clicked(mouse_pos, mouse_click):
current_cert = random.choice(aws_certifications)
# 画面描画
screen.fill(WHITE)
# タイトル描画
title_text = title_font.render("AWS認定資格レコメンダー", True, BLUE)
title_rect = title_text.get_rect(center=(WIDTH//2, 80))
screen.blit(title_text, title_rect)
# 説明文
if not current_cert:
# 日本語テキストのレンダリング
intro_text = cert_font.render("ボタンをクリックしてAWS認定資格を見つけよう!", True, BLACK)
intro_rect = intro_text.get_rect(center=(WIDTH//2, HEIGHT//2 - 50))
screen.blit(intro_text, intro_rect)
else:
# 選択された認定資格を表示
cert_name = current_cert[0]
# 資格名が長い場合は小さいフォントサイズで表示
try:
# 現在のフォントで試す
cert_text = cert_font.render(cert_name, True, BLUE)
if cert_text.get_width() > WIDTH - 100:
# フォントサイズを調整
try:
smaller_font = pygame.font.Font('/System/Library/Fonts/ヒラギノ角ゴシック W6.ttc', 26)
except:
try:
smaller_font = pygame.font.Font('/System/Library/Fonts/AppleGothic.ttf', 26)
except:
smaller_font = pygame.font.SysFont('Arial', 26, bold=True)
cert_text = smaller_font.render(cert_name, True, BLUE)
except:
# エラーが発生した場合はデフォルトフォントで表示
cert_text = pygame.font.SysFont('Arial', 26, bold=True).render(cert_name, True, BLUE)
cert_rect = cert_text.get_rect(center=(WIDTH//2, HEIGHT//2 - 50))
screen.blit(cert_text, cert_rect)
# 説明文を表示(日本語対応)
desc_text = desc_font.render(current_cert[1], True, BLACK)
desc_width = desc_text.get_width()
if desc_width > WIDTH - 100:
# 長い説明文は複数行に分割
words = current_cert[1].split()
lines = []
current_line = ""
for word in words:
test_line = current_line + word + " "
if desc_font.size(test_line)[0] < WIDTH - 100:
current_line = test_line
else:
lines.append(current_line)
current_line = word + " "
if current_line:
lines.append(current_line)
for i, line in enumerate(lines):
desc_text = desc_font.render(line, True, BLACK)
desc_rect = desc_text.get_rect(center=(WIDTH//2, HEIGHT//2 + 20 + i * 30))
screen.blit(desc_text, desc_rect)
else:
# 短い説明文は1行で表示
desc_rect = desc_text.get_rect(center=(WIDTH//2, HEIGHT//2 + 20))
screen.blit(desc_text, desc_rect)
# AWSロゴ風のデザイン要素
pygame.draw.rect(screen, ORANGE, (WIDTH//2 - 100, HEIGHT//2 + 100, 200, 4))
# ボタン描画
recommend_button.draw(screen)
pygame.display.flip()
clock.tick(60)
pygame.quit()
sys.exit()
if __name__ == "__main__":
main()
おわりに
Pygameの利用経験はなかったものの、Amazon Q CLIを用いて簡単なゲームを作成できました。
コード生成に若干の時間は掛かるものの、簡単な指示ベースで自律的にコード生成してくれるのは開発体験として非常に良かったです。(特に、本ゲーム開発・記事執筆を隙間時間で実施したので、メリット大でした。)
また、日本語対応しているのも嬉しい点ですね。対話時のみならず、生成されたコード内のコメントも日本語になっていたのは驚きでした。
是非皆さまも個人開発のお供にAmazon Q CLIを利用してみてはいかがでしょうか。
注意事項
- 本記事は万全を期して作成していますが、お気づきの点がありましたら、ご連絡よろしくお願いします。
- なお、本記事の内容を利用した結果及び影響について、筆者は一切の責任を負いませんので、予めご了承ください。
Discussion