Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Need help with KeyListener issues in Java Pong Applet
Index -> General Programming
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
-PseudoNymX-




PostPosted: Sat Feb 16, 2008 12:42 pm   Post subject: Need help with KeyListener issues in Java Pong Applet

Heya. I sent an email to my teacher regarding this issue, but I figured I'd post it too, in case he didn't get to it. The KeyListener functions (keyPressed, keyReleased) in this code don't seem to be triggering at all. I imported all the right things (I think) and I can't find any issues with my code... can anyone tell me why this isn't working? (Code listed and .java file attached for those too lazy to copy+paste)

// Quinn Strahl's Pong

import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.io.*;
import java.awt.event.*;

public class Pong extends JApplet implements Runnable, KeyListener {
Image dbImage;
Graphics dbg;
boolean[] keys = new boolean[256];

Random die = new Random();

Paddle player1 = new Paddle(20, 125, 0);
Paddle player2 = new Paddle(470, 125, 0);

int x = 250;
int y = 150;
int xv, yv = 0;

public void init() {
addKeyListener(this);
}

public void start() {
player1.points = 0;
player2.points = 0;
for (int i = 0; i < 256; i++) {
keys[i] = false;
}
relaunch();
Thread th = new Thread(this);
th.start();
}

public void stop() {
}

public void destroy() {
}

public void keyTyped(KeyEvent e) {
}

public void keyPressed(KeyEvent e) {
keys[e.getKeyCode()] = true;
System.out.println((char)e.getKeyCode() + " down");
repaint();
}

public void keyReleased(KeyEvent e) {
keys[e.getKeyCode()] = false;
System.out.println((char)e.getKeyCode() + " up");
repaint();
}

public void delay(int t) {
try {
Thread.sleep(t);
}catch (InterruptedException e) {
System.out.println(e);
}
}

public void checkBounds() {
System.out.println("checkBounds");
if (player1.y >= 250) {
player1.y = 250;
player1.yv = -player1.yv / 2;
}else if (player1.y <= 0) {
player1.y = 0;
player1.yv = -player1.yv / 2;
}
if (player2.y >= 250) {
player2.y = 250;
player2.yv = -player2.yv / 2;
}else if (player2.y <= 0) {
player2.y = 0;
player2.yv = -player2.yv / 2;
}
if (x < 3) {
relaunch();
player2.points += 1;
}else if (x > 497) {
relaunch();
player1.points += 1;
}
if (y > 297 || y < 3) {
yv = -yv;
}
if (hitPaddle()) {
xv = -xv;
}
if (player1.yv > 10) {
player1.yv = 10;
}else if (player1.yv < -10) {
player1.yv = -10;
}
if (player2.yv > 10) {
player2.yv = 10;
}else if (player2.yv < -10) {
player2.yv = -10;
}
}

public void checkKeys() {
System.out.println("checkKeys");
if (keys[(int)'q']) {
player1.yv += 1;
System.out.println("player1 ^");
}
if (keys[(int)'a']) {
player1.yv -= 1;
System.out.println("player1 v");
}
if (keys[(int)'o']) {
player2.yv += 1;
System.out.println("player2 ^");
}
if (keys[(int)'l']) {
player2.yv -= 1;
System.out.println("player2 v");
}
}

public void updateLocations() {
System.out.println("updateLocations");
player1.y += player1.yv;
player2.y += player2.yv;
x += xv;
y += yv;
}

public void relaunch() {
x = 250;
y = 150;
if (die.nextInt(2) == 0) {
xv = 1;
}else {
xv = -1;
}
yv = die.nextInt(3) - 1;
}

public boolean hitPaddle() {
if (x == player1.x + 10 && y >= player1.y && y < player1.y + 50)
return true;
else if (x == player2.x - 3 && y >= player2.y && y < player2.y + 50)
return true;
else
return false;
}

public void paint(Graphics g) {
g.setColor(Color.BLACK);
g.fillRect(0,0,500,300);
g.setColor(Color.GREEN);
g.fillRect(player1.x, player1.y, 10, 50);
g.fillRect(player2.x, player2.y, 10, 50);
g.fillOval(x,y,6,6);
g.drawString("" + player1.points,240,10);
g.drawString("" + player2.points,255,10);
}

public void run() {
while (player1.points < 10 && player2.points < 10) {
checkKeys();
updateLocations();
checkBounds();
repaint();
delay(5);
}
}
}

class Paddle {
int x, y, yv, points;

public Paddle(int newX, int newY, int newYv) {
x = newX;
y = newY;
yv = newYv;
points = 0;
}
}



Pong.html
 Description:
the .html file to run the applet from

Download
 Filename:  Pong.html
 Filesize:  165 Bytes
 Downloaded:  186 Time(s)


Pong.java
 Description:
the .java file

Download
 Filename:  Pong.java
 Filesize:  3.69 KB
 Downloaded:  182 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> General Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 1 Posts ]
Jump to:   


Style:  
Search: