void startGame(int xSize, int ySize) { char c; char *intro = "Welcome to Asteroid! Press 'p' to play!"; gfx_text((xSize/2)-(strlen(intro)/2)*5, ySize/2, intro); c = gfx_wait(); while(c != 'p'){ c = gfx_wait(); } }
char hopper(int *frogx, int *frogy) { char direction; direction = gfx_wait(); if (direction == 'Q'){ (*frogx)--; if (*frogx<0) (*frogx)++; }else if (direction == 'R'){ (*frogy)--; if (*frogy<0) (*frogy)++; }else if (direction = 'S'){ (*frogx)++; if (*frogx>=25) (*frogx)--; }else if (direction = 'T'){ (*frogy)++; if (*frogy>=13) (*frogy)--; } printf("%c", direction); return direction; }
int main() { int xsize = 500; int ysize = 300; char c; // Open a new window for drawing. gfx_open(xsize, ysize, "Example Graphics Program"); // Clear the background. gfx_clear(); // Set the current drawing color gfx_color(0,200,100); // Draw a triangle on the screen. gfx_line(100,100,200,100); gfx_line(200,100,150,150); gfx_line(150,150,100,100); // change color, and draw a circle gfx_color(200,100,50); gfx_circle(300,150,40); while(1) { // Wait for the user to press a character. c = gfx_wait(); // Quit if it is the letter q. if(c=='q') break; } return 0; }
int main() { int ysize = 300; int xsize = 300; char c; // Open a new window for drawing. gfx_open(xsize,ysize,"Example Graphics Program"); // Set the current drawing color to green. gfx_color(0,200,100); // Draw a triangle on the screen. gfx_line(100,100,200,100); gfx_line(200,100,150,150); gfx_line(150,150,100,100); while(1) { // Wait for the user to press a character. c = gfx_wait(); if(c=='n') gfx_line(200,200,300,200); gfx_line(300,200,250,250); gfx_line(250,250,200,200); // Quit if it is the letter q. if(c=='q') break; } return 0; }
int main (void) { int xSize=600, ySize=600, xMouse, yMouse, j; float h=100; char c; gfx_open(xSize, ySize, "Window"); while (1) { c = gfx_wait(); xMouse = gfx_xpos(); yMouse = gfx_ypos(); switch (c) { case '0': draw_zero(xMouse, yMouse, h); break; case '1': draw_one(xMouse, yMouse, h); break; case '2': draw_two(xMouse, yMouse, h); break; case '3': draw_three(xMouse, yMouse, h); break; case '4': draw_four(xMouse, yMouse, h); break; case '5': draw_five(xMouse, yMouse, h); break; case '6': draw_six(xMouse, yMouse, h); break; case '7': draw_seven(xMouse, yMouse, h); break; case '8': draw_eight(xMouse, yMouse, h); break; case '9': draw_nine(xMouse, yMouse, h); break; case '-': h = h/2; break; case '=': h = h*2; break; case 'q': return 0; } } return 0; }
int main() { char fractal; int x=width/2,y=height/2,radius=width/3; int margin=20; int x1=margin,x2=width-margin,x3=width/2,y1=margin,y2=margin,y3=height-margin; gfx_open(width,height,"Fractals"); gfx_clear_color(0,0,0); gfx_clear(); gfx_color(255,255,255); printf("Which fractal would you like to see?\n1: Sierpinski Triangles\n2: Shrinking Squares\n3: Spiral Squares\n4: Circular Lace\n5: Snowflake\n6: Tree\n7: Fern\n8: Spiral of Spirals\n\n"); while(fractal!='q'){ fractal=gfx_wait(); gfx_clear(); switch(fractal){ case 'q': return 0; break; case '1': printf("Sierpinski Triangles\n\n"); sierpTriangles(x1,y1,x2,y2,x3,y3); break; case '2': printf("Shrinking Squares\n\n"); shrinkSquares(x,y,width/4); break; case '3': printf("Spiral Squares\n\n"); spiralSquares(x,y,-M_PI/4,width*2/3); break; case '4': printf("Circular Lace\n\n"); circularLace(x,y,width); break; case '5': printf("Snowflake\n\n"); snowflake(x,y,radius); break; case '6': printf("Tree\n\n"); tree(x,height,x,height*3/4,0,height/4); break; case '7': printf("Fern\n\n"); fern(width/2,height-margin,2*height/3,0); break; case '8': printf("Spiral of Spirals\n\n"); spiralSpirals(x,y,0,width); break; default: printf("Please enter a valid option\n\n"); break; } } }
int main () { int x, y, dx, dy, radius = 100; double pi, i = 0.07; char c; pi = M_PI; gfx_open(500,500,"Rotating Animation Emily"); printf("To speed up display press f key; to slow down display press s key.\n"); while (1) { drawPerson(); x = radius*cos(i) + 250; y = radius*sin(i) + 150; gfx_color(0, 0, 255); gfx_circle(x, y, 20); //ball 1 (blue) x = radius*cos(i+1.5) + 250; y = radius*sin(i+1.5) + 150; gfx_color(0, 255, 0); gfx_circle(x, y, 20); //ball 2 (green) x = radius*cos(i+3.) + 250; y = radius*sin(i+3.) + 150; gfx_color(255, 0, 0); gfx_circle(x, y, 20); //ball 3 (red) gfx_flush(); usleep(10000); gfx_clear(); if (gfx_event_waiting()) { c = gfx_wait(); if (c =='s') { //slows down juggling i-=0.1; } else if (c =='f') { //speeds juggling i+=0.1; } else if (c =='q') { break; } } i+= 0.01; } return 0; }
int main() { int width = 400; int height = 550; int gameLoop = 1; int highScore = 0; gfx_open(width, height, "Flappy Bird"); //create two pipes to be recycled Pipe* leadPipe = (Pipe*)malloc(sizeof(Pipe)); Pipe* trailPipe = (Pipe*)malloc(sizeof(Pipe)); while (gameLoop) { //initialize each pipe initializePipe(leadPipe, width, height); initializePipe(trailPipe, width, height); //set background color gfx_clear_color(85, 203, 217); gfx_clear(); //present game home screen char c; int flap = 0; double birdX = 150, birdY = 275, birdR = 12, degrees = 0; while (1) { gfx_clear(); if(gfx_event_waiting()){ if(gfx_wait()==' ') break; } presentHomeScreen(width, height); stationaryBird(&birdY,degrees); flap = drawBird(birdX, birdY, birdR, flap); gfx_flush(); usleep(50000); degrees+=(2*M_PI)/20; } //begin animation int score = startGame(leadPipe, trailPipe, width, height); highScore = endGame(score, highScore, width, height); char d; } return 0; }
int main(void) { gfx_open(400,400,"Bounce"); int stillGoing = 1; float dt = 0.01; float xpos =rand() %400+1; float ypos =rand() %400+1; float xvel =(double)rand()/RAND_MAX * 10.0 - 5.0; float yvel =(double)rand()/RAND_MAX * 10.0 - 5.0; float RIGHT_EDGE = 400; float LEFT_EDGE = 0; float TOP = 0; float BOTTOM = 400; while(stillGoing){ xpos += xvel; ypos += yvel; if(xpos <= LEFT_EDGE || xpos >= RIGHT_EDGE) xvel *=-1; if(ypos <= TOP || ypos >= BOTTOM) yvel *= -1; gfx_clear(); drawPoly(30,xpos,ypos); gfx_flush(); usleep(10000); if(gfx_event_waiting()){ char c = gfx_wait(); if(c==1){ xpos = gfx_xpos(); ypos = gfx_ypos(); xvel =(double)rand()/RAND_MAX * 10.0 - 5.0; yvel =(double)rand()/RAND_MAX * 10.0 - 5.0; } else if (c=='q' || c=='Q'){ break; } } } return 0; }
int endGame(int score, int highScore, int width, int height) { highScore = presentEndScreen(score, highScore, width, height); while (1) { //check click and location if(gfx_wait()== 1) if (gfx_xpos() > width/2-79 && gfx_xpos() < width/2-79+158 && gfx_ypos() > height/2+96 && gfx_ypos() < height/2+96+28) break; } return highScore; }
char win(void) { gfx_clear_color(0,0,255); //blue background gfx_clear(); gfx_color(255,255,255); //white letters //You gfx_fill_rectangle(370,160,40,60); gfx_fill_rectangle(470,160,40,60); gfx_fill_rectangle(390,220,100,20); gfx_fill_rectangle(410,240,60,60); //yOu gfx_fill_rectangle(530,180,40,100); gfx_fill_rectangle(630,180,40,100); gfx_fill_rectangle(550,160,100,20); gfx_fill_rectangle(550,280,100,20); //yoU gfx_fill_rectangle(690,160,40,120); gfx_fill_rectangle(790,160,40,120); gfx_fill_rectangle(710,280,100,20); //Won gfx_fill_rectangle(370,350,40,140); gfx_fill_rectangle(470,350,40,140); gfx_fill_rectangle(410,430,20,40); gfx_fill_rectangle(430,410,20,40); gfx_fill_rectangle(450,430,20,40); //wOn gfx_fill_rectangle(530,370,40,100); gfx_fill_rectangle(630,370,40,100); gfx_fill_rectangle(550,350,100,20); gfx_fill_rectangle(550,470,100,20); //woN gfx_fill_rectangle(690,350,40,140); gfx_fill_rectangle(790,350,40,140); gfx_fill_rectangle(730,370,20,40); gfx_fill_rectangle(750,390,20,40); gfx_fill_rectangle(770,410,20,40); gfx_text(450,520,"Press any key to play Frogger. Press \'q\' to quit."); return gfx_wait(); }
//begin main function int main (void) { int xsize = 800, ysize = 800 ; gfx_open( xsize, ysize, "Test Graph of Function triangle_waveform" ) ; int stillgoing = 1; while (stillgoing) { gfx_clear() ; gfx_color(255, 255, 255) ; //axis is white draw_axes( xsize, ysize) ; gfx_color(150, 0, 255) ; plot_triangle(xsize, ysize, -10., 10, .1) ; gfx_flush() ; char c = gfx_wait() ; if (c == 'q') stillgoing = 0 ; } return 0 ; } //end main
int main() { int size = 500; int value = 1; int wait; int counter = 0; gfx_open(size, size, "Graphing Calculator"); while(value != 0) { gfx_color(255, 255, 255); xAxis(size, size); yAxis(size, size); wait = gfx_wait(); gfx_color(255, 0, 0); switch(wait) { case 1: gfx_clear(); counter = 0; plot(size, size, counter); break; case '=': gfx_clear(); counter++; plot(size, size, counter); break; case '-': gfx_clear(); counter--; plot(size, size, counter); break; case 'q': value = 0; break; default: break; } printf("%i\n", counter); } return 0; }
int main(){ char c; int width = 500, height = 500, num = 1; double x = 1; gfx_open(width, height, "Taylor Series Graph"); while(c != 'q'){ gfx_clear(); xAxis(); yAxis(); plot(num); c = gfx_wait(); switch(c){ case '=': num++; break; case '-': num--; break; } } }
char hopper(int *frogx, int *frogy, int row, int col) { char direction; direction = gfx_wait(); if (direction == 'Q') { //move frog left if (*frogx>0) (*frogx)--; } else if (direction == 'R') { //move frog up if (*frogy>0) (*frogy)--; } else if (direction == 'S') { //move frog right if (*frogx<col-1) (*frogx)++; } else if (direction == 'T') { //move frog down if (*frogy<row) (*frogy)++; } //end if...else if return direction; } //end hopper
int main(void) // we will be constructing a symbolic typewriter using a basic graphics library { int mousePosX; // mouse point position in x and y directions int mousePosY; int screenX = 900; // screen resolution in pixels int screenY = 900; float x1; // position 1 float y1; float x2; // position 2 float y2; float radians; // need radians and radius when dealing with polar coordinated float radius; float pi = atan(1)*4; // pi constant int min = 0; // minimum number of degrees int c = 0; // counter for for loops int randomDegrees = 0; // random integer degree int polygon = 0; // place for number of polygon sides char user; // this will be the value of the user input srand(time(NULL)); // random integer seed // begin symbolic typewriter gfx_open(screenX, screenY, "Symbolic Typewriter"); while( 1 ) { user = gfx_wait(); // waiting for a user-given symbol switch( user ) { case 1: // mouse click, we will print a blue square outline { gfx_color(0, 0, 255); // changing color to blue radius = 25; mousePosX = gfx_xpos(); // storing the current mouse position mousePosY = gfx_ypos(); x1 = mousePosX + radius; // upper right point y1 = mousePosY + radius; x2 = mousePosX - radius; // upper left point y2 = mousePosY + radius; gfx_line((int)x1, (int)y1, (int)x2, (int)y2); // top line x1 = mousePosX + radius; // upper right point y1 = mousePosY + radius; x2 = mousePosX + radius; // lower right point y2 = mousePosY - radius; gfx_line((int)x1, (int)y1, (int)x2, (int)y2); // right line x1 = mousePosX + radius; // lower right point y1 = mousePosY - radius; x2 = mousePosX - radius; // lower left point y2 = mousePosY - radius; gfx_line((int)x1, (int)y1, (int)x2, (int)y2); // lower line x1 = mousePosX - radius; // lower left point y1 = mousePosY - radius; x2 = mousePosX - radius; // upper left point y2 = mousePosY + radius; gfx_line((int)x1, (int)y1, (int)x2, (int)y2); // left line gfx_flush(); // actually draw the square break; } case 't': // display a green triangle outline { gfx_color(0, 255, 0); // changing color to green radius = 25; mousePosX = gfx_xpos(); // storing the current mouse position mousePosY = gfx_ypos(); x1 = mousePosX; // upper point y1 = mousePosY - radius; x2 = mousePosX + radius; // lower right point y2 = mousePosY + radius; gfx_line((int)x1, (int)y1, (int)x2, (int)y2); // right line x1 = mousePosX + radius; // lower right point y1 = mousePosY + radius; x2 = mousePosX - radius; // lower left point y2 = mousePosY + radius; gfx_line((int)x1, (int)y1, (int)x2, (int)y2); // bottom line x1 = mousePosX - radius; // lower left point y1 = mousePosY + radius; x2 = mousePosX; // upper point y2 = mousePosY - radius; gfx_line((int)x1, (int)y1, (int)x2, (int)y2); // left line gfx_flush(); break; } case 'c': // display a white circle outline { gfx_color(255, 255, 255); // change the color to white radius = 25; // circle radius of 5 pixels mousePosX = gfx_xpos(); mousePosY = gfx_ypos(); x2 = mousePosX + radius; // determine the first point y2 = mousePosY; for( c = 0; c <= 360; c++ ) // go through an entire circle (convert to radians) { x1 = x2; // assign the last point to be the first point for the next line y1 = y2; x2 = mousePosX + (cos(c*pi/180)*radius); y2 = mousePosY + (sin(c*pi/180)*radius); gfx_line((int)x1, (int)y1, (int)x2, (int)y2); } gfx_flush(); break; } case '3': case '4': case '5': case '6': case '7': case '8': case '9': // print a polygon with the alotted number of sides { switch ( user ) // find the integer number of sides { case '3': { polygon = 3; min = 80; // setting the minimum degrees between each point break; // the angle must be between this min and 360/sides } case '4': { polygon = 4; min = 65; break; } case '5': { polygon = 5; min = 62; break; } case '6': { polygon = 6; min = 55; break; } case '7': { polygon = 7; min = 50; break; } case '8': { polygon = 8; min = 45; break; } case '9': { polygon = 9; min = 39; break; } } gfx_color(150, 0, 150); // change the color to purple radius = 50; randomDegrees = 0; mousePosX = gfx_xpos(); mousePosY = gfx_ypos(); x2 = mousePosX + radius; // determine the first point y2 = mousePosY; for( c = 1; c < polygon; c++ ) // making only the specified number of points { x1 = x2; // assign the last point to be the first point for the next line y1 = y2; randomDegrees = randomDegrees + (min + (rand() % ((360 / polygon)-min+2))); // random integer degree limit // the degrees added are in the range of min to max, where the max is dependent on the number of sides x2 = mousePosX + (cos(randomDegrees*pi/180)*radius); y2 = mousePosY + (sin(randomDegrees*pi/180)*radius); gfx_line((int)x1, (int)y1, (int)x2, (int)y2); } x1 = x2; // for the last point we need to make sure it returns to the original point y1 = y2; x2 = mousePosX + radius; y2 = mousePosY; gfx_line((int)x1, (int)y1, (int)x2, (int)y2); gfx_flush(); break; } case ' ': // clear the graphics window { gfx_clear(); break; } case 'q': // quit the graphics window { break; } } if( user == 'q' ) // quitting actual while loop { break; } } }
int main (void) { char c; int xsize=350, ysize=350, vseed, j; float x=175, y=175, vx, vy, xMouse, yMouse, dt=1, r=15, ypos, i, di=.1, pi=3.14159; gfx_open(xsize, ysize, "Bouncing Ball"); gfx_color(0, 200, 100); srand(time(0)); vseed = rand()%100; vx = cos((float)vseed/15.915); vy = sin((float)vseed/15.915); while (1) { for (i=0; i<2*pi; i+=di) { gfx_line(r*cos(i)+x, -r*sin(i)+y, r*cos(i+di)+x, -r*sin(i+di)+y); } gfx_flush(); usleep(dt*10000); gfx_clear(); x = x+vx*dt; y = y+vy*dt; if ( x<=15 || x>=335 ) vx = -vx; if ( y<=15 || y>=335 ) vy = -vy; j = gfx_event_waiting(); if (j) { c = gfx_wait(); switch (c) { case 'q': return 0; case 1: xMouse = gfx_xpos(); yMouse = gfx_ypos(); x = xMouse; y = yMouse; vseed = rand()%100; vx = cos((float)vseed/15.915); vy = sin((float)vseed/15.915); break; } } } return 0; }
int main (void) { // Declare all of the variables that will be used in the function char c; int xsize=400, ysize=400, j; float pi=3.14159, rClock, xcc, ycc, rDot, yDotNeg, yDotPos, xDotNeg, xDotPos, bhPos, shPos, bhVel=-2*pi/60, shVel=bhVel/12, i, di=0.01, k, dk=2*pi/3; // Initializes the positions of the big and small hands of the clock bhPos = pi/2; shPos = pi/2; // From here to the first gfx command, all of the size sensitive variables are initialized xcc = xsize/2; ycc = ysize/2; if (xsize <= ysize) { rClock = xsize/2; rDot = xsize/20; } else { rClock = ysize/2; rDot = ysize/20; } yDotNeg = ycc + rClock-20; yDotPos = ycc - rClock+20; xDotNeg = xcc - rClock+20; xDotPos = xcc + rClock-20; //creates screen size and caption gfx_open(xsize, ysize, "Clock - 's'- switch direction, 'q'- quit"); //While loop that generates the clock while (1) { for (i=0; i<=2*pi; i+=di) { gfx_color(255, 255, 255); gfx_line(xcc + rClock*cos(i), ycc + rClock*sin(i), xcc + rClock*cos(i+di), ycc + rClock*sin(i+di)); } for (k=0; k<=2*pi; k+=dk) { gfx_color(0, 100, 200); gfx_line(xcc + rDot*sin(k), yDotNeg + rDot*-cos(k), xcc + rDot*sin(k+dk), yDotNeg + rDot*-cos(k+dk)); } for (k=0; k<=2*pi; k+=dk) { gfx_color(0, 100, 200); gfx_line(xcc + rDot*sin(k), yDotPos + rDot*cos(k), xcc + rDot*sin(k+dk), yDotPos + rDot*cos(k+dk)); } for (k=0; k<=2*pi; k+=dk) { gfx_color(0, 100, 200); gfx_line(xDotNeg + rDot*cos(k), ycc + rDot*sin(k), xDotNeg + rDot*cos(k+dk), ycc + rDot*sin(k+dk)); } for (k=0; k<=2*pi; k+=dk) { gfx_color(0, 100, 200); gfx_line(xDotPos + rDot*-cos(k), ycc + rDot*sin(k), xDotPos + rDot*-cos(k+dk), ycc + rDot*sin(k+dk)); } for (k=0; k<=2*pi; k+=dk/4) { gfx_color(0, 100, 200); gfx_line(xcc + rClock*cos(k)*10/11, ycc + rClock*sin(k)*10/11, xcc + rClock*cos(k)*9/11, ycc + rClock*sin(k)*9/11); } gfx_color(0, 200, 100); gfx_line(xcc, ycc, xcc + rClock*9/10 * cos(bhPos), ycc + rClock*9/10 * -sin(bhPos)); gfx_line(xcc, ycc, xcc + rClock*5/10 * cos(shPos), ycc + rClock*5/10 * -sin(shPos)); bhPos += bhVel; shPos += shVel; gfx_flush(); usleep(300000); gfx_clear(); j = gfx_event_waiting(); if ( j==1 ) { c = gfx_wait(); switch (c) { case 's': bhVel = -bhVel; shVel = -shVel; break; case 'q': return 0; } } } return 0; }
char welcome(void) { gfx_clear_color(0,255,0); //green gfx_clear(); gfx_color(255,255,255); //Frogger gfx_fill_rectangle(50,160,40,140); gfx_fill_rectangle(90,160,100,20); gfx_fill_rectangle(90,220,80,20); //fRogger gfx_fill_rectangle(210,160,40,140); gfx_fill_rectangle(250,160,80,20); gfx_fill_rectangle(310,180,40,60); gfx_fill_rectangle(290,220,20,20); gfx_fill_rectangle(250,240,60,20); gfx_fill_rectangle(270,260,60,20); gfx_fill_rectangle(300,280,60,20); //frOgger gfx_fill_rectangle(370,180,40,100); gfx_fill_rectangle(470,180,40,100); gfx_fill_rectangle(390,160,100,20); gfx_fill_rectangle(390,280,100,20); //froGger gfx_fill_rectangle(570,160,100,20); gfx_fill_rectangle(550,180,40,20); gfx_fill_rectangle(530,200,40,60); gfx_fill_rectangle(550,260,40,20); gfx_fill_rectangle(570,280,100,20); gfx_fill_rectangle(630,260,40,20); gfx_fill_rectangle(610,240,60,20); //frogGer gfx_fill_rectangle(730,160,100,20); gfx_fill_rectangle(710,180,40,20); gfx_fill_rectangle(690,200,40,60); gfx_fill_rectangle(710,260,40,20); gfx_fill_rectangle(730,280,100,20); gfx_fill_rectangle(790,260,40,20); gfx_fill_rectangle(770,240,60,20); //froggEr gfx_fill_rectangle(850,160,40,140); gfx_fill_rectangle(890,160,100,20); gfx_fill_rectangle(890,220,80,20); gfx_fill_rectangle(890,280,100,20); //froggeR gfx_fill_rectangle(1010,160,40,140); gfx_fill_rectangle(1050,160,80,20); gfx_fill_rectangle(1110,180,40,60); gfx_fill_rectangle(1090,220,20,20); gfx_fill_rectangle(1050,240,60,20); gfx_fill_rectangle(1070,260,60,20); gfx_fill_rectangle(1100,280,60,20); gfx_text(530,325, "CSE20211: Final Project"); gfx_text(510,345, "Sara Cunningham and Jenna Wilson"); gfx_text(470,365, "Press any key to play Frogger. Press \'q\' to quit"); return gfx_wait(); } //end welcome
char blood(void) { gfx_clear_color(255,0,0); gfx_clear(); gfx_color(255,255,255); //Game over gfx_fill_rectangle(330,160,100,20); gfx_fill_rectangle(310,180,40,20); gfx_fill_rectangle(290,200,40,60); gfx_fill_rectangle(310,260,40,20); gfx_fill_rectangle(330,280,100,20); gfx_fill_rectangle(390,260,40,20); gfx_fill_rectangle(370,240,60,20); //gAme over gfx_fill_rectangle(450,200,40,100); gfx_fill_rectangle(470,180,40,20); gfx_fill_rectangle(490,160,60,20); gfx_fill_rectangle(530,180,40,20); gfx_fill_rectangle(550,200,40,100); gfx_fill_rectangle(490,240,60,20); //gaMe over gfx_fill_rectangle(610,160,40,140); gfx_fill_rectangle(650,180,20,40); gfx_fill_rectangle(670,200,20,40); gfx_fill_rectangle(690,180,20,40); gfx_fill_rectangle(710,160,40,140); //gamE over gfx_fill_rectangle(770,160,40,140); gfx_fill_rectangle(810,160,100,20); gfx_fill_rectangle(810,220,80,20); gfx_fill_rectangle(810,280,100,20); //game Over gfx_fill_rectangle(290,370,40,100); gfx_fill_rectangle(390,370,40,100); gfx_fill_rectangle(310,350,100,20); gfx_fill_rectangle(310,470,100,20); //game oVer gfx_fill_rectangle(450,350,40,80); gfx_fill_rectangle(550,350,40,80); gfx_fill_rectangle(470,430,40,20); gfx_fill_rectangle(530,430,40,20); gfx_fill_rectangle(490,450,60,20); gfx_fill_rectangle(510,470,20,20); //game ovEr gfx_fill_rectangle(610,350,40,140); gfx_fill_rectangle(650,350,100,20); gfx_fill_rectangle(650,410,80,20); gfx_fill_rectangle(650,470,100,20); //game oveR gfx_fill_rectangle(770,350,40,140); gfx_fill_rectangle(810,350,80,20); gfx_fill_rectangle(870,370,40,60); gfx_fill_rectangle(850,410,20,20); gfx_fill_rectangle(810,430,60,20); gfx_fill_rectangle(830,450,60,20); gfx_fill_rectangle(860,470,60,20); //directions to continue gfx_text(450, 520, "Press any key to play Frogger again. Press \'q\' to quit."); return gfx_wait(); } //end blood
int startGame(Pipe* lead, Pipe* trail, int scrWidth, int scrHeight) { int loop = 1; int score = 0; double birdX = 150, birdY = 275, birdR = 12, birdV = 0, t = .05; int flap = 0; //#1 - to see game over screen make this while (loop < 5) and uncomment #2 while (loop) { //if bird touches the drawpipe loop = 2 gfx_clear(); //draw background drawBackground(scrWidth, scrHeight); //check for loss if ((lead->leadingX-3 <= birdX+16 && lead->trailingX+3 >= birdX-16) && (lead->topHeight >= birdY-16 || lead->bottomHeight <= birdY-16+24)) break; if (birdY-16+24 >= scrHeight) break; flap = drawBird(birdX, birdY, birdR, flap); updateBird(&birdY, &birdV, &t, birdY, birdV, t); if(gfx_event_waiting()){ char c = gfx_wait(); if(c == ' ') birdV = -40; } //draw leading pipe drawPipe(lead, scrWidth, scrHeight); updatePipe(lead); //increase the score upon passing through a pair of pipes if(lead->trailingX == birdX){ score++; } if (lead->trailingX < scrWidth/2) { drawPipe(trail, scrWidth, scrHeight); updatePipe(trail); } if (lead->trailingX <= 0) { //swap the leader Pipe *temp = lead; lead = trail; trail = temp; initializePipe(trail, scrWidth, scrHeight); } //print the score on the top of the screen printScore(score, scrWidth); gfx_flush(); usleep(10000); t+=.001; //loop++ #2 -- to see end screen uncomment this and above } return score; }
int main(){ int xposS = 370; char c; //user input int xdist = 0, ydist =0; int dx = 1; int Bnum = 0; int posB[2][10] = {0}; unsigned int i, j; gfx_open(SIZEX, SIZEY, "Space Invaders"); //open window initializeBullets(posB); while(true){ gfx_clear(); //clear screen dispScore(); uShooter(xposS); drawAliens(xdist, ydist); if(gfx_event_waiting()){ //if true c = gfx_wait(); //find choice if (c == 'b'){ xposS-=10; if (xposS <= 0) xposS = 0; }else if (c == 'n'){ xposS +=10; if (xposS >= SIZEX-30) xposS = SIZEX-30; }else if (c == ' '){ for(i=0; i<10; i++){ if(posB[0][i] == -1){ posB[0][i] = drawBullet(xposS); posB[1][i] = YPOS; } } }else if (c == 'q') return 0; else continue; } xdist+=dx; if(xdist>=370 || xdist <=-100){ dx*=-1; } if(xdist == 370 || xdist ==-100){ ydist +=5; } if(ydist == 225) return 0; //change lives num for(j=0; j<10; j++){ if(posB[0][j] != -1){ moveBullet(posB, j); } checkBullet(posB,j); } usleep(25000); gfx_flush(); } }
int main() { // Print instructions for user printf("Asteroid!\nTry to collect as many colored circles as you can.\nThe game is simple. Use the arrow keys to navigate your rocket.\nUp accelerates\nDown brakes immediately\nLeft and Right turn your rocket.\n\nHave fun!!\n"); // Define variables int xSize=800, ySize=600; char c; int stillgoing=1; float xc=xSize/2, yc=ySize/2, *pxc=&xc, *pyc=&yc; float dr=.1; // rotation increment int sign = 1; // rotation sign int r = 15; // box radius int i; // box counter int j; // triangle counter double rotatea=0; // rotation angle of rocket float dx=0, dy=0; // chane in position of rocket float dt=0; // change in time for acceleration float accel=.07; // acceleration of rocket srand(time(NULL)); int rCir=0; // radius of circle int xCir=xc,yCir=yc; // position of circle's center int rC=255,gC=255,bC=255; // circle color int cirCount=-1; // number of circles collected(starts at -1 because initial circle is created at center, then random placement begins. Fisrt circle placed at center does not count towards total. int isaccel=0; double rotatet=rotatea; double yRotate=0, xRotate=0; float xB, yB; // position of fired shot gfx_open(xSize,ySize,"Rocketship!"); startGame(xSize, ySize); while(stillgoing==1) { gfx_clear(); drawAsteroid(xCir,yCir,rCir,rC,gC,bC); drawRocket(xc,yc,rotatet,r); // uses rotatet, because this will draw the rocket as turning when the left/right arrows are pressed scoreboard(cirCount); gfx_flush(); usleep(2000); if(gfx_event_waiting()){ c=gfx_wait(); switch(c){ case 'R': // Accelerate dt+=.01; dx+=accel*dt; // increases speed dx by increasing the time and adding to dx if(dx>.8) dx=.8; // this caps dx and dy at 1. Thus the maximum speed is dx=dy=1 dy=dx; isaccel=1; // the rocket is currently accelerating break; case 'Q': // Turn left rotatet-=dr; // decreases angle of rotation break; case 'S': // Turn right //printf("ROTATET: %lf\n",rotatet); rotatet+=dr; // increases angle of rotation //printf("ROTATET: %lf\n",rotatet); break; case 'T': // Brake dx=0; // sets speed dx and dy to 0 dy=0; rotatea=rotatet; yRotate=0; xRotate=0; break; case ' ': fire(rotatet, xc, yc, r); break; case 'q': // Quit stillgoing=0; printf("\nYou collected %i circles!\n",cirCount); break; default: break; } } if(isaccel==1){ printf("ROTATEA: %lf\n",rotatea); if(sin(rotatea)<sin(rotatet)){ yRotate-=fabs(sin(rotatea+.1)); printf("check1\n"); }else if(sin(rotatea)>sin(rotatet)){ yRotate+=fabs(sin(rotatea-.1)); printf("check2\n"); }//else direction of acceleration is equal to direction rocket is facing if(cos(rotatea)>cos(rotatet)){ xRotate-=fabs(cos(rotatea+.1)); printf("check3\n"); }else if(cos(rotatea)<cos(rotatet)){ xRotate+=fabs(cos(rotatea-.1)); printf("check4\n"); } if(xRotate!=0) rotatea=atan(yRotate/xRotate); printf("yRotate: %lf\nxRotate: %lf\nrotatea: %lf\nrotatet: %lf\n",yRotate,xRotate,rotatea,rotatet); } xc+=dx*cos(rotatea); // implements x and y speed(dx and dy) by changing position(xc,yc) of rocket at varying rate yc+=dy*sin(rotatea); // why i use rotatea, not rotatet: the rocket will only update the angle of movement, if the rocket is accelerating. This way, you can turn the rocket while it is moving in another direction isaccel=0; // Transports rocket from one side of screen to other wormhole(pxc,pyc,xSize,ySize); // Creates new circle if rocket comes too close if(sqrt(pow((xc-xCir),2)+pow((yc-yCir),2))<1.6*r+rCir){ cirCount++; rCir=10+rand()%20; do{ xCir=rand()%xSize; yCir=rand()%ySize; rC=100+rand()%155; gC=100+rand()%155; bC=100+rand()%155; }while(sqrt(pow((xc-xCir),2)+pow((yc-yCir),2))<50); } } }
void deathAnimation(Location *pacman,Location ghosts[],int xtl,int ytl,int boardHeight,int boardWidth,int height,int width,int lives,int state[],int score,int newScore[],int frightenLoop[],int loop){ /* Death animation is in four states, first full circle, then pacman oriented upward, then half circle, then small sliver below, then gone, then explode */ int r=radius/2-1; int timer=1000000; int x=xtl+pacman->x*radius+radius/2; //first location to draw, will increment 5 times int y=ytl+pacman->y*radius+radius/2; animateMotion(xtl,ytl,boardHeight,boardWidth,pacman,ghosts,height,width,lives,state,score,newScore,frightenLoop,loop); gfx_wait(); gfx_clear(); drawBoard(xtl,ytl,boardHeight,boardWidth,height,width,lives,5,score); gfx_color(255,255,0); gfx_fill_circle(x,y,radius/2-1); //gfx_wait(); gfx_wait(); //usleep(timer); gfx_clear(); drawBoard(xtl,ytl,boardHeight,boardWidth,height,width,lives,6,score); drawPacman(x,y,up,1); gfx_wait(); //usleep(timer); gfx_clear(); drawBoard(xtl,ytl,boardHeight,boardWidth,height,width,lives,7,score); gfx_color(255,255,0); drawPacman(x,y,0,0); gfx_color(0,0,0); gfx_fill_rectangle(x-radius/2-1,y-radius/2+1,radius,radius/2-1); gfx_wait(); //usleep(timer); gfx_clear(); drawBoard(xtl,ytl,boardHeight,boardWidth,height,width,lives,8,score); gfx_color(255,255,0); gfx_fill_circle(x,y,radius/2-1); gfx_color(0,0,0); gfx_fill_triangle(x-r,y-r,x+r,y-r,up); gfx_fill_triangle(x+r,y-r,x+r,y+r,right); gfx_fill_triangle(x-r,y-r,x-r,y+r,left); gfx_wait(); //usleep(timer); gfx_clear(); drawBoard(xtl,ytl,boardHeight,boardWidth,height,width,lives,9,score); gfx_wait(); //usleep(timer); gfx_clear(); drawBoard(xtl,ytl,boardHeight,boardWidth,height,width,lives,10,score); gfx_color(255,255,0); float angle=2*M_PI/5; //partial snowflake int i,xnew[5],ynew[5]; for(i=1;i<=5;i++){ xnew[i-1]=x+r*cos(i*angle+3*M_PI/2); ynew[i-1]=y+r*sin(i*angle+3*M_PI/2); gfx_line(x,y,xnew[i-1],ynew[i-1]); } gfx_wait(); //usleep(timer); gfx_clear(); drawBoard(xtl,ytl,boardHeight,boardWidth,height,width,lives,11,score); gfx_color(255,255,0); angle=2*M_PI/5; //snowflake with short lines for(i=1;i<=5;i++){ xnew[i-1]=x+r*cos(i*angle+3*M_PI/2); ynew[i-1]=y+r*sin(i*angle+3*M_PI/2); gfx_line((x+xnew[i-1])/2,(y+ynew[i-1])/2,xnew[i-1],ynew[i-1]); } gfx_wait(); //usleep(timer); }
int main() { memcpy(board2,board,sizeof(int)*rows*columns); int height=800,width=800; //height and width of screen gfx_open(width,height,"Pacman"); gfx_clear_color(0,0,0); gfx_clear(); int boardHeight=radius*rows,boardWidth=radius*columns;//Height of the board int xtopleft=width/2-boardWidth/2; //x coord of top left corner of board int ytopleft=height/2-boardHeight*9/16; //y coord of top left corner of board char movement; int i,lives=3;//start with 3 lives int win=0; int active=0; //this changes depending on number of dots left int initialDots=dotsNumber(); int remainingDots=dotsNumber(); int score=0; int loop[4]={0,0,0,0}; int frightenLoop[4]={0}; int newScore[3]={0,0,0};//values [0]:new score to display(when ghost is killed),[1]:xvalue,[2]:yvalue Location pacman; Location ghosts[4];// enumerated to blinky, pinky, inky, clyde; /*There are four states: 0: Chase, 1: Scatter, 2: Frighten, 3: Dead, and 4: Housed */ int state[4]={scatter,scatter,scatter,scatter}; while(1){ titleScreen(height,width,ghosts,&pacman,state); score=0; lives=3; win=0; for(i=0;i<=3;i++) loop[i]=0; //reset the game resetBoard(); gfx_wait(); /* This is the gameplay loop, will repeat every time a life is lost */ while(lives>0){ gfx_clear(); /* Initialize pacman's location */ pacman.x=pacman.prevX=7; pacman.y=pacman.prevY=12; pacman.orientation=right; /* Initialize ghost's locations */ for(i=blinky;i<=clyde;i++){state[i]=scatter;} ghosts[blinky].x=7; ghosts[blinky].prevX=8; ghosts[blinky].y=ghosts[blinky].prevY=6; ghosts[blinky].orientation=left; ghosts[pinky].x=7; ghosts[pinky].prevX=7; ghosts[pinky].y=7; ghosts[pinky].prevY=8; ghosts[pinky].orientation=up; ghosts[inky].x=ghosts[inky].prevX=6; ghosts[inky].y=7; ghosts[inky].prevY=8; ghosts[inky].orientation=right; ghosts[clyde].x=ghosts[clyde].prevX=8; ghosts[clyde].y=7; ghosts[clyde].prevY=8; ghosts[clyde].orientation=left; /* These two statements draw pacman and the board to begin the program */ drawBoard(xtopleft,ytopleft,boardHeight,boardWidth,height,width,lives,2,score); drawPacman(xtopleft+radius*pacman.x+radius/2,ytopleft+radius*pacman.y+radius/2,pacman.orientation,0); for(i=blinky;i<=clyde;i++){ drawGhost(xtopleft+radius*ghosts[i].x+radius/2,ytopleft+radius*ghosts[i].y+radius/2,i,ghosts[i].orientation,state,frightenLoop[i]); } /* This loop is the gameplay after all initialization */ while(1){ /*if(gfx_event_waiting()){prevMovement=movement;*/ movement=gfx_wait();//} /* This block updates pacman position, checks for death * then updates ghosts' positions, then checks for death again */ movePacman(&pacman,ghosts,movement,xtopleft,ytopleft,boardHeight,boardWidth,active,state,&score,frightenLoop); if(checkDeath(&pacman,ghosts,xtopleft,ytopleft,boardHeight,boardWidth,height,width,lives,state,&score,newScore,frightenLoop,loop[0])){ //pacman's death? for(i=0;i<=3;i++) loop[i]=0; //reset the game lives--; //decrease the lives printf("LIVES: %i\n",lives); break; } targetGhosts(ghosts,&pacman,xtopleft,ytopleft,boardHeight,boardWidth,active,state); if(checkDeath(&pacman,ghosts,xtopleft,ytopleft,boardHeight,boardWidth,height,width,lives,state,&score,newScore,frightenLoop,loop[0])){ //pacman's death? for(i=0;i<=3;i++) loop[i]=0; //reset the game lives--; //decrease the lives printf("LIVES: %i\n",lives); break; } ghostState(loop,state,frightenLoop); active=activeGhosts(ghosts,loop); /* The next function animates the motion of all of the objects (pacman and ghosts */ animateMotion(xtopleft,ytopleft,boardHeight,boardWidth,&pacman,ghosts,height,width,lives,state,score,newScore,frightenLoop,loop[0]); /* The case to exit the loop is winning, when there are no dots left */ if(dotsNumber()==0){ //The player got all the dots, they won the game win=1; break; } } if(lives<=0){ gfx_clear(); printf("\n\nGAME OVER\n\n"); drawBoard(xtopleft,ytopleft,boardHeight,boardWidth,height,width,lives,0,score); if(tolower(gfx_wait())=='n'){ return 0; }//This will start the entire game over including the title screen else break;} else if(win){ printf("\n\nWINNER!\n\n"); drawBoard(xtopleft,ytopleft,boardHeight,boardWidth,height,width,lives,win,score); if(tolower(gfx_wait())=='n'){ return 0; }//This will end the game else break;} } } }
int main(){ char m; int x, y, r; float t, dt, ax, ay, bx, by; gfx_open(500, 400, "Symbol.c"); while(m != 'q') { m = gfx_wait(); switch(m){ case 'c': x = gfx_xpos(); y = gfx_ypos(); gfx_color(255, 255, 255); gfx_circle(x, y, 20); gfx_flush; break; case 't': x = gfx_xpos(); y = gfx_ypos(); gfx_color(0, 255, 0); r = 20; t = 0; for(dt = 0; dt <= 2*M_PI+1; dt += (2*M_PI)/3){ ax = x + r*cos(t); ay = y + r*sin(t); bx = x + r*cos(dt); by = y + r*sin(dt); gfx_line(ax, ay, bx, by); t = dt; } break; case 1: x = gfx_xpos(); y = gfx_ypos(); gfx_color(0, 0, 200); r = 20; t = 0; for(dt = 0; dt <= 2*M_PI+1; dt += (2*M_PI)/4){ ax = x + r*cos(t); ay = y + r*sin(t); bx = x + r*cos(dt); by = y + r*sin(dt); gfx_line(ax, ay, bx, by); t = dt; } break; case '3': x = gfx_xpos(); y = gfx_ypos(); gfx_color(200, 0, 200); r = 20; t = 0; for(dt = 0; dt <= 2*M_PI+1; dt += (2*M_PI)/3){ ax = x + r*cos(t); ay = y + r*sin(t); bx = x + r*cos(dt); by = y + r*sin(dt); gfx_line(ax, ay, bx, by); t = dt; } break; case '4': x = gfx_xpos(); y = gfx_ypos(); gfx_color(255, 0, 255); r = 20; t = 0; for(dt = 0; dt <= 2*M_PI+1; dt += (2*M_PI)/4){ ax = x + r*cos(t); ay = y + r*sin(t); bx = x + r*cos(dt); by = y + r*sin(dt); gfx_line(ax, ay, bx, by); t = dt; } break; case '5': x = gfx_xpos(); y = gfx_ypos(); gfx_color(255, 0, 255); r = 20; t = 0; for(dt = 0; dt <= 2*M_PI+1; dt += (2*M_PI)/5){ ax = x + r*cos(t); ay = y + r*sin(t); bx = x + r*cos(dt); by = y + r*sin(dt); gfx_line(ax, ay, bx, by); t = dt; } break; case '6': x = gfx_xpos(); y = gfx_ypos(); gfx_color(255, 0, 255); r = 20; t = 0; for(dt = 0; dt <= 2*M_PI+1; dt += (2*M_PI)/6){ ax = x + r*cos(t); ay = y + r*sin(t); bx = x + r*cos(dt); by = y + r*sin(dt); gfx_line(ax, ay, bx, by); t = dt; } break; case '7': x = gfx_xpos(); y = gfx_ypos(); gfx_color(255, 0, 255); r = 20; t = 0; for(dt = 0; dt <= 2*M_PI+1; dt += (2*M_PI)/7){ ax = x + r*cos(t); ay = y + r*sin(t); bx = x + r*cos(dt); by = y + r*sin(dt); gfx_line(ax, ay, bx, by); t = dt; } break; case '8': x = gfx_xpos(); y = gfx_ypos(); gfx_color(255, 0, 255); r = 20; t = 0; for(dt = 0; dt <= 2*M_PI+1; dt += (2*M_PI)/8){ ax = x + r*cos(t); ay = y + r*sin(t); bx = x + r*cos(dt); by = y + r*sin(dt); gfx_line(ax, ay, bx, by); t = dt; } break; case '9': x = gfx_xpos(); y = gfx_ypos(); gfx_color(255, 0, 255); r = 20; t = 0; for(dt = 0; dt <= 2*M_PI+1; dt += (2*M_PI)/9){ ax = x + r*cos(t); ay = y + r*sin(t); bx = x + r*cos(dt); by = y + r*sin(dt); gfx_line(ax, ay, bx, by); t = dt; } break; } } }
int main () { char userInput; int i, sideLength; double theta, dtheta, numberSides; double x, y; double x_new, y_new; gfx_open(500,500,"Symbol_Emily_Koh"); //gfx_xpos(); gets x coordinates of mouse pointer //gfx_ypos(); gets y coordinates of mouse pointer while (1) { gfx_event_waiting(); if (gfx_event_waiting() == True) { userInput = gfx_wait(); } if (userInput == 1) { //if user clicks mouse button 1 then display blue square gfx_color(0, 0, 255); //dictates color as blue x = gfx_xpos(); y = gfx_ypos(); gfx_line(x-50, y-50, x-50, y+50); gfx_line(x-50, y-50, x+50, y-50); gfx_line(x+50, y-50, x+50, y+50); gfx_line(x+50, y+50, x-50, y+50); } else if (userInput == 't') { //if user types in t then display green triangle gfx_color(0, 255, 0); //dictates color as green x = gfx_xpos(); y = gfx_ypos(); gfx_line(x-50, y-50, x+50, y-50); gfx_line(x-50, y-50, x, y+50); gfx_line(x, y+50, x+50, y-50); } else if (userInput == 'c') { //if user types in c then display white circle gfx_color(255, 255, 255); //dictates color as white x = gfx_xpos(); y = gfx_ypos(); gfx_circle(x, y, 50); //circle centered at x, y, and with radius 50 } else if (userInput >= '3' && userInput <= '9') { //if user types in numbers 3~9, display purple polygon with that many sides sideLength = 30; numberSides = userInput - '0'; //makes numberSides = number of polygon sides dtheta = (2*M_PI/numberSides); //calculate the angle at which line will rotate theta = 0; gfx_color(171, 92, 223); //dictates color as purple x = gfx_xpos(); y = gfx_ypos(); x = x + (sideLength/2); //to center polygon, shift polygon by this x and the following y value y = y + (sideLength/(2*tan(M_PI/numberSides))); //apothem - perpendicular y distance for (i = 0; i < numberSides; i++) { theta += dtheta; x_new = x + sideLength*cos(theta); y_new = y - sideLength*sin(theta); gfx_line(x, y, x_new, y_new); gfx_flush(); x = x_new; y = y_new; } userInput = '0'; } else if (userInput == 'q') { //if user types in q then quit program break; } } // gcc symbol.c gfx_mac.o -lX11 -lm -I/opt/X11/include/ -L/opt/X11/lib/ -o symbol return 0; }
void flyship(void) { double x, y; double theta; char c; int i, go, x1ship, x2ship, y1ship, y2ship; int score[2]; score[0]=0; score[1]=3; char str[100]; go=0; //will tell drawship function if forward button is held down so flames can display int iterate=0; //counts iterations of loop to determine when to move asteroids x=350; y=350; //initial conditions theta=0; //the 2d array roids will contain 32 elements, one for each potential asteroid //and each element will have four numbers for the asteroids' properties double astr[32][5]; double shots[32][5]; //2d array for the shots out of the ship and its properties initialize(astr); initialize(shots); drawship(x, y, theta, go); double speed=0; while(1){ if(gfx_event_waiting()) { c=gfx_wait(); if (score[1]==0 || c=='q') { //this will run when lives are 0 and display the score gfx_clear(); sprintf(str, "Score: %i Press q to exit game", score[0], score[1]); gfx_text(250, 350, str); c=gfx_wait(); if(c=='q'){ break; } } if(c=='d'){ theta=theta-PI/20; //this will rotate the ship } if(c=='a'){ theta=theta+PI/20; } if(c=='w'){ speed=speed+1; //moves ship forward if (speed>5){ speed=5; } go=1; //will tell drawship function that ship is moving } if(c=='o') { shoot(shots, theta, x, y); } if(x>700){ x=1; } else if(x<0){ x=699; } else if(y>700){ //this keeps the ship from flying off the screen y=1; } else if(y<0){ y=699; } } gfx_clear(); usleep(10000); x1ship= x+20*cos(theta+4*PI/3); //these variables create a sort of box around the ship, which is later used to check the ship's coordinates for collisions y1ship= y; x2ship= x+20*cos(theta+5*PI/3); y2ship= y-20*sin(theta); move_shots(shots); destroy(astr, shots, score); //checks whether an asteroid should be destroyed move_asteroids(astr, shots, x1ship, y1ship, x2ship, y2ship, &x, &y, &speed, &theta, score); x=x+speed*cos(theta+PI/2); //iterates coordinates of ship based on changes to theta and speed y=y-speed*sin(theta+PI/2); drawship(x, y, theta, go); if(go>=1){ go=go-1; //keeps go at 1 so it stops when stop pressing forward button } if(speed>.025){ speed=speed-.025; } iterate++; sprintf(str, "Score: %d Lives: %d", score[0], score[1]); gfx_text(20, 680, str); } }
int title_screen(const char *commands[100], int *f, double noteinfo[2][1000], char output[1000]){ int xsize=1250; int ysize=1000; int xpos; int ypos; int height=400; int width=(xsize-50)/16; int i,j; char input[20]; char initial[1000]; char c; gfx_open(xsize, ysize, "Digital Piano"); quit_button(); draw_title(100); draw_name(50); draw_name2(50); draw_button(295,600,70,310); draw_button(645,600,70,310); button_label(60); button_label2(60); while (1){ c=gfx_wait(); xpos=gfx_xpos(); ypos=gfx_ypos(); //user clicks the quit button if (xpos>=1110 && xpos<=1210 && ypos>=850 && ypos<=890){ return 0; } //user clicks free play button if (xpos>=295 && xpos<=605 && ypos>=600 && ypos<=670){ gfx_clear(); piano_graphic(commands, f); } //user clicks load file button if (xpos>=645 && xpos<=955 && ypos>=600 && ypos<=670){ terminal_message(); printf("Please enter the name of the file, including the extension.\nThe file content should follow the same format as the examples in lab 8:\n"); scanf("%s", input); //scans file name into input FILE *music=fopen(input, "r"); if ((music=fopen(input, "r")) == NULL){ //returns error if file not found puts("File could not be opened"); return 0; } else{ //scans the file into output for (j=0; j<1000; j++){ fscanf(music, "%c", &output[j]); if (output[j]=='X'){ break; } } } piano2(noteinfo, output); //fork to play sound while lighting up keys if (fork()){ system("play sound.wav"); } else{ gfx_clear(); draw_piano(width, height); draw_arrow(50, f); gfx_color(255,255,255); draw_box(); octave_label(30); quit_button(); gfx_flush(); key_animation(noteinfo, f); return 0; } } } }
int main(void) { // Declare & Define Variables int xsize; // Window dimensions int ysize; char input; // For determining user input float curxpos; // Position (Location) of the shape float curypos; float newx; // For drawing the circle float newy; float oldx; float oldy; double deltat; // Change in time float vx; // Velocities in their respective directions float vy; int radius; // Radius of the figure float PI = 3.14159; // Constant PI float theta; // Angles char c; // Check for input float theta2; // Second theta for rotation radius = 30; xsize = 400; ysize = 400; curxpos = radius; curypos = ysize-radius; oldx = curxpos-radius; oldy = curypos; vx = 0; vy = 0; srand(time(NULL)); deltat = 0.01; theta2 = 0; // Open a new window for drawing gfx_open(xsize, ysize, "Rotate"); while(input != 'q') { gfx_clear(); gfx_color(0,255,255); gfx_line(curxpos,curypos,curxpos+radius*cos(theta2),curypos+radius*sin(theta2)); gfx_line(curxpos,curypos,curxpos-radius*cos(theta2),curypos-radius*sin(theta2)); gfx_line(curxpos,curypos,curxpos+radius*cos(theta2),curypos-radius*sin(theta2)); gfx_line(curxpos,curypos,curxpos-radius*cos(theta2),curypos+radius*sin(theta2)); for (theta = 0; theta <= 2*PI; theta+=.01) { gfx_color(255,0,0); newx = cos(theta)*radius+curxpos; newy = sin(theta)*radius+curypos; gfx_line(oldx,oldy,newx,newy); oldx = newx; oldy = newy; c = gfx_event_waiting(); if (c == 1) { input = gfx_wait(); if (input == 1) { vx += 1; theta2 += PI/16; } if (input == 3) { vx -= 1; theta -= PI/16; } if (input =='1') { gfx_color(0,255,0); } if (input == '2') { gfx_color(0,0,255); } if (input == '3') { gfx_color(255,0,0); } if (input == '4') { gfx_color(255,255,255); } if (input == 'q') break; } } if (newx >= xsize || newx <= radius) { vx = -vx; } curxpos += vx; if (vx > 0) { theta2 += PI/16; } if (vx < 0) { theta2 -= PI/16; } if (vx == 0) { theta2 = curxpos/curypos; } gfx_flush(); usleep(deltat*1000000); } return 0; }