I am trying to create a program for my C programming class that does a crude animation. I am given a .txt file in which I have to read the following characters which are in a 3x3 array.
o
-|-
/ I then have to display him, pause, erase him and draw him in a new position. I have to do this in a two-dimensional world. I have to do this while moving him in random amounts in a -1...+1 range vertically and -3...+3 horizontally. The whole thing should be random. I also have added a break if the figure moves outside of the default 76x24 screen size. I have a very crude random function set up and would like some help simplifying the function. [6*round(((double)rand()/(double)RAND_MAX)-3]and the y which is right below it in the code under animation. I am also having a problem with the colors. There is a 5x5 box around the center of the screen (35,10) beyond that box I have the screen broken up into 4 quads (UR, LR, LL, UL) each quad has a different color. My problem is that the character is staying the original white and I am not sure why. Any help would be great. The full code is below.
Russ
#include <stdio.h>
#include <math.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#define NUMROWS 3
#define NUMCOLS 4
#define BLACK 0
#define DARKBLUE 1
#define DARKGREEN 2
#define DARKCYAN 3
#define DARKRED 4
#define DARKMAGENTA 5
#define DARKYELLOW 6
#define DARKWHITE 7
#define GREY 8
#define BLUE 9
#define GREEN 10
#define CYAN 11
#define RED 12
#define MAGENTA 13
#define YELLOW 14
#define WHITE 15
/********** Define Prototypes **********/
void readData(char drawVals[NUMROWS][NUMCOLS]);
void drawFigure(int x,int y, char drawVals[NUMROWS][NUMCOLS]);
void pause(int pauseTime);
void animate(char drawVals[NUMROWS][NUMCOLS], char blankVals[NUMROWS][NUMCOLS]);
/********** Defining readData Function **********/
void readData(char drawVals[NUMROWS][NUMCOLS])
{
char *filename;
int i;
int j;
FILE *fp;
filename = "data6engr.txt";
if((fp = fopen(filename, "r") == NULL)
{
printf("can't open file %s\n", filename);
}
for(i=0; i<NUMROWS; i++)
{
for(j=0; j<NUMCOLS; j++)
{
drawVals[j] = fgetc(fp);
}
}
fclose(fp);
}
/********** Defining drawFigure Function **********/
void drawFigure(int x,int y, char drawVals[NUMROWS][NUMCOLS])
{
int i;
int j;
for(i=0; i<NUMROWS; i++)
{
for(j=0; j<NUMCOLS; j++)
{
_gotoxy(x+j,y+i);
printf("%c",drawVals[j]);
}
}
fflush(stdout);
}
/********** Defining Pause Function **********/
void pause(int pauseTime)
{
int pause;
pause = clock() + pauseTime;
while(clock() < pause)
{
}
return;
}
/********** Defining Animate Function **********/
void animate(char drawVals[NUMROWS][NUMCOLS], char blankVals[NUMROWS][NUMCOLS])
{
int i;
int x;
int xLow;
int xHigh;
int y;
int yLow;
int yHigh;
int pauseTime;
i = 0;
pauseTime = 30;
x = 35;
y = 10;
xLow = x - 5;
xHigh = x + 5;
yLow = y - 5;
yHigh = y + 5;
while(i < 150)
{
drawFigure(x,y,drawVals);
pause(pauseTime);
drawFigure(x,y,blankVals);
x += 6*round((double)rand() / (double)RAND_MAX) - 3;
y += 3*round((double)rand() / (double)RAND_MAX) - 1;
srand(0);
if(x > xHigh && y < yLow)
_textcolor(BLUE);
if(x > xHigh && y > yHigh)
_textcolor(RED);
if(x < xLow && y > yHigh)
_textcolor(CYAN);
if(x < xLow && y < yLow)
_textcolor(GREEN);
if(x > 76 || x <= 0)
break;
if(y > 23 || y <= 0)
break;
++i;
}
fflush(stdout);
return;
}
int main(void)
{
char drawVals[NUMROWS][NUMCOLS],blankVals[NUMROWS][NUMCOLS] = {' '};
readData(drawVals);
animate(drawVals,blankVals);
return 0;
}
o
-|-
/ I then have to display him, pause, erase him and draw him in a new position. I have to do this in a two-dimensional world. I have to do this while moving him in random amounts in a -1...+1 range vertically and -3...+3 horizontally. The whole thing should be random. I also have added a break if the figure moves outside of the default 76x24 screen size. I have a very crude random function set up and would like some help simplifying the function. [6*round(((double)rand()/(double)RAND_MAX)-3]and the y which is right below it in the code under animation. I am also having a problem with the colors. There is a 5x5 box around the center of the screen (35,10) beyond that box I have the screen broken up into 4 quads (UR, LR, LL, UL) each quad has a different color. My problem is that the character is staying the original white and I am not sure why. Any help would be great. The full code is below.
Russ
#include <stdio.h>
#include <math.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#define NUMROWS 3
#define NUMCOLS 4
#define BLACK 0
#define DARKBLUE 1
#define DARKGREEN 2
#define DARKCYAN 3
#define DARKRED 4
#define DARKMAGENTA 5
#define DARKYELLOW 6
#define DARKWHITE 7
#define GREY 8
#define BLUE 9
#define GREEN 10
#define CYAN 11
#define RED 12
#define MAGENTA 13
#define YELLOW 14
#define WHITE 15
/********** Define Prototypes **********/
void readData(char drawVals[NUMROWS][NUMCOLS]);
void drawFigure(int x,int y, char drawVals[NUMROWS][NUMCOLS]);
void pause(int pauseTime);
void animate(char drawVals[NUMROWS][NUMCOLS], char blankVals[NUMROWS][NUMCOLS]);
/********** Defining readData Function **********/
void readData(char drawVals[NUMROWS][NUMCOLS])
{
char *filename;
int i;
int j;
FILE *fp;
filename = "data6engr.txt";
if((fp = fopen(filename, "r") == NULL)
{
printf("can't open file %s\n", filename);
}
for(i=0; i<NUMROWS; i++)
{
for(j=0; j<NUMCOLS; j++)
{
drawVals[j] = fgetc(fp);
}
}
fclose(fp);
}
/********** Defining drawFigure Function **********/
void drawFigure(int x,int y, char drawVals[NUMROWS][NUMCOLS])
{
int i;
int j;
for(i=0; i<NUMROWS; i++)
{
for(j=0; j<NUMCOLS; j++)
{
_gotoxy(x+j,y+i);
printf("%c",drawVals[j]);
}
}
fflush(stdout);
}
/********** Defining Pause Function **********/
void pause(int pauseTime)
{
int pause;
pause = clock() + pauseTime;
while(clock() < pause)
{
}
return;
}
/********** Defining Animate Function **********/
void animate(char drawVals[NUMROWS][NUMCOLS], char blankVals[NUMROWS][NUMCOLS])
{
int i;
int x;
int xLow;
int xHigh;
int y;
int yLow;
int yHigh;
int pauseTime;
i = 0;
pauseTime = 30;
x = 35;
y = 10;
xLow = x - 5;
xHigh = x + 5;
yLow = y - 5;
yHigh = y + 5;
while(i < 150)
{
drawFigure(x,y,drawVals);
pause(pauseTime);
drawFigure(x,y,blankVals);
x += 6*round((double)rand() / (double)RAND_MAX) - 3;
y += 3*round((double)rand() / (double)RAND_MAX) - 1;
srand(0);
if(x > xHigh && y < yLow)
_textcolor(BLUE);
if(x > xHigh && y > yHigh)
_textcolor(RED);
if(x < xLow && y > yHigh)
_textcolor(CYAN);
if(x < xLow && y < yLow)
_textcolor(GREEN);
if(x > 76 || x <= 0)
break;
if(y > 23 || y <= 0)
break;
++i;
}
fflush(stdout);
return;
}
int main(void)
{
char drawVals[NUMROWS][NUMCOLS],blankVals[NUMROWS][NUMCOLS] = {' '};
readData(drawVals);
animate(drawVals,blankVals);
return 0;
}