Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (576)
games submitted by our members
Games in WIP (497)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Game Development / Newbie & Debugging Questions / Re: My First Java game uses too much CPU usage. Please Help !! on: 2011-11-26 05:22:02
I'm using Macbook Pro 13" (Early 2011 Model) and Here's my code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
 
public void RunIt(){
       final int FRAMES_PER_SECOND = 50;
        final int SKIP_TICKS = 1000 / FRAMES_PER_SECOND;
       long next_game_tick = System.currentTimeMillis();;
       long sleep_time = 0;
       while(true){
         
          gameUpdate();
           next_game_tick += SKIP_TICKS;
            sleep_time = next_game_tick - System.currentTimeMillis();
            if( sleep_time >= 0 ) {
                   try {   Thread.sleep(sleep_time);   } catch (InterruptedException e) { }
            }
            else {  }
       }

    }


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  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
  public void gameUpdate(){
         //Background Animation Control
      for (int i=0; i< backgnd.size(); i++){
         
          if(((Background) backgnd.get(i)).getY()<cont.getHeight()){
             ((Background) backgnd.get(i)).move();  
 
          }
          else{
             backgnd.add(new Background(0, -((Background) backgnd.get(i)).getImg().getHeight(cont) ));
             backgnd.remove(i);      
          }
       }
       
         //Cloud Animation Control
      for (int i=0; i< clouds.size(); i++){
          if(((Cloud) clouds.get(i)).getY()<cont.getHeight()){
             ((Cloud) clouds.get(i)).move();      
          }
          else{
             clouds.add(new Cloud((int)(Math.random()*getWidth()), -(int)(Math.random()*getWidth())));
             clouds.remove(i);
          }
       }
         //High Cloud Animation Control
      for (int i=0; i< highclouds.size(); i++){
          if(((HighCloud) highclouds.get(i)).getY()<cont.getHeight()){
             ((HighCloud) highclouds.get(i)).move();      
          }
          else{
             highclouds.add(new HighCloud((int)(Math.random()*getWidth()), -(int)(Math.random()*getWidth())));
             highclouds.remove(i);
          }
       }  
       
         //Bullet Speed Control
      Rectangle bllt;
       Rectangle enmy;
        for (int i=0 ; i < bullets.size(); i++){  
            if (((Bullet) bullets.get(i)).getY()>0){
               
                ((Bullet) bullets.get(i)).move();
                bllt = ((Bullet) bullets.get(i)).getBounds();
               
                //nested enemy loop
               for(int a=0; a < enemies.size(); a++){
                   enmy = ((Enemy) enemies.get(a)).getBounds();
                   
                   if(enmy.intersects(bllt)){
                      bullets.remove(i);
                     
                      enemies.remove(a);
                      enemies.add(new Enemy((int)(Math.random()*getWidth()), -(int)(Math.random()*getWidth())));              
                   }
                }
            }
            else {
               bullets.remove(i);        
            }
        }    
       
        for (int i=0; i< enemies.size(); i++){
          if(((Enemy) enemies.get(i)).getY()<cont.getHeight()){
             
             ((Enemy) enemies.get(i)).move();      
             
             
          }
          else{
             enemies.add(new Enemy((int)(Math.random()*getWidth()), -(int)(Math.random()*getWidth())));
             enemies.remove(i);
          }
       }
       
        //----
       
        //Ship Coordinating
       if(pressleft == true){
            ship.moveLeft();
        }
       
        if(pressright == true){
            ship.moveRight();
        }
       
        if(pressup == true){
            ship.moveUp();
        }
       
        if(pressdown == true){
            ship.moveDown();
        }
       
        if(pressx == true){          
           
        }        
       
        repaint();
    }
2  Game Development / Newbie & Debugging Questions / My First Java game uses too much CPU usage. Please Help !! on: 2011-11-25 15:31:29


Ok, I know this is a noob question, but I really can't figure out the way to fix this problem. My game is running fine on my computer but it heats my CPU a lot which it shouldn't. This is my first game I ever created and I started learning Java like 1-2 months ago so my previous experience in game programming is zero. I did some googling and I tried adding game loop to this and set the framerate at 50 fps (not quite sure I did it right or not). I assumed that the problem has to do something with the looping. Anyway, I'm waiting for some good suggestions.

Thanks

Here is the link to my game and code:
http://dl.dropbox.com/u/15738531/SkyFighter.zip

 
3  Java Game APIs & Engines / Java 2D / How can I add shapes automatically in paintComponent? on: 2011-09-01 00:51:46
Normally, When I want to draw something I do this:

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.fillRect(3, 7, 10, 10);
    g.fillOval(10,10,10,10);
}

But If I follow the way I did above I only have a limited number of shapes(Rect, Oval and nothing else).

I'm looking for something different, for example, whenever a method addStuff(x, y) has called, it draws "Stuff" automatically at the coordinate x and y.

I used to do this with the acm package and it was easy. Just like the code below.

for (int i = 0; i < NCIRCLES; i++) {
    double r = rgen.nextDouble(MIN_RADIUS, MAX_RADIUS);
    double x = rgen.nextDouble(0, getWidth() - 2 * r);
    double y = rgen.nextDouble(0, getHeight() - 2 * r);
    GOval circle = new GOval(x, y, 2 * r, 2 * r);
    circle.setFilled(true);
    circle.setColor(rgen.nextColor());
    add(circle);
}
As you can see, I can add as many circles as I want with the code above. But now I'm struck with doing the same thing without using acm package. I'm really new in Java game programming. Anyone have any idea Huh
Pages: [1]
 

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

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (12 views)
2014-04-15 18:08:23

BurntPizza (10 views)
2014-04-15 03:46:01

UprightPath (23 views)
2014-04-14 17:39:50

UprightPath (10 views)
2014-04-14 17:35:47

Porlus (27 views)
2014-04-14 15:48:38

tom_mai78101 (49 views)
2014-04-10 04:04:31

BurntPizza (107 views)
2014-04-08 23:06:04

tom_mai78101 (207 views)
2014-04-05 13:34:39

trollwarrior1 (176 views)
2014-04-04 12:06:45

CJLetsGame (182 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
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!