"Krupki" - ultra simple game

(rostok) #1

Control a ball and gather all dots (krupki).

“Krupki” is a very simple game I made with my daughter (7, well almost 8). She designed art, levels (see this one as well) and gameplay. I just implemented it. Levels are stored as XML files generated with my version of modified Gleed2d game editor, sounds are made with BFXR.

Source is placed on the website and what may be of interest here is example of save game mechanism (once I made a tutorial on it but it got lost with the old FP website). Another thing is basic ball physics simulation done only with trivial line segment intersections and reflections. Ah, there is also a fast font rendering class…


BTW: I am really happy to see the community being resurrected. Good work guys.

Line Intersection
(fedyfausto) #2

ooo fantastic the collisions :0 how you do that?

(rostok) #3

The real secret lies in a bunch of geometric functions operating on line segments. Those are tests for intersection, cutting one segment with another, etc. In this case, the most crucial is function to calculate reflection of segment while it collides with another. I won’t get into the math details here as it is some basic algebra and there are plenty of tutorials on the web. Having this in each frame I just calculate the next position of ball (including gravity and user input). Before actually applying the new position I create line segment (or vector) starting at balls current pos and ending at destined pos. This segment is the reflected with all the solid lines in level (actual walls). BTW there is a small error in my code, there should be a loop iterating with all lines untill there is no intersection - but somehow I missed it.

There is code to download but I will post it here:

part of Player.as, earlier declarations

		public var dx:Number = 0; //incremental step to next position
		public var dy:Number = 0;
		public var out:Vector3D = new Vector3D(); // temporary vector for getting line segment reflection calculations 
		public static var frictionFactor:Number = 0.99; 

declaration of reflectOnSegment function

/* cuts second (q) segment with first (p) one then reflects cut off part by first (p) segment
* returns 
* 	- unchanged q1 to q2 segment when no intersection
*  - segment from intersection to reflected end
public static function 
p1x:Number, p1y:Number, p2x:Number, p2y:Number, 
q1x:Number, q1y:Number, q2x:Number, q2y:Number, 
// output's x,y are line segment's first coordinates, and z,w second

and finally checking for collisions

		public function motion():Boolean
			var lines:Vector.<Line> = new Vector.<Line>;
			world.getType("line", lines );
			for each (var l:Line in lines) {
				Utils.reflectOnSegment(l.x1, l.y1, l.x2, l.y2, x, y, x + dx, y + dy, out);
				if (out.w != y + dy) { 
					dy = out.w - y;
					dy *= frictionFactor;
				if (out.z != x + dx) { 
					dx = out.z - x;
					dx *= frictionFactor;
			if (out.w != y + dy || out.z != x + dx) return true;

			return false;

So basically this is just a trick. Instead of a ball physics only single point is being bounced off the walls. Press F1 while playing to see it in action.

(no-jo) #4

That’s a great project to do with your daughter!

Certainly food for thought…A great way of getting them interested, although my eldest son is only 3 and his non-existent drawing skills are on a par with his attention span .

Thanks for sharing.

(Ultima2876) #5

This has got bundles of charm. With some gameplay tweaks and a little extra polish, this could make a fantastic niche mobile game (and a good web viral too). I bet the story behind it would sell it alone, not to mention the cute and charming visual style.

(rostok) #6

Thanks guys for the positive input. I am planning on adding sth like 10-15 levels with couple of extra obstacle types. Also touch input will be converted to left/right/jump buttons only instead of awkward virtual joystick. Maybe the story angle will be covered by mid-level screens with pictures and texts.

@Ultima2876 can you give me some tips? I’d love to hear them.

(Zachary Lewis) #7

Why not tilt to move left and right and touch to jump? I feel like that would be a more engrossing experience.

(Suyash Mohan) #8

Nice Game. Only thing I don’t like is in the starting only I start to fall, I have to jump as soon as we start the level

(Michał Rapacz) #9

@rostok Nice to see some very nice game from Poland! Orzech is my favourite ball :smile: Physics are very good, but sometimes non-predictable: sometimes when the ball falls from large height it bounces high, but sometimes it does not bounce at all. Anyway, I like the game and the level design very much.