Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (741)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (823)
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 4234 times)
0 Members and 1 Guest are viewing this topic.
Offline hapsburgabsinth

Junior Devvie

« 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.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

Junior Devvie

« 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 :
  Graphics2D g2D = (Graphics2D)this.backBuffer.getGraphics();

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


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


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

Junior Devvie

« 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 Devvie

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: 506
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  

xxMrPHDxx (12 views)
2017-11-21 16:21:00

xxMrPHDxx (10 views)
2017-11-21 16:14:31

xxMrPHDxx (10 views)
2017-11-21 16:10:57

Ecumene (113 views)
2017-09-30 02:57:34

theagentd (148 views)
2017-09-26 18:23:31

cybrmynd (246 views)
2017-08-02 12:28:51

cybrmynd (242 views)
2017-08-02 12:19:43

cybrmynd (242 views)
2017-08-02 12:18:09

Sralse (257 views)
2017-07-25 17:13:48

Archive (875 views)
2017-04-27 17:45:51
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51 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‑
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!