improve anaconda.c

make some changes to anaconda:
  -> fix memory leaks
  -> add tabs instead of spaces
  -> show score on screen
  -> adapt to different screen sizes
This commit is contained in:
randomuser 2022-08-15 18:42:26 -05:00
parent d37e632bd8
commit 6b5d621820
1 changed files with 171 additions and 126 deletions

View File

@ -81,10 +81,11 @@ void appendPoint(point *dest, point *origin) {
dest->next = origin; dest->next = origin;
} }
int updateAnaconda(anaconda *anaconda, int w, int h, point *apple) { int updateAnaconda(anaconda *anaconda, point *basepoint, int w, int h, point *apple) {
point *temp, *new, *ptr; point *temp, *new, *ptr;
new = anaconda->chain; new = anaconda->chain;
temp = rotate(mkPoint(10, 0), anaconda->rot);
temp = rotate(basepoint, anaconda->rot);
new = mkPoint( new = mkPoint(
new->x + temp->x, new->x + temp->x,
new->y + temp->y new->y + temp->y
@ -136,6 +137,21 @@ int updateAnaconda(anaconda *anaconda, int w, int h, point *apple) {
return 1; return 1;
} }
void freeAnaconda(anaconda *anaconda) {
point *current = anaconda->chain;
point *next = NULL;
for(;;) {
if(!current) break;
next = current->next;
free(current);
current = next;
}
free(anaconda);
return;
}
point *generateChain(int length) { point *generateChain(int length) {
point *ret = mkPoint(100, 100); point *ret = mkPoint(100, 100);
point *head = ret; point *head = ret;
@ -163,13 +179,20 @@ anaconda *mkAnaconda(point *point, double rot) {
int main(void) { int main(void) {
anaconda *anaconda = mkAnaconda(generateChain(30), 0); anaconda *anaconda = mkAnaconda(generateChain(30), 0);
point *basepoint = mkPoint(10, 0);
char scorebuffer[30];
xinit(); xinit();
srand(time(0)); srand(time(0));
int width = DisplayWidth(d, s); int width = DisplayWidth(d, s);
int height = DisplayHeight(d, s); int height = DisplayHeight(d, s);
point *apple = mkPoint(randrange(20, width / 2 - 20), randrange(20, height / 2 - 20)); point *apple = mkPoint(randrange(20, width / 2 - 20), randrange(20, height / 2 - 20));
int exposed = 0;
while(1) { while(1) {
if(!updateAnaconda(anaconda, width, height, apple)) { if(exposed) {
if(!updateAnaconda(anaconda, basepoint, width, height, apple)) {
freeAnaconda(anaconda);
free(apple);
free(basepoint);
return 0; return 0;
} }
XClearWindow(d, w); XClearWindow(d, w);
@ -178,8 +201,10 @@ int main(void) {
XDrawLine(d, w, gc, ptr->x, ptr->y, ptr->next->x, ptr->next->y); XDrawLine(d, w, gc, ptr->x, ptr->y, ptr->next->x, ptr->next->y);
ptr = ptr->next; ptr = ptr->next;
} }
printf("%f %f\n", apple->x, apple->y);
XDrawArc(d, w, gc, apple->x - (5/2), apple->y - (5/2), 5, 5, 0, 360*64); XDrawArc(d, w, gc, apple->x - (5/2), apple->y - (5/2), 5, 5, 0, 360*64);
int len = snprintf(&scorebuffer, 30, "%i", anaconda->score);
XDrawString(d, w, gc, 25, 25, &scorebuffer, len);
}
while(XPending(d)) { while(XPending(d)) {
XNextEvent(d, &e); XNextEvent(d, &e);
switch(e.type) { switch(e.type) {
@ -192,11 +217,31 @@ int main(void) {
anaconda->rot -= 10; anaconda->rot -= 10;
break; break;
} }
break;
case Expose:
/* hold off drawing until we get an expose event */
exposed = 1;
width = e.xexpose.width;
height = e.xexpose.height;
if(apple->x > width) {
free(apple);
apple = mkPoint(randrange(20, width / 2 - 20), randrange(20, height / 2 - 20));
} else if (apple->y > height) {
free(apple);
apple = mkPoint(randrange(20, width / 2 - 20), randrange(20, height / 2 - 20));
}
break; break;
} }
} }
usleep(100000); usleep(100000);
} }
freeAnaconda(anaconda);
free(apple);
free(basepoint);
return 0; return 0;
} }