Knockout Clone - Discriminated Unions with F#

Monday, April 27, 2009 by gradbot

F# supports discriminated unions. This along with pattern matching makes for some short and elegant code which quite frankly is awesome. Here's a small example.

type PieceType = Breakable | Solid

member this.needsDisposing() =
match pieceType with
| Breakable -> (life <= 0)
| Solid -> false

member this.points() =
match pieceType with
| Breakable -> 1
| Solid -> 0
Here I use a property of type PieceType to both define when a piece on the board needs to be removed and if the piece returns a point when it is hit. The calling class has no knowledge of this state and filters out pieces based on the returned bool flag needsDisposing() along with adding whatever is returned by points() to the total point score.





Breakout Clone - Line Segment Intersection Algorithm

Sunday, April 19, 2009 by gradbot

For my next game Breakout, I need to detect collisions for a moving object. I choose line segments to represent object borders and motion. Calculating line segment intersections is a simple and fast algorithm and there are more advanced scan line algorithms to speed it up if needed. In order to use it, I take the ball's position and old position as the line segment for the ball.

The hard part of any collision algorithm is of course dealing with the collision. Detection is always easy because it's been done before. I'm doing collision with a simple reflection of the velocity off of the line segment it's intersecting. This works great until you collide close to another line segment and you happen to reflect to the other side of it. To fix this, I made the intersection and reflection algorithm recursive. This works great except for possible race conditions. Also, you no longer have a constant speed for your ball. A quick fix for the race condition is to check to make sure you can't intersect with the same line segment twice in a row. Having the ball move a little faster is acceptable.

Project Homepage

Here's a burn test where I had set the ball to a very high speed to see if it could escape it's enclosure or enter one of the other objects. In order to make any errors apparent, I added a line trail of a random colors behind the ball. This ends up filling in all of the movable area.

I must say it's pretty cool to see the ball travel to every little nook and cranny of the board. This must be how particle chaos works. Like a fragrance filling the room.

Filed under , , , having  

Tetris in XNA with F#

Saturday, April 11, 2009 by gradbot

I learned a few things on this project. One is that arrays are not the end all of data structures. My first instinct from my C++ background was to make a 2D array for the tetris game board. After wasting an hour or two I realized that it was over complicated and that a Set would work really well.

Basically each piece in the game is a set of tuple points. The game board boarder is a set of tuple points and the already fallen pieces is a set of pieces. I had to keep the concept of a piece in order to keep the color associated with each tuple point. This setup made collision detection simple. It was just a matter of Seq.union_all and Set.intersect

I'm pretty amazed by the relatively few lines of code it took me to recreate the game. The main game logic is incredibly small. Most of the code is spent setting up the system and handling user input/graphics.

Project Time: 13 hours
Project - Source Code

The game is played with the DPad and A/B buttons.

Filed under , , having