Mail Archives: djgpp/1993/08/05/02:57:32
Ack! My program is locking up my computer so that only a hardware reset will
get me back to the command line. The source follows. To compile, type:
gcc prog.c -o prog -lgrx -lm
You need not use the grx library, it will do the same thing with the regular
gr library as well.
The commands are: (i) - move up
(k) - " " down
(j) - turn left
(l) - turn right
To crash the program, try rotating in one direction by holding down the
(j) or (l) keys. That oughtta do the trick.
Any obvious / not so obvious errors/tips would be greatly appreciated..
-Alan Krause
*------CUT Here--------*
#include <grx.h>
#include <stdio.h>
#include <math.h>
#define STEP 550
#define GRID 100 /* total field size (i.e. -100,100 x -100,100) */
#define RAD 20 /* radar's viewable radius */
#define CX 400
#define CY 300
#define RX 85
#define RY 75
#define FLAGCOLOR 14
#define TANKCOLOR 9
struct flag {
int x;
int y;
int st;
};
struct tank {
double x;
double y;
double angle;
int ammo;
int shields;
};
/***************************************/
/* Function prototypes... */
void draw_flags(struct tank, struct flag *, int, int);
int main(void);
/**************************************/
/* Subroutines used.... */
void draw_flags(struct tank t, struct flag f[], int n, int color)
{
int i;
double a,d;
/*printf("In draw_flags...");*/
for (i=0;i<n;i++) {
/*printf("%d..",i);*/
d=sqrt(pow((double)(t.x-f[i].x),2) + pow((double)(t.y-f[i].y),2));
if (d<RAD) {
a=atan2( (double)(t.y-f[i].y), (double)(t.x-f[i].x));
a+=t.angle;
GrPlot((int)(CX+(double)d/RAD*(RX-1)*sin(a)),
(int)(CY+(double)d/RAD*(RY-1)*cos(a)),color);
}
}
GrPlot(CX,CY,TANKCOLOR);
}
/**************************************/
/* Main program... */
int main()
{
char ch;
int i,j=0;
struct tank tk={0,0,0,40,40};
struct flag fg[4]={
10,0,1,
0,10,1,
-10,0,1,
0,-10,1
};
GrSetMode(GR_default_graphics);
/* Draw our radar scope on the screen */
for (i=0;i<STEP;i++)
{
GrPlot(CX+RX*sin((double)2*PI*((float)i/STEP)),
CY+RY*cos((double)2*PI*((float)i/STEP)),10);
}
GrPlot(CX,CY,9);
draw_flags(tk,fg,4,FLAGCOLOR);
/* This is the main processing loop... all action takes place here... */
do {
ch=getkey();
if (ch=='l' || ch=='6') {
draw_flags(tk,fg,4,0);
tk.angle+=PI/20;
draw_flags(tk,fg,4,FLAGCOLOR);
}
if (ch=='j' || ch=='4') {
draw_flags(tk,fg,4,0);
tk.angle-=PI/20;
/*printf("Redrawing flags..");*/
draw_flags(tk,fg,4,FLAGCOLOR);
/*printf("Flags redrawn!!\n");*/
}
if (ch=='k' || ch=='2') {
draw_flags(tk,fg,4,0);
tk.x-=cos(tk.angle);
tk.y+=sin(tk.angle);
draw_flags(tk,fg,4,FLAGCOLOR);
}
if (ch=='i' || ch=='8') {
draw_flags(tk,fg,4,0);
tk.x+=cos(tk.angle);
tk.y-=sin(tk.angle);
draw_flags(tk,fg,4,FLAGCOLOR);
}
if (tk.angle > 2*PI) tk.angle-=2*PI;
if (tk.angle < 0) tk.angle+=2*PI;
/*printf("Tank angle: (%f) X: (%f) Y: (%f)\n",tk.angle,tk.x,tk.y);*/
} while (ch!='q');
GrSetMode(GR_default_text);
}
- Raw text -