Ludum Dare 34
Coming December 11th-14th Weekend

Posts Tagged ‘code’

For those having trouble with collision detection in Java:

Posted by
Saturday, August 22nd, 2015 6:08 pm

Try putting this in your player class:

public boolean intersects(Tile tile) {
tile = this.tile;
Rectangle r1 = new Rectangle(x, y, width, height);
Rectangle r2 = tile.getRectangle();
if(r1.intersects(r2) || r2.intersects(r1)) {
return true;
} else {
return false;

And in your main class, put:

if(player.intersects(tile) {

To get a visual interpretation of the rectangles, put this code in your method where you render:
// This is at the top of the class
public boolean meshMode;
// Rendering:
public void render(Graphics2D g) {
if(meshMode) {
// You can of course use any color
// If your ints/doubles aren't public, then make some getters.
g.drawRect(player.x, player.y, player.width, player.height);
g.drawRect(tile.x, tile.y, tile.width, tile.height);
// In Key Listeners:
public void keyPressed(KeyEvent e) {
int k = e.getKeyCode();
if(k == KeyEvent.VK_M) {
meshMode = true;
if(k == KeyEvent.VK_N) {
meshMode = false;

This is a very simple way to calculate collision detection in a 2D Java Game.
Hope this helps a lot :)

I haven’t been here in a long time. So I thought I would share this code I made with you and explain how it works (so people who don’t use Gamemaker can reverse-engineer it). Unfortunately I’m not taking part this time round, but I felt like helping those who are!

Gamemaker Script Code

Simply copy and paste these into two seperate scripts:

Ring Health Bars

Circle Health Bars

To execute the scripts simply use:




Meet The Variables:

Ring Health bars

x,y = the coordinates of the center of the ring

radius = the distance from the center to the start of the ring

thickness = the thickness of the ring

rotation = at what angle is the zero mark of the ring?

angle = over how many degrees does the ring span?

max segments = how many segments the ring is split in to

segments = how many of those segments are visible

direction = which way is the ring drawn? 1 for anti-clockwise and -1 for clockwise

For the circle health bar the variables are the same except that thickness is not included.

Demo download

A demo (gif):

Circular Health Bars Demo


The demo is using two rings; one red and one green to simulate health.

What is this sorcery?

When a computer draws a circle, it is actually just a bunch of triangles arranged in the same way as slices of pie.

To find the coordinates of each triangle to be able to draw them we need a big steaming pile of math.


Specifically we need some trigonometry. Let’s start with which way is up:

Ring Health bars angles

Or in this case, which way is right: zero degrees.

A new temporary variable, i, will count our current angle as we sweep around the circle.

After each cycle in the loop it will change by the angle variable divided by the maxsegments.

Ring Health bars sections


Add to the mix our variables for radius and thickness and we can draw a triangle to work out our x and y coordinates for our triangles to draw!

Ring Health bars triangle

From this we can get the coordinates for the two points a and b. To get point b we simply add the thickness to the radius:

Ring Health bars triangle ab


Then we add the angle change to i, and get the variables c and d. Then we can connect the dots and draw one section!

Ring Health bars triangle cd

Ring Health bars section

Repeating this process for how many sections we want to draw will result in a ring!

This process is the same for the circle, except we only need points a, c, and the center for each triangle.


Good luck for LD 32! Hopefully this tutorial will come in handy.

I kinda rushed this. Hopefully you can understand it if you have a basic grip on maths.

Hexagonal Grids!

Posted by (twitter: @theupgradezone)
Wednesday, November 12th, 2014 5:10 am

So, I did the maths on how to draw a perfect hexagon. Now, using the same logic from that
tutorial, in this tutorial I will show you how to make a hexagonal grid!

Let’s say we have a hexagon with width a and height b.
(If you used the last tutorial, then the value of a from the last tutorial becomes twice that
value in this tutorial)


Now we generate a bunch of hexagons to fill up the screen area.
For this example the point of reference is in the center of the hexagon.

Each hexagon will be 3/4*a to the side of the last one, and b above or below:


Remember to keep a and b as whole numbers!

So the screen should look like this: (grey = hexagon, dark grey = overlap)


But we need every 2nd column to go up a bit, so that the hexagons interconnect properly.
So we use this equation, where x and y are the coordinates of the center of each hexagon:


Now your screen will look like this:



Now we can code it!

Pseudo Code:

width = (width of hexagon)
height = (height of hexagon)

Creation Loop:
create hexagon
x = x + (3*width)/4
if edge of screen is reached {
go back to starting edge of screen
y = y + height

Creation code for each hexagon:
y = y + mod(sin((90*(x/width))*height/2)



And there you have a hexagonal grid!

And this is the last of my little group of tutorials :3
Thanks for the comments on how to refine the logic and code of previous tutorials :)

How to draw a perfect Hexagon!

Posted by (twitter: @theupgradezone)
Tuesday, November 11th, 2014 9:55 am

So I did that maths of drawing a perfect octagon. Now, for a perfect hexagon!


First, we must imagine a rectangle with width a and height b.
You must now choose which side, a or b, you want to specify.


If you specified a, to find b you must use this equation:




If you specified b, to find a you must use this equation:


Which can also be written as:


Remember to round to the nearest number!



Once you have a and b, we need to specify the 6 coordinates.
For this example, the frame of reference is the center of the rectangle.

The top and bottom two are b/2 up or down and a/2 to each side.


The two sides are a to the side.


Then you draw lines between the coordinates.


And there you have a perfect hexagon!


Pseudo Code:

// Let’s say we specified the width, a
width = (width of imaginary square)
height = 2 * width * sin(60)


coord_1x = x – width/2
coord_1y = y + height/2
coord_2x = x + width/2
coord_2y = y + height/2
coord_3x = x + width
coord_3y = y
coord_4x = x + width/2
coord_4y = y – height/2
coord_5x = x – width/2
coord_5y = y – height/2
coord_6x = x – width
coord_6y = y




And that’s how to draw a perfect hexagon!

Coming soon: Hexagonal grids!

How to draw a Perfect Octagon!

Posted by (twitter: @theupgradezone)
Tuesday, November 11th, 2014 4:45 am

I did some maths while I was bored, and I calculated a way to draw a perfect octagon.
I thought this might come in useful for some of you :3

Warning! Maths ahead!



Imagine a square, with side length a.


Our octagon will fit in this square.

We will call the length of a side of the octagon b.



Now you put a into this equation:


Which can also be written as:


And you get b.

Now, round b to the nearest number. We can’t be dealing with fractions of pixels now, can we?

Now you have b, we can draw the octagon.



For this example, our reference point will be in the center of the octagon, which is also the center of our imaginary square.


Our first 2 co-ordinates will be a/2 up and b/2 to either side.



For the sides, it will be a/2 to the side and b/2 up or down.


Then, draw lines between the co-ordinates to finish the octagon.


And there you have a regular octagon!


Pseudo code:


size = (length of side of square)
oct_side = round((size*sqrt(2))/(2+sqrt(2)))


coord_1x = x – oct_side/2
coord_1y = y + size/2
coord_2x = x + oct_side/2
coord_2y = y + size/2
coord_3x = x + size/2
coord_3y = y + oct_side/2
coord_4x = x + size/2
coord_4y = y – oct_side/2
coord_5x = x + oct_side/2
coord_5y = y – size/2
coord_6x = x – oct_side/2
coord_6y = y – size/2
coord_7x = x – size/2
coord_7y = y – oct_side/2
coord_8x = x – size/2
coord_8y = y + oct_side/2





And that’s how to draw a perfect octagon!

Coming soon: How to draw a perfect hexagon!


To those interested in the maths involved to get the equation:



I originally calculated this to be able to draw the Aperture Science logo by hand, but I also saw the use of it on the computer!

Base code update

Friday, December 13th, 2013 4:04 pm

I’ve written a little base code project I’m going to start with.

It’s fairly simple and is available here.

To recap, it uses libgdx. If I fancy some postprocessing shader stuff, I’ll use the PostProcessor class from here, and if I need to do some pathfinding, I have my own simple A* implementation I wrote which is available right here.


Hi everyone

****I posted new questions about compilers and editors in the comments please read them :) ****

I am an experienced programmer and know a few languages.

I want to start making games with a windows 7 computer for any operating system (linux, Mac and Windows).

I do not know which is the best coding language to use for that that is free But has a good compiler and runs well. I also do not want to use software like gamemaker or construct. Something like c++ or java or love2D so please take that into consideration!

Also what libraries would I need for 2D games for now to make it with.

Thanks so much for your help in advance! I really want to become some kind of game devloper so this is key.

Hope you all are having fun in the ludum dare! Expect me to be ready for the next one!

Hope my questions make sense!

Please comment!



Yesterday my stream peaked at 126 people watching me program! In order to make it a true 53 hour marathon, I just left the stream going overnight and pointed the camera at my bed. When I woke up, there were still 44 people in the stream who greeted me as I got back on! Aha, ha ah!

More coding action today at! I’m making a massively multiplayer online capture the flag turn based real time game using Django and Coffeescript.  I’m hoping to have a playable game today.  Stay tuned! #ld48


Huldo LD

Posted by
Thursday, April 25th, 2013 1:23 pm

Signing up for the competition seems to be in order. It’s my first one, so hopefully it goes swimmingly.

I’m going to use my old XNA framework engine, but I’ll utilize ANX to get rid of the framework dependency. I tested it a few times and it seems to work mostly fine, except for Rendertargets which leak memory bad and crash the whole thing! Apparently fullscreen support is missing as well which is a bit of a bummer.

I might just ship two versions then eh, when/if I get something done of course.

Here is the codebase for those interested.

Have a nice day.

Chevy Ray’s Codebase

Posted by (twitter: @xMrPhil)
Friday, August 24th, 2012 7:36 am

I took Chevy Ray‘s code that he released as part of his keynote for LD23 and put it up on github.  It is written for his FlashPunk engine.   I used it to make my LD23 entry Seeds of Destruction and it worked out really well for me.

PS Chevy if you happen to see this, I’ll happily transfer the repository to you.

Code on GitHub

Posted by (twitter: @attrition0)
Tuesday, April 24th, 2012 9:14 pm

While more for myself than anything else, I’ve placed my Unity project on GitHub. Maybe somone can make it.

Game: Earth Is Dying!

GitHub: TinyWorld

Till next time!

Lunch #1 and Serious Bug

Posted by (twitter: @xMrPhil)
Saturday, April 21st, 2012 12:04 pm

Lunch 1
It’s almost 3 pm here in Arlington, VA and I’m having my first lunch for LD23. Kind of late but I wait until I’m hungry.

Chicken and Vegetable stir-fry.
Lunch 1

Screenshot 3
It’s coming a long, but I’m feeling the pressure. My task list seems too long. Worst part is I’ve hit a bug! Maybe you can help? My FlashPunk Entity is not calling the Update method! I’ve triple checked and the World Update is being called and calling the supper.Update()!

package SeedsOfDestruction
import com.greensock.easing.Cubic;
import com.greensock.easing.Quad;
import com.greensock.TweenMax;
import flash.display.BitmapData;
import net.flashpunk.Entity;
import net.flashpunk.FP;

public class SeedBullet extends Entity
[Embed(source = “/assets/Seed Bullet.png”)] public static const SEED_BULLET:Class;

public static const SPEED:Number = 200;

public var image:Image;

public function SeedBullet(player:Player)
// Make the Pink transparent
var bitmapData:BitmapData = Global.MakeTransparent(SEED_BULLET);
image = new Image(bitmapData);

super(x, y, image);

FP.angleXY(this, player.stem.angle, 40, player.x, player.y);
image.angle = player.stem.angle;
image.smooth = true;
active = false;
layer = 2;

setHitbox(40, 40);
type = “seedbullet”;


override public function update():void

if (collide(“BadPlant”, x, y))

override public function added():void

var time:Number = FP.distance(x, y, world.mouseX, world.mouseY) / SPEED;, time, { x:world.mouseX, y:world.mouseY, ease:Quad.easeOut, onComplete:explode } );

public function explode():void
world.add(new Explode(this));

Desk Shot + Code

Posted by (twitter: @xMrPhil)
Friday, April 20th, 2012 4:51 pm

Here’s the command center, that’s right I code in a storage closet and that’s my cat Jack.  His hobby is to keep me from using the keyboard or mouse.

Mr. Phil Games Earth Headquarters

Mr. Phil Games Earth Headquarters

I also wanted to share this snippet of AS3 code that lets you choose a color and then convert pixels of that color to transparent pixels:

public static function MakeTransparent(source:*, color:uint = 0x00FF00FF) : BitmapData
// Get the bitmap data so we can fix it
var bitmapData:BitmapData
if (source is Class)
bitmapData = FP.getBitmap(source);
else if (source is BitmapData)
bitmapData = source;

// We aren’t use the transparent feature, hence the color
var bitmapDataNew:BitmapData = new BitmapData(bitmapData.width,
bitmapData.height, true, 0x00000000);
var pt:Point = new Point(0, 0);
var rect:Rectangle = new Rectangle(0, 0, bitmapData.width,
var transparent:uint = 0x00000000;
var maskColor:uint = 0x00FFFFFF;
bitmapDataNew.threshold(bitmapData, rect, pt, “==”, color, transparent, maskColor, true);

return bitmapDataNew;


Ancient Rover

Posted by
Monday, December 19th, 2011 7:28 pm

I finished my game in time for the jam.
If you thought that my last compo typing game was bad, this is a morse code game.
I’m an evil game designer… and loving it.

The only button that works is the spacebar.

Progress Update

Posted by
Sunday, December 18th, 2011 12:14 pm

Ok, so I’ve been busy this weekend, but I managed to get a little bit done.

I have a moon rover and it can be commanded with only the space bar (through dots and dashes a la morse code).

My collisions are still busted.

HELP! (Slick2D)

Posted by (twitter: @caranha)
Sunday, December 18th, 2011 5:44 am

So I´m trying to pretty up my game, but I can´t get Slick2D to render fronts using the UnicodeFont class. (So I can´t use some of the fancier functions from that class)

using the Graphics Class, I manage to:


But when I do:

UnicodeFont font = new Unicodefont(new Font(fontname, Font.Plain,20);

Nothing happens.

I have tried to add a font effect to UnicodeFont, but it is still not displaying :-(

Halp, please?

EDIT: Thanks for all the help! It turns out that I have to font.loadGlyphs() at every render cycle to get my font to display.

[cache: storing page]