Skip to content

Step 5 - Calculated Size

House output

Lastly, let’s look at adding some constants and variables to make this more dynamic. We will let the user tell us what size the house should be, then calculate the positions for the shapes.

House plan illustration

  1. Start with positioning the walls. Determine what we need to calculate.

    We will need to determine:

    • the screen size. Create constants for SCREEN_WIDTH and SCREEN_HEIGHT.
    • the house size - which the user will provide via the terminal at the start
    • to read in house size we will need to read and convert data. So a userInput variable could be created for this.
    • the wall position (its x and y) - this is where we draw the wall.
  2. Code this up and test it out:

    • Add constants for screen width and height - Where else could you use these?
    • Add variables for houseSize and userInput - write the terminal code to read these in
    • Add variables for wallX and wallY - calculate these as noted above.
    • Compile and run and try different wall sizes.
    • As I coded this I realised I needed an extra constant - BOTTOM_GAP. When you find you are hard-coding values you should think if there is a meaningful name to describe the value. Naming the “bottom gap” helps make it clearer what that 100 was about, whereas the 2 does not need a different name. We are dividing by 2 to get half the value.

      using static System.Convert;
      using static SplashKitSDK.SplashKit;
      const int SCREEN_WIDTH = 800;
      const int SCREEN_HEIGHT = 600;
      const int BOTTOM_GAP = 100;
      int houseSize;
      string userInput;
      double wallX, wallY;
      WriteLine("Enter the size of the house: ");
      userInput = ReadLine();
      houseSize = ToInt32(userInput);
      wallX = (SCREEN_WIDTH - houseSize) / 2;
      wallY = SCREEN_HEIGHT - BOTTOM_GAP - houseSize;
      OpenWindow("House Drawing by Andrew", SCREEN_WIDTH, SCREEN_HEIGHT);
      ClearScreen(ColorWhite());
      FillEllipse(ColorBrightGreen(), 0, 400, SCREEN_WIDTH, 400);
      FillRectangle(ColorGray(), wallX, wallY, houseSize, houseSize);
      FillTriangle(ColorRed(), 250, 300, 400, 150, 550, 300);
      RefreshScreen();
      Delay(5000);
  3. Connect the roof. Ok, this may be a bit of a challenge for you. Have a go at coding the roof based on the plan above.

  4. Compile and run, then check your calculation if it doesn’t look right.

  5. Finally, adjust the code so that the size of the hill is calculated from the size of the screen. It is as wide as the screen, appears 2/3rds of the way down the screen, and is 2/3rds the height of the screen.

    Once you change this, try different screen sizes!

    • using static System.Convert;
      using static SplashKitSDK.SplashKit;
      const int SCREEN_WIDTH = 1280;
      const int SCREEN_HEIGHT = 720;
      const int HILL_HEIGHT = SCREEN_HEIGHT * 2 / 3;
      const int BOTTOM_GAP = 100;
      int houseSize;
      string userInput;
      int wallX, wallY;
      int roofLeft, roofMiddle, roofRight, roofTop;
      int roofOverhang, roofHeight;
      Write("Enter the size of the house: ");
      userInput = ReadLine();
      houseSize = ToInt32(userInput);
      wallX = (SCREEN_WIDTH - houseSize) / 2;
      wallY = SCREEN_HEIGHT - BOTTOM_GAP - houseSize;
      roofOverhang = houseSize / 4;
      roofHeight = houseSize * 3 / 4;
      roofLeft = wallX - roofOverhang;
      roofMiddle = wallX + houseSize / 2;
      roofRight = wallX + houseSize + roofOverhang;
      roofTop = wallY - roofHeight;
      OpenWindow("House Drawing by Andrew", SCREEN_WIDTH, SCREEN_HEIGHT);
      ClearScreen(ColorWhite());
      FillEllipse(ColorBrightGreen(), 0, HILL_HEIGHT, SCREEN_WIDTH, HILL_HEIGHT);
      FillRectangle(ColorGray(), wallX, wallY, houseSize, houseSize);
      FillTriangle(ColorRed(), roofLeft, wallY, roofMiddle, roofTop, roofRight, wallY);
      RefreshScreen();
      Delay(5000);

When you have this working you should feel confident that you can start working with graphical programs.