hi,
i'm writing a user-controlled game for a project, but somehow I can't seem to enter anything (as in, when user presses a key nothing happens). is there anything that i'm missing/should learn to fix this? thanks in advance
import java.util.Scanner; import java.awt.Font; import java.awt.GraphicsEnvironment; import java.util.ArrayList; import java.awt.event.*; import javax.swing.*; class Game extends JFrame implements KeyListener { //public void init() { // this.addKeyListener(this); // } public void keyPressed(KeyEvent e) { double hx = 100, hy = 150; // starting (x,y) coordinate position double px = 15.0, py = 150; // starting (x,y) coordinate position // hades controls if ((e.getKeyCode() == KeyEvent.VK_UP) | (e.getKeyCode() == KeyEvent.VK_KP_DOWN)) hy += 2; if ((e.getKeyCode() == KeyEvent.VK_LEFT) | (e.getKeyCode() == KeyEvent.VK_KP_DOWN)) hx -= 2; if ((e.getKeyCode() == KeyEvent.VK_DOWN) | (e.getKeyCode() == KeyEvent.VK_KP_DOWN)) hy -= 2; if ((e.getKeyCode() == KeyEvent.VK_RIGHT) | (e.getKeyCode() == KeyEvent.VK_KP_DOWN)) hx += 2; // persephone controls if (e.getKeyCode() == KeyEvent.VK_W) py += 2; if (e.getKeyCode() == KeyEvent.VK_A) px -= 2; if (e.getKeyCode() == KeyEvent.VK_S) py -= 2; if (e.getKeyCode() == KeyEvent.VK_D) px += 2; // draw stuff, REMEMBER EXCEPTIONS (WITH WALL) StdDraw.setPenColor(StdDraw.BLACK); StdDraw.filledSquare(hx,hy,15); // hades; pen radius is 0.5 StdDraw.filledSquare(px,py,15); // persephone StdDraw.show(); } public void keyReleased(KeyEvent e) { } public void keyTyped(KeyEvent e) { // if typed b, then quit } public void newGame(String command) { // ... code omitted } /** * empty constructor for Game class */ public static void Game(String[] args) { //code } }