Skip to content

199. A variation on the game of Pool

February 20, 2015

I’ve previously made a game of pool, that turned into snooker because drawing pool balls was hard. This time, I’ve created a variation where there are no pockets, and the idea is to make the white ball touch your balls and not those of the opponent. It’s simple, but I found it quite addictive.

This post explains some design decisions and features of the code that you may find interesting.

Table design – Top down 2D using physics

This was quite an easy decision. I’d dearly love to do this in 3D, so you can zoom all around the table and view it from any angle. But it doesn’t really add much to the fun of the game, and handling collisions and rotation in 3D is really, really hard.

It’s not so much the collisions, or even rotating left or right, that’s hard, because I’ve done posts on how to write code for these, yourself. It’s the forward and backward rotation, on top of the sideways rotation, that makes it extremely hard, especially if you have 16 balls bouncing off each other at once.

So I’m going to use the (Box2D) 2D physics engine built into Codea, and make this a top down game. The balls will spin left or right, but not forward or backward (end over end). You also can’t add spin to the white ball by hitting it on the side.

Ball design – standard pool balls

I had some choices here. My game doesn’t need numbered balls, just two different sets of balls, so it would be easiest just to use two solid colours, eg blue and red.

If, on the other hand, I draw the standard pool balls, I have to create the two types

  • solids, which are solid colours with a white spot containing the number, and
  • stripes, which are white balls with a coloured stripe, and  a white spot containing the number

Solids are easy – just two ellipses, one for the ball, and one for the white spot.

Stripes are harder, because the ends of the stripe need to curve to fit the rounded edge of the ball. There is no easy way I can think of, to do that in Codea. However, I remembered that when you draw a line, it has rounded ends, and by experimenting, I found that if I drew a thick line, I could get its rounded ends to line up pretty closely with the edge of the ball.

I hold the 16 balls in a table, each item containing

  • the image of the ball (I create it just once, at the start, and sprite it after that)
  • the physics object that will interact with all the other physics objects, and the screen edges (I put physics edges all around the screen)
  • the starting position

Getting the balls into the usual triangular starting position was tricky, because it you stack them too close, the physics engne immediately pushes them apart, creating a mess. So that took a bit of fiddling around.

Getting a 3D look

The next problem is that the balls look flat, like checkers pieces. Look at the top set of balls, below.

So what I did was create a reflection, by simply making a small image with a series of ellipses that became fainter as they moved out from the middle. I sprite this on top of every ball, after I draw it, and you can see it makes a big difference, in the lower set of balls above. The balls appear to be 3D – even though they only spin right to left, and not end over end.

 Aiming and shooting

I wanted to make this as simple as possible, with no onscreen controls. So you simply touch anywhere on the screen, and drag your finger in the direction you want to shoot.Of course, fingers are very clumsy, so this is going to be very inaccurate.

So I added a visual guide which appears when you touch the screen, starting from the white ball, and going in the same direction as your finger. The further you drag, the more power is used, and a thick line inside the white ball shows the power level. When you stop touching the screen, the shot is taken.

The program figures out the scoring for you, and tells you when it’s the next player’s turn.

The rules

The rules are really simple. When you take a shot, you get a point for every one of your balls that you touch (including multiple touches of any ball), and you lose a point for every opponent’s ball touched. If you score less than +2, you lose your turn, otherwise your score is added to your previous turn, and you keep playing.

The black ball is special. It is not one of the “solid” colour balls, but gives you 5 extra points if it is touched (for it to count, though, you still have to score at least +2 points without it).

First to 50 wins.

The code

is here.

Possible variations

  • add a “hole” in one corner where balls disappear
  • add an area where balls get frozen and can’t move
  • change the scoring to use the ball numbers
  • change the game altogether, eg to “round the clock”, where you have to hit each number in turn from 1 to 16
  • and many, many more

 

One Comment

Trackbacks & Pingbacks

  1. Index of posts | coolcodea

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: