Hacked – A Game – Tennis Rally V1

A simplistic tennis rally game, in 120 lines. You’re not meant to be able to beat the computer opponent, your aim is to keep the rally going. Written using the Hackpad in the Android app “Hacked”, which I’ve written more about here.

Possible Todos:

  1. Control player ball hit direction based on the point of the bat hit, e.g. top half of bat = NE, bottom half = SE, middle of bat = E (would require a 5px bat)
  2. increase speed factor after every X points?
  3. Difficulty levels, longer or shorter bats?
Notes about the code

The grid is based on something like a 22×22 grid, I took the top row for the score/top score.

The direction of the ball is marked by an integer between 1 and 8, Based on this:

    8
  1   7
2	6
  3   5
    4

Bounces are always at 90 degrees angles. A ball hitting the wall at travelling NE (7) will rebound in direction SE (5), and vice versa.

[collapse]
// DrawBat (int xCoord, int yCoord, int batLength)
function f1: var_a, var_b, var_c {
  while var_d < var_c {
    draw(var_a, var_b + var_d)
    var_d++;
  }
}
  
// DrawBall (int[] xyCoord)
function f2: var_a {
  draw(var_a[0], var_a[1]);
}

// CalculateNewBallCoords (int[] xyCoord, int direction)
function f3: var_a, var_b {
  if var_b > 0 && var_b < 4 {
	var_a[0]--;
  }
  if var_b > 4  && var_b < 8 {
    var_a[0]++;
  }
  if var_b == 1 || var_b > 6 {
    var_a[1]--;
  }
  if var_b > 2 || var_b < 6 {
    var_a[1]++;
  } 
  return var_a;
}

// HasHit(int[] xyBallCoords, int playerXCoord, int batLength)
function f4: var_a, var_b, var_c {
  return var_a[0] == 0 && var_a[1] > var_b - 1 && var_a[1] < var_b + var_c;
}

// CalculateNewPlayerBPosition (int[] xyBallCoord, int batLength)
function f5: var_a, var_b {
  var_b = var_a[1] - (var_b / 2);
  if var_b < 1 {
    var_b = 1;
  }
  if var_b > 20 {
    var_b = 20
  }
  return var_b;  
} 

//RandomHitDirection(int currentDirection)
function f6: var_a
{
   return random(3) + 1;
}

//CalculateNewBallDirection(int xyCoord[], int direction)
function f7: var_a, var_b {
	if var_a[1] == 0 { // hitting the top wall
	  if var_b == 7 { // heading NE
		var_b == 5; // bounce SE
	  }
	  if var_b == 1 { // heading NW
	    var_b == 3; // bounce SW
	  }	  
	}

	if var_a[1] = 19 { // hitting the bottom wall
	  if var_b == 3 { // heading SW
	    var_b == 1; // bounce NW
	  }
	  if var_b == 5 { // heading SE
	    var_b = 7; // bounce NE
	  }
	}
	return var_b; // return updated direction
}

//ControlGameSpeed(int speedFactor)
function f8: var_a {
  while var_c < var_ a {
    var_b = 0;
    while var_b < 1000 {
    {
	  var_b++;
    }
	var_c++;
  }
  return true;
}

// var_a = PlayerA XCoordinate
// var_b = ball coords 
// var_c = current score 
// var_d = direction
// var_e = PlayerB XCoordinate
// var_f = speed
// var_i = initialised
// var_l = bat length
// var_n = topScore

//  A title screen
if var_i == 0 {
  // if previous score was higher than top score, update top score
  if var_c > var_n {
    var_n = var_c;
  }

  // title text
  draw_text(6, 2, "TENNIS RALLY V1");
  draw_text(3, 4, "A to begin/restart");
  draw_text(3, 5, "B to serve");
  draw_text(8, 7, "TODO");
  draw_text(2, 8, "* Speed increase per X points?");
  draw_text(2, 9, "* Control over return direction?");
  draw_text(2, 10, "* Bat length  option");
  
  var_b = [].push(18).push(12); // Set ball position.
}

// RESETTING GAME STATE

/ player ready
if b_btn() && var_i == 1
{
  var_d = 2;
  var_i = 2;
}

// RESET STATES
if a_btn() && var_i != 1
  var_a = 10; // reset player X coord to middle
  var_e = 10; // Do the same for the opponent
  var_i = 1; // game has initialised
  var_f = 10; // game speed, slower is faster.
  var_d = 0; // Stop ball movement until player is ready
  var_l = 5; // Bat length
  var_c = 0; // set score to 0
  var_b = [].push(18).push(12); // will hold ball coordinates, opponent serves
}

// player movement

if down() && var_a < 20 && var_i == 2 { // can't go down any further if you reach the bottom
  var_a++;
}

if up() && var_a > 1 && var_i == 2 { // can't go up any further if you reach the top
  var_a--;
}

// move ball position based on direction
var_b = f3(var_b, var_d); 

// COLLISION DETECTION ROUTINES

// check if player 2 is hitting it, change random new direction
if var_b[0] == 19 {
  var_d = f6(var_d);
}

// check if ball bouncing off top wall or bottom wall
var_d = f7(var_b, var_d);

// Score counter
if f4(var_b, var_a, var_l) {
  var_c++;
  var_d = 6; // Player always hits a horizontal return in V1
}

// game over
if var_b[0] < 0 && var_i == 2 {
  var_i = 0; // show title screen, wait for A button press.
}

// PlayerB movement based on current ball position
var_e = f5(var_b, var_l);

// DRAW ROUTINES
draw_text(0, 0, "score " + var_c + " top " + var_n); // draw score info
f1(0, var_a, var_l); // draw player 1 bat
f1(19, var_e, var_l); // draw player 2 bat
f2(var_b); // draw ball
f8(var_f); // delay game speed, lower is faster.

Leave a comment

Your email address will not be published. Required fields are marked *

  • Facebook
  • Google+
  • LinkedIn
  • Twitter
  • YouTube
  • IMDb