Home >Web Front-end >JS Tutorial >Dyson Swarm: How I built a hard science fiction game with AWS services
A Hard Sci-Fi Clicker Game: Dyson Swarm
I'm a passionate science fiction enthusiast, having even run a sci-fi magazine for five years. This love led me to create a series of short games explaining complex sci-fi concepts in an engaging way. My first creation, Dyson Swarm, emerged during the AWS Game Builder Challenge.
Dyson Swarm is an incremental (clicker) game where players dismantle the solar system to construct a Dyson swarm – a megastructure encompassing the sun. Starting as a game developer, you gradually accumulate resources and advance through stages.
I initially estimated 10-20 hours of development, but the project expanded significantly, ultimately consuming around 70 hours (mostly late at night!). This highlights the challenge of accurate software development estimations.
You can play the game here: Dyson Swarm
AWS Architecture
The game uses client-side Javascript and runs entirely in the browser. Hosting is achieved using an S3 static site bucket, enhanced by CloudFront CDN for global, high-speed delivery. CloudFront handles TLS termination, simplifying the process.
Anonymous gameplay metrics (player count and progress) are stored in an RDS Postgres database. Data transmission uses a serverless API built with API Gateway and Lambda. While I used an existing RDS instance, serverless RDS would be equally suitable.
Deployment leveraged the AWS console for S3 and CloudFront, and an AWS CDK stack (Python) for the serverless metrics Lambda.
The CDK Stack
The CDK stack for the serverless metrics Lambda is concise:
<code class="language-python">from aws_cdk import ( aws_lambda as lambda_, aws_apigateway as apigw, aws_ecr as ecr, aws_certificatemanager as acm, aws_route53 as route53, Duration, Stack) from constructs import Construct import os class DysonSwarmStack(Stack): def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None: super().__init__(scope, construct_id, **kwargs) repo = ecr.Repository.from_repository_name(self, "dysonSwarmRepo", "gamesapi") dyson_swarm_lambda = lambda_.DockerImageFunction(self, "dysonSwarmLambda", code=lambda_.DockerImageCode.from_ecr( repository=repo, tag=os.environ["CDK_DOCKER_TAG"] ), memory_size=256, timeout=Duration.seconds(60), architecture=lambda_.Architecture.ARM_64 ) # do auth inside lambda api = apigw.LambdaRestApi(self, "dysonSwarm-endpoint", handler=dyson_swarm_lambda, default_cors_preflight_options=apigw.CorsOptions(allow_origins=["*"]) ) custom_domain = apigw.DomainName( self, "custom-domain", domain_name="gameapi.compellingsciencefiction.com", certificate=acm.Certificate.from_certificate_arn(self,'cert',"[cert ARN here]"), endpoint_type=apigw.EndpointType.EDGE ) apigw.BasePathMapping( self, "base-path-mapping", domain_name=custom_domain, rest_api=api ) hosted_zone = route53.HostedZone.from_hosted_zone_attributes( self, "hosted-zone", hosted_zone_id="[zone id here]", zone_name="compellingsciencefiction.com" ) route53.CnameRecord( self, "cname", zone=hosted_zone, record_name="gameapi", domain_name=custom_domain.domain_name_alias_domain_name )</code>
It leverages an existing ECR repository (containing the Lambda container image) and Route 53 hosted zone (for the custom domain). Essentially, it creates an API Gateway endpoint backed by a Lambda function. The full code is available at: dyson_swarm_stack.py
The Code
The complete game source code is on GitHub: Dyson Swarm GitHub Repo
The main game loop, found in dysonswarm.html
, uses a 100-millisecond interval. Local browser storage (localStorage) handles game state persistence. Button functions (56 in total) are managed in buttonFunctions.js
. Game animations, initially SVG, were transitioned to Canvas for better performance with a large number of elements. Thorough testing and iterative improvements addressed various edge cases in game logic.
Using AWS Q Developer
The AWS Game Builder Challenge encouraged using AWS Q Developer. I found it helpful:
Pros:
/dev
feature generates and applies code diffs, streamlining development.Cons:
/dev
feature can be slow due to multiple LLM calls./dev
sometimes creates new files instead of in-line code additions. Careful prompt engineering is crucial.Open-Source Game
The game's source code is available under the MIT license. Feel free to use it as a foundation for your projects. I welcome hearing about your game creations!
The above is the detailed content of Dyson Swarm: How I built a hard science fiction game with AWS services. For more information, please follow other related articles on the PHP Chinese website!