Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

COMP 1010-Assignment 4

Overview: Infinite Halls

Carlin, the hero of our story, is going through an infinite number of halls looking for the light of truth. This little champion (Carlin means little champion in Irish) faces a villainous giant spider in each hall. The spider’s mission is to catch Carlin and trap her in its poisonous webs to avoid the light of truth being revealed. Carlin tries to doge the spider and reach each hall’s door. But that’s not all; Carlin is equipped with a specific ability too. She can make ice and freeze a particular location of the hall. While she can only use her ability for a limited number of times, if Spider touches the frozen surface of the hall path, it makes the spider much slower and buys more time for Carlin to pass the hall(s).

In this assignment, you will create a game in which the hero character starts at a specific point, passes some blocks in a series of halls, avoids hitting the evil character and tries to reach the hall doors. The game levels up every time that the player reaches the door.  Each question builds on the previous. Ensure one question is working before moving on to the next.

The purpose of this assignment is to practice using functions with parameters and return types as well as nested loops. You must pass data to and from functions as described, and limit the use of global variables to data that must be remembered from one frame to the next. Global final constants are fine- you will need to create several.

Question 1: setup and background (5 marks)

Create the game scene like the one shown below.

 

You must include the following elements:

●   Two black spaces at the top and bottom of the Canvas. (In later questions these spaces will be used to display game information). You may want to store the size of the top black space in a final constant, such as HALL_TOP, as you need to use this value in various places.

●   Create a function - drawHall() - to draw the hall, including gray paths and brown obstacles. The hall includes 16 brown cells (obstacles) and the rest ofthe hall’s cells should be colored gray (paths). All hall’s cells have the same size; you can store this value as SPACING constant. Your function should use two global constants, NUM_ROWS and NUM_COLS and use nested loops to draw the repeated pattern. The idea (that is going to be implemented later) is that the two characters of the game should be able to move around the hall through the gray paths (a connected group of gray cells) but can’t stand/walk on/over the brown obstacles.

●   A function - drawCarlin() - to draw Carlin. At the start of each level, Carlin should be randomly placed at the bottom row of the Hall. Be innovative to create your own character but if you decide to follow the above snapshot, you can use the arc’ command to draw Carlin’s feet.

Every time that you run the game, Carlin’s location in the lowest row of the hall should be re-selected randomly. It should be noted that Carlin always should be centered in any cell that she is located in.

●   A function - drawSpider() - to draw the spider. Every time that you run the game and at the start of each level of the game, the spider should be placed at a random location limited to the top row of the hall.

●   A function - drawDoor() - to draw the door randomly at the top of the hall. The size of the door should not change during the game, but it’s location should randomly change at the start of each level. Pay attention that the door with it’s whole width should be within the canvas. You can store the left position of the door as doorX global variable, and the fixed values of the door width and height in DOOR_WIDTH and DOOR_HEIGHT global constants. The door is not necessarily aligned with one or multiple cells.

●   Hint 1: Considering that each row has 9 columns. You can create a function to randomly pick a column number and then use this single function to determine the random place of Carlin and Spider respectively in the bottom and top rows of the hall, at the start of each level.

●   Hint 2: You need to track the position of the two characters through the game, so consider global variables to store their x and y values, such as carlinX, carlinY, spiderX, and spiderY.

●   Hint 3: Assigning a special value to some of Carlin and spider’s location values at the start of the game as well as at the beginning of each level helps you to identify when you need to randomly place them in the bottom and top rows (for example carlinX=- 1 and spiderX=- 1). Instead of using void functions for drawCarlin() and drawSpider(), they could be int functions that accepts the carlinX or spiderX as input, and in case the value is - 1, update its value after random assignment.

At this point each time you run the code, you should see a fixed view of the hall while the locations of Carlin, the spider and the door are changed by each run.

Question 2: Move spider and Carlin (5 marks)

●   Create a function, namely moveSpider(), that moves the spider in the hall. How fast the spider can move depends on the spider’s speed. This speed will change through the game, so consider a global variable, spiderSpeed, to store this value. Here we measure the speed as the number of pixels the spider moves in each frame. The initial speed of the spider should be  10 pixels per frame. In each frame, your function should randomly select a direction (up, down, left, or right), check if the spider is not going out of canvas or on an obstacle, and move the spider in that direction based on the spider's speed.

To simplify this task, we assume that the center of the spider body, which can be recognized by spiderX and spiderY, should not pass the brown cells’ or hall’s borders.

●   Develop a code, to control Carlin's movements in four directions using  LEFT, RIGHT, UP, and DOWN arrow keys. Here you need the keyPressed() function.

●   Hint 1: Create a function - nextX() - to determine the next X position of either Carlin and the Spider, and call this function in the moveSpider() and keyPressed() functions. This function receives 4 input parameters - int currentX, int currentY, String direction (“left” or “right”), int speed - and returns the next int X position based on the currentX value, the direction, and the received speed.

●   Hint 2: Similarly, consider a function - nextY() - to determine the next Y position of either Carlin  and  the  Spider,  and  call  this  function  in  the  moveSpider()  and  keyPressed() functions. This function receives 4 input parameters too - int currentX, int currentY, String direction (“up” or down”), int speed - and returns the next int Y position based on the currentY value, the received speed, and the direction.

●   Hint 3: You can also consider another function - checkObstacle() - to check if there is an obstacle, shown with a brown cell in the hall, for a given set of X and Y values. This function receives two input parameters, (int positionX, int positionY) and returns a boolean value to specify obstacle status. You can use this function in nextX() and nextY() functions.

●   Hint 4: You should check to ensure that the two characters are not going out of the hall. In other words, the next position can not be outside of the halls’ borders. You can do that either in the checkObstacle() function or in nextY() and nextX().

●   Hint 5: Your keyPressed() function updates the global variables storing Carlin’s X and Y locations based on arrow keys (LEFT, RIGHT, UP, and DOWN).

●   Hint 6: The spider moves by small amounts (10 pixels per frame at the start of the game), unlike Carlin that jumps cell to cell.

By this point, when you run the code, you should see the hall, the spider, the door, and Carlin while the spider randomly moves through the hall and you can move the Carlin in the hall’s pathways using arrow keys on your keyboard.

Question 3: Display score and level up (5 marks)

Every time that Carlin arrives at the door the game level increases by one and for the next level the speed of the spider increases by 10 percent. As it mentioned before, for every new level, Carlin starts at a random cell in the bottom of the hall, the spider moves to a random cell at the top row, and the door location is changed randomly too .

If Spider catches Carlin the game will be ended and final game scores will be displayed.

●   Create a function to show the level at the top black box, such as Level: 2” .

●   Create a function, namely carlinAtTheDoor(), to check if Carlin arrives at the door. To consider Carlin being at the door, she should be in the top row under the door.

●   Create a function, levelUp(), to increase the level by one and the spider speed by 10 percent.

●   Hint 1: if Carlin reaches the door, restart the doorX, carlinX, spiderX and carlinY variables to be the selected special number (- 1 in our example) to ensure that their positions are going to be randomly changed.

●   Create a function to check if Spider catches Carlin. The spider catched Carlin if they are in the same cell. Use a global boolean variable called dead’ to track the state of the game.  The variable status should be updated in this function whenever the spider catches Carlin. ●   Create a function to draw the final score when Carlin dies and the game ends. This changes the canvas background and sets it to black and displays the level in the center of the canvas.

After finishing this question, you have everything from question2 plus the ability to leveling up when Carlin reaches the door; as well as randomly re-positioning the door, the spider, and Carlin after each level up, and also showing the last played level on the black screen whenever the spider catches Carlin.

 

 

Question 4: Add magic to the game (5 marks)

Carlin is equipped with enough mana at the start of the game that allows her to use her magical ability only 3 times through the whole game . Carlin's ability to create ice can be activated by pressing the Enter key, '\n', and it freezes the single cell that Carlin stands on. The magic reduces the spider's speed when it enters the frozen area.

●   Whenever the player hits the Enter key, if Carlin has still enough mana, the cell she stands on at the moment should be colored as light blue and the number of her remaining magical abilities should reduce by one. For this, you want to modify the drawHall() and keyPressed() functions.

●   Hint  1: Consider a global variable to track the number of Carlin’s remaining magical abilities. This should be initiated by 3 at the start of the game.

●   Hint 2: Only one cell can be frozen in each level at the time. It means you need only two global int variables to track the frozen place, such as frozenCol and frozenRow.

●   Hint 3: Pay attention that when Carlin continues to move through the hall, the frozen cell will remain in the same place and it’s not moving with Carlin. Do not simply recolor any cell that Carlin moves into.

●   Hint 4: When Carlin goes to the next level, there shouldn’t be any icy cells in the new hall. You can consider frozenCol = - 1 at the beginning of each level.

●   Create a function to check if Spider entered the frozen cell. If this happens, reduce the spider's speed to %25 of its previous speed. For example if the spider speed was 20, it should be decreased to 5 when the spider touches the ice.

●   Hint 5: The ice should disappear when the spider touches it. As a result, the ice only has effect once.

●   At the start of each level if the spider’s speed is below 10, increase it’s value to 10. In other words, at the beginning of each level, the spider speed is always greater or equal to 10. You need to modify the levelUp() function to include this rule.

●   Create a function to display the spider speed, and the number of remained magical abilities at the bottom black area as follows. It will help the player to decide the    best times to use the Carlin’s magic.

 

 

If you have completed the assignment and enjoyed working on it, take more challenges and advance your game. For example, add more magic powers or make the door size slightly smaller with every level up.