Hi everyone, I am facing a dilemma regarding about my BreakOut game applet that I am developing for the PJEE3.0.2 environment.
It seems that the KeyListener I have implemented in my applet does not respond. I pressed the left button to move the player paddle left, nothing happens.
I tried to press the F1 button to start a new game, nothing happens. You get the idea.
My dilemma starts at the methods keyTyped and keyPressed.
The words RC.LEFT and RC.DOWN etc etc are instances of a class which I have to work with to make this applet compatible with a Phillips Streamium device.
I would appreciate it very much if you to give me any advice on my problem. Thank you.
/**
- @(#)Break.java
- Sample Applet application
- @author
-
@version 1.00 05/06/28
*/
import java.awt.;
import java.applet.;
import java.net.*;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import com.philips.streamium.TVApplet;
import com.philips.streamium.TVLayout;
import com.philips.streamium.ColorButton;
import com.philips.streamium.ColorButtonBar;
import com.philips.streamium.RC;
import com.philips.streamium.Debug;
import com.philips.streamium.LoadingScreen;
import com.philips.streamium.Streamium;
//KEY NOTES TO TAKE OFF WHEN PROGRAMMING AN STREAMIUM APPLET
//
//
//Some Streamium devices can support a Streamium Applet’s maxiumum Width of 780
//
//Most Streamium can support an Applet’s Width of 510
//Most Streamium can support an Applet’s height of 480
//
//The left and right components of a streamium applet measure at :
//105 for its width
//480 for its height
//
//
public class Break extends TVApplet implements Runnable, KeyListener {
Thread THREAD = null;
Image ii;
Graphics gh;
private Image dbImage;
private Graphics dbg;
boolean currently_playing_game;
boolean show_title = true;
String status;
int count;
int number;
int applet_width = 510;
int applet_height = 480;
int bricks_per_line = 16;
int brick_width = 25;
int brick_height = 10;
int border_width = 15;
int space_inbetween_bricks = 5;
int number_of_brick_lines = 10;
int starting_y_position_for_bricks = 50;
int scoreheight = 20;
int paddle_position_for_physics = 240;
int paddle_x_position;
int paddle_y_position = 50;
int ball_x_position = 240;
int ball_y_position = 305;
int ball_y_position_speed = 2;
int ball_x_position_speed;
int paddle_width = 30;
int paddle_height = 7;
int top_border = 15;
int left_border = 15;
int right_border = 495;
int player_score = 0;
int lives_left = 4;
int ball_size = 5;
int movement_rate = 0;
boolean[] show_bricks;
private ColorButtonBar m_colorButtonBar = null;
private ColorButton m_cbTypeface = null;
private ColorButton m_cbSizeInc = null;
private ColorButton m_cbSizeDec = null;
private ColorButton m_cbExit = null;
final int screen_delay = 300;
public void init()
{
//initializes the game's values
//init is simplified for the purposes of documenting the
//game to be simple.
m_colorButtonBar = new ColorButtonBar(this);
m_cbTypeface = null;
m_cbTypeface = new ColorButton("Typeface");
m_colorButtonBar.setButton(ColorButtonBar.RED, m_cbTypeface);
m_cbSizeInc = new ColorButton("Bigger");
m_colorButtonBar.setButton(ColorButtonBar.GREEN, m_cbSizeInc);
m_cbSizeDec = new ColorButton("Smaller");
m_colorButtonBar.setButton(ColorButtonBar.YELLOW, m_cbSizeDec);
m_cbExit = new ColorButton("Exit");
m_colorButtonBar.setButton(ColorButtonBar.BLUE, m_cbExit);
this.add(TVLayout.BUTTONS, m_colorButtonBar);
validate();
this.addKeyListener(m_colorButtonBar);
this.addKeyListener(this);
setBackground(Color.black);
currently_playing_game = false;
show_bricks = new boolean[bricks_per_line * number_of_brick_lines];
gameInitiate();
}
public void gameInitiate()
{
//initializes the game's gameplay values
ball_x_position_speed = 0;
count = screen_delay;
paddle_x_position = 225;
initiateGameBrick();
}
public void initiateGameBrick()
{
//initializes the bricks in the game
int i;
//* For every brick in the game,
for (i=0; i<bricks_per_line * number_of_brick_lines; i++) //* set true.
show_bricks[i] = true;
}
public void keyPressed(KeyEvent keyEvent)
{
int key = keyEvent.getKeyCode();
switch(key)
{
case RC.LEFT:
//Make paddle go left
paddle_x_position-=6;
repaint();
break;
case RC.RIGHT:
//Make paddle go right
paddle_x_position+=6;
repaint();
break;
case RC.RED:
//Set a new game
currently_playing_game = true;
gameInitiate();
repaint();
break;
case RC.GREEN:
//Quit the game
currently_playing_game = false;
repaint();
break;
case RC.BLUE:
Streamium.exit(Streamium.EXIT_OK);
break;
}
}
public void keyTyped(KeyEvent keyEvent)
{
int key = keyEvent.getKeyCode();
/*
if (currently_playing_game) //*
{ //* Checks if the game is started,
if (key == RC.LEFT) //* if not started, it will only respond
paddle_x_position-=6; //* to typing 's' or 'S'.
if (key == RC.RIGHT) //*
paddle_x_position+=6; //* If game is started, the left, right,
if (key == RC.GREEN) //* escape buttons will respond.
currently_playing_game=false; //*
} //* Left and right are the controls
else //* Escape is to quit the game and go back
{ //* to starting screen.
if (key == RC.RED)
{
currently_playing_game=true;
gameInitiate();
}
}
*/
switch(key)
{
case RC.LEFT:
//Make paddle go left
paddle_x_position-=6;
repaint();
break;
case RC.RIGHT:
//Make paddle go right
paddle_x_position+=6;
repaint();
break;
case RC.RED:
//Set a new game
currently_playing_game = true;
repaint();
break;
case RC.GREEN:
//Quit the game
currently_playing_game = false;
repaint();
break;
case RC.BLUE:
Streamium.exit(Streamium.EXIT_OK);
break;
}
}
public void keyReleased(KeyEvent keyEvent){}
public void start()
{
if (THREAD == null)
{
THREAD = new Thread(this);
THREAD.start ();
}
}
public void stop()
{
if (THREAD != null)
{
THREAD.stop();
THREAD = null;
}
}
public void run()
{
Graphics g;
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
g=getGraphics();
while(true)
{
if (currently_playing_game)
PlayGame();
else
ShowIntroScreen();
try
{
paint(g);
THREAD.sleep(50);
}
catch (Exception e)
{
break;
}
}
}
public void update(Graphics g)
{
if (dbImage == null)
{
dbImage = createImage (510, 480);
dbg = dbImage.getGraphics ();
}
// clear screen in background
dbg.setColor (getBackground ());
dbg.fillRect (0, 0,510,480);
// draw elements in background
dbg.setColor (getForeground());
paint (dbg);
// draw image on the screen
g.drawImage (dbImage, 0, 0, this);
//paint(g);