/** * Detects whether ball has collided with some object in window * by checking the four corners of its bounding box (which are * outside the ball's GOval, and so the ball can't collide with * itself). Returns object if so, else NULL. */ GObject detectCollision(GWindow window, GOval ball) { // ball's location double x = getX(ball); double y = getY(ball); // for checking for collisions GObject object; // check for collision at ball's top-left corner object = getGObjectAt(window, x, y); if (object != NULL) { //printf("check for collision at ball's top-left corner \n"); return object; } // check for collision at ball's top-right corner object = getGObjectAt(window, x + 2 * RADIUS, y); if (object != NULL) { //printf("check for collision at ball's top-right corner\n"); return object; } // check for collision at ball's bottom-left corner object = getGObjectAt(window, x, y + 2 * RADIUS); if (object != NULL) { //printf("check for collision at ball's bottom-left corner\n"); return object; } // check for collision at ball's bottom-right corner object = getGObjectAt(window, x + 2 * RADIUS, y + 2 * RADIUS); if (object != NULL) { //printf("check for collision at ball's bottom-right corner\n"); return object; } // no collision return NULL; }
/** * Detects whether laser has collided with some object in window * by checking the four corners of its bounding box (which are * outside the laser's GRect, and so the laser can't collide with * itself). Returns object if so, else NULL. */ GObject detectCollision2(GWindow window, GRect laser) { // ball's location double x = getX(laser); double y = getY(laser); // for checking for collisions GObject object2; // check for collision at ball's top-left corner object2 = getGObjectAt(window, x - 0.1, y - 0.1); if (object2 != NULL) { return object2; } // check for collision at ball's top-right corner object2 = getGObjectAt(window, x + LASERHEIGHT + 0.1, y - 0.1); if (object2 != NULL) { return object2; } // check for collision at ball's bottom-left corner object2 = getGObjectAt(window, x - 0.1, y + LASERWIDTH + 0.1); if (object2 != NULL) { return object2; } // check for collision at ball's bottom-right corner object2 = getGObjectAt(window, x + LASERHEIGHT + 0.1, y + LASERWIDTH + 0.1); if (object2 != NULL) { return object2; } // no collision return NULL; }
int main() { GWindow gw = newGWindow(600, 400); GRect rect = NULL; bool dragging = false; double startX = 0.0; double startY = 0.0; while (true) { GEvent e = waitForEvent(MOUSE_EVENT | KEY_EVENT); if (getEventType(e) == MOUSE_PRESSED) { startX = getX(e); startY = getY(e); rect = getGObjectAt(gw, startX, startY); dragging = (rect != NULL); if (!dragging) { rect = newGRect(startX, startY, 0, 0); setFilled(rect, true); add(gw, rect); } } else if (getEventType(e) == MOUSE_DRAGGED) { double x = getX(e); double y = getY(e); if (dragging) { move(rect, x - startX, y - startY); startX = x; startY = y; } else { double width = fabs(x - startX); double height = fabs(y - startY); x = fmin(x, startX); y = fmin(y, startY); setBounds(rect, x, y, width, height); } } else if (getEventType(e) == MOUSE_CLICKED) { if (rect != NULL) sendToFront(rect); } else if (getEventType(e) == KEY_TYPED) { if (rect != NULL) { string color = "BLACK"; switch (getKeyChar(e)) { case 'b': color = "BLUE"; break; case 'c': color = "CYAN"; break; case 'g': color = "GREEN"; break; case 'm': color = "MAGENTA"; break; case 'o': color = "ORANGE"; break; case 'r': color = "RED"; break; case 'w': color = "WHITE"; break; case 'y': color = "YELLOW"; break; } setColor(rect, color); } } } }
int selectMode(GWindow window) { int mode = 0; //button for 1 player mode GImage single = newGImage("single.png"); add(window, single); setLocation(single, WIDTH/2 - getWidth(single)/2, HEIGHT/2 - 45); //button for 2 player mode GImage two = newGImage("two.png"); add(window, two); setLocation(two, WIDTH/2 - getWidth(two)/2, HEIGHT/2 + 45); while (true) { // check for mouse event GEvent event = getNextEvent(MOUSE_EVENT); // if we heard one if (event != NULL) { // if the event was movement if (getEventType(event) == MOUSE_CLICKED) { // print click's coordinates //printf("%.0f,%.0f\n", getX(event), getY(event)); GObject object = getGObjectAt(window, getX(event), getY(event)); if(object != NULL && object == single ) { mode = 1; break; } if(object != NULL && object == two ) { mode = 2; break; } } } } // remove the buttons removeGWindow(window, single); removeGWindow(window, two); return mode; }
int main(int argc, char* argv[]) { // seed pseudorandom number generator srand48(time(NULL)); // instantiate window GWindow window = newGWindow(WIDTH, HEIGHT); // instantiate bricks initBricks(window); // instantiate ball, centered in middle of window GOval ball = initBall(window); // add ball to window add(window, ball); // instantiate paddle, centered at bottom of window GRect paddle = initPaddle(window); // add paddle to window add(window, paddle); // instantiate scoreboard, centered in middle of window, just above ball GLabel label = initScoreboard(window); // add label to window add(window, label); // declare laser GRect laser = NULL; // number of bricks initially int bricks = COLS * ROWS; // number of lives initially int lives = LIVES; // number of points initially int points = 0; // ball velocity double velocity_x = drand48() * 2 + 3; double velocity_y = drand48() * 2 + 3; bool ballMove = false; // GOD mode on bool God = false; if (argc == 2) { if (strcmp(argv[1], "GOD") == 0) { God = true; } } // keep playing until game over while (lives > 0 && bricks > 0) { // check for mouse event GEvent event = getNextEvent(MOUSE_EVENT); // when clicked, ball starts to move if (event != NULL) { // if the event was moved if (getEventType(event) == MOUSE_CLICKED) { ballMove = true; } } // ball movement if (ballMove == true) { move(ball, velocity_x, velocity_y); // when ball hits the left edge if (getX(ball) <= 0) { velocity_x *= -1; } // when ball hits the right edge else if (getX(ball) + getWidth(ball) >= getWidth(window)) { velocity_x *= -1; } // when ball hits the top edge else if (getY(ball) <= 0) { velocity_y *= -1; } // when ball hit the bottom edge else if (getY(ball) >= getHeight(window)) { lives -= 1; ballMove = false; setLocation(ball, WIDTH / 2, HEIGHT / 2); } pause(10); } // if God mode is on, paddle x-position follows ball if (God == true) { double y = getY(paddle); setLocation(paddle, getX(ball) + RADIUS - PADDLEWIDTH / 2.0, y); } // else if God mode is off, paddle follow mouse movement else { // if we heard one if (event != NULL) { // if the event was moved if (getEventType(event) == MOUSE_MOVED && ballMove == true) { double x = getX(event) - PADDLEWIDTH / 2; double y = getY(paddle); // ensure paddle doesn't go out of window if (getX(event) <= 0 + PADDLEWIDTH / 2) { setLocation(paddle, 0, y); } else if (getX(event)>= getWidth(window) - PADDLEWIDTH / 2) { setLocation(paddle, getWidth(window) - PADDLEWIDTH, y); } else { setLocation(paddle, x, y); } } } } // during gameplay, when mouse is clicked, shoot laser if (event != NULL) { if (ballMove == true && getEventType(event) == MOUSE_CLICKED && laser == NULL) { // instantiate laser laser = newGRect(getX(paddle) + PADDLEWIDTH / 2 - LASERWIDTH / 2, getY(paddle) - LASERHEIGHT, LASERWIDTH, LASERHEIGHT); setColor(laser, "RED"); setFilled(laser, true); add(window, laser); } } // if laser exists if (laser != NULL) { // detect collision of laser with object GObject object2 = detectCollision2(window, laser); if (object2 != NULL) { // if laser hits brick if (strcmp(getType(object2), "GRect") == 0 && object2 != paddle) { // remove laser and brick hit GObject laserObj = getGObjectAt(window, getX(laser), getY(laser)); removeGWindow(window, laserObj); laser = NULL; removeGWindow(window, object2); // bricks higher in the game’s grid are worth more points than are bricks lower in the game’s grid int brickheight = (100 - GAP * (ROWS + 1)) / ROWS; for (int k = 0; k < ROWS; k++) { if (getY(object2) == 50 + (GAP * (k + 1)) + (brickheight * k)) { points += ROWS - k; break; } } updateScoreboard(window, label, points); // decrease paddle width bricks -= 1; setSize(paddle, PADDLEWIDTH - PADDLEWIDTH * 2 / 4.0 * (((ROWS * COLS) - bricks) / (float)(ROWS * COLS)), PADDLEHEIGHT); // increase velocity of ball velocity_x += 0.1; velocity_y += 0.1; } // else if laser hits ball else if (object2 == ball) { GObject laserObj = getGObjectAt(window, getX(laser), getY(laser)); removeGWindow(window, laserObj); laser = NULL; break; } // else if laser over top edge else if (getY(laser) + LASERHEIGHT >= 0) { GObject laserObj = getGObjectAt(window, getX(laser), getY(laser)); removeGWindow(window, laserObj); laser = NULL; } } else { move(laser, 0, -velocity_laser); } } // detect collision of ball with object GObject object = detectCollision(window, ball); if (object != NULL) { // if ball collide with paddle if (object == paddle) { velocity_y *= -1; } // if ball collide with GRect object other than paddle (i.e. bricks) else if (strcmp(getType(object), "GRect") == 0) { velocity_y *= -1; removeGWindow(window, object); // bricks higher in the game’s grid are worth more points than are bricks lower in the game’s grid int brickheight = (100 - GAP * (ROWS + 1)) / ROWS; for (int k = 0; k < ROWS; k++) { if (getY(object) == 50 + (GAP * (k + 1)) + (brickheight * k)) { points += ROWS - k; break; } } updateScoreboard(window, label, points); // paddle width decrease bricks -= 1; setSize(paddle, PADDLEWIDTH - PADDLEWIDTH * 2 / 4.0 * (((ROWS * COLS) - bricks) / (float)(ROWS * COLS)), PADDLEHEIGHT); // velocity of ball increases velocity_x += 0.1; velocity_y += 0.1; } } // when no more live or all bricks are broken if (lives == 0 || bricks == 0) { break; } } // wait for click before exiting waitForClick(); // game over closeGWindow(window); return 0; }
void play(int mode, GWindow window) { /* Initialize the Game */ //create the game-board G3DRect board = newG3DRect(BOARD_X, BOARD_Y, 3*BOX, 3*BOX, true); setColor(board, "LIGHT_GRAY"); setFilled(board, true); add(window, board); //creates the array of small rects GRect rect[9]; int k = 0; for(int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { rect[k] = newGRect(BOX*j + BOARD_X, BOX*i + BOARD_Y, BOX, BOX); setColor(rect[k], "#0000FF"); add(window, rect[k]); k++; } } GLabel status = newGLabel("Player 1"); setFont(status, "SansSerif-26"); setLabel(status, "Player 1"); add(window, status); double x = WIDTH/2 - getWidth(status)/2; double y = HEIGHT/2 + 200; setLocation(status, x, y); A[0][0] = -1; A[0][0] = -1; A[0][0] = -1; A[0][0] = -1; A[0][0] = -1; A[0][0] = -1; A[0][0] = -1; A[0][0] = -1; A[0][0] = -1; int i = 0, player = 0, flag = -1; // if 2 player is seleceted if (mode == 2) { while( i <= 8) { player = i%2; if (player == 0) { setLabel(status, "Player 1"); double x = WIDTH/2 - getWidth(status)/2; double y = HEIGHT/2 + 200; setLocation(status, x, y); } else { setLabel(status, "Player 2"); double x = WIDTH/2 - getWidth(status)/2; double y = HEIGHT/2 + 200; setLocation(status, x, y); } printf("Player %d move....\n", player + 1); // wait for click while (true) { GEvent event = getNextEvent(MOUSE_EVENT); // check for mouse event if (event != NULL) // if we heard one { if (getEventType(event) == MOUSE_CLICKED) // if the event was movement { GObject object = getGObjectAt(window, getX(event), getY(event)); if(object != NULL) { int j; int rightClick = 0; for(j = 0; j < 9; j++) { if (object == rect[j]) { //remove the rect and add appropriate image int x = getX(object); int y = getY(object); printf("Rect[%d] clicked by player %d\n", j, player); removeGWindow(window, object); updateArray(player, j); if(player == 0) { GImage cross = newGImage("cross.png"); addAt(window, cross, x, y); } else { GImage circle = newGImage("circle.png"); addAt(window, circle, x, y); } rightClick = 1; } } if(rightClick == 1) break; } } } }// end of a click operation // check Status if (checkStatus()) { printf("Player %d won\n", player); flag = player; break; } i++; }// end of i = 0 to 8 loop }// if mode == 2 else // if mode == 1 { while( i <= 8) { player = i%2; if (player == 0) { setLabel(status, "Player 1"); double x = WIDTH/2 - getWidth(status)/2; double y = HEIGHT/2 + 200; setLocation(status, x, y); } else { setLabel(status, "Player 2"); double x = WIDTH/2 - getWidth(status)/2; double y = HEIGHT/2 + 200; setLocation(status, x, y); } printf("Player %d move....\n", player + 1); //showPlayer(player, mode); if (player == 0) // aka human being { // wait for click while (true) { GEvent event = getNextEvent(MOUSE_EVENT); // check for mouse event if (event != NULL) // if we heard one { if (getEventType(event) == MOUSE_CLICKED) // if the event was movement { GObject object = getGObjectAt(window, getX(event), getY(event)); if(object != NULL) { int j; int rightClick = 0; for(j = 0; j < 9; j++) { if (object == rect[j]) { //remove the rect and add appropriate image int x = getX(object); int y = getY(object); printf("Rect[%d] clicked by player %d\n", j, player); removeGWindow(window, object); updateArray(player, j); GImage cross = newGImage("cross.png"); addAt(window, cross, x, y); rightClick = 1; } } if(rightClick == 1) break; } } } }// end of a click operation }// end if player == 0 aka human being else // if player is computer { pause(750); // find the position int pos; if(check(59) >= 0) pos = check(59); else if (check(5) >= 0) pos = check(5); else pos = any(); updateArray(player, pos); //remove the rect and add appropriate image int x = getX(rect[pos]); int y = getY(rect[pos]); printf("Rect[%d] clicked by player %d\n", pos, player); removeGWindow(window, rect[pos]); GImage circle = newGImage("circle.png"); addAt(window, circle, x, y); }// end if player is computer // check Status if (checkStatus()) { printf("Player %d won\n", player); flag = player; break; } i++; }// end of i = 0 to 8 loop }// end if mode == 1 if (flag == 0 && mode == 2) { setLabel(status, "Player 1 Wins!!!"); double x = WIDTH/2 - getWidth(status)/2; double y = HEIGHT/2 + 200; setLocation(status, x, y); } else if (flag == 1 && mode == 2) { setLabel(status, "Player 2 Wins!!!"); double x = WIDTH/2 - getWidth(status)/2; double y = HEIGHT/2 + 200; setLocation(status, x, y); } else if (flag == 0 && mode == 1) { setLabel(status, "You Win !!!"); double x = WIDTH/2 - getWidth(status)/2; double y = HEIGHT/2 + 200; setLocation(status, x, y); } else if (flag == 1 && mode == 1) { setLabel(status, "You Lose !!!"); double x = WIDTH/2 - getWidth(status)/2; double y = HEIGHT/2 + 200; setLocation(status, x, y); } else if (flag == -1) { setLabel(status, "Game Draws !!!"); double x = WIDTH/2 - getWidth(status)/2; double y = HEIGHT/2 + 200; setLocation(status, x, y); } printf("flag = %d\n", flag); waitForClick(); }