Skip to main content

can java registered two arrow keys at the same time

1 reply [Last post]
rajababar
Offline
Joined: 2010-06-22
Points: 0

hi my name is babar
im making a 2d car game the problem im facing is that i cannot accelerate and rotate my car at the same time by pressing both arrow keys up and right/left
i have tried alot to do it but aint
i just wanted that is that possible in java

another problem is that i am doing pixel collision detection for my game but the problem is very astonishing
some time the car gets stop by the wall but some it can go through the same wall??
if any one can help me i will be very thankful

here is the code
{CODE}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
import java.applet.*;
import java.net.URL;

public class RaceGame extends JComponent
{

JFrame frame;

static int x=560;
static int y=410;

static int currentAngle=0;
static double hspeed,vspeed;
static double carspeed=0.1;

double hangle,vangle;

Image car;
Image finish;

Image getImage(String filename)
{
URL url = getClass().getResource(filename);

ImageIcon icon = new ImageIcon(url);
return icon.getImage();
}

public RaceGame()
{

car=getImage("car1.jpeg");
//finish=getImage("finish.jpg");

}

public void CreateBase()
{

frame=new JFrame("Dare 2 Race");
frame.setBounds(70,30,650,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Container c=frame.getContentPane();
c.add(new RaceGame());

frame.addKeyListener(new adapter());
c.setBackground(Color.BLACK);

GameMain gm=new GameMain();
gm.MainBase();

}

public void rotate()
{
currentAngle+=1;

if(currentAngle>=360)
{
currentAngle=0;
}
repaint();
}

public void paint(Graphics g)
{
super.paint(g);

Graphics2D g2d=(Graphics2D)g;

AffineTransform origXform=g2d.getTransform();
AffineTransform newXform=(AffineTransform)(origXform.clone());

newXform.rotate(Math.toRadians(currentAngle),x,y);

g2d.setTransform(newXform);

//g2d.drawImage(finish,12,10,null);

if( (x<=640 && x>=420) && y==380 || (x<=640 && x>=320) && y==460 || x==420 && (y<=380 && y>=300) || x==320 && (y<=460 && y>=380) || (x<=420 && x>=125) && y==300 || (x<=320 && x>=230) && y==380 || x==230 && (y>=380 && y<=460) || (x<=230 && x>=2) && y==460 || x==125 && (y>=300 && y<=370) || (x<=125 && x>=105) && y==370 || x==2 && (y<=460 && y>=180) || x==105 && (y<=370 && y>=300) || (x>=2 && x<=450) && y==180 || x==105 && (y<=300 && y>=250) || (x>=105 && x<=550) && y==250 || x==550 && (y<=250 && y>=20) || (x<=550 && x>=275) && y==20 || x==450 && (y<=180 && y>=100) || (x<=450 && x>=360) && y==100 || x==360 && (y>=100 && y<=160) || (x<=360 && x>=10) && y==160 || (x<=360 && x>=10) && y==160 || x==10 && (y<=160 && y>=30) || x==275 && (y>=20 && y<=90) || (x<=275 && x>=110) && y==90 || x==110 && (y<=90 && y>=30) )
{ //here im doing collision detection,if cars hit road it just send it back to starting point

x=560;
y=410;
carspeed=0.1;
repaint();
}

else
{
g2d.drawImage(car,x,y,this);
}

g2d.setTransform(origXform);

if(x<=95 && x>=15 && y<=27 && y>=5)
{

g.setColor(Color.WHITE);
g.setFont(new Font("ArialBlack",Font.BOLD,25));
g.drawString("YOU WON!!",240,240);

}

g.setColor(Color.WHITE);
g.drawLine(640,380,420,380);
g.drawLine(640,460,320,460);
g.drawLine(420,380,420,300);
g.drawLine(320,460,320,380);
g.drawLine(420,300,125,300);
g.drawLine(320,380,230,380);
g.drawLine(230,380,230,460);
g.drawLine(230,460,2,460);
g.drawLine(125,300,125,370);
g.drawLine(125,370,105,370);
g.drawLine(2,460,2,180);
g.drawLine(105,370,105,300);
g.drawLine(2,180,450,180);
g.drawLine(105,300,105,250);
g.drawLine(105,250,550,250);
g.drawLine(550,250,550,20);
g.drawLine(550,20,275,20);
g.drawLine(450,180,450,100);
g.drawLine(450,100,360,100);
g.drawLine(360,100,360,160);
g.drawLine(360,160,10,160);
g.drawLine(10,160,10,30);
g.drawLine(275,20,275,90);
g.drawLine(275,90,110,90);
g.drawLine(110,90,110,30);

g.fillRect(15,5,10,10);
g.fillRect(35,5,10,10);
g.fillRect(55,5,10,10);
g.fillRect(75,5,10,10);
g.fillRect(95,5,10,10);

g.fillRect(25,16,10,10);
g.fillRect(45,16,10,10);
g.fillRect(65,16,10,10);
g.fillRect(85,16,10,10);

g.fillRect(15,27,10,10);
g.fillRect(35,27,10,10);
g.fillRect(55,27,10,10);
g.fillRect(75,27,10,10);
g.fillRect(95,27,10,10);

repaint();
}

class adapter extends KeyAdapter
{

public void keyPressed(KeyEvent e)
{

/*int key;
key=e.getKeyCode();

if(key==KeyEvent.VK_UP || key==KeyEvent.VK_RIGHT)
{

currentAngle++;
carspeed+=0.1;

hangle=Math.toRadians(currentAngle);
vangle=Math.toRadians(currentAngle);

hspeed=(carspeed)*Math.cos(hangle);
vspeed=(carspeed)*Math.sin(vangle);

x = x - (int) hspeed;
y = y - (int) vspeed;

repaint();

}*/

switch(e.getKeyCode())
{
case KeyEvent.VK_LEFT:

currentAngle--;
repaint();
break;

case KeyEvent.VK_RIGHT:

currentAngle++;
repaint();
break;

case KeyEvent.VK_UP:

carspeed+=0.1;
if(carspeed>=5.0)
{

carspeed-=0.1;

}

hangle=Math.toRadians(currentAngle);
vangle=Math.toRadians(currentAngle);

hspeed=(carspeed)*Math.cos(hangle);
vspeed=(carspeed)*Math.sin(vangle);

x = x - (int) hspeed;
y = y - (int) vspeed;

repaint();
break;

case KeyEvent.VK_DOWN:

carspeed-=0.1;
if(carspeed<=5.0)
{

carspeed+=0.1;

}
hangle=Math.toRadians(currentAngle);
vangle=Math.toRadians(currentAngle);

hspeed=(carspeed)*Math.cos(hangle);
vspeed=(carspeed)*Math.sin(vangle);

x = x + (int)hspeed;
y = y + (int)vspeed;
repaint();
break;
}

}

}

public static void main(String[]args)
{

RaceGame race=new RaceGame();
race.CreateBase();

//race.setDoubleBuffered(true);
}

class GameMain extends JComponent implements ActionListener
{
JFrame frame2;
JOptionPane dialog1,dialog2;
JButton button;

JMenuItem m1;
JMenuItem m2;
JMenuItem m3;

JPanel panel;
ImageIcon Back;

//Image background=getImage("background.jpeg");

public void MainBase()
{

frame2=new JFrame("Dare 2 Race");
frame2.setBounds(70,30,650,500);
frame2.setVisible(true);
frame2.setDefaultCloseOperation(frame2.EXIT_ON_CLOSE);

Container c2=frame2.getContentPane();
c2.setLayout(null);
c2.add(new GameMain());

button=new JButton("Play");
button.setBounds(280,300,80,40);
button.addActionListener(this);

panel=new JPanel();
panel.setBounds(0,0,650,500);
c2.add(panel);
panel.add(button);
Back = new ImageIcon("Sunset.jpg");
JLabel background = new JLabel(Back);
background.setBounds(0, 0, Back.getIconWidth(), Back.getIconHeight());
frame2.getLayeredPane().add(background, new Integer(Integer.MIN_VALUE));
panel.setOpaque(false);
frame2.setContentPane(panel);

JMenuBar menu=new JMenuBar();
JMenu game=new JMenu("Game");
JMenu htp=new JMenu("Instruction");
JMenu abtus=new JMenu("Help");

menu.add(game);
menu.add(htp);
menu.add(abtus);

m1=new JMenuItem("Exit");
m1.addActionListener(this);

m2=new JMenuItem("How To Play");
m2.addActionListener(this);

m3=new JMenuItem("About Us");
m3.addActionListener(this);

game.add(m1);
htp.add(m2);
abtus.add(m3);

frame2.setJMenuBar(menu);

}

public void actionPerformed(ActionEvent e)
{

if(e.getSource()==button)
{
frame2.setVisible(false);
frame.setVisible(true);
}

else

if(e.getSource()==m1)
{
System.exit(0);
}

else

if(e.getSource()==m2)
{
dialog1.showMessageDialog(null,"1. Drive the car using Up,Down,Left,Right arrow keys\n"+"\nUp-for acceleration\n"+"Down-for Brake/Reverse\n"+"Right-for steer right\n"+"Left-for steer left\n"+"\n 2. Avoid hitting road\n"+"\n 3. Try to reach finish line quickly to WIN!!","How To Play",JOptionPane.INFORMATION_MESSAGE);
}
else

if(e.getSource()==m3)
{
dialog2.showMessageDialog(null,"***Developed by Babar,Ehsan & Sidra***\n\n"+" Arid-UIIT\n"+"\n\n Copyrights: All rights reserved","About Us",JOptionPane.INFORMATION_MESSAGE);

}

}
/*
public void paint(Graphics g)
{
super.paint(g);

//Graphics2D g2d=(Graphics2D)g;

g.drawImage(background,12,10,this);

g.setColor(Color.red);
g.drawLine(300,100,100,300);
g.fillRect(100,100,200,100);

repaint();
}
*/

}

}
{/CODE}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
zaczek
Offline
Joined: 2006-07-04
Points: 0

Try this code. It is specifically written for the up/down/left/right keys and it may help.

One weird thing is depending on which keyboard you have, you might not be able to have multiple keys register together. In my case, the arrow keys are goofy because I can hold left/right/down but not up...or I can hold up/down/right but not left. But, if I use the arrow keys in the number pad then I can hold down all four and it works....there are other keys on the keyboard that have this limitation as well and I noticed it in video games, but I won't bother explaining.

[code]
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;

public class MultipleKeys extends JPanel {

private JLabel inputs = new JLabel();

public MultipleKeys() {
setLayout(new BorderLayout());
add(inputs, BorderLayout.CENTER);
setPreferredSize(new Dimension(200, 30));
setFocusable(true);
addKeyListener(new KeyHandler());
}

class KeyHandler extends KeyAdapter {
private boolean up = false;
private boolean down = false;
private boolean left = false;
private boolean right = false;
private boolean ctrldown = false;

public KeyHandler() { }

@Override
public void keyTyped(KeyEvent e) { }

@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();

switch (keyCode) {
case KeyEvent.VK_CONTROL:
ctrldown = true;
break;
case KeyEvent.VK_UP:
case KeyEvent.VK_DOWN:
case KeyEvent.VK_LEFT:
case KeyEvent.VK_RIGHT:
if (keyCode == KeyEvent.VK_UP) {
up = true;
}
if (keyCode == KeyEvent.VK_DOWN) {
down = true;
}
if (keyCode == KeyEvent.VK_LEFT) {
left = true;
}
if (keyCode == KeyEvent.VK_RIGHT) {
right = true;
}
e.consume();
update();
break;
default:
}
}

@Override
public void keyReleased(KeyEvent e) {
int keyCode = e.getKeyCode();
switch (keyCode) {
case KeyEvent.VK_CONTROL:
ctrldown = false;
break;

case KeyEvent.VK_UP:
case KeyEvent.VK_DOWN:
case KeyEvent.VK_LEFT:
case KeyEvent.VK_RIGHT:
if (keyCode == KeyEvent.VK_UP) {
up = false;
} else if (keyCode == KeyEvent.VK_DOWN) {
down = false;
} else if (keyCode == KeyEvent.VK_LEFT) {
left = false;
} else if (keyCode == KeyEvent.VK_RIGHT) {
right = false;
}
e.consume();
update();
break;
default:
}
}

private void update() {
StringBuilder str = new StringBuilder();

if (up) {
if (ctrldown) {
str.append(" [ctrl-up]");
} else {
str.append(" [up]");
}
}

if (down) {
if (ctrldown) {
str.append(" [ctrl-down]");
} else {
str.append(" [down]");
}
}

if (left) {
if (ctrldown) {
str.append(" [ctrl-left]");
} else {
str.append(" [left]");
}
}

if (right) {
if (ctrldown) {
str.append(" [ctrl-right]");
} else {
str.append(" [right]");
}
}

inputs.setText(str.toString());
}
}

public static void main(String[] args) {
JFrame frame = new JFrame("Multiple Keys");
frame.getContentPane().add(new MultipleKeys());
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
[/code]