Join us on Discord!
You can help CodeWalrus stay online by donating here.

Keyboard inputs in Java ( WalrusKingdom ) [Java]

Started by p2, November 13, 2016, 06:51:10 PM

Previous topic - Next topic

0 Members and 2 Guests are viewing this topic.

p2

Hi guys,
I'm currently working on my WalrusKingdom (former PocketWalrus) game and ran into problems...
meaning I'm totally stuck here and dont know what to do :(

thanks to @c4ooo and @kotu I have my game running and the map is displayed.
But the key inputs won't work.
As I added key inputs, they worked but the rendering stoped working (without an error, simply stops showing any graphics).
Putting the key input stuff in comments again, the graohics come back.
(Always only one of them is working).
I really hope someone can help me on this as I'm stuck here since over 2 weeks now :(

the rendering code as well as the keyboard code are both perfectly working but they seem to overwrite each other or not play together very well or something like that, idk.
My github project: https://github.com/KaliPhobos/WalrusKingdom
The key input method I tried to implement: http://codewalr.us/1649/46276
The WINDOW in the main file containing the MAIN class. As you can see I out the keyboard inouts in comments right now.
When trying to run my project, make sure you have my picture in a special folder: "/resources/tiles.png"
Thank you very much, I'd really appreciate any help here... <3
  • Calculators owned: ti-83+, ti-84+, ti-84+, ti-84+se, ti-84+se(te), ti-nsphire, ti-nsphire CAS, ti-nsphire CX-CAS, ti-voyage, ti-voyage, Who reads this list anyways...?
Anyway war sucks. Just bring us your food instead of missiles  :P ~ DJ Omnimaga (11.10.2016 20:21:48)
if you cant get a jframe set up, draw stuff to it, and receive input, i can only imagine how horrible your game code is _._   ~ c4ooo (14.11.2016 22:44:07)
If they pull a Harambe on me tell my family I love them ~ u/Pwntear37d (AssangeWatch /r/)
make Walrii great again ~ DJ Omnimaga (28.11.2016 23:01:31)
God invented the pc, satan the smartphone I guess ~ p4nix (16.02.2017 22:51:49)

kotu

hi p2 i got it working :)

here is your new main()....

public static void main(String[] args) {
LoadBlocks(); // Loading background block data
Screen.setSize(width/blocksize, height/blocksize); // Setup Screen (results in 0-11 x 0-17)
JFrame window = Screen.createWindow(); // Create window object (JFrame)

System.out.println("hello");
Keys keys = new Keys(window);
window.pack();
window.setVisible(true);

Map.setSize(100, 100); // Create map with given size
//Player Walrii = Player.createPlayer(0, 10, 10); // Define Player
TileArea tileArea = Screen.createTileArea(window);
Player.setXPos(0);
Player.setYPos(22);
Map.createRandom(0, 96);
Map.loadCity1();
Screen.update();
Screen.render(true);
while (true) { // MAIN GAME LOOP
    Screen.update();
    Screen.render(true);
    Screen.UpdateOldData();
    window.repaint();
    //KeyControls.checkInput();
    Keys.checkInput();
    General.sleep(10);
}
}


Here is the new file Keys.java.......

package CodeW;

import javax.swing.JFrame;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;

public class Keys implements KeyListener
{
public static boolean keyDown[];

public void keyPressed(KeyEvent ke)
{
keyDown[ke.getKeyCode()] = true;
}
public void keyReleased(KeyEvent ke)
{
keyDown[ke.getKeyCode()] = false;
}
public void keyTyped(KeyEvent ke)
{
}

public Keys(JFrame _window)
{
keyDown = new boolean[65536];
_window.addKeyListener(this);
}

public static void checkInput()
{
for (int _x=0;_x<65536;_x++)
{
        if (keyDown[_x] == true) {
        System.out.println(_x+" pressed");
        }
        }
}
}


Note there is more I want to do this, particularly I don't think the key array size should be 65536, but I will look at it later. Enjoy!
  • Calculators owned: TI 84+CE-T
  • Consoles, mobile devices and vintage computers owned: Sega Master System, Sony PlayStation 3
SUBSCRIBE TO THE FUTURERAVE.UK MAILING LIST
http://futurerave.uk

p2

okeyyyy thanks to kotu IT WORKS NOW!!! Thank you so much! :D :D
Next thing is implementing correct walking (working on that) as well as a modified rendering mechanism (only rendering changes and stuff) ^^
thx a lot :D <3
  • Calculators owned: ti-83+, ti-84+, ti-84+, ti-84+se, ti-84+se(te), ti-nsphire, ti-nsphire CAS, ti-nsphire CX-CAS, ti-voyage, ti-voyage, Who reads this list anyways...?
Anyway war sucks. Just bring us your food instead of missiles  :P ~ DJ Omnimaga (11.10.2016 20:21:48)
if you cant get a jframe set up, draw stuff to it, and receive input, i can only imagine how horrible your game code is _._   ~ c4ooo (14.11.2016 22:44:07)
If they pull a Harambe on me tell my family I love them ~ u/Pwntear37d (AssangeWatch /r/)
make Walrii great again ~ DJ Omnimaga (28.11.2016 23:01:31)
God invented the pc, satan the smartphone I guess ~ p4nix (16.02.2017 22:51:49)

kotu

Quote from: p2 on November 13, 2016, 10:41:44 PM
as well as a modified rendering mechanism (only rendering changes and stuff) ^^

i wouldn't do that it's very difficult and not a robust technique.
  • Calculators owned: TI 84+CE-T
  • Consoles, mobile devices and vintage computers owned: Sega Master System, Sony PlayStation 3
SUBSCRIBE TO THE FUTURERAVE.UK MAILING LIST
http://futurerave.uk

p2

I call the render method based on a fixed rate (modulo of milliseconds) but then fasten the method like that.
actually it' pretty easy, was already using it in my AutoIt version of the game ^^ (And its already partially embedded here, but not functional yet) ^^
  • Calculators owned: ti-83+, ti-84+, ti-84+, ti-84+se, ti-84+se(te), ti-nsphire, ti-nsphire CAS, ti-nsphire CX-CAS, ti-voyage, ti-voyage, Who reads this list anyways...?
Anyway war sucks. Just bring us your food instead of missiles  :P ~ DJ Omnimaga (11.10.2016 20:21:48)
if you cant get a jframe set up, draw stuff to it, and receive input, i can only imagine how horrible your game code is _._   ~ c4ooo (14.11.2016 22:44:07)
If they pull a Harambe on me tell my family I love them ~ u/Pwntear37d (AssangeWatch /r/)
make Walrii great again ~ DJ Omnimaga (28.11.2016 23:01:31)
God invented the pc, satan the smartphone I guess ~ p4nix (16.02.2017 22:51:49)

kotu

what's the framerate currently? i think it's pretty fast
  • Calculators owned: TI 84+CE-T
  • Consoles, mobile devices and vintage computers owned: Sega Master System, Sony PlayStation 3
SUBSCRIBE TO THE FUTURERAVE.UK MAILING LIST
http://futurerave.uk

p2

for whatever reason it's pretty unstable... :/
But I'll add a framerate counter once I'm done with the modified render algorythms :)
  • Calculators owned: ti-83+, ti-84+, ti-84+, ti-84+se, ti-84+se(te), ti-nsphire, ti-nsphire CAS, ti-nsphire CX-CAS, ti-voyage, ti-voyage, Who reads this list anyways...?
Anyway war sucks. Just bring us your food instead of missiles  :P ~ DJ Omnimaga (11.10.2016 20:21:48)
if you cant get a jframe set up, draw stuff to it, and receive input, i can only imagine how horrible your game code is _._   ~ c4ooo (14.11.2016 22:44:07)
If they pull a Harambe on me tell my family I love them ~ u/Pwntear37d (AssangeWatch /r/)
make Walrii great again ~ DJ Omnimaga (28.11.2016 23:01:31)
God invented the pc, satan the smartphone I guess ~ p4nix (16.02.2017 22:51:49)

kotu

#7
ok @p2 .... i checked out the values of the keycode and on  my keyboard they go from 8 to 525

i therefore changed the Keys.java file array size to be 1024

i used a constant (defined in java using the final keyword) for this, and added some bounds checking to make the code safe

here is the new file.


package CodeW;

import javax.swing.JFrame;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;

public class Keys implements KeyListener
{
private static final int NUM_KEYS_STORED = 1024;
public static boolean keyDown[];

public void keyPressed(KeyEvent ke)
{
if (ke.getKeyCode() < NUM_KEYS_STORED) {
keyDown[ke.getKeyCode()] = true; // 8 to 525
}
}
public void keyReleased(KeyEvent ke)
{
if (ke.getKeyCode() < NUM_KEYS_STORED) {
keyDown[ke.getKeyCode()] = false;
}
}
public void keyTyped(KeyEvent ke)
{
}

public Keys(JFrame _window)
{
keyDown = new boolean[NUM_KEYS_STORED];
_window.addKeyListener(this);
}

public static void checkInput()
{
for (int x = 0; x < NUM_KEYS_STORED; x++)
{
                if (keyDown[x] == true) {
                System.out.println(x + " pressed");
                }
                }
}
}



*edit*
also just noticed this...........
http://docs.oracle.com/javase/7/docs/api/java/awt/event/KeyEvent.html#KEY_FIRST
  • Calculators owned: TI 84+CE-T
  • Consoles, mobile devices and vintage computers owned: Sega Master System, Sony PlayStation 3
SUBSCRIBE TO THE FUTURERAVE.UK MAILING LIST
http://futurerave.uk

Powered by EzPortal