Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Game Development / Newbie & Debugging Questions / Re: Key types while in a loop on: 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);}
    }

}
2  Game Development / Newbie & Debugging Questions / Re: Key types while in a loop on: 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
3  Game Development / Newbie & Debugging Questions / Re: Key types while in a loop on: 2009-02-19 10:49:34
hm i already have
1  
try { Thread.sleep(20); } catch (Exception e) {}
4  Game Development / Newbie & Debugging Questions / Key types while in a loop on: 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?
Pages: [1]
 

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

The first screenshot will be displayed as a thumbnail.

rwatson462 (36 views)
2014-12-15 09:26:44

Mr.CodeIt (29 views)
2014-12-14 19:50:38

BurntPizza (61 views)
2014-12-09 22:41:13

BurntPizza (98 views)
2014-12-08 04:46:31

JscottyBieshaar (58 views)
2014-12-05 12:39:02

SHC (74 views)
2014-12-03 16:27:13

CopyableCougar4 (76 views)
2014-11-29 21:32:03

toopeicgaming1999 (137 views)
2014-11-26 15:22:04

toopeicgaming1999 (127 views)
2014-11-26 15:20:36

toopeicgaming1999 (37 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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