HW31
COP-3223H
Submit via git.
Using the following code template, define the draw_sprite function to draw a 3x4 sprite from a given (x,y) position on the terminal. It takes two integers, for x and y, and one 3x4 character array. For instance, sprite2 should be printed at (10, 9) and look like this:
^__^ (o-) (__)
This is the code template:
#include <stdio.h>
#include <assert.h>
#include <stdbool.h>
#include <unistd.h>
struct character {
int x;
int y;
int state;
char sprite1[3][4];
char sprite2[3][4];
};
// TODO: define the draw_sprite function here
int main(int argc, char **argv) {
char cow1[3][4] = { { '^', '_', '_', '^' },
{ '(', 'o', 'o', ')' },
{ '(', '_', '_', ')' } };
char cow2[3][4] = { { '^', '_', '_', '^' },
{ '(', 'o', '-', ')' },
{ '(', '_', '_', ')' } };
struct character cow;
cow.x = 1;
cow.y = 1;
cow.state = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
cow.sprite1[i][j] = cow1[i][j];
cow.sprite2[i][j] = cow2[i][j];
}
}
while (1) {
printf("\x1b[2J");
printf("\x1b[H");
cow.x = 10;
cow.y = 9;
switch (cow.state) {
case 0:
draw_sprite(cow.x, cow.y, cow.sprite1);
break;
case 1:
draw_sprite(cow.x, cow.y, cow.sprite2);
break;
default:
assert(false);
}
cow.state = (cow.state + 1) % 2;
usleep(500 * 1000);
}
}