[java] am i doing right logic for putting tiles?(Me a newbie)

Started by
2 comments, last by farhanx 20 years, 1 month ago
hi! i am putting tiles in my games, well tiles are coming in horizontal way like aeroplane is moving upward and screen is coming downward, problem is every tiles comes with its full wide size so it seems so strange animation and i kind see smoothness ..... what would u all suggest to make my logic better?? here is the code..... main tiles draw function is drawmap and it will move the screen up and down if ill use keys .....but how can i make them smooth please help ....... import java.awt.*; import java.awt.event.*; import java.applet.*; import java.awt.image.*; public class tiles extends Applet implements Runnable, KeyListener{ public Image img[]=new Image[10]; Image a; Image bufImg; Graphics bufg; int WorldSizeX=10,WorldsizeY=47,qw=0; int scroll=10; int[][] map = { {2, 2, 2, 2, 2, 2, 2, 2, 2, 2}, {2, 1, 1, 1, 1, 1, 1, 1, 1, 2}, {2, 1, 1, 1, 1, 1, 1, 1, 1, 2}, {2, 1, 1, 1, 2, 2, 2, 1, 1, 2}, {2, 1, 1, 1, 2, 2, 2, 1, 1, 2}, {2, 1, 1, 1, 2, 2, 2, 1, 1, 2}, {2, 1, 1, 1, 1, 1, 1, 1, 1, 2}, {2, 1, 1, 1, 1, 1, 1, 1, 1, 2}, {2, 1, 1, 1, 1, 1, 1, 1, 1, 2}, {2, 1, 1, 1, 1, 1, 1, 1, 1, 2}, {2, 1, 1, 1, 1, 1, 1, 1, 1, 2}, {2, 2, 2, 2, 2, 2, 2, 2, 2, 2}, {2, 2, 2, 2, 2, 2, 2, 2, 2, 2}, {2, 3, 1, 1, 1, 1, 1, 1, 1, 2}, {2, 1, 3, 1, 1, 1, 1, 1, 1, 2}, {2, 3, 1, 1, 2, 2, 2, 1, 1, 2}, {2, 1, 3, 1, 2, 2, 2, 1, 1, 2}, {2, 1,3, 1, 2, 2, 2, 1, 1, 2}, {2, 3, 1, 1, 1, 1, 1, 1, 1, 2}, {2, 1, 3, 1, 1, 1, 1, 1, 1, 2}, {2, 1,3, 1, 1, 1, 1, 1, 1, 2}, {2, 3, 1, 1, 1, 1, 1, 1, 1, 2}, {2, 1,3, 1, 1, 1, 1, 1, 1, 2}, {2, 2, 3, 2, 2, 2, 2, 2, 2, 2},//24 {2, 1, 2, 2, 2, 2, 2, 2, 2, 2}, {2, 2, 1, 1,2, 3, 4,5, 6,7}, {2, 3, 3, 1, 1, 1, 1, 1, 1, 2}, {2, 4, 1, 1, 2, 2, 2, 1, 1, 2}, {2, 5, 3, 1, 2, 2, 2, 1, 1, 2}, {2, 6,3, 1, 2, 2, 2, 1, 1, 2}, {2, 3, 1, 1, 4, 1,4, 1, 1, 2}, {2, 1, 3, 1, 4, 1, 4, 1, 1, 2}, {2, 1,3, 1, 4, 1, 4, 1, 1, 2}, {2, 3, 1, 1, 4, 1, 4, 1, 1, 2}, {2, 1,3, 1, 1, 1, 1, 1, 1, 2}, {2, 2, 3, 2, 2, 2, 2, 2, 2, 2},//35 {3, 1, 2, 2, 2, 2, 2, 2, 2,3}, {3, 2, 1, 1,2, 3, 4,5, 6, 3}, {3, 3, 3, 1, 1, 1, 1, 1, 1,3}, {3, 4, 1, 1, 2, 2, 2, 1, 1,3}, {3, 5, 3, 1, 2, 2, 2, 1, 1,3}, {3, 6,3, 1, 2, 2, 2, 1, 1, 3}, {3, 3, 1, 1, 4, 1,4, 1, 1, 3}, {3, 1, 3, 1, 4, 1, 4, 1, 1,3}, {3, 1,3, 1, 4, 1, 4, 1, 1, 3}, {3, 3, 1, 1, 4, 1, 4, 1, 1,3}, {3, 1,3, 1, 1, 1, 1, 1, 1, 3}, {3, 3, 3, 3, 3,3,3, 3, 3, 3}//35 }; Thread th; /**Initialize the applet*/ public void init() { a=getImage(getCodeBase(),"tiles.gif"); // a=getImage(getCodeBase(),"skullbg.jpg"); loadMapSprite(3,3,img,a,1); addKeyListener(this); requestFocus(); if(th==null) { th=new Thread(); th.start(); } } /**Component initialization*/ public void paint(Graphics g) { drawmap(g); g.drawString(""+qw,11,10); //g.drawImage(img[0],30,30,this); // g.drawImage(img[6],22,33,this); } public void loadMapSprite (int sp_totalRows,int sp_totalCols,Image img[],Image a,int dec) { try { MediaTracker t = new MediaTracker(this); t.addImage(a,0); t.waitForID(0); int iw = a.getWidth(this); int ih =a.getHeight(this); int Tw = iw / sp_totalCols; int Th = ih / sp_totalRows; CropImageFilter f; FilteredImageSource fis; t = new MediaTracker(null); for(int cols=0; cols0; loop--) { for(int loop2=0; loop2<10; loop2++) {m=map[loop][loop2]; g.drawImage(img[m],x,y,this); x+=30; } x=0; y+=30; } }//end function public void run() { qw++; while(th!=null) { repaint(); try{ th.sleep(100); } catch(Exception e) { } } }//end function }//end class thanks in advance
Things has been changed but the just the way it is....
Advertisement
Embed your source properly and explain more clearly...
"That MSMAGNET program trashed my hard drive!"
Actully i cant see smoothness in vertical scrolling , while all rectanguler tiles are perfectly doing their work with drawmap funcion....or if u have any idea to tell me goodway displayimg tiles in vertical way as aeroplane is going upward.... above source code is working properly but without smoothness.
Things has been changed but the just the way it is....
Uh, I think I understand the question. You''ve got to clip the tiles. Check out the clipping stuff in the Graphics class. The clipping rectangle can be your viewport. Use setClipBounds to set the width and height to the screen dimensions and the x and y to the starting position. And then decrement the y a bit before you draw the tiles every frame. I think that should work, although I haven''t really tested it...

This topic is closed to new replies.

Advertisement