Please help with my game applet, getting desperate!
Sorry to post so much code but Most of it is absolutely fine.
I cannot get keyboard interaction to work in this simple bat and ball applet.
Its part of my university work and VERY important i get it working.
Please look at my comments, those are where the problems are
Im having trouble with KeyListeners. I want the programme in detect when I press left key or right key and move the paddle by incrementing or decrementing the co-ordinates.
import java.lang.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import java.applet.*;
import java.io.*;
import java.net.*;
import java.awt.Graphics;
import java.awt.Event;
import java.awt.Color;
import java.awt.Point;
import java.awt.Rectangle;
public class BallGame extends Applet
{
Ball myBall, myBall2;
Paddle myPaddle,myPaddle2;
Color ballcolor,ballcolor2, bkcolor, padcolor;
int d=0, s=0, px=160, py=370, ppx=60, ppy = 10,qqx=60,qqy=10,qdx=1,qx=160,qy=70;
KeyEvent e;
public void init()
{
BallGame f;
f = new BallGame();
AudioClip music = getAudioClip(getCodeBase(), "thing.wav");
music.loop();
}
public BallGame ()
{
setSize(600, 400);
bkcolor = new Color(0,0,64);
setBackground(bkcolor);
myBall= new Ball(50,50);
myBall2= new Ball(200,300);
myPaddle= new Paddle(160);
setVisible(true);
}
public void paint(Graphics g)
{
ballcolor = new Color(255,0,255);
ballcolor2 = new Color(0,255,200);
g.setColor(ballcolor);
myBall.move();
myBall.paint(g);
g.setColor(ballcolor2);
myBall2.move();
myBall2.paint(g);
g.setColor(padcolor);
this.setFocusable(true);
myPaddle.move(); // THIS IS WHERE MOVE IS "REFERENCED" see move in class "paddle" below :SICK:
myPaddle.paint(g);
try {Thread.sleep(1);}
catch(InterruptedException e){};
repaint();
}
public class Ball extends Canvas
{
private int dx=1;
private int dy=1;
private int x;
private int y;
Ball(int xin,int yin)
{
x=xin;
y=yin;
}
void move()
{
if(x>=600-40)
{dx=-dx;s++;
play(getDocumentBase(),"Bonk02.wav");}
if(x<=0)
{dx=-dx;s++;
play(getDocumentBase(),"Bonk02.wav");}
if(y>=400-40)
{dy=-dy;s=s-10;
play(getDocumentBase(),"Bonk02.wav");}
if(y<=0)
{dy=-dy;s++;
play(getDocumentBase(),"Bonk02.wav");}
y=y+dy;
x=x-dx;
if((y+40>=py)&&
((x+20>=px)&&(x+20<px+ppx)))
{dx=-dx;dy=-dy;s++;
play(getDocumentBase(),"Boink.wav");}
}
public void paint(Graphics g)
{
g.fillOval(x, y, 40, 40);
g.fillRect(299,10,2,380);
g.drawString("SCORE :"+s,50,50);
}
public void update(Graphics g)
{
paint(g);
}
}
//class BallGame$Paddle
public class Paddle extends Applet implements KeyListener
{
Paddle(int padin)
{
px=padin;
}
public void keyPressed (KeyEvent e){myPaddle.move();} //I DONT KNOW WHERE TO PUT THESE REALLY?
public void keyReleased (KeyEvent e){}
public void keyTyped (KeyEvent e){}
public void move()
{
if((e.getKeyCode()==KeyEvent.VK_LEFT) //IF I INCLUDE THIS CODE I GET NULLPOINTER EXCEPTIONS AND THE GAME FREEZES
&&(px<=0))
{
px--;
}
else if ((e.getKeyCode()==KeyEvent.VK_RIGHT)
&&(px>=(600-ppx)))
{
px++;
}
px=px;
if(qx>600-qqx)
{qdx=-qdx;}
if(qx<=0)
{qdx=-qdx;}
qx=qx+qdx;
}
public void paint(Graphics g)
{
padcolor = new Color(255,255,0);
g.fillRect(px,py,ppx,ppy);
g.fillRect(qx,qy,qqx,qqy);
}
}
}
PLEASE!!!! :( im desperate
[4433 byte] By [
jonboi47] at [2007-11-11 8:07:49]

# 2 Re: Please help with my game applet, getting desperate!
I'm confused. You have two classes that extend Applet... why?
Okay, about KeyListeners...
There are three ways to add them.
1) implement KeyListener, and add "this"
2) add a class that is a KeyListener
a. make that class an anonymous inner-class
With the first way, you must add all the methods in the interface. Take this applet for example. I have written it the 3 ways posted above.
1)
import java.applet.Applet;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
public class KeyListenerExample extends Applet implements KeyListener {
public void init() {
addKeyListener(this);
}
public void keyPressed(KeyEvent e) {
System.out.println("keyPressed: " + e.getKeyChar());
}
public void keyTyped(KeyEvent e) {
System.out.println("keyTyped: " + e.getKeyChar());
}
public void keyReleased(KeyEvent e) {
System.out.println("keyReleased: " + e.getKeyChar());
}
}
2)
import java.applet.Applet;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class KeyListenerExample extends Applet {
public void init() {
addKeyListener(new MyKeyListener());
}
private class MyKeyListener extends KeyAdapter {
public void keyPressed(KeyEvent e) {
System.out.println("keyPressed: " + e.getKeyChar());
}
public void keyTyped(KeyEvent e) {
System.out.println("keyTyped: " + e.getKeyChar());
}
public void keyReleased(KeyEvent e) {
System.out.println("keyReleased: " + e.getKeyChar());
}
}
}
3)
import java.applet.Applet;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
public class KeyListenerExample extends Applet {
public void init() {
addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent e) {
System.out.println("keyPressed: " + e.getKeyChar());
}
public void keyTyped(KeyEvent e) {
System.out.println("keyTyped: " + e.getKeyChar());
}
public void keyReleased(KeyEvent e) {
System.out.println("keyReleased: " + e.getKeyChar());
}
});
}
}
Be aware that although mine are applets, this will work with any subclass of Component.
For checking if the key pressed is a certain type, you can do:
// assume that e is the KeyEvent parameter
if (e.getKeyCode() == KeyEvent.VK_UP) {
System.out.println("up was pressed");
}
Since the key codes are integral types, you can also do:
// assume that e is the KeyEvent parameter
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
System.out.println("up was pressed");
break;
}
I tend to use the switch-case way when I am checking for multiple key events.
Hope this helps.
destin at 2007-11-11 22:37:22 >

# 4 Re: Please help with my game applet, getting desperate!
Thanks, i dont think i do have two classes etending applet, although one extends canvas and i think thats probably pointless.
Look at your code. You have a class BallGame that extends Applet and a class Paddle that extends Applet.
I think one problem is that id tried to use keypressed qithout keyreleased.
If you are implementing KeyListener then you need to include all methods (keyPressed(KeyEvent), keyReleased(KeyEvent), keyTyped(KeyEvent)).
And as for movement, it would be best to have boolean variables for your movement. If you just moved by pressing, people could set their key repeat speeds up, and move faster.
Add a KeyListener similar to this:
import java.awt.event.*;
// ...
boolean leftPressed = false;
boolean rightPressed = false;
// ...
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
leftPressed = true;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
rightPressed = true;
}
}
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
leftPressed = false;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
rightPressed = false;
}
}
});
Hope this helps.
destin at 2007-11-11 22:39:26 >
