CS 2261 Lab 04: States, Structs, and Pooling

$30.00

Category: Tags: , , , , , , You will Instantly receive a download link for .zip solution file upon Payment || To Order Original Work Click Custom Order?

Description

5/5 - (3 votes)

Provided Files
● main.c
● myLib.c
● myLib.h
● game.c
● game.h
Files to Edit/Add
● main.c
● game.c
● game.h
● Makefile
● .vscode
○ tasks.json
Instructions
In this lab, you will be completing several different TODOs, which will piece by piece, make
a simple Space-Invaders-like game. Each TODO represents a component of the game, and is
broken down into sub-TODOs. Your code will likely not compile until you complete an
entire TODO block, at which point the game should compile with the new component added
and working.
After you download and unzip the files, add your Makefile and your tasks.json, and then
compile and run it. At this point, you should see just a blank black screen. Complete the
TODOs in order, paying close attention to the instructions.
CS 2261 Fall 2020
TODO 1 – State Machine
● For our game to be user-friendly, we need to implement a state machine first. The
state machine for this assignment will have the following states:
○ START
■ Consists of a blank PINK screen (given macro in myLib.h)
■ The seed for the random number generator increases each frame spent in this
state
■ Pressing SELECT takes you to the GAME state, seeds the random number
generator, and calls initGame()
○ GAME
■ Consists of a blank black background with a game running on top
of it (we will add in the game in later TODOs)
■ Calls updateGame(), waitForVBlank(), and drawGame()
■ Pressing SELECT takes you to the PAUSE state
■ Pressing B takes you to the WIN state (for now)
■ Pressing A takes you to the LOSE state
○ PAUSE
■ Consists of a blank GOLD screen (given
macro in myLib.h
■ Pressing SELECT takes you to the GAME state without reinitializing the game
■ Pressing START takes you back to the START state
○ WIN
■ Consists of a blank green screen
■ Pressing SELECT takes you back to the START state
○ LOSE
■ Consists of a blank red screen
■ Pressing SELECT takes you back to the START state
CS 2261 Fall 2020
● To help you visualize, the state machine looks like this:
● TODO 1.0: In the main while loop, create the switch-case for the state machine.
● TODO 1.1: Below the already-written initialize function, make the functions for all of
your states with their respective goTo functions. Make sure that the states work like the
ones described above, and that you don’t do things every frame that only need to be done
once (like fillScreen)
● TODO 1.2: Now that you have made these functions, put the prototypes for them at the
top of main.c.
CS 2261 Fall 2020
● TODO 1.3: Call your goToStart function in the initialize function so that when the game
starts, the START state is first. If you haven’t already done so, call your state functions in
the state machine switch-case.
● Compile and run. You should be able to travel through all the states by pressing the
respective buttons. If not, fix this before going further.
TODO 2 – Cannon
● Now that we have our state machine set up, we can begin adding in the game. We will do
this in game.c and game.h, and call those functions in main.c (like we already are
with initGame(), updateGame(), and drawGame()). Let’s start by getting the cannon
working.
● TODO 2.0: In game.h, create the struct for the cannon, typedef-ed PLAYER. The
cannon should have a row, col, oldRow, oldCol, cdel, height, width, color, and a bulletTimer
(we will see the use for this later). All of the cannon members can be of type int except
color, which should be an unsigned short.
● UNCOMMENT 2.0: Below this, uncomment the line that declares the cannon.
● UNCOMMENT 2.1: In game.c, uncomment the line that declares the cannon here.
● UNCOMMENT 2.2: Below the drawBar function, uncomment initCannon(),
updateCannon(), and drawCannon().
● UNCOMMENT 2.3: Now that we have these cannon functions, uncomment the
prototypes in game.h
● UNCOMMENT 2.4: Uncomment initCannon() in initGame()
● UNCOMMENT 2.5: Uncomment updateCannon() in updateGame()
● UNCOMMENT 2.6: Uncomment drawCannon() and drawBar() in drawGame()
● Compile and run. When you enter the game state, you should see the cannon under the
red bar, and move the cannon left and right. If not, fix this before going further.
TODO 3 – Bullets
● Now that the cannon is moving, let’s give it a pool of bullets to shoot.
● UNCOMMENT 3.0: In game.c, uncomment the line that declares the pool of bullets
here.
● UNCOMMENT 3.1: Below the drawCannon function, uncomment initBullet(),
fireBullet(), updateBullet(), and drawBullet().
CS 2261 Fall 2020
● TODO 3.0: Complete initBullets() by initializing all the bullets with the following
values:
○ height – 2
○ width – 1
○ row – negative the bullet’s height
○ col – 0
○ oldRow – the cannon’s row
○ oldCol – the cannon’s col
○ rdel – negative 2
○ color – white
○ active – 0
● TODO 3.1: Complete the updateBullets() function. This takes in a pointer to a
bullet. If the bullet is inactive, the function does nothing. If it is active, it moves the bullet up
the screen. If the bullet goes off the top of the screen, make it inactive.
● UNCOMMENT 3.2: Now that we have these bullet functions, uncomment the prototypes
in game.h
● UNCOMMENT 3.3: Uncomment initBullets() in initGame()
● TODO 3.2: In updateGame(), call updateBullet() for each of your bullets.
● TODO 3.3: Complete fireBullet(). This should iterate through the bullets to find the
first inactive bullet in the pool and initialize it. When you are finished initializing, break
out of the loop. Initialize the bullet like so:
○ row – the cannon’s row
○ col – cannon col + (cannon width / 2) + (bullet width / 2)
■ This is the center of the cannon’s top
○ active – 1
○ erased – 0
● UNCOMMENT 3.4: In updateCannon(), uncomment fireBullet() so that they
actually fire
● TODO 3.4: Since pressing B fires a bullet, it should not also win the game every time you
press it. So, back in main.c, comment out the fact that pressing B wins the game.
● TODO 3.5: In drawGame(), call drawBullet() for all of the bullets.
● Compile and run. When you enter the game state, you should be able to fire bullets by
CS 2261 Fall 2020
pressing B. You should be able to see three at once if you fire quickly enough. If not, fix this
before going further
TODO 4 – Balls
● Now that the cannon is moving and shooting, let’s give it something to shoot at.
● TODO 4.0: Most of the code to make the balls work has already been written for you, so
in updateGame(), call updateBall() for each of your balls.
● TODO 4.1: In drawGame(), call drawBall() for all of the balls.
● Compile and run. When you enter the game state, you should be able to see several balls
bouncing around. When you pause and unpause, they shouldn’t have moved. If not, fix this
before going further. Every time you re-run the game, the starting positions of the balls
should be different. The positions may change only slightly, or may look to be the same if
you idle in the START state for the same duration of time each run. Ensure srand() is
working correctly by entering the GAME state immediately upon starting your game (i.e.
running your Project.gba file), and by entering the GAME state 30 seconds after starting
your game. If the starting position of the balls differ, you’re fine. If not, you aren’t using
srand() correctly.
TODO 5 – Collision
● Having all those balls bounce around isn’t very fun yet. Make it so that shooting all of
them allows you to win the game.
● TODO 5.0: In updateBall(), loop through all of the bullets. If an active bullet is
colliding with this ball, make both the bullet and the ball inactive, then decrement
ballsRemaining.
● TODO 5.1: In order for the cannon to be able to win the game, you need to go back into
main.c, and, in the game state function, transition to the WIN state if ballsRemaining is 0.
● Compile and run. When you enter the game state, you should be able to shoot the balls. If
a bullet hits a ball, they both disappear. If you shoot all the balls, the win state should begin.
If not, fix this.
At this point, you should be able to travel to all of the states, play the game, and win the
game. If so, then you are done.
Tips
● Review lecture and recitation material for how to implement a state machine and
CS 2261 Fall 2020
pooling
● Follow each TODO in order, and only move forward if everything is correct
Submission Instructions
Zip up your entire project folder, including all source files, the Makefile, and everything
produced during compilation (including the .gba file). Submit this zip on Canvas. Name
your submission Lab03_FirstameLastname, for example:“Lab03_FrankGallagher.zip”.