Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (577)
games submitted by our members
Games in WIP (498)
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  
  Pause the game until key pressed  (Read 209 times)
0 Members and 1 Guest are viewing this topic.
Offline Bloodnut

Junior Newbie





« Posted 2014-01-22 13:22:19 »

Hello JGO community! newb here.

I'm using LWJGL to create a small game that should have the ability the be paused (preferably by ESC key) and unpaused (the same way).
I'm guessing I could do it with threads wait/notify but I was hoping there would be an easier method.

thanks,
B.
Offline Kefwar

Junior Member


Medals: 1



« Reply #1 - Posted 2014-01-22 13:26:07 »

How about skipping the game ticking if the game is paused.
1  
2  
3  
4  
5  
6  
7  
8  
9  
boolean isPaused = false;
//The update method
public void update() {
  if (isKeyDown(KEY_ESCAPE))
            isPaused = !isPaused;
  if(!isPaused) {
    //update the game
 }
}


I'm quite new to this too, it's just an idea.
Online trollwarrior1
« Reply #2 - Posted 2014-01-22 13:27:33 »

Here is my keyboardhandler class.

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  
public class KeyboardHandler {

   public List<Character> charsPressed = new ArrayList<Character>();

   public KeyboardHandler() {
   }

   private void processInput() {

      while (Keyboard.next()) {

         int key = Keyboard.getEventKey();
         boolean state = Keyboard.getEventKeyState();

         charsPressed.add(Keyboard.getEventCharacter());

         toggle(key, state);

      }

   }

   private void toggle(int keycode, boolean state) {
      for (Key key : keys) {
         if (key.keycode == keycode) key.toggle(state);
      }
   }

   public void tick() {

      processInput();

      for (Key key : keys) {
         key.tick();
      }
   }
   
   public void releaseAll() {
      for(int i=0;i<keys.size();i++) {
         keys.get(i).pressed = false;
         keys.get(i).down = false;
      }
   }


   public List<Key> keys = new ArrayList<Key>();
   
   public Key KEY_W = new Key(Keyboard.KEY_W);
   public Key KEY_A = new Key(Keyboard.KEY_A);
   public Key KEY_S = new Key(Keyboard.KEY_S);
   public Key KEY_D = new Key(Keyboard.KEY_D);

   public Key KEY_CTRL = new Key(Keyboard.KEY_LCONTROL);
   public Key KEY_R = new Key(Keyboard.KEY_R);
   public Key KEY_T = new Key(Keyboard.KEY_T);

   public Key KEY_K = new Key(Keyboard.KEY_K);

   public Key KEY_V = new Key(Keyboard.KEY_V);



   public class Key {

      public int keycode;

      public boolean down = false, pressed = false;
      private boolean wasDown = false;

      public Key(int keycode) {
         this.keycode = keycode;
         keys.add(this);
      }

      public void toggle(boolean bool) {
         down = bool;
      }

      public void tick() {

         if (!wasDown && down) {
            pressed = true;
         } else {
            pressed = false;
         }

         wasDown = down;
      }

   }

}


You need to call KeyboardHandler.tick method every time you update your game. Key.down is a boolean which says if key is currently down. Key.pressed is a boolean which says if the key was pressed.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Bloodnut

Junior Newbie





« Reply #3 - Posted 2014-01-22 13:36:07 »

@Kefwar
well, yeah I suppose I could do that. didn't think of it Smiley

@trollwarrior1
what's the difference between a key that is 'down' and 'pressed'?
Offline Seiya02

Senior Member


Medals: 1
Projects: 3



« Reply #4 - Posted 2014-01-22 13:48:08 »

Key Pressed should just be the Moment when they Key actually pressed while Key Down repeatly is true when the Key is still Down
Online trollwarrior1
« Reply #5 - Posted 2014-01-22 13:49:51 »

pressed only happens once. When you press key. if(!wasDown && down) that will trigger once when they key wasn't down and is down.

down is always true when key is down.
Pages: [1]
  ignore  |  Print  
 
 

 

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

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (20 views)
2014-04-15 18:08:23

BurntPizza (16 views)
2014-04-15 03:46:01

UprightPath (29 views)
2014-04-14 17:39:50

UprightPath (14 views)
2014-04-14 17:35:47

Porlus (30 views)
2014-04-14 15:48:38

tom_mai78101 (55 views)
2014-04-10 04:04:31

BurntPizza (112 views)
2014-04-08 23:06:04

tom_mai78101 (212 views)
2014-04-05 13:34:39

trollwarrior1 (181 views)
2014-04-04 12:06:45

CJLetsGame (187 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
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!