Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (495)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Multiple keys pressed at once!  (Read 1254 times)
0 Members and 1 Guest are viewing this topic.
Offline Smeghead

Junior Newbie




Java games rock!


« Posted 2004-11-16 02:11:45 »

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!  
Offline Smeghead

Junior Newbie




Java games rock!


« Reply #1 - Posted 2004-11-16 02:13:13 »

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

Senior Member




while (true) System.out.println("WOO!!!!");


« Reply #2 - Posted 2004-11-16 02:35:12 »

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.

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline dranonymous

Junior Member




Hoping to become a Java Titan someday!


« Reply #3 - Posted 2004-11-17 08:22:31 »

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.  Smiley

Dr. A>
Offline Smeghead

Junior Newbie




Java games rock!


« Reply #4 - Posted 2004-11-17 22:16:00 »

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.



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)
Offline Malohkan

Senior Member




while (true) System.out.println("WOO!!!!");


« Reply #5 - Posted 2004-11-17 22:26:24 »

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.

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
Offline Abuse

JGO Knight


Medals: 12


falling into the abyss of reality


« Reply #6 - Posted 2004-11-21 11:18:03 »

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
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);
   }
}

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Dwinin (28 views)
2014-09-12 09:08:26

Norakomi (57 views)
2014-09-10 13:57:51

TehJavaDev (74 views)
2014-09-10 06:39:09

Tekkerue (38 views)
2014-09-09 02:24:56

mitcheeb (57 views)
2014-09-08 06:06:29

BurntPizza (45 views)
2014-09-07 01:13:42

Longarmx (28 views)
2014-09-07 01:12:14

Longarmx (34 views)
2014-09-07 01:11:22

Longarmx (35 views)
2014-09-07 01:10:19

mitcheeb (40 views)
2014-09-04 23:08:59
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!