Flash ball vs ball game concept

Read all posts about "" game

This is a prototype sent me by Rory Kamminga.

It’s an unfinished game where you control a blue ball with arrow keys. Your aim is to kick a red ball out of the stage.

Obviously the red ball will try to do the same with you.

The interesting thing is that Rory took the studies of Raman Pfaff’s Physics of an Elastic Collision and its derivated tutorials such as Managing ball vs ball collision with Flash by myself and Elastic collisions by Marijn van Aerle and come out with a good prototype where the red ball is moved by AI.

That’s what Rory says:

Hi Emanuele, I’m a huge fan of your site, and I thought I’d make a contribution in the form of a concept game that’s still in its early stages.
Basically, you control the blue ball with the arrow keys and you have to knock the red ball off the edge. Clicking the mouse resets it.

and

I’m thinking of having several stages, including walls.
I want to have multiple AI balls, but I don’t know how.

So, if you have any clue about multiple AI balls, just give feedback.

The source code is well commented, for your understanding:

//functions
function findAnAngle(xthing, ything) {
	//very basic angle finder..returns value in degrees  
	term = Math.PI/180;
	//degree->radian
	if (xthing<0) {
		t = 180+Math.atan(ything/xthing)/term;
	} else if (xthing>0 && ything>=0) {
		t = Math.atan(ything/xthing)/term;
	} else if (xthing>0 && ything<0) {
		t = 360+Math.atan(ything/xthing)/term;
	} else if (xthing == 0 && ything == 0) {
		t = 0;
	} else if (xthing == 0 && ything>=0) {
		t = 90;
	} else {
		t = 270;
	}
	return t;
}
function doCollision(ball, ball2) {
	dx = ball._x-ball2._x;
	dy = ball._y-ball2._y;
	phi = Math.atan(dy/dx);
	//find the collision angle
	term = Math.PI/180;
	//degree->radian
	//find out the magnitude of the velocities of the two balls
	v1i = Math.sqrt(ball.vx*ball.vx+ball.vy*ball.vy);
	v2i = Math.sqrt(ball2.vx*ball2.vx+ball2.vy*ball2.vy);
	//find out the angle of the velocity vector
	ang1 = findAnAngle(ball.vx, ball.vy)*term;
	ang2 = findAnAngle(ball2.vx, ball2.vy)*term;
	//find the velocities in the new coordinate system
	v1xr = v1i*Math.cos(ang1-phi);
	v1yr = v1i*Math.sin(ang1-phi);
	v2xr = v2i*Math.cos(ang2-phi);
	v2yr = v2i*Math.sin(ang2-phi);
	//find the final velocities in the normal reference frame
	//the x velocities will obey the rules for a 1-D collision
	v1fxr = ((ball.mass-ball2.mass)*v1xr+(ball2.mass+ball2.mass)*v2xr)/(ball.mass+ball2.mass);
	v2fxr = ((ball.mass+ball.mass)*v1xr+(ball2.mass-ball.mass)*v2xr)/(ball.mass+ball2.mass);
	//the y velocities will not be changed
	v1fyr = v1yr;
	v2fyr = v2yr;
	//convert back to the standard x,y coordinates
	_root.ball.vx = Math.cos(phi)*v1fxr+Math.cos(phi+Math.PI/2)*v1fyr;
	_root.ball.vy = Math.sin(phi)*v1fxr+Math.sin(phi+Math.PI/2)*v1fyr;
	_root.ball2.vx = Math.cos(phi)*v2fxr+Math.cos(phi+Math.PI/2)*v2fyr;
	_root.ball2.vy = Math.sin(phi)*v2fxr+Math.sin(phi+Math.PI/2)*v2fyr;
}
//move ball
MovieClip.prototype.move = function() {
	this.onEnterFrame = function() {
		//check for collision with walls, done pretty quick
		//the balls might get stuck in the walls if they move too fast
		if ((ball._x<45) or (ball._x>579)) {
			bdead = true;
		}
		if ((ball._y<40) or (ball._y>424)) {
			bdead = true;
		}
		if ((ball2._x<45) or (ball2._x>579)) {
			b2dead = true;
		}
		if ((ball2._y<40) or (ball2._y>424)) {
			b2dead = true;
		}
		//move 
		this._x += this.vx;
		this._y += this.vy;
	};
};
_root.onLoad = function() {
	ball.mass = 20;
	ball.vx = 0;
	ball.vy = 0;
	ball.move();
	ball2.mass = 20;
	ball2.vx = 0;
	ball2.vy = 0;
	ball2.move();
	coll = 0;
	friction = 0.1;
	gws = 0;
	ba = 0.8;
	nmea = 0.8;
	radius = 15;
	bdead = false;
	b2dead = false;
};
_root.onEnterFrame = function() {
	//we check for collision using the distance between the two centre points of the balls
	//if that distance is smaller than two times the radius of the balls we have collision
	//the radius is hardcoded
	distance_x = Math.abs(ball._x-ball2._x);
	distance_y = Math.abs(ball._y-ball2._y);
	distance = Math.sqrt(distance_x*distance_x+distance_y*distance_y);
	//make sure the balls don't get stuck into eachother
	if (distance<=30 && coll == 0) {
		coll = 1;
		doCollision(ball, ball2);
	} else if (distance>30) {
		coll = 0;
	}
	//Player One controls 
	if (Key.isDown(Key.RIGHT) && ball.vx<10) {
		ball.vx += ba;
	}
	if (Key.isDown(Key.LEFT) && ball.vx>-10) {
		ball.vx -= ba;
	}
	if (Key.isDown(Key.UP) && ball.vy>-10) {
		ball.vy -= ba;
	}
	if (Key.isDown(Key.DOWN) && ball.vy<10) {
		ball.vy += ba;
	}
	//Friction 
	if (ball.vx>0) {
		ball.vx -= friction;
	} else if (ball.vx<0) {
		ball.vx += friction;
	}
	if (ball.vy>0) {
		ball.vy -= friction;
	} else if (ball.vy<0) {
		ball.vy += friction;
	}
	if (ball2.vx>0) {
		ball2.vx -= friction;
	} else if (ball2.vx<0) {
		ball2.vx += friction;
	}
	if (ball2.vy>0) {
		ball2.vy -= friction;
	} else if (ball2.vy<0) {
		ball2.vy += friction;
	}
	//Gravity well! 
	if (ball._x>320) {
		ball.vx -= gws;
	} else if (ball._x<320) {
		ball.vx += gws;
	}
	if (ball._y>240) {
		ball.vy -= gws;
	} else if (ball._y<240) {
		ball.vy += gws;
	}
	if (ball2._x>320) {
		ball2.vx -= gws;
	} else if (ball2._x<320) {
		ball2.vx += gws;
	}
	if (ball2._y>240) {
		ball2.vy -= gws;
	} else if (ball2._y<240) {
		ball2.vy += gws;
	}
	//Tracking AI 
	if ((ball2._xball._x) && ball2.vx>-10) {
		ball2.vx -= nmea;
	}
	if ((ball2._y>ball._y) && ball2.vy>-10) {
		ball2.vy -= nmea;
	}
	if ((ball2._y0) {
			ball._xscale -= 2;
			ball._yscale -= 2;
			ball._alpha -= 1;
		}
	}
	if (b2dead == true) {
		ball.vx = 0;
		ball.vy = 0;
		ball2.vx = ball2.vx/3;
		ball2.vy = ball2.vy/3;
		if (ball2._xscale>0) {
			ball2._xscale -= 2;
			ball2._yscale -= 2;
			ball2._alpha -= 1;
		}
	}
};
_root.onMouseDown = function() {
	ball.vx = 0;
	ball.vy = 0;
	ball2.vx = 0;
	ball2.vy = 0;
	ball._x = 245;
	ball._y = 170;
	ball2._x = 165;
	ball2._y = 222;
	bdead = false;
	b2dead = false;
	ball._xscale = 100;
	ball._yscale = 100;
	ball._alpha = 100;
	ball2._xscale = 100;
	ball2._yscale = 100;
	ball2._alpha = 100;
};

And this is the result:

Take a look, download the source code and let me know if you can turn this prototype into a blockbuster

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