beanz Magazine

City Building Game

Doug on Flickr

Use Scratch to become the architect of your very own digital metropolis.

We’ve done a few different kinds of Scratch games over the last year, but now we’re going to do something different and a bit more complex: a city-builder/idle game. I’d consider this an intermediate Scratch project.

You can find my version of the game I’ll be describing at the following link: https://scratch.mit.edu/projects/209530385/#player

You may have seen idle games on your phones or tablets. They’re a kind of “casual” game where you let the game sit and work for stretches of time, usually accumulating some kind of levels or money. This game is going to be a little bit of a cross between the original Sim City and a tap heavy idle phone game.

We’ll start by describing some of the basics of the game mechanics and then give some hints and tricks for implementing it. I don’t intend for you to necessarily make this exact game, but rather use it as an example for making your own simulation or idle-style games.

The basic idea is that you’re trying to build a city that has a total of ten million people in it. You can build three basic kinds of districts: housing, shopping, and offices.

You need housing for people to move in and the more housing you have the faster your population can grow. However, in order for people to want to move to your city you’re going to need to have jobs ready for them. Building offices gives businesses a place to move and so they make your maximum population go up. Shopping centers also add some jobs, but they mostly help your city make money with that sweet, sweet sales tax.

Speaking of taxes, you also get money from your citizens paying taxes once a second. You get to set the tax rate, but you need to be careful because the higher your taxes the less likely people will be to move to your city!

So the game play is going to be a balance of building the different kinds of properties while also finding a good sweet spot for the tax rate. You can also speed up the growth of your city by repeatedly clicking on the BOOST button to triple your growth rate or by buying special properties like libraries or public parks.

My “I’m totally going to add graphics… someday” version of the game looks like:

The three buttons on the left hand side are the districts you can build. The green are the housing, or residential, districts, the blue are the shopping districts, and the pink are the office buildings. You can click on any of them and, if you have the money, you can drop a district down on the map on unoccupied space. You can’t overlap them because without the pressure of running out of space you’d have no reason to upgrade!

Once you’ve placed a district, if you click on it and you have 10x the money it initially cost you can upgrade it. Upgrading will have a varied, but large, effect.

Now, there’s a lot of comments in the code itself on how the game is implemented but we’ll cover what I think are some important points.

First, the buttons for building are implemented with a little in-this-sprite-only variable that keeps track of whether they’re the original or a clone, and clicking on it to build only works if it’s the original.

Second, the menu is implemented just by sending messages for Show menu and Exit menu that show or hide the respective sprites. This means, though, that the game is still technically running even when you’re in the menu—that seemed right to me, though.

Third, a lot of the game is organized by having visible-to-everyone variables that control what upgrades are in place and what the tick speed should be. The end result is that it’s pretty easy to add new upgrades and control the core functionality of the game. These visible-to-everyone variables are all given their initial values in code that runs on the stage and not any particular sprite.

At this point I think you can look at the project, read the comments, and be ready to remix it and mess around or add features.

If you want to try adding some features to the game here’s a few ideas:

  • Change the game so that there’s a lose condition, like your population hitting zero or going bankrupt
    • You might want to change the rules so that if your city is too unbalanced or taxes are too high your population goes down
  • Add a ton more buyable upgrades that give you different advantages
  • Add a transportation system where you have to buy roads and you can only build along them and nowhere else

Learn More

Other Scratch games to look at in the genres we’re playing in:

A really well done idle clicker game

https://scratch.mit.edu/projects/141654464/

A simple idle clicker game that’s a nice example

https://scratch.mit.edu/projects/75793722/

An elaborate city building game with an animated tutorial

https://scratch.mit.edu/projects/71983440/

Also In The June 2018 Issue

Code up your digital turtle mascot and watch him dash around the screen in this simple Python coding activity.

A phone and tablet app exposes the invisible waves that connect our computers.

How AI technology is helping fans keep the magic alive for one more chapter.

Use Scratch to become the architect of your very own digital metropolis.

Use SketchUp to create dizzying patterns and shapes, Escher-style.

Whiz around your computer’s folders and modify files at lightning speed like a pro.

Use micro:bit and cardboard to create a Jedi knight that sounds the alarm when evil approaches!

Learn about the infamous Enigma machine and how its “unbreakable” code was finally defeated.

Take your 3D-printed gizmos to the next level with harder, sleeker, and stronger material.

How daily coding puzzles with constant feedback can be a useful tool to help students master text-based languages.

Scientists draw inspiration from nature to create remarkable specialized robots.

Links from the bottom of all the June 2018 articles, collected in one place for you to print, share, or bookmark.

Interesting stories about computer science, software programming, and technology for June 2018.