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  
  Pause the game until key pressed  (Read 280 times)
0 Members and 1 Guest are viewing this topic.
Offline Bloodnut

Junior Newbie





« Posted 2014-01-22 12: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

Senior Member


Medals: 5
Projects: 1



« Reply #1 - Posted 2014-01-22 12: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 12: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 12: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'?
Online Seiya02

JGO Coder


Medals: 3
Projects: 4
Exp: 5 years


No Text Here :x


« Reply #4 - Posted 2014-01-22 12: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 12: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  
 
 
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.

BurntPizza (14 views)
2014-09-19 03:14:18

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

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

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

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

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

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

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

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

Longarmx (36 views)
2014-09-07 01:10:19
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!