Add a pictures to a card value? - java

I'm currently making a card game(made a custom one instead of lame poker), and basically I want to add pictures based on the value of the card. It's separated into a/b(a being the suit, b being the value).
Chunk of code I'm assuming will change to add pictures (or it's the card class?)
cards = new ArrayList < > ();
for (int a = 0 ; a <= 3 ; a++)
{
for (int b = 0 ; b <= 12 ; b++)
{
cards.add (new Card (a, b));
}
}
}
//Method to draw a card and remove card from the current deck
public Card PlayerCardDraw ()
{
Random generator = new Random ();
int index = generator.nextInt (cards.size ());
return cards.remove (index);
}
//Adds a card to player 1's hand kept in an arraylist to group cards to an individual player
public ArrayList <Card> p1Hand;
public ArrayList Card> P1CardDraw ()
{
p1Hand = new ArrayList < > ();
p1Hand.add (PlayerCardDraw ());
return p1Hand;
}

While I typed up the following answer, the asker wrote that he's using slick2D. That changes things.
New Answer
You should add slick2d as one of the tags for your question to get attention of the right crowd.
Old Answer
It seems like you have started with the underlying algorithm but you don't have any graphics or UI(User Interface) code done yet.
This means you need to decide What technology you will use to write your code.
If you want to write a game that sits on a computer (desktop/laptop) as an application and runs on it, then your options are Swing/AWT or Eclipse/SWT. They are pretty comparable, so I'd say whichever technology you can find an expert in.
Another option is that this is a game that will sit in a web server, and people will play it through web browser. In that case you need to look into javascript or Swing/AWT. Google has this technology called GWT which works over web and integrates java and javascript well-together. That might be a great option for your needs.
On the other hand, you might be writing a mobile game. If it's android then I'd suggest you look at it's sdk and make your decision. I believe you can write java apps for android. It might also be using GWT but not sure.
Or it could be an iPhone app. I don't think iPhone apps can be written in Java. You need to get iPhone's SDK and learn it.
Once you have decided on your platform and technology, that's when you are close to asking the question that you are asking right now.
Still before you ask this question, you need to figure out, what your main window will look like, what kind of buttons and other widgets will be there.
In short you are long way from putting that picture on the card, but with hard work and study of the technologies, you can get there.

Related

Separate User-Input and Graphical-Preview of a Game In Java

I made a simple "snake" game in java. As an exercise I want to control the user-input and the graphical-preview of the game separately using interfaces.
I have created 2 interfaces for that: InputSystem and GraphicalSystem.
So my main-loop of the game looks something like that:
while (snake.isAlive()) {
byte direction = inputSystem.getDirection(snake); // get input from user
snake.moveDirection(direction); // move in the game
graphicalSystem.update(); // preview current game state
The problem I've encountered is the implementation of the InputSystem interface. More precisely I try to make it a keyboard-input kind of thing. (while my GraphicalSystem is a GUI that I have already implemented).
The only way I found to receive a keyboard input was using some kind of GUI. But my GraphicalSystem is already using GUI. I also don't want to add the keyListener into the GraphicalSystem, because it will miss the whole idea of separating inputs and graphics. Do you have any suggestions for what should I do? I would love any idea.
Thanks in advance :)

ActionListener and dynamic(?) GUI

I've read through a dozen or so actionlistener/loop related questions here, but I'm not sure I've found my answer. I started on my first large Java project, a text RPG that's spiraled into around 5K lines of logic and game features which was functioning as intended using just the console - when I decided I'd try to build a Java swing GUI for it instead. Here's my problem:
I use a Room object which handles the description of where the player is at and also has an array of options for the player to choose next which it creates dynamically based on what cell the room's id is in on a csv file and what is beside it. I stopped outputting this to the console and instead started creating JButtons based on the options array like so:
public void showNarrate(){
add(dd,gridConstraints);
optionCopy.clear();
int i = 0;
for(JButton j : optionButtons){
//adding and formatting buttons to gridBagConstraint I also set actionCommand for each button to the triggerValue (ID of the next room which the button should take the player to)
}
//I tried using a copy of my JButton array here so I could have something to iterate over in actionListener after clearing out the original
//(Since it needs to be cleared so the next Room's buttons can be built after the player chooses an option)
for(JButton j : optionButtons){
optionCopy.add(j);
}
optionButtons.clear();
//dd is a seperate drawingComponent I used for outputting room descriptions which may be totally unnecessary at this point :/
dd.repaint();
setVisible(true);
}
Over in actionlistener (Same class) this is how I tried to swing it:
for(JButton j : optionCopy){
if(e.getActionCommand().equals(j.getActionCommand())){
Main.saveRoom = Main.currentRoom;
Main.currentRoom = j.getActionCommand();
System.out.println(Main.currentRoom);
}
}}
Then in my main class I call:
narrator.narrate(currentRoom, saveRoom); which takes care of some other logic concerning locked doors, encounters, etc.
Also in Main loop are some other methods related to autosave and tracking which rooms the player has visited. I know from other q/a i'v read on here that this is all pretty bad design, and I'm sttarting to understand that now, but my issue is this:
The first room of the game loads up fine, when I click a button it outputs to console(Just for testing) the correct trigger value of the room the button should be calling, so I'm getting that far, but how can I call the same method over again now?
-If I call narrate from actionListener it will wind up calling itself again and complain about ConcurrentModification.
-If I try to keep a loop going in my Main class it will keep looping and won't allow for the player to actually choose a button.
I've never used threads before, which I wonder might be the answer,and the closest thing to a related answer I've found is this:
Java: Method wait for ActionListener in another class
but I don't think moving actionListener to Main class would resolve my problem which is actionListener winding up calling itself recursively. And as for the observer-observable pattern... I just can't understand it :(
I appreciate any and all help, I've learned a LOT trying to make this thing work without seeking help as much as possible but this has stumped me.
Your loop in actionPerformed only checks whether a JButton exists in your optionList with the given actionCommand. However this can be done before actually doing something:
boolean contained = false;
for (JButton j : optionButtons)
if (j.getActionCommand().equals(e.getActionCommand()))
contained = true;
if (contained) {
// change room
}
now you can call narrate because you have finished iterating over the collection beforehand and will not get a ConcurrentModificationException

How to loop a GUI using java?

im creating a game using java language. I am new to GUI. I want to be able to somehow loop a frame. For example, if the user pressed 2 players, the second frame should be able to show twice like "Player 1 name: " then after pressing enter "player 2 name: " should be the next one and then after pressing next another frame will show. I want to be able to update my GUI every time the enter button is clicked, and limit the update depending on how many players was chosen. The images shown below have different .java files
while(i < game.getnPlayers()) {
if (action.getActionCommand().equals("enter")) {
// codes
}
i++;
}
im trying to start with this code, but when i do. it doesnt get updated
I see you have tagged the post with IntelliJ-idea, so I am assuming you are using the GUI designer. Graphical user interfaces use listeners to listen for certain input (OnMouseClick, ActionListener and so on), and then performs methods you assign to the listener. I would suggest reading some documents or watching some videos on the topic before moving on with your game.
A source from oracle on writing Event listeners
A video on making Java Swing GUIs using IntelliJ
Hope this helps! Good luck!
On your GUI class you must have the button function. There you can create an function for when the button is pressed (in this case the "ENTER"), then you will see how many players were selected.

Why do i get a NullPointerException when loading in ImageViews

I am currently making a poker game in java. The model works just fine. I do however also have to make a proper view for the game. I have to use javaFX scenebuilider as well as a graphic view class with actual code. In order to do this I made a superview to include both. When i am trying to run my application I get a white window and a nullpointerexception (for the code I included).
I do not get what I am doing wrong, since no one ever explained to me the basics of Imageviews and loading images in.
I made a package "res" where I embedded all the pics that represent my cards in the deck. Also note that k.toString() automatically generates the proper name of the file including the ".png" part. I printed it out and I am sure it works!
I use the for-loop to go through my list of cards that make the deck. My cards are generated in the Class Card. So basically I am going through an ArrayList of cards.
public void initializeDeck(){
for(Card c:model.getRound().getDeck().getList()){
int index = model.getRound().getDeck().getList().indexOf(c);
imv = new ImageView(new Image(getClass().getResourceAsStream("/res/"+k.toString()),95, 141, false, true));
deckView.add(index, imv);
}
getChildren().addAll(stapelView);
}

Best way to check selected weapon and draw without field name duplication

Hi everyone I am making a simple Libgdx game on Android that involves touching the screen and hitting things. However I have came across a slight problem. I have a small selection of weapons to choose from but I don't know what the best way is to actually draw/access the selected weapons. They are all inherited from a basic weapon class. Right now I have a normal hammer class and a fast hammer class which extends hammer.
The fast hammer has some special methods and swings faster. When I just instantiate the classes I want to test it works fine. However I want to do a check to see which hammer has been selected beforehand and then access it and draw it. I can't think of a very elegant way to do this other than a whole mass of if statements.
I originally tried giving a check variable an int. Then If int 1 then hammer = new normalhammer(); else if int 2 then hammer = new fasthammer(); but this clearly won't work because my hammer variable is assigned to the normalhammer class i.e. Hammer hammer; What is the best way to do this thanks.
Hammer hammer;
FastHammer hammer;//this obviously won't work because duplicate names
if(selected==1){//this was the plan but again won't work because duplication
hammer = new Hammer();
}
else if (selected==2){
hammer = new FastHammer();
}
hammerframe = hammer.HammerAnimation.getKeyFrame(hammer.hammerTime+=delta, false);
//then accessing the class variables won't work because again hammer is a duplicate field. I basically want to check what the check int is and then set hammer to the right class based on that int and the rest of the code will automatically retrieve what I need. Is this possible?
Well, here's how I would do it.
Make all your weapon classes implement some interface/superclass that will help with rendering
Keep an "armory" of weapon objects, one for each weapon type
When the player selects a weapon, set the representative existing object into a "current weapon" variable
For rendering, use the existing "current weapon" variable
Then you don't need to deal with the different types in the renderer - it's abstracted behind the interface!

Categories