Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (497)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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  
  2d hack 'n' slash - Problem with drawing the game world right  (Read 2638 times)
0 Members and 1 Guest are viewing this topic.
Offline hapsburgabsinth

Senior Newbie





« Posted 2011-09-16 20: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 Coder


Medals: 5



« Reply #1 - Posted 2011-09-16 20: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

Senior Newbie





« Reply #2 - Posted 2011-09-16 20: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! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Bonbon-Chan

JGO Coder


Medals: 12



« Reply #3 - Posted 2011-09-17 13: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

Senior Newbie





« Reply #4 - Posted 2011-09-17 13: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

Junior Member


Projects: 1



« Reply #5 - Posted 2011-11-24 01: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


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #6 - Posted 2011-11-24 03: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]
  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.

BurntPizza (25 views)
2014-09-19 03:14:18

Dwinin (39 views)
2014-09-12 09:08:26

Norakomi (68 views)
2014-09-10 13:57:51

TehJavaDev (93 views)
2014-09-10 06:39:09

Tekkerue (47 views)
2014-09-09 02:24:56

mitcheeb (68 views)
2014-09-08 06:06:29

BurntPizza (51 views)
2014-09-07 01:13:42

Longarmx (38 views)
2014-09-07 01:12:14

Longarmx (44 views)
2014-09-07 01:11:22

Longarmx (40 views)
2014-09-07 01:10:19
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

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!