Results 1 to 13 of 13
  1. #1
    BaLLiN is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default Need Help with my Game

    I'm trying to make it so multiple stars show up at the top but the problem is I don't know how too unless I hard code the game completely. Eventually I'll make the stars fall down bu I want to get multiple stars to show up first. I think you have to use arrays but I'm not too good with them. I'm very new to java so help is appreciated.

    Java Code:
    import javax.swing.*;
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Random;
    
    public class FallingStars extends JApplet implements MouseListener, ActionListener
    {
    	//Variables
    	Timer time;
    
    	Image NinjaStar;
    	Image Ninja;
    	Image Dojo;
    
    	Random g;
    
    	myKeyBoard move;
    
    	int StarX, StarY;
    	int NinjaX = 475, NinjaY = 380;
    	int MouseX, MouseY;
    
    	int score;
    	int starz;
    
    	Star star;
    
    	public void init()
    	{
    		g = new Random();
    		time = new Timer(10, this);
    
    		star = new Star();
    
    		StarX = star.getX();
    		StarY = star.getY();
    
    		NinjaStar = getImage(getDocumentBase(), "ninjastar.gif");
    		Ninja = getImage(getDocumentBase(), "square.png");
    		Dojo = getImage(getDocumentBase(), "dojo.jpg");
    
    		setSize(950, 430);
    		setContentPane(new DrawingPanel());
    		setLayout(null);
    
    		addMouseListener(this);
    		move = new myKeyBoard();
    		addKeyListener(move);
    	}
    
    	public class myKeyBoard implements KeyListener
    	{
    		public void keyTyped(KeyEvent e){}
    		public void keyReleased(KeyEvent e){}
    		public void keyPressed(KeyEvent e)
    		{
    			if(e.getKeyCode() == KeyEvent.VK_LEFT) //Left
    			{
    				NinjaX = NinjaX - 5;
    			}
    			if(e.getKeyCode() == KeyEvent.VK_RIGHT) //Right
    			{
    				NinjaX = NinjaX + 5;
    			}
    
    			repaint();
    		}
    	}
    
    	public void actionPerformed(ActionEvent e)
    	{
    		Object obj = e.getSource();
    
    		if (time == obj)
    		{
    
    		}
    
    		repaint();
    	}
    
    	public void mousePressed (MouseEvent e){}
    	public void mouseClicked (MouseEvent e)
    	{
    		MouseX = e.getX();
    		MouseY = e.getY();
    	}
    
    	public void mouseReleased (MouseEvent e){}
    	public void mouseEntered (MouseEvent e){}
    	public void mouseExited (MouseEvent e){}
    
    	public class DrawingPanel extends JPanel
    	{
    		public void paintComponent(Graphics p)
    		{
    			super.paintComponent(p);
    
    			p.drawImage(Dojo, 0, 0, this);
    			p.drawImage(NinjaStar, StarX, StarY, this);
    			p.drawImage(Ninja, NinjaX, NinjaY, this);
    
    
    			p.drawString("SCORE: " + score, 500, 10);
    		}
    	}
    }
    Java Code:
    import java.util.Random;
    
    public class Star
    {
    	int StarX;
    	int StarY;
    
    	Random g;
    
    	public Star()
    	{
    		g = new Random();
    	}
    	public int fall()
    	{
    		StarY = StarY - 5;
    		return StarY;
    	}
    	public int getX()
    	{
    		StarX = g.nextInt(900);
    		return StarX;
    	}
    	public int getY()
    	{
    		return StarY;
    	}
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,446
    Rep Power
    25

    Default

    What is supposed to happen when the applet is executed? I get a screen with text: SCORE 0 at the top
    and three images.

  3. #3
    BaLLiN is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default

    Well right now it should be just one star spinning at the top that picks a random X value every time. I want to have it so multiple stars show up without hard coding. They eventually will be falling and the ninja has to dodge them.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Use Random class to generate x, a random number. Then use a loop to generate x number of stars.

  5. #5
    BaLLiN is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default

    Can you show me how to do a loop here?

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Are you friggin serious????

    If you do not know how to write a loop then why are you attempting something so advanced? If you want to learn how to write a loop then there are thousands of books and online tutorials. Why should I waste my time repeating what they all say just for you?

  7. #7
    BaLLiN is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default

    I know how to do loops. Like what would I put in it?

    Java Code:
    for(int index = 0; index < stars; index++)
    				p.drawImage(NinjaStar, StarX, StarY, this);
    I tried this but it did not work but it probably because I'm looping the wrong thing.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,446
    Rep Power
    25

    Default

    I want to have it so multiple stars show up
    How many start? Is it a range say between 10 and 20 that is different every time or do you always want 14? If a range then the random number generator can help you select the number.

  9. #9
    BaLLiN is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default

    Well I want to get them to fall at different times and over time have more fall down at once as they progress in levels.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,446
    Rep Power
    25

    Default

    Divide the problem up into small steps and do them one at a time.
    First: display one star,
    then get it to move down.
    This will require a timer to change the position of the star and call repaint which will cause a call to paintComponent to draw the star at its new location.

  11. #11
    BaLLiN is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default

    I got that all done.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,446
    Rep Power
    25

    Default

    Now add a second star. Then a third etc

  13. #13
    Mr. Hyde's Avatar
    Mr. Hyde is offline Member
    Join Date
    Jun 2011
    Posts
    4
    Rep Power
    0

    Default

    are the falling stars going to have any pattern or progression as the game continues, It would seem that the longer you survive the falling stars the more stars you should have, perhaps for every given time increment you could ajust your random generator to create more stars making the game harder. just a suggestion.

Similar Threads

  1. Replies: 1
    Last Post: 06-02-2011, 09:29 AM
  2. 1st Game
    By linabestprogrammer in forum Java 2D
    Replies: 1
    Last Post: 09-05-2010, 10:32 PM
  3. Replies: 2
    Last Post: 08-05-2010, 04:49 AM
  4. game code for any game
    By deathnote202 in forum Java Gaming
    Replies: 4
    Last Post: 06-10-2010, 08:06 AM
  5. 2D strategy game or 2D war game
    By led1433 in forum Java 2D
    Replies: 5
    Last Post: 02-10-2009, 06:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •