1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
#include <iostream>
#include <conio.h>
#include <string>
#include <windows.h>
using namespace std;
int x = 1;
int y = 1;
enum direction {STOP, UP, LEFT, DOWN, RIGHT};
direction dir;
bool gameOver = false;
string test [38];
string display [38];
string futuredisplay [38];
HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE);
//The goal of this program was to refresh my graphics in a more reliable way than using
//system("cls") (because it makes the screen flicker when it clears it and redraws)
//This new code shouldn't do that since it isn't actually clearing the screen just writing over it.
//Unfortunately, the cursor position does seem to change when it tries to write over it, but not
//to the correct location, and doesn't seem to do anything...
//I populate the three arrays of strings I have with '#' to represent my rectangular game map.
//For some reason when I try to copy it over Code::Blocks crashes, so I do it manually each time.
//I'm cutting this part out because it is too long for my post, but I just make 38 strings that are 100 chars long
//and make a boarder of #s.
//Like this but bigger test[0] = "#######"; (I do this with each array.)
// test[1] = "# #";
// test[37]= "#######";
void setup ()
{
//I give the display and future display arrays, an element with the 'o' character to represent my coordinates.
for (int a = 0; a < 38; a++)
for (int b = 0; b < 100; b++)
{
if (a == y && b == x)
{
display[a][b] = 'o';
futuredisplay[a][b] = 'o';
}
}
//I print the display array.
for (int c = 37; c >=0; c--)
for (int d = 0; d < 100; d++)
{
if (d < 99)
cout << display[c][d];
else
cout << display[c][d] << endl;
}
}
void draw()
{
//The objective of this code is to move the cursor and print over any differences between the display and futuredisplay arrays
//Nothing should happen the first time around since they are the same.
for (int y = 0; y < 38; y++)
for (int z = 0; z < 100; z++)
{
char transitive = display[y][z];
if (transitive != futuredisplay[y][z])
{
COORD pos = {z, y};
SetConsoleCursorPosition(output, pos);
cout << futuredisplay[y][z];
}
}
//After it writes over the screen, it makes display equal to futuredisplay (which will be changed again in the logic section)
for (int i = 0; i < 38; i++)
for (int j =0; j < 100; j++)
{
char transfer = futuredisplay[i][j];
display[i][j] = transfer;
}
}
//this is standard input
void input()
{
if (_kbhit())
{
switch (_getch())
{
case('w'):
dir = UP;
break;
case('a'):
dir = LEFT;
break;
case('s'):
dir = DOWN;
break;
case('d'):
dir = RIGHT;
break;
}
}
}
//just changes the coordinates of x and y based on input
void logic()
{
switch (dir)
{
case (UP):
y++;
break;
case(DOWN):
y--;
break;
case(LEFT):
x--;
break;
case(RIGHT):
x++;
break;
}
//Once x and y changes, it uses the test array to get rid of the 'o' character in futuredisplay and make it barren again.
for (int e = 0; e < 38; e++)
for (int f = 0; f < 100; f++)
{
char transfer = test[e][f];
futuredisplay[e][f] = transfer;
}
//It now adds the new 'o' character based on updated coordinates.
//If all goes according to plan, when this new futuredisplay is compared to display in draw(), it will write over it, and let me use WASD to move the circle without any flickering.
for (int g = 0; g < 38; g++)
for (int h = 0; h < 100; h++)
{
if (g == y && h == x)
{
futuredisplay[g][h] = 'o';
}
}
}
int main()
{
setup();
while (gameOver == false)
{
draw();
input();
logic();
}
}
|