Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (492)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (556)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Generating random sprites  (Read 944 times)
0 Members and 1 Guest are viewing this topic.
Offline konvictboy85

Junior Newbie





« Posted 2013-08-16 11:12:23 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
package minigames;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class fallingball extends JPanel {
   
   
   int x = 250;
   int y = 0;
   
   private void moveBall() {
      y = y + 1;
     
     
   }
   

   public void paint(Graphics g){
      super.paint(g);
      Graphics2D g2d = (Graphics2D) g;
      g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
      g2d.setColor(Color.black);
      g2d.fillOval(x, y, 30, 30);
   }
   
   public static void main(String[] args) throws InterruptedException{
      JFrame f = new JFrame("Falling Ball");
      fallingball game = new fallingball();
      f.add(game);
      f.setSize(500, 500);
      f.setResizable(false);
      f.setLocationRelativeTo(null);
      f.setVisible(true);
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
      while (true) {
         game.moveBall();
         game.repaint();
         Thread.sleep(10);
      }
   }

}


only 1 ball is generated and it gets fallen down. i wanted to generate balls falling randomly and from different x co-ordinate in the frame how can i do that
Offline sirkarpfen

Junior Member


Medals: 2
Exp: 2 years



« Reply #1 - Posted 2013-08-16 12:27:58 »

1. This is kind of the wrong forum to post this kind of question.

2. You have to create a loop if you want more balls to fall down.

3. You have to randomly set the x-coordinate using maybe the java.util.Random class.

And last but not least: You don't begin a question with a bunch of code. Maybe next time start with a little introduction on what you try to accomplish...
Offline konvictboy85

Junior Newbie





« Reply #2 - Posted 2013-08-16 13:02:21 »

oh m sorry but can u tell me where can i get hep for those types of problems. and m sorry for unusual topic in the incorrect  place.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #3 - Posted 2013-08-16 13:15:02 »

Google Java Random and click on the link for the java documents site. Read up on it. Then learn the basic structure of a for loop. This is simple stuff, why did you get into graphics before learning the basics of the language?

Offline sirkarpfen

Junior Member


Medals: 2
Exp: 2 years



« Reply #4 - Posted 2013-08-16 13:18:02 »

Maybe someone of the dukes could move this post to the right forum? Smiley

I allready answered your questions. But it seems you kinda lack some java basics. Maybe you should start with that before trying to write games.

Here is an example for creating a random amount of balls with random x-coordinates:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
public class FallingBallTest {

      private int[] balls; // Array of x values for the various balls
     private int y;
      private int width;
      public boolean running = false;

      public FallingBallTest() {
            JFrame f = new JFrame("Falling Ball Test");
            f.add(this);
            f.setSize(500, 500);
            f.setResizable(false);
            f.setLocationRelativeTo(null);
            f.setVisible(true);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            width = this.getWidth();
            Random rand = new Random();
            balls = new int[rand.nextInt(10)];
            for(int i = 0; i < xCoords.length; i++)
            balls[i] = rand.nextInt(width);
            this.startGame();
      }

      public void startGame() {
            running = true;
            while(running) {
                  this.update();
                  this.repaint();
            }
      }

      public void update() {
            y++
      }

      public void paintComponent(Graphics g) {
            super.paint(g);
            Graphics2D g2d = (Graphics2D) g;
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.setColor(Color.black);
            for(int x : balls) {
                  g2d.fillOval(x, y, 30, 30);
            }
      }

      public void main(String[] args) {
            new FallingBallTest();
      }
}


I give no guarantee that it actually works, was just scribbling it here before i have to go Smiley.
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #5 - Posted 2013-08-16 13:25:03 »

The only thing I want to point out in your code so that the OP understands is that
1  
xCoords[]
is actually the array of balls. Sorry but it seemed like a very vague and not related name for an array of balls!

Offline sirkarpfen

Junior Member


Medals: 2
Exp: 2 years



« Reply #6 - Posted 2013-08-16 17:13:21 »

The only thing I want to point out in your code so that the OP understands is that
1  
xCoords[]
is actually the array of balls. Sorry but it seemed like a very vague and not related name for an array of balls!

Thanks for pointing that out, i changed it Smiley.
Offline jonjava
« Reply #7 - Posted 2013-08-16 19:31:46 »

I'd say you're missing a few layers of useful abstraction. In particular a separate ball object and a rough entity system which is just a fancy term of "a list of sprites" basically. Also you should separate some of your logic into their own functions, your game loop for instance.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
public class fallingball extends JPanel {
   
   // Make a list of balls
  List<Ball> balls = new ArrayList<Ball>();

   // separate the coordinates and move() function into its own Ball class
  class Ball {
      int x;
      int y;

      Ball(int x, int y) {
         this.x = x;
         this.y = y;
      }
   
      void move() {
         y = y + 1;
      }
   }

   public void paint(Graphics g){
      super.paint(g);
      Graphics2D g2d = (Graphics2D) g;
      g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
      g2d.setColor(Color.black);

      // go through every Ball in the list of balls and paint each one.
     for (int i = 0; i < balls.size(); i++) {
         Ball b = balls.get(i);
         g2d.fillOval(b.x, b.y, 30, 30);
      }
   }

   public void doGameLoop() {
      // go through every Ball in the list of balls and update each one.
     for (int i = 0; i < balls.size(); i++) {
         Ball b = balls.get(i);
         b.move();
      }
      game.repaint();
   }
   
   public static void main(String[] args) throws InterruptedException{
      JFrame f = new JFrame("Falling Ball");
      fallingball game = new fallingball();
      f.add(game);
      f.setSize(500, 500);
      f.setResizable(false);
      f.setLocationRelativeTo(null);
      f.setVisible(true);
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
      // Create a new random generator
     Random r = new Random();

      // create 10 balls at random places
     for (int i = 0; i < 10; i++) {
         int x = r.nextInt(100);
         int y = 0;

         // Create the ball
        Ball ball = new Ball(x,y);

         // add the newly created ball to the list of balls.
        balls.add(ball);
      }

      while (true) {
         doGameLoop();
         Thread.sleep(10);
      }
   }

}

Offline sirkarpfen

Junior Member


Medals: 2
Exp: 2 years



« Reply #8 - Posted 2013-08-16 20:04:47 »

I'd say you're missing a few layers of useful abstraction. In particular a separate ball object and a rough entity system which is just a fancy term of "a list of sprites" basically. Also you should separate some of your logic into their own functions, your game loop for instance.

I'd say the OP would need some more basic java, because that is what you learn when learning the basics of the language...
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #9 - Posted 2013-08-16 21:51:22 »

I agree. I almost would go as far to say that he doesn't know what classes are. Besides, for such a small project that extra ball class really isn't needed.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Andre Lopes
« Reply #10 - Posted 2013-08-17 01:58:56 »




Kon, First, lets make things easier.

Make a Ball Class.

The ball class must contain some things ;

int positionX,positionY;

and two methods at least

update()
render()

Then to make random Numbers , you can use Math.Random(..);

But since you probably isnt so familiar with java,
i recommend two channels :
http://www.youtube.com/user/dermetfan

and
http://www.youtube.com/watch?v=Hl-zzrqQoSE


Hope it helps :p
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #11 - Posted 2013-08-17 02:01:14 »

OP really isn't advanced enough to probably understand what you're talking about. Plus, if you would've read the other posts, this question has already been answered a few times.

Offline Andre Lopes
« Reply #12 - Posted 2013-08-17 02:24:48 »

OP really isn't advanced enough to probably understand what you're talking about. Plus, if you would've read the other posts, this question has already been answered a few times.

I was just trying to help in a simple"r" way.

Plus i put the video tutorials if you would've read my post fully. Tongue
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Nickropheliac (16 views)
2014-08-31 22:59:12

TehJavaDev (23 views)
2014-08-28 18:26:30

CopyableCougar4 (33 views)
2014-08-22 19:31:30

atombrot (42 views)
2014-08-19 09:29:53

Tekkerue (41 views)
2014-08-16 06:45:27

Tekkerue (35 views)
2014-08-16 06:22:17

Tekkerue (26 views)
2014-08-16 06:20:21

Tekkerue (37 views)
2014-08-16 06:12:11

Rayexar (73 views)
2014-08-11 02:49:23

BurntPizza (49 views)
2014-08-09 21:09:32
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!