Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (600)
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  
  Wondering effeciency of my code.  (Read 1669 times)
0 Members and 1 Guest are viewing this topic.
Offline bgilb

Senior Newbie




Java games rock!


« Posted 2005-07-18 10:27:06 »

Okay so I'm new to alot of techniques in Java, such as OOP. My code is messy, execution of things are a bit wierd. Im wondering what way I could better organize my code so that it is easier to read and runs more effecient!

Please any tips are welcome!
(Paste is of my 2D tile engine)

Sorry if there is a bit much. Just want to make sure I am on the right track, and not doing any bad habits..

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  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
import java.net.*;
import java.io.*;
import java.util.*;
import java.lang.*;  
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class vanMain
{
   public static Game myGame;
   public static void main(String args[])
   {
      myGame = new Game();
      myGame.setSize(640,480);
      myGame.setVisible(true);
      myGame.setResizable(false);
      myGame.setTitle("Vandrel Online");
      myGame.mainGame();  
   }
}
class Game extends Frame implements KeyListener
{
   public Image dbImage = null;
   public Graphics dbg = null;
   
   ArrayList clientList = new ArrayList();
   
   Image[] tiles = new Image[80];
   
   TileManager tileMan = new TileManager();
   Layer map01;
   Layer map02;
   Layer map03;
   Layer map04;
   Player myPlayer = new Player();
   boolean gameOn=true;
   
   long startTime = 0;
   float currentTime=0;
   float speedFactor=0;
   float deltaTime=0;
   float lastTime=0;
   
   public void mainGame()
   {
      startTime=System.currentTimeMillis();
     
      this.addKeyListener(this);
      myPlayer.loadPlayerData();
//      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      loadMap("nicehouse.MAP");
      while(gameOn)
      {
         //try{
         //Thread.sleep(5);
         //}catch(Exception e){}
         myPlayer.movePlayer();
         this.repaint();  
      }
   }
   public void addMapOffset(int x, int y)
   {
      map01.offsetX+=x;
      map02.offsetX+=x;
      map03.offsetX+=x;
      map04.offsetX+=x;
     
      map01.offsetY+=y;
      map02.offsetY+=y;
      map03.offsetY+=y;
      map04.offsetY+=y;
   }
   public void subMapOffset(int x, int y)
   {
      map01.offsetX-=x;
      map02.offsetX-=x;
      map03.offsetX-=x;
      map04.offsetX-=x;
     
      map01.offsetY-=y;
      map02.offsetY-=y;
      map03.offsetY-=y;
      map04.offsetY-=y;
   }
   
   public void addNewPlayer(String name,int x,int y)
   {
      Client singleClient = new Client(name,x,y);
      clientList.add(singleClient);
   }
   public void update (Graphics Screen)
   {
      if (dbImage == null)
      {
         dbImage = createImage (this.getSize().width, this.getSize().height);
         dbg = dbImage.getGraphics ();
      }
      dbg.setColor (getBackground ());
      dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
      dbg.setColor (getForeground());
      paint (dbg);
      Screen.drawImage (dbImage, 0, 0, this);
   }
   public void paint(Graphics g)
   {
     
      g.setClip(0,0,640,480);
      g.setColor(Color.black);
      g.fillRect(0,0,640,480);
     
      map01.drawLayer(g);
      map02.drawLayer(g);
      myPlayer.drawPlayer(g);
      map03.drawLayer(g);
      if(map04.grid[myPlayer.corX][myPlayer.corY]==0)
         map04.drawLayer(g);
   }
   
   
   public void loadMap(String mapName)
   {
         try
         {
         BufferedReader Input = new BufferedReader(new FileReader(mapName));
         int newSize = Integer.parseInt(Input.readLine());
         map01 = new Layer(newSize,newSize,0);
         map02 = new Layer(newSize,newSize,0);
         map03 = new Layer(newSize,newSize,0);
         map04 = new Layer(newSize,newSize,0);
         for(int x = 0;x<=newSize-1;x++)
         for(int y = 0;y<=newSize-1;y++)
         {
            map01.grid[x][y]=Integer.parseInt(Input.readLine());
            map02.grid[x][y]=Integer.parseInt(Input.readLine());
            map03.grid[x][y]=Integer.parseInt(Input.readLine());
            map04.grid[x][y]=Integer.parseInt(Input.readLine());
            map01.isWall[x][y]=Integer.parseInt(Input.readLine());
         }
         Input.close();
      }catch(IOException e){}
     
   }
   
   public void keyTyped(KeyEvent e){}
   public void keyReleased(KeyEvent e)
   {
      //myPlayer.playerDir=0;
      //System.out.println("wee");
   }
   public void keyPressed(KeyEvent e)
   {
      if (e.getKeyCode() == KeyEvent.VK_LEFT)
      {
         if(myPlayer.playerDir==0)
            myPlayer.playerDir=1;
      }
      if (e.getKeyCode() == KeyEvent.VK_RIGHT)
      {
         if(myPlayer.playerDir==0)
            myPlayer.playerDir=2;
      }
      if (e.getKeyCode() == KeyEvent.VK_DOWN)
      {
         if(myPlayer.playerDir==0)
            myPlayer.playerDir=3;
      }  
      if (e.getKeyCode() == KeyEvent.VK_UP)
      {
         if(myPlayer.playerDir==0)
            myPlayer.playerDir=4;
      }
   }
}

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  
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
class Layer
{
   int[][] grid;
   int[][] isWall;
   int x,y,n;
   int offsetX;
   int offsetY;
   public Layer(int x, int y, int n)
   {
      this.n = n;
      Toolkit toolkit = Toolkit.getDefaultToolkit();
      this.x=x-1;
      this.y=y-1;
      grid = new int[x][y];
      isWall = new int[x][y];
     
   }
   public void drawLayer(Graphics g)
   {
      for(int x = 0;x<=this.x;x++)
      {
         for(int y = 0;y<=this.y;y++)
         {
            //System.out.println("drawing tile!");
            //switch(grid[x][y].tile_id)
            //{
               int tileNum=grid[x][y];
               if(tileNum!=0)
                  g.drawImage(vanMain.myGame.tileMan.tileImg[tileNum],offsetX+(x*20),offsetY+(y*20),null);
            //}
         }
      }
   }
}
class TileManager
{
   BufferedReader Input=null;

   Image[] tileImg = new Image[200];
   String[] tileList = new String[100];
   int numTiles;
   public TileManager()
   {
      Toolkit toolkit = Toolkit.getDefaultToolkit();
      String inVal="";
      int lcv=1;
      try
      {
         Input = new BufferedReader(new FileReader("tiles.lis"));
         while((inVal=Input.readLine()) != null)
         {
            tileList[lcv]=inVal;
            tileImg[lcv]= toolkit.getImage(inVal);
            System.out.println(inVal);
            lcv++;
         }
         numTiles=lcv;
   
      }catch(IOException e){}
   }
   
}

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  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
import java.util.*;
import javax.swing.*;
import java.io.*;
import java.lang.*;  
import java.awt.*;
class Player
{
   int speedX=0;
   int speedY=0;
   int x=0;
   int y=0;
   int corX=0;
   int corY=0;
   int playerDir=0;
   float moveAmnt=0;
   Image[] playerImg = new Image[4];
   public Player()
   {
   }
   public void movePlayer()
   {
      if(System.currentTimeMillis()-vanMain.myGame.startTime>=0)
      {
         //System.out.println("poo");
         int moveSpeed=2;
         //System.out.println(moveSpeed);
         if(playerDir==0 || moveAmnt==0)
         {
            corX=-1*(vanMain.myGame.map01.offsetX-320)/20;
            corY=(-1*(vanMain.myGame.map01.offsetY-240)/20)+1;
         }
         if(playerDir==1 && (vanMain.myGame.map01.isWall[corX-1][corY]==0))  
         {
            if(moveAmnt<=10)
            {
               vanMain.myGame.addMapOffset(moveSpeed, 0);
               moveAmnt++;
            }
            if(moveAmnt>=10)
               moveAmnt=0;
               
         }
         if(playerDir==2 && (vanMain.myGame.map01.isWall[corX+1][corY]==0))  
         {
            if(moveAmnt<=10)
            {
               vanMain.myGame.subMapOffset(moveSpeed, 0);
               moveAmnt++;
            }
            if(moveAmnt>=10)
               moveAmnt=0;
         }
         if(playerDir==3 && (vanMain.myGame.map01.isWall[corX][corY+1]==0))  
         {
            if(moveAmnt<=10)
            {
               vanMain.myGame.subMapOffset(0,moveSpeed);
               moveAmnt++;
            }
            if(moveAmnt>=10)
               moveAmnt=0;
         }
         if(playerDir==4 && (vanMain.myGame.map01.isWall[corX][corY-1]==0))  
         {
            if(moveAmnt<=10)
            {
               vanMain.myGame.addMapOffset(0,moveSpeed);
               moveAmnt++;
            }
            if(moveAmnt>=10)
               moveAmnt=0;
         }
         if(moveAmnt==0)
            playerDir=0;
         vanMain.myGame.startTime=System.currentTimeMillis();
      }
     
     
         


         //vanMain.myGame.map01.offsetY+=y;vanMain.myGame.map02.offsetY+=y;
         //vanMain.myGame.map03.offsetY+=y;vanMain.myGame.map04.offsetY+=y;
      //}
   }
   public int checkCollision()
   {
      int returnVar=0;
      int mapSize = vanMain.myGame.map01.x;
      Layer tempLayer = vanMain.myGame.map01;
      for(int x = 0;x<=mapSize;x++)
      {
      for(int y = 0;y<=mapSize;y++)
      {
         // check right side
            int tileLeft = x*20 - tempLayer.offsetX;
           
            System.out.println("Tiles left side: "+tileLeft+ "Players leftside: "+x);
            if((tempLayer.isWall[x][y]==1) && ((this.x+10+tempLayer.offsetX)>=tileLeft))
            {
               returnVar=1;
               break;
            }
         }
      }
      return returnVar;
   }
   public void loadPlayerData()
   {
      Toolkit toolkit = Toolkit.getDefaultToolkit();
      playerImg[0]= toolkit.getImage("player1.png");
      //playerImg[1]= toolkit.getImage("player2.png");
      //playerImg[2]= toolkit.getImage("player3.png");
      //playerImg[3]= toolkit.getImage("player4.png");
   }
   public void drawPlayer(Graphics g)
   {
      g.drawImage(this.playerImg[0],(vanMain.myGame.getWidth()/2),6+vanMain.myGame.getHeight()/2,null);
   }
}
class Client
{
   int x,y;
   public Client(String name, int x, int y)
   {
      this.x=x;
      this.y=y;
   }
}
Offline kevglass

« JGO Spiffy Duke »


Medals: 211
Projects: 24
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #1 - Posted 2005-07-18 10:35:03 »

First, your code isn't that bad at all, especially if OOP is completly new to you.

Second, at first glance one of the problems would appear to be your use of repaint(). The way you've currently got it setup you won't get regular updates because you're calling plain old repaint(). repaint() with no parameters asks the AWT layer to repaint "as soon as possible". If you ask for another repaint() before the thread has had a chance to action the first they just get compressed into one call.

While this works really nicely for GUI updates, for games it doesn't work well. You might consider using repaint(0) which tells the thread that you want a repaint with 0 milliseconds (i.e. now!). However, you might also consider moving to an accelerated canvas where the graphics are directly under control and you utilise the system's graphics hardware.

To toot my own trumper, here's a tutorial on accelerated graphics in Java2D -
http://www.cokeandcode.com/info/tut2d.html

Kev

Offline Soulfly32

Senior Newbie





« Reply #2 - Posted 2005-07-18 16:08:35 »

Another advice I can give to you is to comment your code.
So you know what happen at any line of the code.

_____________________________________
www.soulfly-design.de
www.soulflyhome.com
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Ask_Hjorth_Larsen

Junior Devvie




Java games rock!


« Reply #3 - Posted 2005-07-18 18:43:47 »

Not bad. Maybe a bit more empty lines between methods would serve to make it easier to navigate. Also, your keyPressed method could use a switch, or if-else chaining (this would hardly affect efficiency in this case, but in general it would probably be better). What's up with the String argument of the Client constructor (bottom)?
Offline bgilb

Senior Newbie




Java games rock!


« Reply #4 - Posted 2005-07-18 19:52:29 »

Oh the Client I am still working on.

Thanks for the comments guys!

Im thinking about removing the Game class, as it serves no real purpose and instead using the vanMain class as the main class,

would look something like:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
public class vanMain, extends Frame
{
       public static void main(String args[])
       {
              vanMain vm = new vanMain();
       }
       vanMain()
       {
              //init stuff goes here
              while(gameOn)
              {
                    this.repaint();
              }


       }
}
Offline Jeff

JGO Coder




Got any cats?


« Reply #5 - Posted 2005-07-19 03:42:52 »

One mreo comment.

If you really want to tune your code for best performance, start profiling!

There really is no substitute

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline bgilb

Senior Newbie




Java games rock!


« Reply #6 - Posted 2005-07-19 04:35:00 »

Yeah I took a look at JProfiler, I didn't really understand any of the data it gave me though Undecided
Offline Jeff

JGO Coder




Got any cats?


« Reply #7 - Posted 2005-07-19 05:42:23 »

Try the Netbeans profiler.

http://www.netbeans.org/servlets/NewsItemView?newsItemID=665

I think you'll find it much clearer and easier to use.

You'll need to install the MUstang preview and Netebans 4.1 to use it. Its all explaiend on that site.


Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline oNyx

JGO Coder


Medals: 2


pixels! :x


« Reply #8 - Posted 2005-07-19 19:23:16 »

The keyboard handling just asks for trouble Smiley
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
int [] controls = new int[0xFF];
[...]
public void keyPressed(KeyEvent ke)
   {
      controls[ke.getKeyCode()&0xff] = 1;
   }

   public void keyReleased(KeyEvent ke)
   {
      controls[ke.getKeyCode()&0xff] = 0;
   }


Moving the player can be than done as part of the logic like...

1  
2  
3  
int x=controls[KeyEvent.VK_RIGHT]-controls[KeyEvent.VK_LEFT];
int y=controls[KeyEvent.VK_DOWN]-controls[KeyEvent.VK_UP];
move(x,y);


If left and right are pressed at the same time they nullify each other etc.

弾幕 ☆ @mahonnaiseblog
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.

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

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

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

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

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

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

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

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

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

toopeicgaming1999 (29 views)
2014-11-26 15:20:08
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

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!