How should i program mouse/key input in an advanced java game? - java

I am a self taught programmer, so I do not know the proper ways to do things. I have made simple games like asteroids and snake, but in those games, you can easily modify the variables within the keyevent functions. Here is how I did it in my simple Asteroids game:
/*
* key listener events
*/
public void keyReleased(KeyEvent k){
int keyCode = k.getKeyCode();
switch(keyCode){
case KeyEvent.VK_LEFT:
turnLeft = false;
break;
case KeyEvent.VK_RIGHT:
turnRight = false;
break;
case KeyEvent.VK_UP:
accel = false;
break;
case KeyEvent.VK_1:
cls = true;
break;
case KeyEvent.VK_ENTER:
break;
case KeyEvent.VK_SPACE:
fire = false;
}
}
public void keyTyped(KeyEvent K){}
public void keyPressed(KeyEvent k){
int keyCode = k.getKeyCode();
switch(keyCode){
case KeyEvent.VK_LEFT:
turnLeft = true;
break;
case KeyEvent.VK_RIGHT:
turnRight = true;
break;
case KeyEvent.VK_UP:
accel = true;
break;
case KeyEvent.VK_1:
cls = false;
break;
case KeyEvent.VK_ENTER:
clearAllBullets();
break;
case KeyEvent.VK_SPACE:
fire = true;
}
}
If I were to make a more advanced game (with a main menu, options, main game, etc.) How should I do the key/mouse input?
Also, if I were to go into the single-player, should I put all of the gameplay code into one class? Is there a way to put the single player code into a separate class and somehow have the key input still modify the variables and such?
Thank you for your time!
P.S. Any links or source is extremely appreciated. :D

"Is there a way to put the single player code into a separate class and somehow have the key input still modify the variables and such?"
Yes, there is way to encapsulate all event-based behaviours- http://en.wikipedia.org/wiki/Mediator_pattern but, it can make yor code very complicated, so firstly try to implement your game in more "normal" way.
Anyway - you will need other classes, to present domain model of your game (Player, Gameboard, UserInterfaces etc...)

Related

Button needs to be clicked twice. Any ideas why?

Basically what I'm trying to do is setting the color of the buttons. The "ganzjahrbutton" includes the other buttons. Therefore I wanna change the color of all the buttons (besides the ganzjahrbutton) to the grey color (162,162,162) if it's clicked.
If any of the other buttons is clicked, they should turn green and the ganzjahrbutton should become grey again. It kinda works, but the buttons need to be pressed twice.
Does anybody have an idea why?
switch (v.getId()){
case R.id.ganzjahrbutton:
ganzjahrbtnstate = !ganzjahrbtnstate;
if (ganzjahrbtnstate==true){
ganzjahrbutton.setBackgroundColor(Color.rgb(30,168,1));
fruhlungbutton.setBackgroundColor(Color.rgb(162,162,162));
sommerbutton.setBackgroundColor(Color.rgb(162,162,162));
herbstbutton.setBackgroundColor(Color.rgb(162,162,162));
winterbutton.setBackgroundColor(Color.rgb(162,162,162));
}
else {ganzjahrbutton.setBackgroundColor(Color.rgb(162,162,162));}
break;
case R.id.fruhlingbutton:
fruhlingbtnstate = !fruhlingbtnstate;
if (fruhlingbtnstate==true){fruhlungbutton.setBackgroundColor(Color.rgb(30,168,1));
ganzjahrbutton.setBackgroundColor(Color.rgb(162,162,162)); }
else {fruhlungbutton.setBackgroundColor(Color.rgb(162,162,162));}
break;
case R.id.sommerbutton:
sommerbtnstate = !sommerbtnstate;
if (sommerbtnstate==true){sommerbutton.setBackgroundColor(Color.rgb(30,168,1));
ganzjahrbutton.setBackgroundColor(Color.rgb(162,162,162)); }
else {sommerbutton.setBackgroundColor(Color.rgb(162,162,162));}
break;
case R.id.herbstbutton:
herbstbtnstate = !herbstbtnstate;
if (herbstbtnstate==true){herbstbutton.setBackgroundColor(Color.rgb(30,168,1));
ganzjahrbutton.setBackgroundColor(Color.rgb(162,162,162)); }
else {herbstbutton.setBackgroundColor(Color.rgb(162,162,161));}
break;
case R.id.winterbutton:
winterbtnstate = !winterbtnstate;
if (winterbtnstate==true){winterbutton.setBackgroundColor(Color.rgb(30,168,1));
ganzjahrbutton.setBackgroundColor(Color.rgb(162,162,162)); }
else {winterbutton.setBackgroundColor(Color.rgb(162,162,162));}
break;
Move the lines where you change the boolean states to the end of cases just before break statements
case R.id.ganzjahrbutton:
****FROM HERE*****ganzjahrbtnstate = !ganzjahrbtnstate;****
if (ganzjahrbtnstate==true){
ganzjahrbutton.setBackgroundColor(Color.rgb(30,168,1));
fruhlungbutton.setBackgroundColor(Color.rgb(162,162,162));
sommerbutton.setBackgroundColor(Color.rgb(162,162,162));
herbstbutton.setBackgroundColor(Color.rgb(162,162,162));
winterbutton.setBackgroundColor(Color.rgb(162,162,162));
}
else {ganzjahrbutton.setBackgroundColor(Color.rgb(162,162,162));}
TO HERE*****ganzjahrbtnstate = !ganzjahrbtnstate;****
break;

Best way to create options in RPG

I am following a tutorial I found on youtube on creating a basic RPG with a GUI. The tutorial is very helpful in giving directions on how to handle panels and frames, but the way they handle user decisions seems very impractical. They have a switch case that uses the player's location, and then another switch case within that then handles the actions depending on what button the user enters. Im not saying that it doesn't get the job done, but it leads me to have a LONG switch case. I was wondering what would be the best approach to have a cleaner and more manageable code.
snippet of the code:
//depending on your position, you are given different action options
switch(position) {
//if you are in town, these are your options
case "townGate":
switch(yourOption) {
//talk to the guard
case "c1": talkGaurd(); break;
//attack the guard
case "c2": attackGuard(); break;
}
break;
//if you are talking to the guard, these are your options
case "talkingToGaurd":
switch(yourOption) {
//leave to the outside of the gate
case "c1": townGate(); break;
}
break;
//if you are attacking the guard, these are your options
case "attackingGuard":
switch(yourOption) {
//leave the the outside of the gate
case "c1": townGate(); break;
}
break;
}

Converting Java code to work on android troubles? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
i'm having trouble making some java code i have work with android studio, the main problems i'm having is turning keyboard inputs into swipes and presses from the user, this is my current code for keyboard inputs:
class TAdapter extends KeyAdapter {
public void keyPressed(KeyEvent e) {
if (!isStarted || curPiece.getShape() == foodShapes.NoShape) {
return;
}
int keycode = e.getKeyCode();
if (keycode == 'p' || keycode == 'P') {
pause();
return;
}
if (isPaused)
return;
switch (keycode) {
case KeyEvent.VK_LEFT:
tryMove(curPiece, curX - 1, curY);
break;
case KeyEvent.VK_RIGHT:
tryMove(curPiece, curX + 1, curY);
break;
case KeyEvent.VK_DOWN:
tryMove(curPiece.rotateRight(), curX, curY);
break;
case KeyEvent.VK_UP:
tryMove(curPiece.rotateLeft(), curX, curY);
break;
case KeyEvent.VK_SPACE:
dropDown();
break;
case 'd':
oneLineDown();
break;
case 'D':
oneLineDown();
break;
}
any help would be appreciated, thankyou.
This page is available within the android documentation and tutorials....
public class MainActivity extends Activity {
...
// This example shows an Activity, but you would use the same approach if
// you were subclassing a View.
#Override
public boolean onTouchEvent(MotionEvent event){
int action = MotionEventCompat.getActionMasked(event);
switch(action) {
case (MotionEvent.ACTION_DOWN) :
Log.d(DEBUG_TAG,"Action was DOWN");
return true;
case (MotionEvent.ACTION_MOVE) :
Log.d(DEBUG_TAG,"Action was MOVE");
return true;
case (MotionEvent.ACTION_UP) :
Log.d(DEBUG_TAG,"Action was UP");
return true;
case (MotionEvent.ACTION_CANCEL) :
Log.d(DEBUG_TAG,"Action was CANCEL");
return true;
case (MotionEvent.ACTION_OUTSIDE) :
Log.d(DEBUG_TAG,"Movement occurred outside bounds " +
"of current screen element");
return true;
default :
return super.onTouchEvent(event);
}
}
I would suggest, however, that you use Google in the future, before seeking assistance on stack overflow. The attached link was located via Google on the first page of results for "android - detect input gestures within your app". In addition, I believe that I have come across a question that was directly related to this and it was answered in full.
Please remember; Stack Overflow is not here so that you can be lazy - its here so that you can seek assistance when it is actually required..

how to make key press not pause? [duplicate]

This question already has answers here:
Java KeyListener stutters
(3 answers)
Closed 9 years ago.
Okay, so I am making a 2D side scroller game for school. It is pretty much just like Mario (well, that is my hope for when I finish.) When I press the right arrow key to move my player along the map, he moves once, pauses, and then begins to smoothly move a lot. I think this is because of the pause that is in all apple laptops when you hold a key down, but I do not know how to get around this for my game. Any advice? Thank you.
One solution is to set boolean keyUP = true inside the keypressed method and keyUP = false inside the keyreleased method. This way once the key has been pressed your game registers this as "keep moving up" until you release the key, instead of "Up was pressed move up, up was pressed move up....". This removes that initial lag (very noticable on linux) on input.
From our game:
private boolean up, down, left, right;
public void keyReleased(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_S:
down = false;
break;
case KeyEvent.VK_W:
up = false;
break;
case KeyEvent.VK_A:
left = false;
break;
case KeyEvent.VK_D:
right = false;
break;
}
}
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_S:
down = true;
break;
case KeyEvent.VK_W:
up = true;
break;
case KeyEvent.VK_A:
left = true;
break;
case KeyEvent.VK_D:
right = true;
break;
Although you have not sent any code snippet I can assume the following. I think that you have implemented key listener that does something when key is pressed. If user remains pressing the key your program gets the second event after some delay and then starts getting the events faster.
This is not what you need. You probably should hold some kind of application state keyIsPressed that is changed only once when the key is down (not pressed) first time. Then turn this variable off only when key is up. When key is down first time start background thread that moves your object and checks the flag keyIsPressed periodically to know when to stop.
Good luck to become a competitor of Mario... :)
If this is a Swing GUI, then use Key Bindings, not a key listener,
start a Swing Timer on key press
and stop it on key release.
All KeyEvents are integers, so you can make boolean[] which will store all changes in KeyEvents. For Java2d am using something like this:
import java.awt.KeyEventDispatcher;
import java.awt.event.KeyEvent;
public final class Keyboard implements KeyEventDispatcher {
public static boolean[] keyPressed = new boolean[256];
public static boolean[] keyHold = new boolean[256];
public static boolean[] keyReleased = new boolean[256];
public static void update() {
for (int i=0; i<128; i++) {
keyPressed[i] = false;
keyReleased[i] = false;
}
}
public boolean dispatchKeyEvent(KeyEvent e) {
if (e.getID()==KeyEvent.KEY_PRESSED) {
keyHold[e.getKeyCode()] = true;
keyPressed[e.getKeyCode()] = true;
}
else if (e.getID()==KeyEvent.KEY_RELEASED) {
keyHold[e.getKeyCode()] = false;
keyReleased[e.getKeyCode()] = true;
}
return false;
}
}
I am calling update() after each frame.

Android Multitouch Issues

This is a fairly simple question I am sure, but I can't seem to figure out a way around this little thing. First, here is my code:
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
Log.w("Platformer", "primary down");
break;
case MotionEvent.ACTION_UP:
Log.w("Platformer", "primary up");
break;
case MotionEvent.ACTION_POINTER_1_DOWN:
Log.w("Platformer", "secondary down");
break;
case MotionEvent.ACTION_POINTER_1_UP:
Log.w("Platformer", "secondary up");
break;
}
All I wanted it to do was when I pressed down, it would show me in the log whether the primary or secondary pointer was down, and when I release, do the same thing. The problem is, it always works on the down, but on up, whatever the 1st finger that comes up is, regardless of if it was the primary or secondary pointer originally, it returns that the secondary pointer was removed. Any idea around this? I am guessing it is something simple, but I don't know where to look truthfully. Thanks in advance.
WWaldo
ACTION_UP means that the gesture is over, that is, all pointers are up.
ACTION_POINTER_UP means that a non-primary pointer is up, you'll have to check the event to figure out which, i.e.:
int pointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
MotionEvent.ACTION_POINTER_INDEX_SHIFT;
int pointerId = event.getPointerId(pointerIndex)
ACTION_POINTER_1_UP and ACTION_POINTER_2_UP are deprecated now.
You should note that this is extremely buggy on Android versions less than Gingerbread. I find that if I lift the first of two touches and place it back down, the pointers swap index and ID. Very frustrating.
Use getPointerId to get which pointer is being selected.
Edit
To answer the question in the comment, try:
for (int i = 0; i < ev.getPointerCount(); i++) {
switch (ev.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_POINTER_DOWN:
Log.d(TAG,"down "+ ev.getPointerId(i));
break;
case MotionEvent.ACTION_MOVE:
Log.d(TAG,"move "+ ev.getPointerId(i));
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
Log.d(TAG,"up "+ ev.getPointerId(i));
break;
}
}

Categories

Resources