Multiple keys pressed at once!

Yes hi

I am currently taking a AP programming class and my group of friends and i are trying to make a Tank wars game. We are having a problem with making it so two players can move thier tank at the same time. Here is our code so you can see what we have.

/*
Trivial applet that displays a string
*/

import java.awt.;
import java.applet.Applet;
import javax.swing.
;
import java.awt.event.;
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.
;
import java.awt.event.;
import javax.swing.
;

public class TrivialApplet extends Applet
{
Image image;
Image image2;
Image image3;
Image image4;
Image image5;
Image display;
int x_pos = 10;
int y_pos = 10;
private Image dbImage;
private Graphics dbg;

 public void init()
 {
      image = getImage(getDocumentBase(), "Tank1.2LEFT.GIF");
      image2 = getImage(getDocumentBase(), "Tank1.2UP.GIF");
      image3 = getImage(getDocumentBase(), "Tank1.2DOWN.GIF");
      image4 = getImage(getDocumentBase(), "Tank1.2RIGHT.GIF");
      image5 = getImage(getDocumentBase(), "MAP-1 copy.jpg");
 }

  public void update (Graphics g)  
  {      
        if (dbImage == null)
        {
              dbImage = createImage (this.getSize().width, this.getSize().height);
              dbg = dbImage.getGraphics();            
        }

        dbg.setColor (getBackground());       
        dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);

        dbg.setColor (getForeground());
        paint (dbg);

        g.drawImage (dbImage, 0, 0, this);
  } 

   public boolean keyDown(Event e, int key) {
   
      if (key == Event.LEFT){
              x_pos = x_pos -3;
              display = image;
        }
        else if (key == Event.RIGHT){
              x_pos = x_pos +3;
              display = image4;
        }
        else if (key == Event.UP){
              y_pos = y_pos -3;
              display = image2;
        }
        else if (key == Event.DOWN){
              y_pos = y_pos +3;
              display = image3;
        }
        repaint();
        return true;
   
   }
   
    public void paint(Graphics g)
 {
      g.drawImage(image5, 0, 0, this);
      repaint();
      return true;
      g.drawImage(display, x_pos, y_pos, this);
 }

}

Now what we want to acomplish is to make it so player one with the keys Up, down, left, and right can be pushing thier keys at the same time as player two who has the keys w, s, a, and d without overiding the other players commands. We have an idea that we are going to have to multi thread but don’t know how to.

Also we want to have music playing in the background and sounds can you tell us how to do those as well.

on another note we are working on making it so the tanks can fire but when the tanks fires a “Bullet” it will just pass over the other tank. how do we make it reconize that it touched the other “Tank”.

If you could help us that would be very helpfull.

Please send your hints or answers to smeeging@hotmail.com as well as post them.

again this would be very helpfull.

Smeghead out!

Sorry this code sample is for one tank. if you want our code for both tanks just ask and i’ll post it here.

whenever a key is pressed, trigger the switch of a boolean. Don’t actually act on it until you run all of the logic of your game.

Drop me a line at -

antamiga gmail com

I wouldn’t mind answering more questions for you. You can also send whatever code you have there.

If you’re lucky, you’ll keep Mal interested in this thread, since he is really in the know about all things java. :slight_smile:

Dr. A>

[quote]whenever a key is pressed, trigger the switch of a boolean. Don’t actually act on it until you run all of the logic of your game.
[/quote]
You mean like have it switch threads at such a fast rate that the human eye cannot notice that it is switching between the threads. I understand what that does but not reall sure how to accomplish that. No one that we seem to know, knows how to Multithread. through our reacsurch online we kinda figured out that we mostlikly need to use the try command and sleep command. Is this true?

thanks for your help
(Smeghead)

just implement KeyListener and add it to whatever content pane you’re using to hold your game. Java will call the implemented methods for you. It’ll be called just like your keyDown() method. I’m just telling you to use it differently. When that method is called, trigger a boolean. Say, when the up arrow is pressed, set a boolean “upPressed” to true. Then in your run loop, say, “if (upPressed) y_pos = y_pos -3;”. What you’re doing now IS called in a separate Thread. This is a problem because now a character can move as fast as he can push the button, with no regard to framerate or speed caps or anything. Key events that you handle should ONLY flip booleans in a game like this.

Something along these lines (salvaged from forums.java.sun.com)


import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
public class Game extends Frame implements Runnable, KeyListener
{
   static final int FPS = 60;
   
   static final int SCREEN_WIDTH = 640, SCREEN_HEIGHT = 480, BIT_DEPTH = 16, REFRESH_RATE = 60;
   
   public Game(boolean fullscreen)
   {
      GraphicsConfiguration gc = getGraphicsConfiguration();
      GraphicsDevice gd = gc.getDevice();
      
      if(fullscreen)
      {
         setUndecorated(true);
         gd.setFullScreenWindow(this);
         gd.setDisplayMode(new DisplayMode(SCREEN_WIDTH,SCREEN_HEIGHT,BIT_DEPTH,REFRESH_RATE));
      }
      else
      {         
         setBounds(0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
         setVisible(true);
      }
      addKeyListener(this);
      Thread t = new Thread(this);
      t.start();
   }
   
   public void run()
   {
      final int NANO_FRAME_LENGTH = 1000000000/FPS;
      
      long startTime = System.nanoTime();
      int frameCount = 0;
      while(controls[KeyEvent.VK_ESCAPE]==0)
      {
         BufferStrategy bs = getBufferStrategy();
         Graphics g = bs.getDrawGraphics();
         
         //misc rendering
         g.setColor(new Color(frameCount&0xFFFFFF));
         g.fillRect(0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
         
         //misc rendering;
         
         
         bs.show();
         frameCount++;
         while((System.nanoTime()-startTime)/NANO_FRAME_LENGTH <frameCount)
         {
            Thread.yield();
         }
         
      }
      dispose();
   }
   
   
   int [] controls = new int[256];
   public void keyPressed(KeyEvent ke)
   {
      controls[ke.getKeyCode()] = 1; //key handlers should ONLY set flags. The interpretation of the flags is best left to the game loop 
   }
   
   public void keyReleased(KeyEvent ke)
   {
      controls[ke.getKeyCode()] = 0; //key handlers should ONLY set flags. The interpretation of the flags is best left to the game loop
   }
   
   public void keyTyped(KeyEvent ke) {}
   
   public static void main(String [] args)
   {
      new Game(false);
   }
}