Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Need help for my java game

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help for my java game

    Hello,i"ve created a game called rapid roll game using java but i am unabe to connect the notepad file to it which contains the games' high score and i also would like to know how to attach a help menu to my existing menu bar which when clicked on will give me info about the game,can u plz help me find the solution or give me any other idea as to how to go about this.

    Thanks in advance
    import java.awt.*;                      
    import java.awt.event.*;                
    import java.util.*;
    import java.io.*;
     
    class HighSc
    {
     
      int hsc;
      BufferedReader br;
      FileInputStream fis;
      String sths;
      public String getHighScore()  throws IOException
      {
         fis = new FileInputStream("highscore.txt");
         br = new BufferedReader(new InputStreamReader(fis));
     
         sths = br.readLine();
     
         fis.close();
         return sths;
         }
     
     }
     
     
    class GameFrame extends Frame implements  Runnable, KeyListener
    ,ActionListener
    {
     
     
       MenuBar mb;
       Menu m1;
       MenuItem mi1,mi2,mi3,mi4,mi5,mi6;
     
        Button b1,b2,b3;
        Button ball;
        Button oneUp;
     
        final int TOPLINE = 50;
        final int BOTTOMLINE = 350;
        final int LEFTLINE = 20;
        final int RIGHTLINE = 350 ;
     
        int scrollSpeed = 25;
     
        int x=50, y=300;
        int x2=125,y2=200;
        int x3 = 90,y3=100;
        int bx = 80;
        int by = 239;
        int ox = 0;
        int oy = 0;
        int score = 0;
        int oneUpCount = 1;
     
        int  t1,t2,t3;
     
        int chances=3;
        int a,b,c;
        int diff = 65;
        String msg = "";
        String chns = "";
        String st = "";
     
     
        char ch;
        int kcode;
        boolean flagLKey = true;
        boolean flagRKey = true;
        boolean flagTop1 = false;
        boolean flagTop2 = false;
        boolean flagTop3 = false;
        boolean flagDrop = true;
        boolean flagMove = true;
        boolean flagBetween = true;
        boolean flagOnx = true;
        boolean flagOnx2 = false;
        boolean flagOnx3 = false;
        boolean flagJump = true;
        boolean flagNew1 = false;
        boolean flagOneUp = false;
     
        Thread t;
     
        GameFrame()
        {
     
     
         mb = new MenuBar();
         m1  = new Menu("File");
         mi1 = new MenuItem("New Game");
         mi2 = new MenuItem("HighScores");
         mi3 = new MenuItem("Exit");
     
         setMenuBar(mb);
         mb.add(m1);
     
         m1.add(mi1);
         m1.add(mi2);
         m1.add(mi3);
     
     
     
            setTitle("simple frame");
            setSize(400,350);
     
            setLayout(null);
            b1 = new Button("");
            b2 = new Button("");
            b3 = new Button("");
            ball = new Button("o");
            oneUp = new Button("0");
            add(ball);
            add(b1);
            add(b2);
            add(b3);
            add(oneUp);
            b1.setBounds(x,y,70,20);
            b2.setBounds(x2,y2,70,20);
            b3.setBounds(x3,y3,70,20);
     
            ball.setBounds(bx,by,10,10);
            oneUp.setBounds(bx-5,by,5,5);
     
            setBackground(Color.blue);
            setForeground(Color.white);
            ball.addKeyListener(this);
     
            mi1.addActionListener(this);
            mi2.addActionListener(this);
    	mi3.addActionListener(this);
     
             addWindowListener(new WindowAdapter()
             {
                 public void windowClosing(WindowEvent we)
                 {
                      closeAll();
                    }
                });
     
              t = new Thread(this,"game");
              t.start();
     
          }  //end of constructor
     
          public void actionPerformed(ActionEvent ae)
          {
              st = ae.getActionCommand();
              if(st.equals("New Game"))
              {
     
                  stop();this.setVisible(false);
                  Frame ng = new GameFrame();
                  ng.setVisible(true);
     
                 }
              else if(st.equals("HighScores"))
              {
     
    	     HighSc hs = new HighSc();
                 try
                 {
                   msg = "";
                   msg  =  hs.getHighScore(); repaint();
                   }
                 catch(IOException ie)
                 {
    		System.out.println(ie);
                   }
    	    }
              else if(st.equals("Exit"))
              {
                  closeAll();
    	     }
     
            }
     
          public void closeAll()
          {
     
            stop();
            t = null;
             System.exit(0);
     
            }
     
          public void keyPressed(KeyEvent ke)
          {
                kcode = ke.getKeyCode();
     
     
     
     
                 switch(kcode)
                 {
     
                    case KeyEvent.VK_LEFT:
                                           if(flagDrop)
                                              bx -= 4;
     
     
     
     
     
    if(ox<=bx+2&&ox>=bx&&(oy>=by&&oy<=by+6))
                                           {
    					 chances++;ox = 0;oy = 0;
    					  repaint(); flagOneUp = false;
    					 }
     
    		     while(flagLKey)
                         {
     			 if(((bx>=x&&bx<=x+70)&&(by<y&&by>=y-11))
    		           ||((bx>=x2&&bx<=x2+70)&&(by<y2&&by>=y2-11))
    			   ||((bx>=x3&&bx<=x3+70)&&(by<y3&&by>=y3-11)))
     
    	   		 {
                 	             flagBetween = true;
                                 bx  -= 2; flagDrop = false;
                               }
                            else
    			{
    			    flagDrop = true;
                 		    flagBetween = false;
                                if(flagOnx)
                                   flagOnx = false;
                                else if(flagOnx2)
                                   flagOnx2 = false;
                                else if(flagOnx3)
                                   flagOnx3 = false;
     
    				 break;
    			  }
     
                             repaint();  break;
     			}
     
     
                                                break;
     
                    case KeyEvent.VK_RIGHT:
                                            if(flagDrop)
    					  bx +=4;
     
     
    if(ox<=bx+2&&ox>=bx&&(oy>=by&&oy<=by+6))
    					{
    					   chances++;	ox = 0; oy = 0;
    					   repaint();	flagOneUp = false;
    					  }
     
                          while(flagRKey)
                          {
     
    			if(((bx>=x&&bx<=x+70)&&(by<y&&by>=y-11))
    		           ||((bx>=x2&&bx<=x2+70)&&(by<y2&&by>=y2-11))
    			   ||((bx>=x3&&bx<=x3+70)&&(by<y3&&by>=y3-11)))
     
    			{
    	                    bx  += 2;
                                flagBetween = true;
    			    flagDrop = false;
                                repaint(); break;
    			   }
                            else
     			 {
                                flagBetween = false;
                                flagDrop = true;
     
      			    if(flagOnx)
                                   flagOnx = false;
                                else if(flagOnx2)
                                   flagOnx2 = false;
                                else if(flagOnx3)
                                   flagOnx3 = false;
     
    			    break;
    			    }
                        	}
                           break;
                     }
     
              }
         public void keyReleased(KeyEvent ke)
          {
               if(bx<LEFTLINE)
                {
                   flagLKey = false;
                   flagRKey = true;
                  }
               else if(bx>BOTTOMLINE)
               {
                   flagRKey = false;
                   flagLKey = true;
                 }
               else
               {
                   flagLKey = true;
                   flagRKey = true;
                  }
     
     
              }
     
          public void keyTyped(KeyEvent ke)
          {
     
              }
     
         public void flagDropFun()
         {
     
     
                while(flagDrop)
                {
    	       by += 1;
     
                  if(((bx>=x&&bx<=x+70)&&(by<y&&by>=y-11))
    	 	           ||((bx>=x2&&bx<=x2+70)&&(by<y2&&by>=y2-11))
    			   ||((bx>=x3&&bx<=x3+70)&&(by<y3&&by>=y3-11)))
                   {
     
                     flagDrop = false;
    		 flagBetween = true;
     
     
    if(!flagOnx2&&!flagOnx3&&(bx>=x&&bx<=x+70)&&(by<y&&by>=y-11))
    		 {
    		     flagOnx = true;	flagJump = true;score +=5;
    		     flagOnx2 = false;
    		     flagOnx3 = false;
    		   }
    		 else 
    if(!flagOnx&&!flagOnx3&&(bx>=x2&&bx<=x2+70)&&(by<y2&&by>=y2-11))
    		 {
     
    		     flagOnx2 = true; 	flagJump = true; score += 5;
                         flagOnx  = false;
    		     flagOnx3 = false;
                       }
                     else
    if(!flagOnx&&!flagOnx2&&(bx>=x3&&bx<=x3+70)&&(by<y3&&by>=y3-11))
                     {
    		     flagOnx3 = true;   flagJump = true; score +=5;
                         flagOnx  = false;
                         flagOnx2 = false;
                       }
                     else
                      {
                         flagOnx = false;
                         flagOnx2 = false;
                         flagOnx3 = false;
     
                        }
     
                     }
     
                   repaint(); break;
                 }
     
           }
     
     
           public void run()
           {
                try
                {
     
                 while(flagMove)
                 {
     
                   while(flagDrop)
    	       {
    		  flagDropFun();break;
    		 }
     
     
                  a = (int)(Math.random()*100);
    	      b = (int)(Math.random()*100);
    	      c = (int)(Math.random()*10);
     
     
                  if(y<TOPLINE||y2<TOPLINE||y3<TOPLINE)
                  {
    		oneUpCount++;
    		if(oy<TOPLINE)
    		{
    		     ox=0; oy =0;     flagOneUp = false;
    	          }
                    }
     
                    if(y<TOPLINE)
                     {
    	                     x = a+b+c;
                                 y = y2+100;
     
     
    	            if(by<TOPLINE-11||flagNew1)
                         {
                             bx = x+30;
    			 by = y-11;
    			 flagNew1 = false;
    			 flagOnx = true;
                             flagBetween = true;
     
    			}
     
     
    		     }
     
                      else if(y2<TOPLINE)
                      {
     
                                 x2 = a+b+c;
                                 y2 = y3+100;
     
     
     
                        if(by<39||flagNew1)
                         {
                             bx = x2+30;
    			 by = y2-11;
    		         flagNew1 = false;
                             flagOnx2 = true;
    			 flagBetween = true;
     
    			}
     
                         }
     
                      else if(y3<TOPLINE)
                       {
                                 x3 = a+b+c;
                                 y3 = y+100;
     
     
     
                        if(by<39||flagNew1)
                         {
                             bx = x3+30;
    			 by = y3-11;
    			 flagOnx3 = true;
    			 flagNew1 = false;
      			 flagOnx3 = true;
    			 flagBetween = true;
    			}
     
                         }
                          y -= 1;
                          y2 -=1;
                          y3 -= 1;
     
                       if(flagOneUp)
    		   {
    		      if(t2==1)
                          {
    			ox = x+t2+t3;
    			oy = y-5;
    			}
     
    		      else if(t2==2)
                         {
    			ox = x2+t2+t3;
    			oy = y2-5;
     
    			}
    		      else if(t2==3)
                          {
    			ox = x3+t2+t3;
    			oy = y3-5;
     
    			}
     
    		      }
     
    		    if(oneUpCount==7)
                        {
    			t1 =(int)(Math.random()*10);
    			t3 = (int)(Math.random()*10);
    			t2 = (int)(t1/3)+1;
    			flagOneUp = true;
    			oneUpCount = 1;
    	              }
     
                        if(by<TOPLINE)
                        {
     
                               if(by<TOPLINE-10)
                               {
                                 t.sleep(100);
    			     chances--;
                                 }
                               if(chances==0)
    			   {
                                  msg = "Game Over"; repaint();
                                  stop();
    			     }
                         }
     
     
     
                    if((flagOnx&&flagBetween))
                    {
     
                      if(!flagLKey&&!flagRKey)
                      {
                         bx = x+30;
                        }
                       by = y-11;
                       flagJump = false;
     
                      }
     
                    else if((flagOnx2&&flagBetween))
                    {
                     if(!flagLKey&&!flagRKey)
    		    bx = x2+30;
                      by = y2-11;
                      flagJump = false;
     
                      }
                    else if(flagOnx3&&flagBetween)
                    {
    		  if(!flagLKey&&!flagRKey)
    	             bx = x3+30;
                      by = y3-11;
                      flagJump = false;
     
                      }
     
                   while(flagDrop)
                    {
                        by += 1;
    		    if(by>BOTTOMLINE)
                        {
                            chances--;
    			flagNew1 = true;
    			flagDrop = false;
                            t.sleep(100);
    			if(chances == 0)
    			{
                              msg = "Game Over"; repaint();
                                  stop();
                              }
     
                           }
    		    repaint(); break;
                      }
     
           /*             if(score%50==0)
    		    {
    			scrollSpeed -= 3; score += 5;
                          }	     */
     
    	            repaint();
     
                        Thread.sleep(scrollSpeed);
     
                     } //end of while flagMove
     
                   }
                  catch(InterruptedException ie)
                  {
                       System.out.println(ie);
                     }
     
               }
              public void stop()
              {
                    flagMove = false;
                    t = null;
                  }
     
           public void paint(Graphics g)
            {
                 b1.setBounds(x,y,70,20);
                 b2.setBounds(x2,y2,70,20);
                 b3.setBounds(x3,y3,70,20);
                 ball.setBounds(bx,by,10,10);
                 oneUp.setBounds(ox,oy,5,5);
    	     g.drawString("Score :"+score,280,65);
    	     g.drawString("Chances :"+chances,280,75);
     
                 g.drawString(msg,100,100);
     
               }
     
     } // end of  BFrame
     
    public class RapidRollGame
    {
        public static void main(String []args)
         {
            Frame f1 = new GameFrame();
            f1.setVisible(true);
     
            }
     
      }
    Last edited by blunderblitz; March 2nd, 2010 at 07:03 AM.


  2. #2
    Junior Member
    Join Date
    Mar 2010
    Location
    Home-Ujjain /Job-Mumbai
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help for my java game

    hi dear, Please follo this link to know how reading and writing text file
    Java Practices -> Reading and writing text files

    adding help menu is same as you have added file menu
    Menu m2=new Menu("Help");
    mb.add(m2);
    and on click of these menu open a new frame like
    class HelpFrame extends Frame
    {
    //Here you can add a items for helping
    }
    good luck
    Programmer

Similar Threads

  1. Programmer for a Java based game project
    By Takkun in forum Project Collaboration
    Replies: 4
    Last Post: June 14th, 2010, 05:47 PM
  2. Java 2D game...Floor and Monsters?
    By DarrenReeder in forum Java Theory & Questions
    Replies: 0
    Last Post: February 19th, 2010, 08:44 AM
  3. Setting up the Floor for 2d JAva Game?
    By DarrenReeder in forum Java Theory & Questions
    Replies: 1
    Last Post: January 19th, 2010, 06:36 AM
  4. Java hangman game help...
    By AnotherNoob in forum AWT / Java Swing
    Replies: 16
    Last Post: December 4th, 2009, 11:17 PM
  5. Help with 1st Java applet game!
    By Sneak in forum Java Applets
    Replies: 0
    Last Post: November 28th, 2009, 11:20 AM