Roller Applet

by Neil: Y9 Age ~14

Introduction

Neil needed to rewrite the Pascal program to enable the game to run in an applet and in doing so he showed the perseverance that is essential in a good programmer. He needed a thread to make the game run reasonably smoothly. The code is split into several sections. The init method of the applet initialises variables. The run method of the thread includes calculations that must be performed at set intervals. The mousePressed method responds to mouse clicks to make the ball jump or to start a new game. The paint method uses the results of these calculations to render the graphics. Neil has not included a scoring system in this version and leaves this as an exercise for the enthusiastic reader.

Roller is proving to be popular, since players can improve their skill while playing for a few minutes. You are likely to take advantage of the offer to restart simply by clicking the mouse button:


Game ready for restart

Game ready for restart

Click here to play the game in an applet on a web page.

The Code of RollerApplet.pas

namespace roller_applet;

{
    Copyright (c) 2013 Neil

    Licensed under the Apache License, Version 2.0 (the "License"); you may not
    use this file except in compliance with the License, as described at
    http://www.apache.org/licenses/ and http://www.pp4s.co.uk/licenses/ }


interface

uses
  java.util, java.applet.*, java.awt, java.math, java.awt.event;

type
  RollerApplet = public class(Applet, MouseListener, Runnable)
  private
    pX, pY, barbX, barbY, Distance : Integer;
    ballMovement : String;
    t : Thread;
    playAgain, threadSuspended, Collision : Boolean;

  public
    method init; override;
    method mouseEntered(e : MouseEvent);
    method mouseExited(e : MouseEvent);
    method mouseClicked(e : MouseEvent);
    method mousePressed(e : MouseEvent);
    method mouseReleased(e : MouseEvent);
    method destroy; override;
    method start; override;
    method stop; override;
    method run;
    method paint(g: Graphics); override;  
  end;

implementation

method RollerApplet.mouseEntered(e : MouseEvent);
begin
end;

method RollerApplet.mouseExited(e : MouseEvent);
begin
end;

method RollerApplet.mouseClicked(e : MouseEvent);
begin
end;

method RollerApplet.mousePressed(e : MouseEvent);
begin
  if pY = 200 then 
    ballMovement := 'up';
  if Collision = true then
    playAgain := true;
  e.consume;
end;

method RollerApplet.mouseReleased(e : MouseEvent);
begin
end;

method RollerApplet.init;
begin
  addMouseListener(self);
  setBackground(Color.lightGray);
  pX := 100;
  pY := 200;
  barbX := 615;
  barbY := Integer(Math.random * 43 + 220);
  ballMovement := 'stationary';
  playAgain := false;
end;

method RollerApplet.destroy;
begin
end;

method RollerApplet.start;
begin
  if (t = nil) then
    begin
      t := new Thread(self);
      threadSuspended := false;
      t.start;
    end
  else
    begin
      if threadSuspended then
        begin
          threadSuspended := false;
          locking self do
            begin
              notify;
            end;
        end;
    end;
end;

method RollerApplet.stop;
begin
  threadSuspended := true;
end;

method RollerApplet.run;
begin
  try
    while (true) do
      begin
        if (playAgain = false) and (collision = false) then   
          barbX := barbX - 9;
        if ballMovement = 'up' then
          pY := pY - 6;
        if pY <= 100 then
          ballMovement := 'down';
        if ballMovement = 'down' then
           pY := pY + 6;
        if pY >= 200 then
           ballMovement := 'stationary';
        //Calculate distance between centres using Pythagoras
        Distance := (pX - barbX - 2) * (pX - barbX - 2) + (pY - barbY + 63) * (pY - barbY + 63); 
        if Distance < 600 then 
          Collision := true;      

        if ((barbX <= 0) and (playAgain = false) and (Collision = false)) or
           ((playAgain = true) and (Collision = true))	then
          begin
            barbX := 615;
            barbY := Integer(Math.random * 43 + 220);
          end;
		  
        //Thread checks to see if it should suspend itself
        if threadSuspended then
          begin
            locking self do
              begin
                while threadSuspended do
                  begin
                    wait;
                  end;
              end;
          end;
        repaint;
        t.sleep(15); //Interval given in milliseconds
      end; //while true
    except
      on e : InterruptedException do
        begin
        end;
    end; //of try
end;

method RollerApplet.paint(g: Graphics);
const
  barb = '*';
begin
  g.Color := Color.red;
  g.fillOval(pX, pY, 26, 26);
  g.Color := Color.green;
  g.fillRect(0, 226, 640, 360);
  g.Color := Color.black;
  g.Font := new Font( "Arial", Font.PLAIN, 90);
  g.drawString(barb, barbX, barbY);
  
  if Collision = true then 
    begin 
      g.Font := new Font("Courier", Font.PLAIN, 42);
      g.Color := Color.blue;
      g.drawString('YOU LOSE', 220, 50);
      g.Font := new Font("Courier", Font.PLAIN, 30);
      g.drawString('Click to play again.', 130, 100);
      ballMovement := 'stationary';
    end;

  if playAgain = true then 
    begin
      Collision := false;
      playAgain := false;
      pY := 200;
    end;
end;

end.

Programming - a skill for life!

by Neil: Y9 Age ~14