I am trying to make a game in Java and this is my first time dealing with multithreading (well, asides from playing music concurrently via the Clip class.) I have a Canvas class which extends JPanel, but within the Canvas class I also have a KeyListener to take input, shown here:
private class myKeyListener implements KeyListener
{
public void keyPressed(KeyEvent keyEvent)
{
if(keyEvent.getKeyCode() == KeyEvent.VK_ESCAPE){System.exit (0);}
Thread thread3 = new Thread()
{
public void run() {
if(keyEvent.getKeyCode() == KeyEvent.VK_RIGHT){moveX(5, player1);}
if(keyEvent.getKeyCode() == KeyEvent.VK_LEFT){moveX(-5, player1);}
if(keyEvent.getKeyCode() == KeyEvent.VK_UP){moveY(-5, player1);}
if(keyEvent.getKeyCode() == KeyEvent.VK_DOWN){moveY (5, player1);}
}
};
Thread thread4 = new Thread()
{
public void run() {
if(keyEvent.getKeyCode() == KeyEvent.VK_D){moveX(5, player2);}
if(keyEvent.getKeyCode() == KeyEvent.VK_A){moveX(-5, player2);}
if(keyEvent.getKeyCode() == KeyEvent.VK_W){moveY(-5, player2);}
if(keyEvent.getKeyCode() == KeyEvent.VK_S){ moveY(5, player2);}
}
};
Thread thread5 = new Thread()
{
public void run() {
repaint();
}
};
thread3.start();
thread4.start();
thread5.start();
try{
thread3.join();
thread4.join();
thread5.join();
}
catch (Exception e){System.out.println(e);}
repaint();
}
public void keyReleased(KeyEvent keyEvent)
{
}
public void keyTyped(KeyEvent keyEvent)
{
}
}
My goal is to have it so that one can move both players (which are Rectangles) at the same time on the canvas. Currently, one can only move them one at a time, but never both at the same time. This is my first time dealing with multithreading so I apologize in advance if I am making a rookie mistake.
Here is the code.
You need just one thread, to do the "game loop".
import java.awt.*;
import java.awt.event.*;
import javax.swing.JPanel;
import java.util.ArrayList;
public class Canvas2 extends JPanel {
// attributes
private Rectangle player1;
private Rectangle player2;
private ArrayList<KeyEvent> log;
private boolean player1left = false;
private boolean player1right = false;
private boolean player1up = false;
private boolean player1down = false;
private boolean player2left = false;
private boolean player2right = false;
private boolean player2up = false;
private boolean player2down = false;
// constructor
public Canvas2() {
// initialize object
player1 = new Rectangle(50, 50, 50, 50);
player2 = new Rectangle(50, 50, 50, 50);
log = new ArrayList<KeyEvent>();
// set canavs background colour
setBackground(Color.white);
// add the key listener in the constructor of your canavas/panel
addKeyListener(new myKeyListener());
// ensure focus is on this canavas/panel for key operations.
setFocusable(true);
Thread gameLoop = new Thread() {
public void run() {
while (true) {
updatePlayers();
repaint();
pause(10);
}
}
};
gameLoop.start();
}
private void updatePlayers() {
if (player1left) {
moveX(-5, player1);
}
if (player1right) {
moveX(5, player1);
}
if (player1up) {
moveY(-5, player1);
}
if (player1down) {
moveY(5, player1);
}
if (player2left) {
moveX(-5, player2);
}
if (player2right) {
moveX(5, player2);
}
if (player2up) {
moveY(-5, player2);
}
if (player2down) {
moveY(5, player2);
}
}
// painting
public void paintComponent(Graphics graphics) {
super.paintComponent(graphics);
Graphics2D graphics2d = (Graphics2D) graphics;
graphics.setColor(Color.blue);
graphics2d.fill(player1);
graphics2d.fill(player2);
}
// function which essentially re-creates rectangle with varying x
// orientations. (x-movement)
public void moveX(int mutationDistance, Rectangle sampleObject) {
sampleObject.setBounds(sampleObject.x + mutationDistance,
sampleObject.y, sampleObject.width, sampleObject.height);
}
// function which essentially re-creates rectangle with varying y
// orientations. (y-movement)
public void moveY(int mutationDistance, Rectangle sampleObject) {
sampleObject.setBounds(sampleObject.x, sampleObject.y
+ mutationDistance, sampleObject.width, sampleObject.height);
}
// listener
private class myKeyListener implements KeyListener {
// implement all the possible actions on keys
public void keyPressed(final KeyEvent keyEvent) {
if (keyEvent.getKeyCode() == KeyEvent.VK_ESCAPE) {
System.exit(0);
}
if (keyEvent.getKeyCode() == KeyEvent.VK_RIGHT) {
player1right = true;
}
if (keyEvent.getKeyCode() == KeyEvent.VK_LEFT) {
player1left = true;
}
if (keyEvent.getKeyCode() == KeyEvent.VK_UP) {
player1up = true;
}
if (keyEvent.getKeyCode() == KeyEvent.VK_DOWN) {
player1down = true;
}
if (keyEvent.getKeyCode() == KeyEvent.VK_D) {
player2right = true;
}
if (keyEvent.getKeyCode() == KeyEvent.VK_A) {
player2left = true;
}
if (keyEvent.getKeyCode() == KeyEvent.VK_W) {
player2up = true;
}
if (keyEvent.getKeyCode() == KeyEvent.VK_S) {
player2down = true;
}
}
public void keyReleased(KeyEvent keyEvent) {
if (keyEvent.getKeyCode() == KeyEvent.VK_RIGHT) {
player1right = false;
}
if (keyEvent.getKeyCode() == KeyEvent.VK_LEFT) {
player1left = false;
}
if (keyEvent.getKeyCode() == KeyEvent.VK_UP) {
player1up = false;
}
if (keyEvent.getKeyCode() == KeyEvent.VK_DOWN) {
player1down = false;
}
if (keyEvent.getKeyCode() == KeyEvent.VK_D) {
player2right = false;
}
if (keyEvent.getKeyCode() == KeyEvent.VK_A) {
player2left = false;
}
if (keyEvent.getKeyCode() == KeyEvent.VK_W) {
player2up = false;
}
if (keyEvent.getKeyCode() == KeyEvent.VK_S) {
player2down = false;
}
}
public void keyTyped(KeyEvent keyEvent) {
}
}
public static void pause(int secs) {
try {
Thread.sleep(secs);
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.JPanel;
import java.util.ArrayList;
/**
* This code was originally by the user "UniBrain" from the website:
* http://forum.codecall.net/topic/74377-moving-graphics-across-up-and-down-a-jpanel/?p=652384
*
* but has been modified by me for my own purposes
*/
public class Canvas2 extends JPanel
{
//attributes
private Rectangle player1;
private Rectangle player2;
private ArrayList<KeyEvent> log;
//constructor
public Canvas2()
{
//initialize object
player1 = new Rectangle (50, 50, 50, 50);
player2 = new Rectangle (50, 50, 50, 50);
log = new ArrayList<KeyEvent>();
//set canavs background colour
setBackground (Color.white);
//add the key listener in the constructor of your canavas/panel
addKeyListener(new myKeyListener());
//ensure focus is on this canavas/panel for key operations.
setFocusable(true);
}
//painting
public void paintComponent(Graphics graphics)
{
super.paintComponent(graphics);
Graphics2D graphics2d =(Graphics2D)graphics;
graphics.setColor(Color.blue);
graphics2d.fill(player1);
graphics2d.fill(player2);
}
//function which essentially re-creates rectangle with varying x orientations. (x-movement)
public void moveX(int mutationDistance, Rectangle sampleObject)
{
sampleObject.setBounds(sampleObject.x + mutationDistance, sampleObject.y, sampleObject.width, sampleObject.height);
}
//function which essentially re-creates rectangle with varying y orientations. (y-movement)
public void moveY(int mutationDistance, Rectangle sampleObject)
{
sampleObject.setBounds(sampleObject.x, sampleObject.y + mutationDistance, sampleObject.width, sampleObject.height);
}
public void move(){
/*
* This method of keeping track of key events and using a loop is taken from the user Michael Meyers at
* http://stackoverflow.com/questions/752999/how-do-i-handle-multiple-key-presses-in-java
*/
Thread thread1 = new Thread()
{
public void run() {
for(KeyEvent keyEvent: log){
if(keyEvent.getKeyCode() == KeyEvent.VK_RIGHT){moveX(5, player1);}
if(keyEvent.getKeyCode() == KeyEvent.VK_LEFT){moveX (-5, player1);}
if(keyEvent.getKeyCode() == KeyEvent.VK_UP){moveY (-5, player1);}
if(keyEvent.getKeyCode() == KeyEvent.VK_DOWN){moveY (5, player1);}
}
}
};
Thread thread2 = new Thread()
{
public void run() {
for(KeyEvent keyEvent: log){
if(keyEvent.getKeyCode() == KeyEvent.VK_D){moveX(5, player2);}
if(keyEvent.getKeyCode() == KeyEvent.VK_A){moveX(-5, player2);}
if(keyEvent.getKeyCode() == KeyEvent.VK_W){moveY(-5, player2);}
if(keyEvent.getKeyCode() == KeyEvent.VK_S){moveY(5, player2);}
}
}
};
//Starts the threads
thread1.start();
thread2.start();
//Waits for them to finish
try{
thread1.join();
thread2.join();
log = new ArrayList<KeyEvent>();
}
catch (Exception e){System.out.println(e);}
}
//listener
private class myKeyListener implements KeyListener
{
//implement all the possible actions on keys
public void keyPressed(KeyEvent keyEvent)
{
if(keyEvent.getKeyCode() == KeyEvent.VK_ESCAPE){System.exit (0);}
Thread thread3 = new Thread()
{
public void run() {
if(keyEvent.getKeyCode() == KeyEvent.VK_RIGHT){moveX(5, player1);}
if(keyEvent.getKeyCode() == KeyEvent.VK_LEFT){moveX(-5, player1);}
if(keyEvent.getKeyCode() == KeyEvent.VK_UP){moveY(-5, player1);}
if(keyEvent.getKeyCode() == KeyEvent.VK_DOWN){moveY (5, player1);}
}
};
Thread thread4 = new Thread()
{
public void run() {
if(keyEvent.getKeyCode() == KeyEvent.VK_D){moveX(5, player2);}
if(keyEvent.getKeyCode() == KeyEvent.VK_A){moveX(-5, player2);}
if(keyEvent.getKeyCode() == KeyEvent.VK_W){moveY(-5, player2);}
if(keyEvent.getKeyCode() == KeyEvent.VK_S){ moveY(5, player2);}
}
};
Thread thread5 = new Thread()
{
public void run() {
repaint();
}
};
thread3.start();
thread4.start();
thread5.start();
try{
thread3.join();
thread4.join();
thread5.join();
}
catch (Exception e){System.out.println(e);}
repaint();
}
public void keyReleased(KeyEvent keyEvent)
{
}
public void keyTyped(KeyEvent keyEvent)
{
}
}
public static void pause(int secs){
try{Thread.sleep(secs*0);} catch(Exception e){}
}
}
which is then executed by this class
import javax.swing.JFrame;
import java.awt.Dimension;
/**
* This code was taken from the user "UniBrain" from the website:
* http://forum.codecall.net/topic/74377-moving-graphics-across-up-and-down-a-jpanel/?p=652384
*/
public class Display
{
public static void main ( String [ ] arguments )
{
JFrame frame = new JFrame("key listener demo");
Canvas2 panel = new Canvas2();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.setContentPane(panel);
frame.setPreferredSize(new Dimension(800, 600));
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.pack();
}
}
Related
I will only supply the relevant parts of the code as it is quite large. Help me figure out why I never get keyboard events to the GameBoard JPanel. The game code runs just fine until I added the new feature where I wanted it to have a welcome screen.
Edit: If you would like to view the entire project (it's an eclipse project), here is the link to download it Eclipse project, I honestly can't format almost 1000 lines of code here on StackOverflow, that'll be painful even for you.
Main.java
private void initUI() throws IOException {
cardLayout = new CardLayout();
mainPanel = new JPanel(cardLayout);
welcomeMenu = new Welcome(cardLayout, mainPanel);
mainPanel.add(welcomeMenu, "welcome");
game = new GameBoard();
mainPanel.add(game, "game");
add(mainPanel);
setTitle("Pacman");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(380, 420);
setLocationRelativeTo(null);
}
This is the welcome/menu panel (some section of it).
Welcome.java (servers as a menu)
#Override
public void actionPerformed(ActionEvent evt) {
if (evt.getActionCommand() == Actions.EXIT.name()) {
System.exit(0);
} else if (evt.getActionCommand() == Actions.CONFIG.name()) {
JOptionPane.showMessageDialog(null, "Not yet implemented");
} else if (evt.getActionCommand() == Actions.PLAY.name()) {
// The part that switches to game, works as expected.
cl.show(mp, "game");
}
}
GameBoard class has a KeyAdapter class within it that is used to listen to key events, however, in the addition of the new feature using CardLayout, I could not get the key events to the panel so my PacMan is just stuck in one place opening and closing its mouth like a fish.
GameBoard.java
private void initBoard() {
addKeyListener(new TAdapter());
setFocusable(true);
setBackground(Color.black);
initGame();
}
...
class TAdapter extends KeyAdapter {
#Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (inGame) {
if (key == KeyEvent.VK_LEFT) {
req_dx = -1;
req_dy = 0;
} else if (key == KeyEvent.VK_RIGHT) {
req_dx = 1;
req_dy = 0;
} else if (key == KeyEvent.VK_UP) {
req_dx = 0;
req_dy = -1;
} else if (key == KeyEvent.VK_DOWN) {
req_dx = 0;
req_dy = 1;
} else if (key == KeyEvent.VK_ESCAPE && timer.isRunning()) {
inGame = false;
} else if (key == KeyEvent.VK_PAUSE) {
if (timer.isRunning()) {
timer.stop();
} else {
timer.start();
}
}
} /*
* else { if (key == 's' || key == 'S') { inGame = true; initGame(); } }
*/
}
#Override
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if (key == Event.LEFT || key == Event.RIGHT || key == Event.UP || key == Event.DOWN) {
req_dx = 0;
req_dy = 0;
}
}
}
The issue was using Key Listener when I should have been using Key Binding in the GameBoard JPanel. So instead of this,
class TAdapter extends KeyAdapter {
#Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (inGame) {
...
} else if (key == KeyEvent.VK_UP) {
req_dx = 0;
req_dy = -1;
} else if (key == KeyEvent.VK_DOWN) {
...
}
}
}
I replaced it with this approach (I will only demonstrate Arrow Up scenario),
private class UpAction extends AbstractAction {
private static final long serialVersionUID = 1L;
#Override
public void actionPerformed(ActionEvent e) {
if (!inGame)
return;
req_dx = 0;
req_dy = -1;
}
}
...
private void setupKeyListeners() {
getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "doUp");
...
getActionMap().put("doUp", new UpAction());
...
}
And later on,
private void initBoard() {
setupListeners();
setFocusable(true);
setBackground(Color.black);
initGame();
}
I'm still new to java and I can't get my JFrame to detect key presses/releases, Here are the classes used
Main:
package initilizer;
import java.awt.Canvas;
import java.awt.Graphics;
import javax.swing.JFrame;
import input.Keyboard;
public class Main extends Canvas{
static boolean rRunning = true;
static int width = 640;
static int height = 480;
int cx = width/2;
int cy = height/2;
boolean initilized = false;
Camera cam = new Camera(1.0, 5.0, 3.0);
Camera cam1 = new Camera(10.0, 50.0, 30.0);
long lastFpsCheck = System.currentTimeMillis();
public static JFrame frame = new JFrame("3D Engine");
Keyboard keyboard = new Keyboard();
public static void main(String[] args) {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Canvas canvas = new Main();
canvas.setSize(width, height);
frame.add(canvas);
frame.pack();
frame.setVisible(true);
}
void init() {
// Nothing currently
}
double[] rotate2D(double[] pos,double[] rot) {
double x = pos[0];
double y = pos[1];
double s = rot[0];
double c = rot[1];
double[] result = {(x * c) - (y * s), (y * c) + (x * s)};
return result;
}
public void paint(Graphics g) {
if (initilized == false) {
initilized = true;
init();
}
// Storing start time for FPS Counting
long startTime = System.currentTimeMillis();
g.dispose();
if (keyboard.getA() == true) {
System.out.println("A HELD!!");
}
// Limiting FPS
try {
Thread.sleep(16);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Calculating FPS
long endTime = System.currentTimeMillis();
if ((lastFpsCheck + 1000) < endTime) {
lastFpsCheck = endTime;
double delta_time = (endTime - startTime);
frame.setTitle("3D Engine - FPS: " + (int) (1000/delta_time));
}
// Draw next frame
repaint();
}
}
Keyboard:
package input;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import initilizer.Main;
public class Keyboard implements ActionListener, KeyListener{
public boolean W_HELD = false;
public boolean A_HELD = false;
public boolean S_HELD = false;
public boolean D_HELD = false;
public boolean E_HELD = false;
public boolean Q_HELD = false;
public Keyboard() {
Main.frame.addKeyListener(this);
}
public boolean getW() {
return W_HELD;
}
public boolean getA() {
//System.out.println(A_HELD);
return A_HELD;
}
public boolean getS() {
return S_HELD;
}
public boolean getD() {
return S_HELD;
}
public boolean getE() {
return E_HELD;
}
public boolean getQ() {
return Q_HELD;
}
public void keyPressed(KeyEvent e) {
System.out.println("HII");
int key = e.getKeyCode();
if (key == KeyEvent.VK_W) {
W_HELD = true;
}
if (key == KeyEvent.VK_A) {
A_HELD = true;
}
if (key == KeyEvent.VK_S) {
S_HELD = true;
}
if (key == KeyEvent.VK_D) {
D_HELD = true;
}
if (key == KeyEvent.VK_E) {
E_HELD = true;
}
if (key == KeyEvent.VK_Q) {
Q_HELD = true;
}
}
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_W) {
W_HELD = false;
}
if (key == KeyEvent.VK_A) {
A_HELD = false;
}
if (key == KeyEvent.VK_S) {
S_HELD = false;
}
if (key == KeyEvent.VK_D) {
D_HELD = false;
}
if (key == KeyEvent.VK_E) {
E_HELD = false;
}
if (key == KeyEvent.VK_Q) {
Q_HELD = false;
}
}
public void keyTyped(KeyEvent e) {
// Unmodified
}
#Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}
I've been stuck trying to fix this but couldn't, Tried adding key listeners through the main class but that also didn't work, If you know the problem then please tell me, Thank you.
You have to add a KeyListener to your Canvas to be able to receive input from it, e.g.
canvas.add(new Keyboard());
I am creating a Binding of Isaac-esque Roguelike that is generated using an Array List using four Booleans. The four Booleans being North, East, South, and West. Corresponding to which doors is open.
The problem with Array Lists is that it can 'overlap'. For example, if I go east, than south, than west, than north, I'll be in a whole new room, and not the original.
Is there a way to stop this from happening, or at least prevent it? Such as if the room above room 1 (room 2) has south set to true, than north will be true in room 1 to connect the two rooms. Or if room 2 has south set to false, than north will be false in room 1 to stop overlap.
Here is my current code (I know my code isn't exactly clean and has a lot of magic numbers, but those shouldn't be important for this question)-
GameState:
public class GameState extends JFrame implements KeyListener {
Container contentPane=this.getContentPane();
Graphics bufferGraphics;
int characterX=463;
int characterY=486;
int oldCharacterX=463;
int oldCharacterY=486;
int xAxis;
int yAxis;
int minimapX=1300;
int minimapY=515;
Image characterNorth = CustomImages.createImageIcon("Images/characterNorth.jpg").getImage();
Image characterEast = CustomImages.createImageIcon("Images/characterEast.jpg").getImage();
Image characterSouth = CustomImages.createImageIcon("Images/characterSouth.jpg").getImage();
Image characterWest = CustomImages.createImageIcon("Images/characterWest.jpg").getImage();
Image brickWall = CustomImages.createImageIcon("Images/brickWall.jpg").getImage();
Image brickFloor = CustomImages.createImageIcon("Images/brickFloor.jpg").getImage();
Image character=characterNorth;
boolean pressed=false;
boolean minimap=true;
ArrayList<RoomState> map = new ArrayList<RoomState>();
RoomState currentRoom = new RoomState();
RoomState currentRoomState=new RoomState();
GameState() {
this.setBounds(0, 0, 1680, 1050);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addKeyListener(this);
setFocusable(true);
requestFocusInWindow();
}
public void move(int x, int y) { //Check Move
currentRoomState=currentRoomState.MoveToNextRoom(true, false, false, false);
currentRoomState=currentRoomState.MoveToNextRoom(false, true, false, false);
currentRoomState=currentRoomState.MoveToNextRoom(false, false, true, false);
currentRoomState=currentRoomState.MoveToNextRoom(false, false, false, true);
}
public void paint(Graphics g) { //Graphics
for(xAxis=58;xAxis<=858;xAxis=xAxis+50) {
for(yAxis=81;yAxis<=881;yAxis=yAxis+50) {
g.drawImage(brickFloor,xAxis,yAxis,null);
}
yAxis=31;
}
for(xAxis=8;xAxis<958;xAxis=xAxis+50) {
g.drawImage(brickWall,xAxis,yAxis,null);
}
yAxis=931;
for(xAxis=8;xAxis<=908;xAxis=xAxis+50) {
g.drawImage(brickWall,xAxis,yAxis,null);
}
xAxis=8;
for(yAxis=81;yAxis<=881;yAxis=yAxis+50) {
g.drawImage(brickWall,xAxis,yAxis,null);
}
xAxis=908;
for(yAxis=81;yAxis<=881;yAxis=yAxis+50) {
g.drawImage(brickWall,xAxis,yAxis,null);
}
if(currentRoom.northDoor) {
g.drawImage(brickFloor,458,31,null);
}
if(currentRoom.eastDoor) {
g.drawImage(brickFloor,908,481,null);
}
if(currentRoom.southDoor) {
g.drawImage(brickFloor,458,931,null);
}
if(currentRoom.westDoor) {
g.drawImage(brickFloor,8,481,null);
}
g.drawImage(character,characterX,characterY,null);
g.drawString("[ ]",minimapX,minimapY);
g.setColor(Color.RED);
g.drawString("[ ]", 1300, 515);
}
#Override
public void keyPressed(KeyEvent arg0) { //Character Rotation/Movement.
if(pressed==false) {
pressed=true;
oldCharacterX=characterX;
oldCharacterY=characterY;
if(arg0.getKeyCode() == KeyEvent.VK_W || arg0.getKeyCode() == KeyEvent.VK_UP) {
if(character==characterNorth) {
if(characterY>86 && characterX>13 && characterX<913) {
characterY=characterY-50;
}else if(currentRoom.northDoor && characterX==463) {
oldCharacterY=characterY;
characterY=characterY-50;
if(characterY==-14) {
if(currentRoom.rs_NorthDoor != null) {
currentRoom=currentRoom.rs_NorthDoor;
}else {
RoomState nextRoom = new RoomState(currentRoom,false, false, true, false);
currentRoom.rs_NorthDoor = nextRoom;
map.add(nextRoom);
currentRoom = nextRoom;
nextRoom = null;
}
minimapY=minimapY-10;
characterX=463;
characterY=936;
repaint();
}
}
}else {
character=characterNorth;
}
}
if(arg0.getKeyCode() == KeyEvent.VK_A || arg0.getKeyCode() == KeyEvent.VK_LEFT) {
if(character==characterWest && characterY>36 && characterY<926) {
if(characterX>63) {
oldCharacterX=characterX;
characterX=characterX-50;
}else if(currentRoom.westDoor && characterY==486) {
oldCharacterX=characterX;
characterX=characterX-50;
if(characterX==-37) {
if(currentRoom.rs_WestDoor != null) {
currentRoom = currentRoom.rs_WestDoor;
}else {
RoomState nextRoom = new RoomState(currentRoom,false, true, false, false);
currentRoom.rs_WestDoor = nextRoom;
map.add(nextRoom);
currentRoom = nextRoom;
nextRoom = null;
}
minimapX=minimapX-8;
characterX=913;
characterY=486;
repaint();
}
}
}else {
character=characterWest;
}
}
if(arg0.getKeyCode() == KeyEvent.VK_S || arg0.getKeyCode() == KeyEvent.VK_DOWN) {
if(character==characterSouth) {
if(characterY<871 && characterX>13 && characterX<913) {
oldCharacterY=characterY;
characterY=characterY+50;
}else if(currentRoom.southDoor && characterX==463) {
oldCharacterY=characterY;
characterY=characterY+50;
if(characterY==986) {
if(currentRoom.rs_SouthDoor != null) {
currentRoom=currentRoom.rs_SouthDoor;
}else {
RoomState nextRoom = new RoomState(currentRoom,true, false, false, false);
currentRoom.rs_SouthDoor = nextRoom;
map.add(nextRoom);
currentRoom = nextRoom;
nextRoom = null;
}
minimapY=minimapY+10;
characterX=463;
characterY=36;
repaint();
}
}
}else {
character=characterSouth;
}
}
if(arg0.getKeyCode() == KeyEvent.VK_D || arg0.getKeyCode() == KeyEvent.VK_RIGHT) {
if(character==characterEast && characterY>36 && characterY<926) {
if(characterX<848) {
oldCharacterX=characterX;
characterX=characterX+50;
}else if(currentRoom.eastDoor && characterY==486) {
oldCharacterX=characterX;
characterX=characterX+50;
if(characterX==963) {
if(currentRoom.rs_EastDoor != null) {
currentRoom = currentRoom.rs_EastDoor;
}else {
RoomState nextRoom = new RoomState(currentRoom,false, false, false, true);
currentRoom.rs_EastDoor = nextRoom;
map.add(nextRoom);
currentRoom = nextRoom;
nextRoom = null;
}
minimapX=minimapX+8;
characterX=13;
characterY=486;
repaint();
}
}
}else {
character=characterEast;
}
}
if(oldCharacterX != characterX || oldCharacterY != characterY) {
repaint(oldCharacterX,oldCharacterY,40,40);
}
repaint(characterX,characterY,40,40);
}
}
#Override
public void keyReleased(KeyEvent arg0) { //Prevents Holding Down Keys.
if(arg0.getKeyCode() == KeyEvent.VK_W || arg0.getKeyCode() == KeyEvent.VK_UP) {
pressed=false;
}
if(arg0.getKeyCode() == KeyEvent.VK_A || arg0.getKeyCode() == KeyEvent.VK_LEFT) {
pressed=false;
}
if(arg0.getKeyCode() == KeyEvent.VK_S || arg0.getKeyCode() == KeyEvent.VK_DOWN) {
pressed=false;
}
if(arg0.getKeyCode() == KeyEvent.VK_D || arg0.getKeyCode() == KeyEvent.VK_RIGHT) {
pressed=false;
}
}
#Override
public void keyTyped(KeyEvent arg0) {
}
}
RoomState:
public class RoomState {
boolean northDoor;
boolean eastDoor;
boolean southDoor;
boolean westDoor;
boolean doorsOpen;
int northRoll;
int eastRoll;
int southRoll;
int westRoll;
Random r=new Random();
//Reference to the adjacent rooms
RoomState rs_NorthDoor=null;
RoomState rs_EastDoor=null;
RoomState rs_SouthDoor=null;
RoomState rs_WestDoor=null;
RoomState() { //Initial
while(!doorsOpen) {
northRoll=r.nextInt(6)+1;
eastRoll=r.nextInt(6)+1;
southRoll=r.nextInt(6)+1;
westRoll=r.nextInt(6)+1;
if(northRoll==1) {
northDoor=true;
}else {
northDoor=false;
}
if(eastRoll==1) {
eastDoor=true;
}else {
eastDoor=false;
}
if(southRoll==1) {
southDoor=true;
}else {
southDoor=false;
}
if(westRoll==1) {
westDoor=true;
}else {
westDoor=false;
}
if(northDoor==false && eastDoor==false && southDoor==false && westDoor==false) {
doorsOpen=false;
}else {
doorsOpen=true;
}
}
}
RoomState(RoomState previousState, boolean north, boolean east, boolean south, boolean west) {
this();
if(north) {
rs_NorthDoor=previousState;
northDoor=true;
}else if(east) {
rs_EastDoor=previousState;
eastDoor=true;
}else if(south) {
rs_SouthDoor=previousState;
southDoor=true;
}else if(west) {
rs_WestDoor=previousState;
westDoor=true;
}
}
public RoomState MoveToNextRoom(boolean north, boolean east, boolean south, boolean west) {
if(north) {
if(rs_NorthDoor==null) {
rs_NorthDoor=new RoomState(this,north,east,south,west);
}
return rs_NorthDoor;
}
if(east) {
if(rs_EastDoor==null) {
rs_EastDoor=new RoomState(this,north,east,south,west);
}
return rs_EastDoor;
}
if(south) {
if(rs_SouthDoor==null) {
rs_SouthDoor=new RoomState(this,north,east,south,west);
}
return rs_SouthDoor;
}
if(west) {
if(rs_WestDoor==null) {
rs_WestDoor=new RoomState(this,north,east,south,west);
}
return rs_WestDoor;
}
return null;
}
}
I just now trying to make a game where there are few borders on the map, which is being generated from a text document. The text document has 1s and 0s where ther is 1 it shows a wall. So how do I make it so the character stops infront of the wall
My Code:
MAIN CLASS:
public class JavaGame {
public static void main(String[] args) {
final Platno p = new Platno();
final JFrame okno = new JFrame("Test");
Mapa map = new Mapa();
okno.setResizable(false);
okno.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
okno.setSize(800, 600);
okno.setVisible(true);
map.nacti();
okno.add(p);
p.mapa = map;
okno.addKeyListener(new KeyListener() {
#Override
public void keyTyped(KeyEvent e) {
}
#Override
public void keyPressed(KeyEvent e) {
int kod = e.getKeyCode();
if(kod == KeyEvent.VK_LEFT)
{
Platno.x -= 3;
p.repaint();
}
else if(kod == KeyEvent.VK_RIGHT)
{
Platno.x +=3;
p.repaint();
}
else if(kod == KeyEvent.VK_UP)
{
Platno.y -=3;
p.repaint();
}
else if(kod == KeyEvent.VK_DOWN)
{
Platno.y +=3;
p.repaint();
}
}
#Override
public void keyReleased(KeyEvent e) {
}
});
/* Timer = new Timer(1, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e)
{
Platno.y -=3;
p.repaint();
}
}); */
}`
Map loader class:
public void nacti()
{
try (BufferedReader br = new BufferedReader(new FileReader("map1-1.txt")))
{
String radek;
int cisloRadku = 0;
while((radek = br.readLine()) != null)
{
for(int i = 0; i < radek.length(); i++)
{
char znak = radek.charAt(i);
int hodnota = Integer.parseInt(String.valueOf(znak));
pole[i][cisloRadku] = hodnota;
}
cisloRadku++;
}
}
catch (Exception ex)
{
JOptionPane.showMessageDialog(null, "Error: "+ex.getMessage());
}
}
public void vykresli(Graphics g)
{
try {
wall = ImageIO.read(ClassLoader.getSystemResource("Images/wall.gif"));
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, "Error: "+ex.getMessage());
}
for(int i = 0; i < pole[0].length; i++)
{
for(int j = 0; j < pole.length; j++)
{
if(pole[j][i] == 1)
{
g.setColor(Color.RED);
// g.fillRect(j*40, i*40, 40, 40);
g.drawImage(wall, j*40, i*40, null);
}
}
}
}`
and the Hero class:
public class Hero {
public int poziceX;
public int poziceY;
public static boolean upB = false;
public static boolean downB = false;
public static boolean rightB = false;
public static boolean leftB = false;
BufferedImage up;
BufferedImage down;
BufferedImage right;
BufferedImage left;
public Hero()
{
try {
up = ImageIO.read(ClassLoader.getSystemResource("Images/Hero_Up.png"));
down = ImageIO.read(ClassLoader.getSystemResource("Images/Hero_Down.png"));
right = ImageIO.read(ClassLoader.getSystemResource("Images/Hero_Right.png"));
left = ImageIO.read(ClassLoader.getSystemResource("Images/Hero_Left.png"));
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, "Error: "+ex.getMessage());
}
}
public void vykreslit(Graphics g)
{
if(upB == true)
{
g.drawImage(up, poziceX, poziceX, null);
}
else if(downB == true)
{
g.drawImage(down, poziceX, poziceX, null);
}
else if(leftB == true)
{
g.drawImage(left, poziceX, poziceX, null);
}
else if(rightB == true)
{
g.drawImage(right, poziceX, poziceX, null);
}
}`
Thanks for your help :)
You could calculate the 'future position' for a move, and test for collisions.
If a collision occur, dont'move, otherwise you're ok to move.
See if this logic can help you:
public boolean willCollide(int row, int col, Board map)
{
return map[row][col] == 1;
}
public void moveLeft(Hero hero, Board map)
{
//watch for index out of bounds!
int futureCol = hero.y - 1;
if (! willCollide(hero.row, futureCol)
hero.y = futureCol;
}
I want to be able register a key pressed event that triggers a boolean variable. I've done it in my main class, but now I am trying to put it into classes, and it doesn't seem to be working.
Here is the hero class:
public class Hero extends Main {
private boolean downPressed;
private boolean leftPressed;
private boolean rightPressed;
public void init() {
}
public void paint(Graphics g, int x_pos, int y_pos) {
if (isDownPressed() && isLeftPressed()) {
this.sprite = hero225;
} else if (isDownPressed() && isRightPressed()) {
this.sprite = hero135;
} else if (isUpPressed() && isRightPressed()) {
this.sprite = hero45;
} else if (isUpPressed() && isLeftPressed()) {
this.sprite = hero315;
} else if (isLeftPressed() == true) {
this.sprite = hero270;
} else if (isRightPressed() == true) {
this.sprite = hero90;
} else if (isUpPressed() == true) {
this.sprite = hero;
System.out.println("Succsess");
} else if (isDownPressed() == true) {
this.sprite = hero180;
}
// this.sprite will contain value set on last "movement"
g.drawImage(this.sprite, x_pos, y_pos, this);
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
setLeftPressed(true);
System.out.println("keyPressed");
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
setRightPressed(true);
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
setUpPressed(true);
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
setDownPressed(true);
}
}
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
setLeftPressed(false);
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
setRightPressed(false);
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
setUpPressed(false);
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
setDownPressed(false);
}
}
public void keyTyped(KeyEvent e) {
}
public void mouseClicked(MouseEvent e) {
System.out.println("HIT!");
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent e) {
boolean isButtonPressed = true;
}
public void mouseReleased(MouseEvent e) {
boolean isButtonPressed = false;
}
public void setDownPressed(boolean downPressed) {
this.downPressed = downPressed;
}
public boolean isDownPressed() {
return downPressed;
}
public void setLeftPressed(boolean leftPressed) {
this.leftPressed = leftPressed;
}
public boolean isLeftPressed() {
return leftPressed;
}
public void setRightPressed(boolean rightPressed) {
this.rightPressed = rightPressed;
}
public boolean isRightPressed() {
return rightPressed;
}
public void setUpPressed(boolean upPressed) {
this.upPressed = upPressed;
}
public boolean isUpPressed() {
return upPressed;
}
And here is the level class which calls it:
public class Level extends Main {
Hero hero = new Hero();
public void paint(KeyEvent e, Graphics g, double x_pos, double x_pos2) {
repaint();
}
And here is the Paint function in the Main class which calls that:
public void paint(Graphics g) {
Level level = new Level();
level.paint(e, g, x_pos, y_pos);
The problem causing this doesn't seem to be apparent.
You should implement java.awt.event.KeyListener interface in the class(es) that you want to listen for events, in this case Hero, so:
public class Hero extends Main implements java.awt.event.KeyListener
and then register for the events in some method, maybe init or somewhere else using:
addKeyListener(this);
//rest of your code
Or if you want to listen for just some events, you could instead of implement the interface KeyListener, register an adapter:
addKeyListener(new java.awt.KeyAdapter()
{
public void keyPressed(java.awt.KeyEvent e)
{
//handle just this event
}
}
);
//rest of of your code
Sounds like a focus problem: Only the active component will get KeyEvents.
There are several ways to solve this problem, but I found this tutorial covers the ideas quite nicely. But if you want a quick and dirty (dirty? Not sure, easy and simple isn't bad I'd think; just gets a bit bloated for large projects) solution you could just implement a listener for every component and forward the events to some general class that handles it.