Allegro.cc - Online Community

Allegro.cc Forums » Game Design & Concepts » how does this drawtank function draws a rectangular tank?

This thread is locked; no one can reply to it. rss feed Print
how does this drawtank function draws a rectangular tank?
b aryal
Member #17,071
April 2019

void drawtank(int num)
{
int x = tanks[num].x;
int y = tanks[num].y;
int dir = tanks[num].dir;
//draw tank body and turret
rectfill(screen, x-11, y-11, x+11, y+11, tanks[num].color);
rectfill(screen, x-6, y-6, x+6, y+6, 7);
//draw the treads based on orientation
if (dir == 0 || dir == 2)
{
rectfill(screen, x-16, y-16, x-11, y+16, 8);
rectfill(screen, x+11, y-16, x+16, y+16, 8);
}
else
if (dir == 1 || dir == 3)
{
rectfill(screen, x-16, y-16, x+16, y-11, 8);
rectfill(screen, x-16, y+16, x+16, y+11, 8);
}
//draw the turret based on direction
switch (dir)
{
case 0:
rectfill(screen, x-1, y, x+1, y-16, 8);
break;
case 1:
rectfill(screen, x, y-1, x+16, y+1, 8);
break;
case 2:
rectfill(screen, x-1, y, x+1, y+16, 8);
break;
case 3:
rectfill(screen, x, y-1, x-16, y+1, 8);
break;
}
}

Edgar Reynaldo
Member #8,592
May 2007
avatar

That is allegro 4 code. It draws a tank using filled rectangles only.

b aryal
Member #17,071
April 2019

0: Move toward the northwest
1: Move toward the southwest
2: Move toward the northeast
3: Move toward the southeast
is this how directions work in allegro everytime..

context-: if (dir==0...and so on)

Edgar Reynaldo
Member #8,592
May 2007
avatar

0 is north.

You've got to learn how to read code for yourself that's the true mark of a coder, being able to read someone else's code.

It has nothing to do with allegro, except that y decreases going up the screen.

EDIT
Welcome to allegro by the way.

If you use <code>code goes here</code> tags, you can highlight your code, and it will provide (outdated) links to the manual, like so :

#SelectExpand
1void drawtank(int num) 2{ 3int x = tanks[num].x; 4int y = tanks[num].y; 5int dir = tanks[num].dir; 6//draw tank body and turret 7rectfill(screen, x-11, y-11, x+11, y+11, tanks[num].color); 8rectfill(screen, x-6, y-6, x+6, y+6, 7); 9//draw the treads based on orientation 10if (dir == 0 || dir == 2) 11{ 12rectfill(screen, x-16, y-16, x-11, y+16, 8); 13rectfill(screen, x+11, y-16, x+16, y+16, 8); 14} 15else 16if (dir == 1 || dir == 3) 17{ 18rectfill(screen, x-16, y-16, x+16, y-11, 8); 19rectfill(screen, x-16, y+16, x+16, y+11, 8); 20} 21//draw the turret based on direction 22switch (dir) 23{ 24case 0: 25rectfill(screen, x-1, y, x+1, y-16, 8); 26break; 27case 1: 28rectfill(screen, x, y-1, x+16, y+1, 8); 29break; 30case 2: 31rectfill(screen, x-1, y, x+1, y+16, 8); 32break; 33case 3: 34rectfill(screen, x, y-1, x-16, y+1, 8); 35break; 36} 37}

Go to: