/* Program 1.13 */ /* simple rectangle animation */ /* hint: use the cursors to move the funky rectangle, press 'q' to quit */ /* Used header files */ /* header files */ #include #include #include #include /* !!! This is important part - the variables must be declared before they are used !!!! */ int r_x_pos=0; /* starting x position of rectangle*/ int r_y_pos=0; /* starting y position of rectangle*/ int r_height=100; /* rectangle height*/ int r_width=100; /* rectangle width */ int delta_x=0; /* in which x direction our rectangle will be moving x-axis*/ int delta_y=0; /* how many pixels our rectangle will be moving on y-axis */ /* declaration of used functions */ void draw_rectangle(int colour); /* implementation of user defined functions */ void draw_rectangle(int colour) { setcolor(colour); moveto(r_x_pos,r_y_pos); lineto(r_x_pos+r_width,r_y_pos); lineto(r_x_pos+r_width,r_y_pos+r_height); lineto(r_x_pos,r_y_pos+r_height); lineto(r_x_pos,r_y_pos); } /* end of function(or procedure with parameter you naughty pascal Ý$%&%*!) draw_rectangle */ /* Main function - our program starts here*/ int main(void) { /* initialization of variables */ int driver,mode,err=0; int kod_znaku=0; driver = VDI; mode = VDIMODE; initgraph(&driver, &mode, ""); /* Damn, still no drivers for Atari! Why !? ;))))))*/ err = graphresult(); if(err) { puts(grapherrormsg(err)); return(err); } while(1==1) { draw_rectangle(BLUE); /* Now, we are checking if the key was pressed */ if (kod_znaku=getch()) { switch (kod_znaku) /* switch on :))) */ { case 72: /* arrow up pressed */ {delta_y=-10;delta_x=0;break;} case 80: /* arrow down pressed */ {delta_y=10;delta_x=0;break;} case 75: /* left arrow pressed */ {delta_x=-10;delta_y=0;break;} case 77: /* right arrow pressed */ {delta_x=10;delta_y=0;break;} case 113: /* if 'q' pressed quit proggy */ { closegraph(); printf("You have quitted the program. See ya!!"); return(0); } } /* end of switch (switch it off ;)))) ) */ } /* end of if */ /* Moving of the rectangle*/ if ( ((r_x_pos + delta_x) > -10) && ((r_x_pos + delta_x) < (getmaxx()-r_width)) ) r_x_pos=r_x_pos + delta_x; if (((r_y_pos+delta_y)>-10)&&((r_y_pos+delta_y)<(getmaxy()-r_height))) r_y_pos=r_y_pos+delta_y; cleardevice(); /* clear the whole screen */ } /* end of while loop */ } /* our main program ends here */