I mean, that would be good, but honestly if you're just making a little console game like this for fun, using functions like getch isn't the end of the world.
Yes, the first two things I see are a lack of indentation, and inconsistent spacing.
1 2 3
|
if (x >= width) x =0;else if (x <0) x = width -1;
if (y >= height) y = 0; else if (y < 0) y = height - 1;
| |
Saving a few lines of code does not improve readability. Just separate things statements to be on to their own lines. Especially statements that are separated by semi-colons (a small if statement you could get away with being on one line, but personally I think it's a bad habit).
1 2 3 4 5 6 7 8 9
|
if (x >= width)
x = 0;
else if (x < 0)
x = width - 1;
if (y >= height)
y = 0;
else if (y < 0)
y = height - 1;
| |
On the other hand, having a bunch of double-spacing also does not helping readability.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
case LEFT:
x--;
break;
case RIGHT:
x++;
break;
case UP:
y--;
| |
- As highwayman said, globals should mostly be avoided; having too many just keeps the state of your game hard to keep track of. Pass the necessary variables of the game needed into, e.g. your draw function.
- Having a function called "algorithm" tells me just about nothing.
"prev2x"
You only use your prev2x and prev2y variables inside your for loop, so they could be defined there as well. In general, try to keep variables in the smallest scope possible. This goes back to minimizing the state you have to keep track of at once.