HTML5 prototype to drag and rotate a wheel and control your character like in “Puppet Bird” game

Read all posts about "" game

Some years ago I had a couple of hours of fun playing Puppet Bird, a game in the Flappy Bird family, which unfortunately seems to be no longer available on the stores, but if you do not know the game, here it is a video of the gameplay:

You control a little string-secured puppet with a wheel at the bottom of the screen and move it through a moving obstacle course.

In this first part, we’ll build a prototype to move the character in this original way.

The core of the concept is you must drag to rotate a wheel, or a crank, and since there is no need to reinvent the wheel, there is an interesting plugin by rexrainbow at this page.

This is what I was able to build in about 50 lines of code:

Drag and rotate the crank to raise or lower the bird.

The two red circles define the active drag area. It’s quite fun to control a character this way, isn’t it? The original game was flawed by too much ads and a too hard gameplay, with a lot of obstacles, but I still think there is potential in this prototype.

And here it is the completely commented source code, for you to enjoy and improve the prototype:

let game;
let gameOptions = {

	// min and max radius from crank centre where dragging is allowrd
	dragRadius: [50, 250],

	// ratio between crank rotation, in degrees, and rope movement, in pixels
	crankRatio: 0.3
}
window.onload = function() {
    let gameConfig = {
        type: Phaser.AUTO,
		backgroundColor: 0x222222,
        scale: {
            mode: Phaser.Scale.FIT,
            autoCenter: Phaser.Scale.CENTER_BOTH,
            parent: "thegame",
            width: 750,
            height: 1334
        },
        scene: playGame
    }
    game = new Phaser.Game(gameConfig);
    window.focus();
}
class playGame extends Phaser.Scene {
    constructor() {
        super("PlayGame");
    }
	preload() {

		// load plugin responsible of drag rotate, see the official page at
		// https://rexrainbow.github.io/phaser3-rex-notes/docs/site/dragrotate/
		this.load.plugin("rexdragrotateplugin", "dragrotate-plugin.js", true);
		this.load.image("crank", "crank.png");
		this.load.image("rope", "rope.png");
		this.load.image("bird", "bird.png");
	}
    create() {

		// rope sprite
		this.rope = this.add.sprite(game.config.width / 2, game.config.height / 3, "rope");

		// set registration point to the bottom
		this.rope.setOrigin(0.5, 1);

		// bird sprite
		this.bird = this.add.sprite(game.config.width / 2, game.config.height / 3, "bird");

		// crank sprite
		this.crank = this.add.sprite(game.config.width / 2, game.config.height / 4 * 3, "crank");

		// here I just draw two circles to show drag area
		let graphics = this.add.graphics();
		graphics.lineStyle(4, 0xff0000, 1);
		graphics.strokeCircle(this.crank.x, this.crank.y, gameOptions.dragRadius[0]);
		graphics.strokeCircle(this.crank.x, this.crank.y, gameOptions.dragRadius[1]);

		// drag rotate plugin instance
		let dragRotate = this.plugins.get("rexdragrotateplugin").add(this, {

			// horizontal coordinate of origin point
    		x: this.crank.x,

			// vertical coordinate of origin point
    		y: this.crank.y,

			// dragging is valid when distance between touch pointer and
			// origin position is larger then minRadius and less then maxRadius.
    		minRadius: gameOptions.dragRadius[0],
    		maxRadius: gameOptions.dragRadius[1]
		});

		// the core of the script, "drag" listener
		dragRotate.on("drag", function(e) {

			// deltaAngle is the dragging angle around origin position, in degrees
			// we determine new rope position according to dragging angle
			let newPosition = this.rope.y + e.deltaAngle * gameOptions.crankRatio;

			// we are going to limit rope position to prevent bird to fly off the screen
			if(newPosition > 50 &amp;&amp; newPosition < game.config.height / 4 * 3) {

				// adjust rope position
				this.rope.y += e.deltaAngle * gameOptions.crankRatio;

				// adjust bird position
				this.bird.y += e.deltaAngle * gameOptions.crankRatio;

				// adjust crank angle
				this.crank.angle += e.deltaAngle
			}
		}, this);
	}
}

Next time we’ll add some enemies, and maybe this old game may get a new life, meanwhile download the source code.

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

214 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers