Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
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  
  Key types while in a loop  (Read 1722 times)
0 Members and 1 Guest are viewing this topic.
Offline Ritzau

Junior Newbie





« Posted 2009-02-18 23:30:30 »

I'm making Space Invaders, and the game is running with a while-loop.
When the game is not in a loop it respons to key presses, but when i have started the loop it don't respond. It's the same object that implements the keylistener and the loop method, so is it possible that they can't do both at the same time? Will i have to create another object just for keylistening so i can type in loop?
Offline Del-ONE

Senior Newbie





« Reply #1 - Posted 2009-02-19 00:54:33 »

I'm not entirely sure, but I think your problem is caused by not using

try{Thread.sleep(sleepTime);}catch(InterruptedException e){}

at the end of each cycle, which means the CPU is choked by the loop and doesn't have time to process KeyEvents .  I believe I've had the same thing happen when omitting that part of game loops.
Offline Ritzau

Junior Newbie





« Reply #2 - Posted 2009-02-19 10:49:34 »

hm i already have
1  
try { Thread.sleep(20); } catch (Exception e) {}
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Ritzau

Junior Newbie





« Reply #3 - Posted 2009-02-19 11:32:53 »

now I have tried to make a keylistener both as a separate class and as a inner class, and it's the same problem. Before the while loop starts it respons, but after it doens't.

I have been looking at this tutorial:
http://www.cokeandcode.com/info/showsrc/showsrc.php?src=../spaceinvaders/org/newdawn/spaceinvaders/Game.java
and he did it with a private class, and I'm quite sure i did it like he did, so now I'm pretty lost  Huh
Offline Abuse

JGO Knight


Medals: 12


falling into the abyss of reality


« Reply #4 - Posted 2009-02-19 11:39:03 »

What Thread is your game loop running in? main, EDT, a thread you have constructed, or something else?

Post your code.

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline Ritzau

Junior Newbie





« Reply #5 - Posted 2009-02-19 12:16:08 »

In short, this is what happens when i run the game:
The main method creates a game object --> the game waits for a "any key press" (that thing works) --> the game starts-->it  initializes the player object, and starts the loop.

all the comments are danish, but i believe i made all the varibles english

Wall of code
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  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
package Main;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;

public class Game extends JPanel {

    //Variabler
   //True hvis spillet kører
   Boolean looping = true;
    //Array med spilleren og alle fjende objekterne
   ArrayList<Object> spaceships = new ArrayList<Object>();
    //spillets vindue
   JFrame frame;
    //Spillets panel som der tegnes i
   JPanel panel;
    //Grafikken der bruges til at tegne med
   Graphics g;
    //spiller objektet
   Player player;
    //Spillerens score
   int score;
    //hvor lang tid spilleren har spillet
   int time;
    //1/50 del af et sekund, bruges til at tælle tiden
   int split_sec;
    //True hvis spillet venter på at man skal gå igang
   boolean press_any_key = true;
    //Objektet der skal holde styr på tastetryk
   KeyboardListener listener;

       
    public Game() {
       
        //Opret vinduet
       frame = new JFrame("Space Invaders");
        frame.setLocation(200,200);
        panel = (JPanel)frame.getContentPane();
        panel.setLayout(null);
        panel.setPreferredSize(new Dimension(650,510));
        panel.setBackground(Color.black);
        setBounds(0,0,650,510);
        panel.add(this);
        setIgnoreRepaint(true);
        frame.pack();
        frame.setResizable(false);
        frame.setVisible(true);
        requestFocus();        
       
        //Lav en KeyListener
       listener = new KeyboardListener();
        listener.game_link = this;
        //Sæt spillet til at lytte efter tastetryk med en indre klasse
       addKeyListener(listener);

        //Sætter grafikken
       g = panel.getGraphics();
   
        try { Thread.sleep(1000); } catch (Exception e) {}
       
        //Venter på spillerens start
       waiting();
    }    

   
    public static void main(String[] args) {
       
        Game game = new Game();
   
    }
   
    public void loop(){
   
        while (looping == true){
           
            //Tegn baggrund
           g.setColor(Color.black);
            g.fillRect(0, 0, 650, 510);
           
            //Fjendens opdatering
           
            //Spillerens opdatering
           player.step();
            player.draw(g);
           
            //Interface opdatering
           interface_update();
            draw_interface();
           
            //vent 1/50 del af et sekund før løkken gentages
           try { Thread.sleep(25); } catch (InterruptedException e) {}
        }
    }
   
    public void init(){
        //Sætter så spillet kan kører i ring
       looping = true;
       
        //Lav spilleren
       player = new Player();
        //Gør så spiller objektet kan refererer til dette objekt
       player.game = this;
        //Læg spilleren i arrayen med alle rumskib-objekterne
       spaceships.add(player);
       
        listener.player_link = player;
       
    }
   
    public void remove_enemy(Enemy e){
        spaceships.remove(e);
    }
   
    public void add_enemy(Enemy e){
        spaceships.add(e);
    }
   
    public void start_game(){
        //Start et nyt spil
       init();
        loop();
    }
   
    public void reset(){
        //Fjerner alle nuværende objekter
       spaceships.clear();
       
        //Stopper spillet
       looping = false;
       
        //Sætter så spillet venter på at man skal starte forfra
       waiting();
    }
       
    public void waiting(){
        //Venter på på at spilleren skal starte
       press_any_key = true;
               
        g.setColor(Color.black);
        g.fillRect(0, 0, 650, 510);
        g.setColor(Color.red);
        g.drawString("PRESS ANY KEY", 270, 450);        
    }
   
    public void draw_interface(){
        //Tegn tiden
       g.drawString("Time: "+time, 50, 50);
    }
   
    public void interface_update(){
        split_sec++;
            if (split_sec == 40) {
                split_sec = 0;
                time++;
            }
    }
   
}


and the KeyListener object

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  
package Main;

import java.awt.event.*;

public class KeyboardListener implements KeyListener{
   
    public Game game_link;
    public Player player_link;
   
    public KeyboardListener(){
       
    }
   
    public void keyTyped(KeyEvent e) {
       
        if (game_link.press_any_key == true){
            game_link.start_game();
            game_link.press_any_key = false;
            return;
        }
    }
   
    public void keyPressed(KeyEvent e) {
        System.out.println("Keypress test");
       
        //Hvis piletasterne er trykket ned skal den sætte spillerens fart
       if (e.getKeyCode() == KeyEvent.VK_LEFT) {player_link.set_x_speed(-3);}
        if (e.getKeyCode() == KeyEvent.VK_RIGHT) {player_link.set_x_speed(3);}
        if (e.getKeyCode() == KeyEvent.VK_UP) {player_link.set_y_speed(-3);}
        if (e.getKeyCode() == KeyEvent.VK_DOWN) {player_link.set_y_speed(3);}
    }

    public void keyReleased(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_LEFT) {player_link.set_x_speed(0);}
        if (e.getKeyCode() == KeyEvent.VK_RIGHT) {player_link.set_x_speed(0);}
        if (e.getKeyCode() == KeyEvent.VK_UP) {player_link.set_y_speed(0);}
        if (e.getKeyCode() == KeyEvent.VK_DOWN) {player_link.set_y_speed(0);}
    }

}
Offline Abuse

JGO Knight


Medals: 12


falling into the abyss of reality


« Reply #6 - Posted 2009-02-19 12:26:39 »

Your game loop is executing on the EDT (Event Dispatch Thread); hence once started you will no-longer be able to receive events.

Your code seems rather confused & is consequently far more complicated than it needs to be.
I suggest you find an example of a simple game loop demonstrating the fundamentals.

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline fletchergames

Senior Member





« Reply #7 - Posted 2009-02-19 22:29:53 »

You need to separate your drawing code from your game code.  Your loop needs to go in a Thread other than the EDT, and it can't include any of the drawing methods.

Also, the "Graphics g" variable shouldn't be an instance variable of the class.  Instead, the parameter used by paintComponent should be used.  Since you're ignoring repaint, you will have to call paint yourself, and paint will call paintComponent.  You should look up BufferStrategy in the Java API and the Java Tutorial to see how you're supposed to use this.

I tried to fix up your code, but it got to be too much.  It would be easier to just give you code than to try writing it all over again.

You should buy either Developing Games in Java or Killer Game Programming in Java (preferably both) from Amazon.com.  After reading those two books, you'll probably have an idea of what to do.  These will explain active rendering, which it seems like you think you're doing but you're not.

Reading a book about threads (e.g. Java Concurrency in Practice) would probably be helpful too, but it might be overkill.
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #8 - Posted 2009-02-20 15:54:08 »

why do you have the keyboard reading in another class. there is an AWT library for it, and lwjgl and slick can do it, other probably can as well. you can just implement keyboardListener and then jsut make a method for keypress keyrelease and keyTyped I think. or jsut have it check the keys every loop if you use LWJGL or Slick.

but then again the coke and code tut did a lot of the stuff it didnt need ot do.


if there is a reason for making a class to read the keys please inform me.
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.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (19 views)
2014-09-24 16:12:06

Grunnt (45 views)
2014-09-23 14:38:19

radar3301 (27 views)
2014-09-21 23:33:17

BurntPizza (63 views)
2014-09-21 02:42:18

BurntPizza (33 views)
2014-09-21 01:30:30

moogie (41 views)
2014-09-21 00:26:15

UprightPath (50 views)
2014-09-20 20:14:06

BurntPizza (54 views)
2014-09-19 03:14:18
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!