i need help creating a simple game.

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2008
Posts: 7
Reputation: saintb is an unknown quantity at this point 
Solved Threads: 0
saintb saintb is offline Offline
Newbie Poster

i need help creating a simple game.

 
0
  #1
Apr 23rd, 2008
trying to make a program(game) for two players. the game has an interface and three text fields "player1","player2", and "winning amount". the interface also has a button called rand which when pressed generates random numbers from 1 to 10 and assigns them to a particular field. assuming that the first player to click rand is player1 and the second is player2. the game should end when two consecutive numbers generated are the same. the sum of all the generated numbers should be added and sent to the winning amount text field. there is also a clear button which resets the text fields for starting a new game. please help me with code am a beginner in java.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,467
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 267
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: i need help creating a simple game.

 
0
  #2
Apr 23rd, 2008
Be more than happy to help you. Once you show us what you have. We are not going to do it for you, though.

You need, JFrame, JLabel, JTextField, JButton, GridLayout (as the easiest), ActionListener, and Random.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 7
Reputation: saintb is an unknown quantity at this point 
Solved Threads: 0
saintb saintb is offline Offline
Newbie Poster

i need help creating a simple game.

 
0
  #3
Apr 24th, 2008
following masijade advice i tried busting my head abit. i have written the following code

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;


public class Game extends JApplet implements ActionListener
{
//Creating a new JPanel called panel1
JPanel panel1 = new JPanel();
//Creating a JLabel for panel1
JLabel header = new JLabel("Simple Random Numbers Game 4 Two Players");
//Seting the font for panel1
Font bigFont = new Font("Tahoma", Font.ITALIC, 18);

//Creating a new JPanel called panel2
JPanel panel2 = new JPanel();

//Creating a JLabel R1 which receives a parameter "player1"
JLabel R1 = new JLabel("Player 1");
//Creating a JTextField Player1
JTextField player1 = new JTextField("",6);
JLabel R2 = new JLabel("Player 2");
JTextField player2 = new JTextField("",6);

JLabel amount = new JLabel("Winning Amount");
JTextField amnt = new JTextField("",6);


//panel3 and its contents
JPanel panel3 = new JPanel();
JButton random = new JButton("Random");
JButton clear = new JButton("Clear");

//seting the layout to be used in the game applet
FlowLayout flow = new FlowLayout();

//initiallising the Applet
public void init()
{
header.setFont(bigFont);
Container con = getContentPane();

//defining the layouts for each of the three panels
panel1.setLayout(new GridLayout(4,1,10,10));
panel2.setLayout(new GridLayout(1,1,6,6));
panel3.setLayout(new GridLayout(1,1,5,5));

//adding the panels and components to the container
con.add(panel1);
panel1.setBorder(new TitledBorder ("Controls"));
panel1.add(header);
panel1.add(panel2);
panel1.add(panel3);
panel2.setBorder(new TitledBorder (""));
panel2.add(R1);
panel2.add(player1);
panel2.add(R2);
panel2.add(player2);
panel2.add(amount);
panel2.add(amnt);
panel3.setBorder(new TitledBorder (""));
panel3.add(random);
panel3.add(clear);
con.setLayout(new FlowLayout());//setting the defined layout
//registering text tips to the buttons
random.setToolTipText("Press to play");
clear.setToolTipText("Press to reset");
//registering mnemonics to the buttons
random.setMnemonic('R');
clear.setMnemonic('C');
}
public void actionPerformed(ActionEvent thisEvent)
{
Object source = thisEvent.getSource();
if(source == random)
{
String name = amnt.getText();
System.out.println("it ends here"+amount);
}
}
}
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,467
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 267
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: i need help creating a simple game.

 
0
  #4
Apr 24th, 2008
And? What's it doing?

Are you getting compiler messages?

Are you getting an exception?

Are you seeing an unexpected effect?

In short, what is wrong?

Post the complete messages, and, if it's the third, a complete description of what you expect and a complete description of what you actually get. (And repost your code using code tags please.)
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 7
Reputation: saintb is an unknown quantity at this point 
Solved Threads: 0
saintb saintb is offline Offline
Newbie Poster

Re: i need help creating a simple game.

 
0
  #5
Apr 24th, 2008
the code is giving a use interface that i require plus the text fields and the random and clear button.but i cant generate any random numbers with the rand button and later on out put them please help. the code is given below
  1. import java.awt.event.*;
  2. import java.awt.*;
  3. import javax.swing.*;
  4. import javax.swing.border.*;
  5.  
  6.  
  7. public class Game extends JApplet implements ActionListener
  8. {
  9. //Creating a new JPanel called panel1
  10. JPanel panel1 = new JPanel();
  11. //Creating a JLabel for panel1
  12. JLabel header = new JLabel("Simple Random Numbers Game 4 Two Players");
  13. //Seting the font for panel1
  14. Font bigFont = new Font("Tahoma", Font.ITALIC, 18);
  15.  
  16. //Creating a new JPanel called panel2
  17. JPanel panel2 = new JPanel();
  18.  
  19. //Creating a JLabel R1 which receives a parameter "player1"
  20. JLabel R1 = new JLabel("Player 1");
  21. //Creating a JTextField Player1
  22. JTextField player1 = new JTextField("",6);
  23. JLabel R2 = new JLabel("Player 2");
  24. JTextField player2 = new JTextField("",6);
  25.  
  26. JLabel amount = new JLabel("Winning Amount");
  27. JTextField amnt = new JTextField("",6);
  28.  
  29.  
  30. //panel3 and its contents
  31. JPanel panel3 = new JPanel();
  32. JButton random = new JButton("Random");
  33. JButton clear = new JButton("Clear");
  34.  
  35. //seting the layout to be used in the game applet
  36. FlowLayout flow = new FlowLayout();
  37.  
  38. //initiallising the Applet
  39. public void init()
  40. {
  41. header.setFont(bigFont);
  42. Container con = getContentPane();
  43.  
  44. //defining the layouts for each of the three panels
  45. panel1.setLayout(new GridLayout(4,1,10,10));
  46. panel2.setLayout(new GridLayout(1,1,6,6));
  47. panel3.setLayout(new GridLayout(1,1,5,5));
  48.  
  49. //adding the panels and components to the container
  50. con.add(panel1);
  51. panel1.setBorder(new TitledBorder ("Controls"));
  52. panel1.add(header);
  53. panel1.add(panel2);
  54. panel1.add(panel3);
  55. panel2.setBorder(new TitledBorder (""));
  56. panel2.add(R1);
  57. panel2.add(player1);
  58. panel2.add(R2);
  59. panel2.add(player2);
  60. panel2.add(amount);
  61. panel2.add(amnt);
  62. panel3.setBorder(new TitledBorder (""));
  63. panel3.add(random);
  64. panel3.add(clear);
  65. con.setLayout(new FlowLayout());//setting the defined layout
  66. //registering text tips to the buttons
  67. random.setToolTipText("Press to play");
  68. clear.setToolTipText("Press to reset");
  69. //registering mnemonics to the buttons
  70. random.setMnemonic('R');
  71. clear.setMnemonic('C');
  72. }
  73. public void actionPerformed(ActionEvent thisEvent)
  74. {
  75. Object source = thisEvent.getSource();
  76. if(source == random)
  77. {
  78. String name = amnt.getText();
  79. System.out.println("it ends here"+amount);
  80. }
  81. }
  82. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,467
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 267
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: i need help creating a simple game.

 
0
  #6
Apr 24th, 2008
I would, but I have not a clue what you just said.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 7
Reputation: saintb is an unknown quantity at this point 
Solved Threads: 0
saintb saintb is offline Offline
Newbie Poster

Re: i need help creating a simple game.

 
0
  #7
Apr 25th, 2008
what i need is code that will help me generate the numbers and then assign them to the text field.and code that will help me to reset the text field.please people am just a beginner help me
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 47
Reputation: Jens is an unknown quantity at this point 
Solved Threads: 5
Jens's Avatar
Jens Jens is offline Offline
Light Poster

Re: i need help creating a simple game.

 
0
  #8
Apr 25th, 2008
The button to reset your text field is just an actionlistener that sets the value of your textfield.text property to "" (or null).

For the generation of numbers, I suggest a class where you declare a variable (int var) and then use
[code = java]var = (int)(100 * Math.random())
[/code]
This particular example gives you a value between one and hundred.
After you stored this value in the variable, you can load the variable in your textbox using textbox.text = var.tostring().

At least, that is what I think it is out of my head. Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 11
Reputation: timon_zed is an unknown quantity at this point 
Solved Threads: 0
timon_zed timon_zed is offline Offline
Newbie Poster

Re: i need help creating a simple game.

 
0
  #9
Apr 30th, 2008
i think i know why you can't seem to eplain your code my friend. are you even in the slightes bit sure of your code is doing. to start with you just leave us hunging. what does your program do at the state it's at right now?
why do i get the feeling you just got that code off someone else?
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 1
Reputation: ravenduz is an unknown quantity at this point 
Solved Threads: 0
ravenduz ravenduz is offline Offline
Newbie Poster
 
0
  #10
Oct 9th, 2009
can create a visual games
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the Java Forum


Views: 2017 | Replies: 10
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC