Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
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 04: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 14: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-08-31 22: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.

rwatson462 (38 views)
2014-12-15 09:26:44

Mr.CodeIt (31 views)
2014-12-14 19:50:38

BurntPizza (62 views)
2014-12-09 22:41:13

BurntPizza (99 views)
2014-12-08 04:46:31

JscottyBieshaar (60 views)
2014-12-05 12:39:02

SHC (74 views)
2014-12-03 16:27:13

CopyableCougar4 (77 views)
2014-11-29 21:32:03

toopeicgaming1999 (138 views)
2014-11-26 15:22:04

toopeicgaming1999 (127 views)
2014-11-26 15:20:36

toopeicgaming1999 (38 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!