dark mode light mode Search Menu
Search

Charred Cats Game

Maarten Takens on Flickr

In this article, we’ll be showing how to build a small, simple game in Scratch, based off of the ancient DOS game Scorched Earth. We won’t entirely go through how to make the game step-by-step, but rather explain each big piece and leave you to fill in the details! I’ve posted both the end result and the partial versions of the game with each big step done.

The game: Charred Cats

How it plays: a player and a computer take turns firing at each other. The first one to hit the other player with a projectile wins! You can control the angle at which you fire and how hard you lob your projectile, but that’s all. Projectiles that don’t hit a player blow up and destroy the terrain, opening up new ways to fire at each other. In our initial version, it’s not possible to blow yourself up.

My solution: available at https://scratch.mit.edu/projects/164497555/ and all the partial solutions as well as enhancements are at my project page. Links are at the bottom of this article. Try playing through it just to see how it is. You control the angle of firing with left and right arrow keys and the power of the shot with the up and down arrows. You fire by hitting the spacebar. Clicking on the green flag will reset the game back to its initial state.

Step 1: Build the Scene

The first thing to do is create our terrain and place our sprites. To start, go down underneath Stage and select the paintbrush icon so that you can paint the backdrop. The way I did this was to just fill the stage with a solid color, I chose green, and then I took the eraser and erased out a craggy mountainous stage with two flat spots for our two sprites.

Step 2: Code the Projectiles

This is probably the most complicated part! The basic outline is that every time a projectile is fired you’re making a clone of the sprite you’re coding in this step. You’ll probably want to make two different projectile sprites: one for the player controlled sprite and one for the opponent.

If you’re comfortable with the idea of sine and cosine functions or you’ve had some basic physics, then you should be able to code the movement of the projectiles by figuring out the horizontal and vertical velocity from the angle and power the projectile is fired at. You account for gravity by having the vertical velocity decrease every step.

If you haven’t seen those ideas before, you can either copy the code from the partial version called “Charred Cats – movement code” or you can read the tutorials below on the physics of projectiles!

What happens when the projectile hits a target? It needs to blow up the terrain! You can “destroy” the terrain by covering it up with white. You can do that by switching the projectile to a different costume and using the stamp command.

Also, how do you reset the terrain at the end of the game? That code probably belongs in these sprites.

Step 3: Fire your Shots

Now, you need to write the user interface code that allows you to play the game. You’ll need an event for each keyboard press that you want to record. I chose

  • up key for increasing power
  • down key for decreasing power
  • left key for increasing angle
  • right key for decreasing angle
  • space bar for firing

We already said that projectiles are all going to be clones of the base projectile sprites. So what block do you need to use to fire a projectile?

Step 4: Code your Opponent

Making your opponent is easy as long as its really bad at playing the game. The only complication is that it needs to know when to fire. You’ll need the computer player to listen for a message that tells it to fire. It can fire by randomly choosing an angle and power and then creating a clone like you did for your own player.

Step 5: Taking Turns

If you’ve tested things so far, you’ve probably noticed that you can spam the firing button while your opponent is firing. That’s not very fair! This may be a war, but you still have to take turns.

How do you make sure that you’re taking turns? Well, your opponent is already waiting until you fire before taking a shot, you just need to return the courtesy.

You do that by modifying the code for when you press the spacebar. Rather than always firing, you need to have it fire if the enemy’s projectile has landed. You’ll need a variable to keep track of whether you’re ready to fire. This variable will change based on a message the player sprite receives, and the computer projectile is going to send a message when it lands.

Step 6: End the Game

Finally, to end the game you’re going to have to detect whether the player or computer sprite is hit by their opponent’s projectile. You can do this by changing the projectile’s code so it checks to see if it hits the terrain or the enemy sprite before blowing up. If it hits the terrain and blows up, you’ll need to check and see if the explosion costume is big enough to hit the sprite. The projectile then sends a message depending on who won.

You should have the sprite who won say that they won and then “stop” the game.

Now as you’ve noticed there’s a lot missing so far and a lot of room to add things to make this more of a real game. Here’s a (partial!) list of things you can do to make this game more interesting:

  • Your tanks float in mid-air if the terrain under them is destroyed. That’s a bit silly! Try adding a bit of code to make sure that when the cats fall onto something solid rather than float.
  • There’s no real “explosion” when your projectile blows up. Add an extra costume change so that it looks more like a big explosion.
  • Add sound effects! You can use the blocks in the Sound tag in order to put sound effects in when you fire, get hit, or an explosion happens!
  • Add different weapons! This is a harder one and you’ll need to add a way of choosing weapons and make your firing code do something different based on what weapon you chose. Some weapons to try making are:
    • A cluster shot that when it hits launches a bunch of smaller “basic” projectiles that each make a small explosion. There’s one big complication to this: how do you know that a turn is done if there’s a bunch of projectiles instead of just one?
    • A triple shot that fires three projectiles in a random spread centered around the angle you chose.
    • A wall gun that explodes in mid air and adds terrain instead of destroying it. The wall gun can’t destroy an enemy if it hits them.
  • Add a 2-player mode! This is another harder one. You’ll need to add a start screen that lets you choose whether you’re two player or single player and then spawns either a computer controlled opponent or a player controlled component.
  • Add a proper AI. One way to do it is to actually have the computer observe whether its shot was too short or too far and then adjust parameters in the right direction.

Happy hacking!

Learn More

My Scratch page

https://scratch.mit.edu/users/adjunction/

Scorched Earth

https://en.wikipedia.org/wiki/Scorched_Earth_(video_game)

Some websites that describe how projectile motion works

http://physics.info/projectiles
http://ux1.eiu.edu/~cfadd/1150/03Vct2D/proj.html