Scratch for Java

Cat Platformer

The Scratch for Java cat in a small platformer. Collect the coffee beans, jump on the bugs or punch them, and climb up to the steaming mug. Like the Coin Collector, it needs no files: the cat, the beans, the bugs, the mug and the hearts are built into the library, just like the grass tiles and the sounds.

The cat on a platform, with beans, bugs and the mug

Walk with the left and right arrow keys, jump with space, punch with X, crouch with the down arrow, and climb the ladder with up and down.

The cat's animations

Every animation of the cat is built in. Each frame is 64 by 64 pixels with the feet on the bottom row, and the frames of an animation are numbered from 1, so one line adds a whole animation:

this.addAnimation("walk", "cat_walk_%d", 6);

All animations of the cat

Animation Frames Plays
idle 26 in a loop
walk 6 in a loop
crouch 4 in a loop
jump 3 once, going up
fall 2 once, going down
land 2 once
punch 6 once
hurt 3 once
ko 5 once
cheer 4 in a loop
climb 4 in a loop

The cat faces right, like a sprite in Scratch with direction 90. With setRotationStyle(RotationStyle.LEFT_RIGHT) it turns around when it walks left.

The things around it are built in too: coffee_bean_1 to coffee_bean_4, bug_walk_1 to bug_walk_4, bug_squished_1 and bug_squished_2, goal_mug_1 to goal_mug_4, heart_full and heart_empty.

Which animation?

The cat picks its animation from what it is doing, every frame, most important first:

if (this.hurtFrames > 40) {
  this.play("hurt", true);
} else if (this.punching) {
  this.play("punch", true);
} else if (!this.onGround) {
  this.play(this.fallSpeed > 0 ? "jump" : "fall", true);
} else if (walking) {
  this.play("walk", false);
} else {
  this.play("idle", false);
}

play is a small helper in Cat. An animation that plays once has to start at its first frame, so the helper resets the animation whenever it changes:

private void play(String name, boolean once) {
  if (!name.equals(this.animation)) {
    this.animation = name;
    this.resetAnimation();
  }
  this.playAnimation(name, once);
}

Run it here

Click the stage so it takes the keyboard.

Cat Platformer

Create Shareable URL

Select Sections