This site requires JavaScript, please enable it in your browser!
Greenfoot back
VideoGame
VideoGame wrote ...

2014/1/5

Hegonal Map

VideoGame VideoGame

2014/1/5

#
Hello everybody, My friend and I are new on Greenfoot. We decided to code a videogame about ''Kingdom'', a board game, for school. The particularity of Kingdom is the fact that the map is made by many little hexagons and we don't find how to code this. We thought to draw an hexagon but we don't know what to do after that. Thank you in advance for your help and excuse us if our english isn't really good. VideoGame...
michaelbc03 michaelbc03

2014/1/5

#
public AnimatedImage(String fileName, int noFrames, int frameWidth, int frameHeight)
{
  GreenfootImage image = new GreenfootImage( fileName );

  this.frameWidth = frameWidth;
  this.frameHeight = frameHeight;
  this.noFrames = noFrames;

  frames = new GreenfootImage[noFrames];
  int frame = 0;
  for (int y = 0; y < image.getHeight()-1; y += frameHeight) {
    for (int x = 0; x < image.getWidth()-1; x += frameWidth) {
      frames[frame] = new GreenfootImage( frameWidth, frameHeight );
      frames[frame].drawImage( image, -x, -y );
      frame++;
    }
  }
}
VideoGame VideoGame

2014/1/5

#
Thanks a lot for your answer. We will try to understand and code our map. =) Cordially, VideoGame
danpost danpost

2014/1/6

#
I came up with the following:
import greenfoot.*;

public class HexWorld extends World
{
  public HexWorld()
  {
    super(35, 30, 20);
    int size = getCellSize()*2; // hexagon width and height
    // create hexagon image
    int[] xs = { size/4, size*3/4, size, size*3/4, size/4, 0 },
          ys = { 0, 0, size/2, size, size, size/2 };
    GreenfootImage image = new GreenfootImage(size+1, size+1);
    image.drawPolygon(xs, ys, 6);
    // fill world background image with hexagons
    for (int i=0; i<getWidth(); i++)
      for (int j=0; j<getHeight(); j++)
        if ((i%2+j%3+(j/3%2))%2 == 0)
          getBackground().drawImage(image, i*size*3/4, j*size/2);
  }
}
However, the grid does not match with the hexagons. Then I got this:
import greenfoot.*;

public class HexWorld extends World
{
    public HexWorld()
    {
        super(700, 600, 1);
        int size = 40;
        int[] xs = { size/4, size*3/4, size, size*3/4, size/4, 0 },
              ys = { 0, 0, size/2, size, size, size/2 };
        GreenfootImage image = new GreenfootImage(size+1, size+1);
        image.drawPolygon(xs, ys, 6);
        for (int i=0; i<getWidth()-size*3/4; i += size*3/4)
            for (int j=20*((i/(size*3/4))%2); j<getHeight()-size*3/4; j += size)
        {
            getBackground().drawImage(image, i, j);
        }   
    }
}
VideoGame VideoGame

2014/1/7

#
Thank you for your answers, We will decide together wich codes to use. Cordially, VideoGame... =)
You need to login to post a reply.