Fix Code

// TODO: The score is not showing...
// TODO: Ensure the score increases when the user splats a fruit.

#include "splashkit.h"

using namespace std;

#define CHERRY 0
#define GOOSEBERRY 1
#define BLUEBERRY 2
#define POMEGRANATE 3
#define APRICOT 4
#define RASPBERRY 5
#define BLACKBERRY 6
#define STRAWBERRY 7
#define CURRANT 8
#define NUM_FRUIT 9

void load_resources()
{
load_bitmap("Cherry", "Cherry.png");
load_bitmap("Gooseberry", "Gooseberry.png");
load_bitmap("Blueberry", "Blueberry.png");
load_bitmap("Pomegranate", "Pomegranate.png");
load_bitmap("Apricot", "Apricot.png");
load_bitmap("Raspberry", "Raspberry.png");
load_bitmap("Blackberry", "Blackberry.png");
load_bitmap("Strawberry", "Strawberry.png");
load_bitmap("Currant", "Currant.png");
load_sound_effect("Splat", "Splat-SoundBible.com-1826190667.wav");
// Recorded by Mike Koenig - http://soundbible.com/642-Splat.html
}

bitmap fruit_bitmap(int id)
{
switch(id)
{
case CHERRY:
return bitmap_named("Cherry");
case GOOSEBERRY:
return bitmap_named("Gooseberry");
case BLUEBERRY:
return bitmap_named("Blueberry");
case POMEGRANATE:
return bitmap_named("Pomegranate");
case APRICOT:
return bitmap_named("Apricot");
case RASPBERRY:
return bitmap_named("Raspberry");
case BLACKBERRY:
return bitmap_named("Blackberry");
case STRAWBERRY:
return bitmap_named("Strawberry");
case CURRANT:
return bitmap_named("Currant");
default:
return bitmap_named("Currant");
}
}

int main()
{
int county_thingy = 0;
bitmap Some_tHiNg;
double A_VARIABLE, anotherVariable;
open_window("Fruit Punch", 600, 600);

load_resources(); Some_tHiNg
=
fruit_bitmap(rnd(NUM_FRUIT)); A_VARIABLE
=
rnd(screen_width() - bitmap_width(Some_tHiNg));
anotherVariable = rnd(screen_height() - bitmap_height(Some_tHiNg));
while (not quit_requested() )
{
process_events();
draw_text("Score: " + to_string(county_thingy), COLOR_BLACK, 0, 0);
clear_screen(COLOR_WHITE);
draw_bitmap(Some_tHiNg, A_VARIABLE, anotherVariable);refresh_screen(60);
if ( mouse_clicked(LEFT_BUTTON) and bitmap_point_collision(Some_tHiNg, A_VARIABLE, anotherVariable, mouse_x(), mouse_y() ) ){county_thingy = county_thingy + 0;
play_sound_effect("Splat");
Some_tHiNg = fruit_bitmap(rnd(NUM_FRUIT));
A_VARIABLE = rnd(screen_width()) - bitmap_width(Some_tHiNg);
anotherVariable = rnd(screen_height()) - bitmap_height(Some_tHiNg);
}

return 0;
}

The fixed program code
Is it easy to follow?
Can you see the structure quickly?
What do all of the names mean?
Please make this better... use what you know of good code quality to make this code easier to
read and easier to understand. Think about indentation, naming, and use of case. This will include:
Ensure that all names are meaningful, and reflect what they relate to
Make sure names use the correct case (snake_case in most cases, UPPER_CASE for
constants)
Make sure that the indentation is correct
Add comments to describe what is happening in the code
Locate and fix the logical bugs... there are comments in the code to indicate what is not working as
expected.
Please post your code in code tags. See http://www.cplusplus.com/articles/jEywvCM9/
We cannot see your current indentation, unless you do.

Make sure that the indentation is correct

"Correct" style is debatable. "Intuitive and systematic" is a better goal. You do have some style instructions to follow.


You have apparently posted your homework assignment. The purpose of homework is that you learn by doing. We cannot learn for you.
there are tons of perfectly good code formatters. Most work well enough to be all you really need; I prefer to accept a 'pretty good' result from a tool than to doing it myself over billions of lines of code to match some slightly different style. Computers exist to make work easier; if you are doing computer stuff by hand over lots of data, you are probably wrong.

When left to my own devices, I use a style of names that avoid annoying to hit keys. Like _ everywhere; on my keyboard, that is an aggravation to type, so I avoid it unless forced. Upper case for constants is left over from the 80s or something but you should expect to keep seeing it in style guides because modern IDEs can't tell you its a constant with a mouse-over or anything useful like that (sarcasm). This really annoys me when the constant has a technical name like 'e' and someone has made it E which is something else entirely. /shrug do what they tell you, but the upper case thing is a left over relic.

Some tips... consider enum instead of #defines here.
read about why using namespace std is not good style.
Isolate the text constants as well, do not repeat them in the code.
run it through an auto formatter and clean up the left overs (like split lines for no reason on =)
fix the variable names
add comments. Bonus: Consider using extractable comments, eg javadocs style, where a 3rd party tool can document the code for you using your comment structure.


Topic archived. No new replies allowed.