|
@@ -1,3 +1,5 @@
|
|
1
|
+[English](#markdown-header-selection-structures-car-scrolling-game) | [Español](#markdown-header-estructuras-de-seleccion-juego-de-desplazamiento-de-carro)
|
|
2
|
+
|
1
|
3
|
#Estructuras de decisión - Juego de desplazamiento de carro
|
2
|
4
|
|
3
|
5
|
![main1.png](images/main1.png)
|
|
@@ -165,26 +167,154 @@ Utiliza "Entrega" en Moodle para entregar el archivo `work.cpp` que contiene el
|
165
|
167
|
|
166
|
168
|
# Selection Structures - Car Scrolling Game
|
167
|
169
|
|
|
170
|
+![main1.png](images/main1.png)
|
|
171
|
+![main2.png](images/main2.png)
|
|
172
|
+![main3.png](images/main3.png)
|
|
173
|
+
|
|
174
|
+When we want to solve a problem, most of the time there is one or more options that depend on certain conditions being met. Computer programs are written to solve problems, and should therefore have a structure that allows it to make decisions. In C++ the decision (or conditional) instructions are structured using if, else, else if or switch. These structures usually involve the use of relational expressions and logical operations. In today's laboratory experience you will practice the use of certain decision structures to complete the design of a car and obstacle collision game application.
|
|
175
|
+
|
|
176
|
+##Objectives:
|
|
177
|
+
|
|
178
|
+1. Use relational expressions and select appropriate logical operators to make decisions.
|
|
179
|
+2. Apply decision structures.
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+## Pre-Lab:
|
|
183
|
+
|
|
184
|
+Before arriving at the laboratory you should have:
|
|
185
|
+
|
|
186
|
+1. Reviewed the following concepts:
|
|
187
|
+
|
|
188
|
+ a. logical operators
|
|
189
|
+
|
|
190
|
+ b. if, else, else if, switch
|
|
191
|
+
|
|
192
|
+2. Studied the concepts and instructions for the laboratory session.
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+---
|
|
196
|
+
|
|
197
|
+---
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+##Car Scrolling Game
|
|
201
|
+
|
|
202
|
+Collision games consist of maneuvering an object in order to dodge incoming objects to avoid possible collision. The collision can lower the player's score, or stop the game. Sometimes, colliding with certain objects may increase the player's score. In the game from today's laboratory experience, the object the player controls is a car, the objects that cause the game to stop are pines, holes, among others, and the objects that add points to the score are flags.
|
|
203
|
+
|
|
204
|
+The controls for this game are simple: the up arrow moves the car up and the down arrow moves the car down. During gameplay, the game simulates the movement of the car towards the right using a simple method: moving the background with its obstacles and flags towards the left while the car stays in the same place. The player can move the car up or down to dodge obstacles and to capture the flags. If the car collides with an obstacle, the game ends. The player can continue by pressing the `Retry` button.
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+---
|
|
208
|
+
|
|
209
|
+---
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+## Laboratory Session:
|
|
213
|
+
|
|
214
|
+In this laboratory experience you will practice the use of mathematical expressions and condition structures to implement the car's collision detection against obstacles and flags.
|
|
215
|
+
|
|
216
|
+Your task is to complete the design of the game application.
|
|
217
|
+
|
|
218
|
+### Exercise 1: Familiarize yourself with the pre-defined functions
|
|
219
|
+
|
|
220
|
+The first step in this laboratory experience is to familiarize yourself with the pre-defined functions (methods) in the code. You will invoke some of these functions in the code you will complete to detect the collisions.
|
|
221
|
+
|
|
222
|
+**Instructions**
|
|
223
|
+
|
|
224
|
+1. Download the `Conditionals-CarScrollingGame` folder from `Bitbucket` using the terminal, moving to the directory `Documents/eip`, and writing the command `git clone http://bitbucket.org/eip-uprrp/conditionals-carscrollinggame`.
|
168
|
225
|
|
169
|
|
-# Objectives
|
|
226
|
+2. Load the Qt project `CarScrollingGame` by double clicking the `CarScrollingGame.pro` file found within the `Documents/eip/Conditionals-CarScrollingGame` directory on your computer.
|
170
|
227
|
|
171
|
|
-Throughout this exercise the students will practice:
|
|
228
|
+3. Configure the project. The project consists of various files. **You will only write code on the file** `work.cpp`**. You should not change anything on the other files.**
|
172
|
229
|
|
173
|
|
-* if/else
|
174
|
|
-* do/while
|
175
|
|
-* setter/getters
|
|
230
|
+4. You are going to need a few methods defined in the following files to create your code.
|
176
|
231
|
|
177
|
|
-## Concepts
|
|
232
|
+ * `car.h` y `car.cpp`: contains the definition of the class `Car`, the methods of the class and their declarations.
|
|
233
|
+ * `flag.h` y `flag.cpp`: contains the definition of the class `Flag`, the methods of the class and their declarations.
|
|
234
|
+ * `obtstacle.h` y `obstacle.cpp`: contains the definition of the class `Obstacle`, the methods of the class and their declarations.
|
|
235
|
+ * `play.h` y `play.cpp`: contains the definition of the class `Play`, the methods of the class and their declarations.
|
178
|
236
|
|
179
|
|
-This laboratory consist on a simple side scrolling game. The user controls a car on the left edge of the screen, that can be moved up and down. Obstacles and checkpoints are created on the right edge of the screen, and these scroll toward the user on the left edge. The user earns a score, shown at the bottom, and the game ends when the user hits an obstacle. This laboratory is an adaptation of the assigment presented by Dave Feinberg [1].
|
|
237
|
+Familiarize yourself with the methods in these files. Emphasize the following methods:
|
180
|
238
|
|
181
|
|
-## Exercise 1
|
|
239
|
+ * `Car::getYCar()`: Returns the $$Y$$ coordinate of the car's position on the track.
|
|
240
|
+ * `Flag::getXFlag()`: Returns the $$X$$ coordinate of the flag's position on the track.
|
|
241
|
+ * `Flag::getYFlag()`: Returns the $$Y$$ coordinate of the flag's position on the track.
|
|
242
|
+ * `Flag::hide()`: Hides the flag.
|
|
243
|
+ * `Obstacle::getXObstacle()`: Returns the $$X$$ coordinate of the obstacle's position on the track.
|
|
244
|
+ * `Obstacle::getYObstacle()`: Returns the $$Y$$ coordinate of the obstacle's position on the track.
|
|
245
|
+ * `Play::setScore(n)`: Receives an integer number and adds it to the game's total score.
|
|
246
|
+
|
|
247
|
+There isn't a `getXCar()` method because the car does not move on the $$X$$ axis.
|
|
248
|
+
|
|
249
|
+### Exercise 2: Complete the function to change the game's track.
|
|
250
|
+
|
|
251
|
+In this exercise you will use the C++ condition structure called **switch** to change the attributes of the track. You will complete the `setTrack` method that can be found in the `work.cpp` file. This method changes the environment of the track depending on the value of `track_type` that is given as parameter.
|
|
252
|
+
|
|
253
|
+The method `setTrack` receives a value of type `Track` that can be:
|
|
254
|
+
|
|
255
|
+ * `play::DAY` - for a day track
|
|
256
|
+ * `play::NIGHT` - for a night track
|
|
257
|
+ * `play::BEACH` - for a beach track
|
|
258
|
+ * `play::CANDYLAND` - for a candy track
|
|
259
|
+
|
|
260
|
+The track's attributes that can be changed are:
|
|
261
|
+
|
|
262
|
+ * the image on the track using the function `setTrackPixmap()`
|
|
263
|
+ * the image of the obstacles using the function `setObstaclesPixmap()`
|
|
264
|
+
|
|
265
|
+The `setTrackPixmap()` function is already defined and receives a variable of type `Track` that can be one of the following (**play::DAY**, **play::NIGHT**, **play::BEACH**, **play::CANDYLAND**).
|
|
266
|
+
|
|
267
|
+The `setObstaclesPixmap()` function is already defined and receives a variable of type `string` that can be one of the following (**"hole"**, **"cone"**, **"it"**, **"zombie"**, **"spongebob"**, **"patric"**, **"monster"**).
|
|
268
|
+
|
|
269
|
+**Instructions**
|
|
270
|
+
|
|
271
|
+To complete the `setTrack()` function:
|
|
272
|
+
|
|
273
|
+1. Change the image on the track according to the `Track` value received.
|
|
274
|
+
|
|
275
|
+2. Change the image of the obstacles using the `switch` selection structure such that the obstacles change according to the value received by `setTrack()`. If the type of the track that is received is:
|
|
276
|
+
|
|
277
|
+ * `play::DAY` - the obstacles are of type "hole" or "cone"
|
|
278
|
+ * `play::NIGHT` - the obstacles are of type "it" or "zombie"
|
|
279
|
+ * `play::BEACH` - the obstacles are of type "spongebob" or "patric"
|
|
280
|
+ * `play::CANDYLAND` - the obstacles are of type "monster"
|
|
281
|
+
|
|
282
|
+ With the options that have two possible obstacles use `rand() % 2` to randomly select between an obstacle or the other.
|
|
283
|
+
|
|
284
|
+### Exercise 3: Complete the function for collision with obstacles
|
|
285
|
+
|
|
286
|
+In this exercise you will complete the `obstacleCollision` method that can be found in the `work.cpp` file. The function receives an object of the `Obstacle` class and another object of the `Car` class, and should detect if there is a collision or not between the car and the obstacle. The function returns true if there is a collision between the car and an obstacle and false if there is no collision.
|
|
287
|
+
|
|
288
|
+To detect the collision, the function should ask for the coordinates of the obstacle and the car's $$Y$$ coordinate. Remember that the car does not move on the $$X$$ axis, since that coordinate is fixed and stored in a constant variable called `CARX`. The collision occurs if the obstacle has the same $$X$$ coordinate, and is a certain distance above and below the car's $$Y$$ coordinate as shown in Figure 1. The range of the distance of the car's center upwards and downwards is stored in the variable called `OBSTACLERANGE`.
|
|
289
|
+
|
|
290
|
+If a collision is detected, the function returns `true`, and if not the function should return `false`.
|
182
|
291
|
|
|
292
|
+---
|
|
293
|
+
|
|
294
|
+![ColisionC.png](images/ColisionC.png)
|
|
295
|
+
|
|
296
|
+**Figure 1.** The image shows the coordinates $$(CARX,Y)$$ that the obstacle should have for a collision to occur.
|
|
297
|
+
|
|
298
|
+---
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+### Exercise 4: Complete the function for collision with flags
|
|
302
|
+
|
|
303
|
+In this exercise you will complete the `flagCollision` method that can be found in the `work.cpp` file. The function receives an object of the `Obstacle` class and another object of the `Car` class, and should detect if there is a collision or not between the car and the flag. This function is very similiar to the function in Exercise 3, except that the function does not return a value. The actions that occur when a collision is detected are done inside the function.
|
|
304
|
+
|
|
305
|
+In this case if a collision is detected, the game's score should increase by 30 points using the `setScore` function and hide the flag using the `flag.hide()` function to create the illusion that the flag was picked up during the collision.
|
|
306
|
+
|
|
307
|
+---
|
|
308
|
+
|
|
309
|
+---
|
183
|
310
|
|
184
|
311
|
### Deliverables
|
185
|
312
|
|
186
|
|
-In the following text box, copy the code that you developed for the program. Remember to properly comment the code and use good indentation and variable naming practices.
|
|
313
|
+Use "Deliverables" in Moodle to hand in the `work.cpp` file that contains the function calls and changes you made to the program. Remember to use good programming techniques, include the name of the programmers involved, and document your program.
|
|
314
|
+
|
|
315
|
+---
|
|
316
|
+
|
|
317
|
+---
|
187
|
318
|
|
188
|
319
|
### References
|
189
|
320
|
[1] Dave Feinberg, http://nifty.stanford.edu/2011/feinberg-generic-scrolling-game/
|
190
|
|
-
|