* WalrusIRC

You need to have 5 posts and not be part of restricted usergroups in order to use the WalrusIRC embedded shoutbox. However, you can also access our IRC channel called #CodeWalrus via EFnet.

Author Topic: Keyboard inputs in Java ( WalrusKingdom )  (Read 399 times)

0 Members and 1 Guest are viewing this topic.

Offline p2

  • CodeWalrus Staff
  • Super User
  • Moderator
  • CodeWalrus Supporter
  • *
  • Join Date: Aug 2016
  • Location: Germany
  • Posts: 2030
  • Post Rating Ratio: +8/-0
  • *Wubba lubba dub dub*
    • KaliPhobos
    • View Profile
  • Gender: Male
Keyboard inputs in Java ( WalrusKingdom )
« on: November 13, 2016, 06:51:10 pm »
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 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)

Offline kotu

  • Walrus Dehumidifier
  • Super User
  • Join Date: Aug 2016
  • Location: UK
  • Posts: 847
  • Post Rating Ratio: +6/-6
    • dj_kotu
    • @UCAy2ZF4hbc08iStnTn6jgxQ
    • rowanmedhurst
    • @djkotu
    • @romanov-psyco
    • View Profile
    • futurerave.uk
  • Gender: Male
Re: Keyboard inputs in Java ( WalrusKingdom )
« Reply #1 on: November 13, 2016, 10:32:33 pm »
hi p2 i got it working :)

here is your new main()....
Code: [Select]
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.......
Code: [Select]
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

Offline p2

  • CodeWalrus Staff
  • Super User
  • Moderator
  • CodeWalrus Supporter
  • *
  • Join Date: Aug 2016
  • Location: Germany
  • Posts: 2030
  • Post Rating Ratio: +8/-0
  • *Wubba lubba dub dub*
    • KaliPhobos
    • View Profile
  • Gender: Male
Re: Keyboard inputs in Java ( WalrusKingdom )
« Reply #2 on: November 13, 2016, 10:41:44 pm »
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 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)

Offline kotu

  • Walrus Dehumidifier
  • Super User
  • Join Date: Aug 2016
  • Location: UK
  • Posts: 847
  • Post Rating Ratio: +6/-6
    • dj_kotu
    • @UCAy2ZF4hbc08iStnTn6jgxQ
    • rowanmedhurst
    • @djkotu
    • @romanov-psyco
    • View Profile
    • futurerave.uk
  • Gender: Male
Re: Keyboard inputs in Java ( WalrusKingdom )
« Reply #3 on: November 13, 2016, 10:42:43 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

Offline p2

  • CodeWalrus Staff
  • Super User
  • Moderator
  • CodeWalrus Supporter
  • *
  • Join Date: Aug 2016
  • Location: Germany
  • Posts: 2030
  • Post Rating Ratio: +8/-0
  • *Wubba lubba dub dub*
    • KaliPhobos
    • View Profile
  • Gender: Male
Re: Keyboard inputs in Java ( WalrusKingdom )
« Reply #4 on: November 13, 2016, 10:57:06 pm »
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 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)

Offline kotu

  • Walrus Dehumidifier
  • Super User
  • Join Date: Aug 2016
  • Location: UK
  • Posts: 847
  • Post Rating Ratio: +6/-6
    • dj_kotu
    • @UCAy2ZF4hbc08iStnTn6jgxQ
    • rowanmedhurst
    • @djkotu
    • @romanov-psyco
    • View Profile
    • futurerave.uk
  • Gender: Male
Re: Keyboard inputs in Java ( WalrusKingdom )
« Reply #5 on: November 13, 2016, 10:58:45 pm »
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

Offline p2

  • CodeWalrus Staff
  • Super User
  • Moderator
  • CodeWalrus Supporter
  • *
  • Join Date: Aug 2016
  • Location: Germany
  • Posts: 2030
  • Post Rating Ratio: +8/-0
  • *Wubba lubba dub dub*
    • KaliPhobos
    • View Profile
  • Gender: Male
Re: Keyboard inputs in Java ( WalrusKingdom )
« Reply #6 on: November 13, 2016, 10:59:55 pm »
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 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)

Offline kotu

  • Walrus Dehumidifier
  • Super User
  • Join Date: Aug 2016
  • Location: UK
  • Posts: 847
  • Post Rating Ratio: +6/-6
    • dj_kotu
    • @UCAy2ZF4hbc08iStnTn6jgxQ
    • rowanmedhurst
    • @djkotu
    • @romanov-psyco
    • View Profile
    • futurerave.uk
  • Gender: Male
Re: Keyboard inputs in Java ( WalrusKingdom )
« Reply #7 on: November 14, 2016, 03:55:26 am »
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.

Code: [Select]
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
« Last Edit: November 14, 2016, 04:24:16 am by kotu »
  • Calculators owned: TI 84+CE-T
  • Consoles, mobile devices and vintage computers owned: Sega Master System, Sony Playstation 3

 


You can also use the following HTML or bulletin board code to share it on your page or forum signature!


Also do not forget to check our affiliates below.
Planet Casio TI-Planet Calc.news BroniesQC BosaikNet Velocity Games