Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  2d hack 'n' slash - Problem with drawing the game world right  (Read 1480 times)
0 Members and 1 Guest are viewing this topic.
Offline hapsburgabsinth

JGO n00b
*

Posts: 46



« on: 2011-09-16 16:05:25 »

So I've decided to make a 2d hack and slash tile game....

I've decided to go on with a model view control pattern for my "gui hook" into the game...

I can draw the game like snake would do. Gameworld static, and player moves around the screen.

I want to draw my game with the player fixed in the middle, and the world moving "under" the player.

until now my drawing method is like this:


public void draw(){
      Graphics g = getGraphics();
      Graphics bbg = this.backBuffer.getGraphics();
      BufferedImage image = null;

      bbg.setColor(Color.BLACK);
      bbg.fillRect(0, 0, windowWidth, windowHeight);
      Player player = game.getTileMap().getPlayer();

      

      for(int y = 0; y < 12; y++){
         for(int x = 0; x < 20; x++){
            Position position = new Position(x,y);
            //Draw tiles
            Tile tile = game.getTileMap().getTileAt(position);
            if(tile != null){
               image = tile.getImage();
            }

            //Draw items
            Item item = game.getTileMap().getItemAt(position);
            if(item != null){
               image = item.getImage();
            }

            //Draw units
            Unit unit = game.getTileMap().getUnitAt(position);
            if(unit != null && !(unit instanceof Player)){
               image = unit.getImage();
            }
            bbg.drawImage(image, x*40, y*40, this);
         }
      }
      bbg.drawImage(player.getImage(), player.getPosition().getX()*40, player.getPosition().getY()*40, this);

      g.drawImage(this.backBuffer, 0, 0, this);
   }

So my question is: How do I draw the world calculated from the player position, and not fixed like the code is now?
Offline h3ckboy

JGO Kernel
*****

Posts: 1644
Medals: 4



« Reply #1 on: 2011-09-16 16:13:27 »

what you have to do IIRC is that you have to offset the world.

so the player is alwyas in the center, but hwne you draw the world, u change the x and y according to the x and y of the player

pseudo code:

instead of:
bbg.drawImage(image, x*40, y*40, this);

do something like this:
bbg.drawImage(image,x*40- player.getPosition().getX()*20,y*40 -  player.getPosition().getY()*20,this);


this is just rough, not exact.

also though I suggest (although I dunno if it makes a dif) that you draw all the images in your for loop onto an image, and then draw that image (if that makes any sense)

also though, just google "how to do scrolling map"
Offline hapsburgabsinth

JGO n00b
*

Posts: 46



« Reply #2 on: 2011-09-16 16:38:48 »

Thx alot! I knew it was something with the player position somewhere, but couldn't figure out where, but pretty obvious actually Tongue Have tried to figure this out for days now Cheesy
Games published by our own members! Go get 'em!
Offline Bonbon-Chan

Sr. Member
**

Posts: 417
Medals: 14



« Reply #3 on: 2011-09-17 09:06:14 »

Another way to do it, is to do a translation at the beging :
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
  Graphics2D g2D = (Graphics2D)this.backBuffer.getGraphics();

...
 AffineTransform trans = g2D.getTransform(); // Allways good to backup

 g2D.translate(-camera.x*40,-camera.y*40);

  for(int y=camera.y;y<camera.y+12;y++)
 {
     for(int x=camera.x;x<camera.x+20;x++)
    {
       ...
    }
 }

  g2D.setTransform(trans);


By the way, the "Position position = new Position(x,y);" is "horrible"  Grin. You are creating 240 objects by frame for nothing. Create a method "Position.set(x,y);" and reuse the object.
Offline hapsburgabsinth

JGO n00b
*

Posts: 46



« Reply #4 on: 2011-09-17 09:53:10 »

Ill try this when I come back to "the office" Smiley Thx for the post, and can see the point in the position argument Smiley
Offline SmokeNWrite

JGO n00b
*

Posts: 41



« Reply #5 on: 2011-11-23 20:09:23 »

This isn't the way I would paint my graphics.

I use double buffering with the main panel being repainted with a worker thread a certain amount every second.

Just sayin'.
Offline ra4king

JGO Kernel
*****

Posts: 3131
Medals: 195


I'm the King!


« Reply #6 on: 2011-11-23 22:12:20 »

This isn't the way I would paint my graphics.

I use double buffering with the main panel being repainted with a worker thread a certain amount every second.

Just sayin'.
Please don't derail this thread. The way he's doing his painting is fine. Your way is not recommended since it is passive rendering Wink

Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.121 seconds with 20 queries.