Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 609 610 [611] 612 613 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 908740 times)

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9150 on: February 27, 2016, 08:13:58 pm »

<roguelike>
If your roguelike is turn-based, I suggest using the keypress events instead of checking for down-ness. If your roguelike is realtime, then why not just find the first pressed key and use that?
Its safer in games to check for a switch from down > up status, unless your game specifically requires the ability to hold down buttons.  Negates the impact of accidental button presses and allows people to reconsider their actions, which is especially important in roguelikes.  Also prevents those glitches where the game thinks a button is being held down when its not.
I can't think of any roguelike that uses the release event for actions instead of the press event.
Logged

DragonDePlatino

  • Bay Watcher
  • [HABIT:COLLECT_WEALTH]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9151 on: February 27, 2016, 09:08:46 pm »

I used a python dictionary of {keycode:command} that actually worked pretty well but I ditched it because a huge string of if::else statements requires less thinking, and I'm not very good at thinking.

Man, if only. I've previously programmed in python and I thought dictionaries were really awesome. Unfortunately, pygame is kind of awful (lacks basics like Vsync) so I have to use SDL 2.0 and C++ if I want to get anything done. :/

If your roguelike is turn-based, I suggest using the keypress events instead of checking for down-ness. If your roguelike is realtime, then why not just find the first pressed key and use that?

I actually used to do that. SDL 2.0 has two different ways to get keyboard input: SDL_PollEvent (which handles KEYUP and KEYDOWN) and SDL_GetKeyboardState (one KEYHELD bool). I used to use SDL_PollEvent, but you have to make a painfully huge if-statement for every key. SDL_KeyboardState is what I current use, which only requires a single line of code to update my KEYHELD bools. To solve the obvious problem of repeated inputs, I just stop taking input while everything is walking around and animating.

3man75

  • Bay Watcher
  • I will fire this rocket
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9152 on: February 27, 2016, 09:15:14 pm »

Can someone please tell me what's wrong with my code when it's trying to return a 4 letter password?

Code: [Select]
password = (String) (passcode.nextInt(26)) + 'a';
EDIT: NVM

Found a way to generate a 4 letter password. If anyone wants to know PM me and i'll share 4 hours of tinkering with you.
« Last Edit: February 27, 2016, 09:36:29 pm by 3man75 »
Logged

Parsely

  • Bay Watcher
    • View Profile
    • My games!
Re: if self.isCoder(): post() #Programming Thread
« Reply #9153 on: February 27, 2016, 10:06:36 pm »

Can someone please tell me what's wrong with my code when it's trying to return a 4 letter password?

Code: [Select]
password = (String) (passcode.nextInt(26)) + 'a';
EDIT: NVM

Found a way to generate a 4 letter password. If anyone wants to know PM me and i'll share 4 hours of tinkering with you.
import java.util.Random;
Logged

3man75

  • Bay Watcher
  • I will fire this rocket
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9154 on: February 27, 2016, 10:52:44 pm »

I don't mean to be snobby but I already had that lol. Problem was USING the libraries synthax.

I'd hate to complain but this mini semester in Java programming has been shit. Haven't really had the time to look into things now that I have a 15 hour job and tackling a math class at the same time.

Thanks anyways Gunn.
Logged

Rose

  • Bay Watcher
  • Resident Elf
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9155 on: February 27, 2016, 11:06:56 pm »

For directions, rather than require only one button being pressed, I'd build an average direction based on all the pressed buttons, snap it to the 8 cardinal directions, and move there.
Logged

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9156 on: February 27, 2016, 11:33:27 pm »

For my first game (and first C++ program, really) I'm making a roguelike. Like most roguelikes, each directional key corresponds to an x and y direction, like so:

I think it would be better and more extensible to take the move code outside the for loop. Basically, you do a first for-loop scan by keys, but also increment a "numKeysHit" variable for each key that is held down.

After the loop, you do the move code only if(numKeysHit == 1). This would get rid of the huge if statement and make the system more extensible. e.g.:


Spoiler (click to show/hide)

^ this one has about 40% less characters than the old version, even though it's split across more lines. And it would also run faster (the old version does a calculation on each key twice if one was pressed).
« Last Edit: February 27, 2016, 11:56:28 pm by Reelya »
Logged

DragonDePlatino

  • Bay Watcher
  • [HABIT:COLLECT_WEALTH]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9157 on: February 28, 2016, 12:18:14 am »

I think it would be better and more extensible to take the move code outside the for loop. Basically, you do a first for-loop scan by keys, but also increment a "numKeysHit" variable for each key that is held down.

That's a waaaaay better way to do it! More lines, yeah, but infinitely more scalable and readable. Unfortunately, I can't try that out in my program at the moment. :P After moving some classes into their own headers and trying to fix it for a few hours, I can't even get it to compile. It's a virtual rat's nest of global variables and derived classes!

Tomorrow, I'll be starting from scratch and trying to limit my variable scope as much as possible. I should be frustrated at this, but I've actually learned a ton about C++ in the past week so I'm relieved to start fresh. Better now than later!

For directions, rather than require only one button being pressed, I'd build an average direction based on all the pressed buttons, snap it to the 8 cardinal directions, and move there.

That sounds like it'd work pretty well too! So if you're mashing, say, LEFTKEY, RIGHTKEY and UPKEY, it automatically averages out to UPKEY? I might have to use SDL_PollEvent to get that to work but at this point it seems like the best option. I'll do that for my rewrite.
« Last Edit: February 28, 2016, 12:19:52 am by DragonDePlatino »
Logged

3man75

  • Bay Watcher
  • I will fire this rocket
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9158 on: February 28, 2016, 12:53:17 am »

Is there a way to reset an object in Java?

Code: [Select]

// JOptionPane.showMessageDialog(null, "Hello User.");
Object[] menuPossibilities = {"Create a New Account", "Deposit", "Withdraw", "Display Balance", "Exit"};

Object menuValues = JOptionPane.showInputDialog(null, "Please pick an option from this menu: ",
"input", JOptionPane.INFORMATION_MESSAGE, null, menuPossibilities, menuPossibilities [0]);
while (!menuValues.equals("Exit")){

Bank newBank = new Bank();
// Bank newBank1 = new Bank();
// Bank newBank2 = new Bank(); Do the same thing as below but switch out
// bank1 and bank2 as a substitute.
ArrayList<BankAccount> bankList = newBank.getBankAccounts();

if (menuValues.equals("Create a New Account")){

newBank.openAccount();
}

else if (menuValues.equals("Deposit")){
newBank.deposit();
}
else if (menuValues.equals("Withdraw")){
newBank.withdraw();
}
else if (menuValues.equals("Display Balace")){
newBank.deposit();
}
else if (menuValues.equals("Exit")){
System.out.println("Thank you for using our service.");
}

//Object menuValues = null;

}

At first I got around this problem by asking the same question again but it led to an issue where the question is being asked too many times.

If I can make the object become null or return to it's previous state of 'unanswered' as it loops back then that would be perfect.
Logged

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9159 on: February 28, 2016, 12:58:51 am »

make a new one

Parsely

  • Bay Watcher
    • View Profile
    • My games!
Re: if self.isCoder(): post() #Programming Thread
« Reply #9160 on: February 28, 2016, 01:08:42 am »

I'd hate to complain but this mini semester in Java programming has been shit. Haven't really had the time to look into things now that I have a 15 hour job and tackling a math class at the same time.
I know the feeling, but really, Oracle.com is your friend when it comes to Java libraries.
Logged

Rose

  • Bay Watcher
  • Resident Elf
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9161 on: February 28, 2016, 01:43:23 am »

For directions, rather than require only one button being pressed, I'd build an average direction based on all the pressed buttons, snap it to the 8 cardinal directions, and move there.

That sounds like it'd work pretty well too! So if you're mashing, say, LEFTKEY, RIGHTKEY and UPKEY, it automatically averages out to UPKEY? I might have to use SDL_PollEvent to get that to work but at this point it seems like the best option. I'll do that for my rewrite.
It also means that you can press up and left to move diagonally, if it fits into the game.
Logged

TheBiggerFish

  • Bay Watcher
  • Somewhere around here.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9162 on: February 28, 2016, 08:38:32 am »

Is there a way to reset an object in Java?

Code: [Select]

// JOptionPane.showMessageDialog(null, "Hello User.");
Object[] menuPossibilities = {"Create a New Account", "Deposit", "Withdraw", "Display Balance", "Exit"};

Object menuValues = JOptionPane.showInputDialog(null, "Please pick an option from this menu: ",
"input", JOptionPane.INFORMATION_MESSAGE, null, menuPossibilities, menuPossibilities [0]);
while (!menuValues.equals("Exit")){

Bank newBank = new Bank();
// Bank newBank1 = new Bank();
// Bank newBank2 = new Bank(); Do the same thing as below but switch out
// bank1 and bank2 as a substitute.
ArrayList<BankAccount> bankList = newBank.getBankAccounts();

if (menuValues.equals("Create a New Account")){

newBank.openAccount();
}

else if (menuValues.equals("Deposit")){
newBank.deposit();
}
else if (menuValues.equals("Withdraw")){
newBank.withdraw();
}
else if (menuValues.equals("Display Balace")){
newBank.deposit();
}
else if (menuValues.equals("Exit")){
System.out.println("Thank you for using our service.");
}

//Object menuValues = null;

}

At first I got around this problem by asking the same question again but it led to an issue where the question is being asked too many times.

If I can make the object become null or return to it's previous state of 'unanswered' as it loops back then that would be perfect.

o=null;
Logged
Sigtext

It has been determined that Trump is an average unladen swallow travelling northbound at his maximum sustainable speed of -3 Obama-cubits per second in the middle of a class 3 hurricane.

3man75

  • Bay Watcher
  • I will fire this rocket
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9163 on: February 28, 2016, 04:19:50 pm »


O meaning object?

So..

Object = null?

Idk why but when I did this

Code: [Select]
else if (menuValues.equals("Exit")){
System.out.println("Thank you for using our service.");
}

menuValues = JOptionPane.showInputDialog(null, "Since you did not pick 5 please pick another option: ",
                   "input", JOptionPane.INFORMATION_MESSAGE, null, menuPossibilities, menuPossibilities [0]);

it sorta just started to work..i think. I seem to be able to start all over again and again like I want to just fine. I don't seem to be getting extra asks from the program now which is odd. Maybe it was the way I phrased it but I don't remember what I had before then.

Thanks for the help everyone. Right now i'm trying to get the program to accept an ID of numbers and a password of letters to access  from a an array list of accounts..to no avail. Anyone know of a solution? Idk why but this just refuses to work on me





Code: [Select]
for (int i = 0; i < bankAccounts.size(); i++)//Loops through accounts in my arraylist.
{
if (acctID == bankAccounts.get(i).getAccountId() && inputPassword == bankAccounts.get(i).getPassword())//If ID and password work are true then it goes in here.
{

String depositAmount = JOptionPane.showInputDialog("Please input how much money you want to "
+ "input");//An here is where you would be able to spit out how much money to deposit.

depositingAmount = Integer.parseInt(depositAmount);

bankAccounts.get(i).deposit(depositingAmount);

break;

}
}
}

EDIT: I found a .equals() method in oracle but it dosen't seem to work either. Can someone please help?
« Last Edit: February 28, 2016, 05:38:54 pm by 3man75 »
Logged

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9164 on: February 28, 2016, 06:01:55 pm »

No, he meant null has a value of zero in most c-based languages, not "letter o".

But you don't have to use literal "null". You can set a variable to some value that's not normally used, as your "default" and just check for that whenever you need to. And to "clear" the field, you set it back to your personally chosen "default" value. any time that could become a problem, just use an "if" statement to prevent the program printing this value.

 e.g. when I'm searching an array (where the indexes are from zero to n-1), I usually set the initial "found" index to -1, and if it's still -1 after the for loop, I didn't find what i am looking for.
« Last Edit: February 28, 2016, 06:08:40 pm by Reelya »
Logged
Pages: 1 ... 609 610 [611] 612 613 ... 796