Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Course: CS 10 <FALL 2010>
- //
- // First Name: JOSHUA
- // Last Name: LEE
- // Course username: <jlee119>
- // email address: <jlee119@ucr.edu>
- //
- // Lecture Section: <001>
- // Lab Section: <023>
- // TA: Emine Celikkaya
- //
- // Assignment: <ASSN#8>
- //
- // I hereby certify that the code in this file
- // is ENTIRELY my own original work.
- //
- // =================================================================
- #include "instinct.h"
- #include <vector>
- #include <string>
- const double FROG_SIZE = 5.0;
- const double XMOVE = FROG_SIZE;
- const double YMOVE = FROG_SIZE;
- const int LIVES = 3;
- const int FROG_WAIT = 100;
- const int FRAME_WAIT = 20;
- const double PAD_H = FROG_SIZE;
- const double PAD_W = FROG_SIZE;
- const double PAD_YPOS = FROG_SIZE * 13;
- const double PAD_XPOS1 = FROG_SIZE * 5;
- const double PAD_XPOS2 = FROG_SIZE * 14;
- const double TRUCK_H = FROG_SIZE;
- const double TRUCK_W = FROG_SIZE * 4;
- const double TRUCK_LANE1 = FROG_SIZE * 2;
- const double TRUCK_LANE2 = FROG_SIZE * 4;
- const double TRUCK_LANE1_SPEED = TRUCK_W * 0.03;
- const double TRUCK_LANE2_SPEED = TRUCK_W * 0.02;
- const double TRUCK_LANE1_START = 0;
- const double TRUCK_LANE2_START = 4;
- const double CAR_H = FROG_SIZE;
- const double CAR_W = FROG_SIZE * 2;
- const double CAR_LANE1 = FROG_SIZE;
- const double CAR_LANE2 = FROG_SIZE * 3;
- const double CAR_LANE3 = FROG_SIZE * 5;
- const double CAR_LANE1_SPEED = CAR_W * 0.01;
- const double CAR_LANE2_SPEED = CAR_W * 0.03;
- const double CAR_LANE3_SPEED = CAR_W * 0.02;
- const double CAR_LANE1_START = 2;
- const double CAR_LANE2_START = 3;
- const double CAR_LANE3_START = 1;
- const double LOG_H = FROG_SIZE;
- const double LOG_W = FROG_SIZE * 6;
- const double LOG_LANE1 = FROG_SIZE * 8;
- const double LOG_LANE2 = FROG_SIZE * 9;
- const double LOG_LANE3 = FROG_SIZE * 11;
- const double LOG_LANE1_SPEED = LOG_W * 0.015;
- const double LOG_LANE2_SPEED = LOG_W * 0.005;
- const double LOG_LANE3_SPEED = LOG_W * 0.01;
- const double LOG_LANE1_START = 0;
- const double LOG_LANE2_START = 4;
- const double LOG_LANE3_START = 2;
- const double TURTLE_H = FROG_SIZE;
- const double TURTLE_W = FROG_SIZE * 2;
- const double TURTLE_LANE1 = FROG_SIZE * 7;
- const double TURTLE_LANE2 = FROG_SIZE * 10;
- const double TURTLE_LANE3 = FROG_SIZE * 12;
- const double TURTLE_LANE1_SPEED = TURTLE_W * 0.01;
- const double TURTLE_LANE2_SPEED = TURTLE_W * 0.03;
- const double TURTLE_LANE3_SPEED = TURTLE_W * 0.005;
- const double TURTLE_LANE1_START = 1;
- const double TURTLE_LANE2_START = 3;
- const double TURTLE_LANE3_START = 5;
- const int OBJECT_GAP = FROG_SIZE * 6;
- const int TRUCK_GAP = OBJECT_GAP + TRUCK_W;
- const int CAR_GAP = CAR_W * 3;
- const int LOG_GAP = OBJECT_GAP + LOG_W;
- const int TURTLE_GAP = OBJECT_GAP + TURTLE_W;
- const Point FROG_START = Point(45, 0);
- const double XPOS_OFFSET_FROG_MSG = 5.0;
- const double YPOS_OFFSET_FROG_MSG = 3.0;
- const Message MSG_LIVES_LEFT = Message(Point(5, -3),
- "Lives left:",
- BLACK);
- void output_splat( Point frog );
- void move_frog( Rect &frog, Timer &frog_tmr );
- void move_trucks(vector<Rect> &trucks);
- void move_cars(vector<Rect> &cars);
- void move_logs(vector<Rect> &logs);
- void move_turtles(vector<Rect> &turtles);
- Rect create_truck(double x, double y);
- Rect create_car(double x, double y);
- Rect create_log(double x, double y);
- Rect create_turtle(double x, double y);
- void init_frog(Rect &frog);
- void init_trucks(vector<Rect> &trucks);
- void init_cars(vector<Rect> &cars);
- void init_logs(vector<Rect> &logs);
- void init_turtles(vector<Rect> &turtles);
- void init_pads(vector<Rect> &pads);
- void output_result(Point frog, string text);
- bool collision(Rect r1, vector<Rect> &rectangles);
- bool inside(Point p, Rect r);
- bool is_border_overlap(Point p1, Point p2, Rect r);
- void buffer_rectangles(vector<Rect> &rectangles);
- void reset(Rect &frog, vector<Rect> &trucks, vector<Rect> &cars,
- vector<Rect> &logs, vector<Rect> &turtles);
- int instinct_main()
- {
- win.coord(0, -5, 100, 100);
- win.play_music("omen.mp3", 1);
- Message msg_num_lives = Message(Point(25, -3),
- LIVES, BLACK);
- Rect background = Rect(Point(0, 0),
- win.get_coord_width(), win.get_coord_height(),
- BLACK, true);
- Rect river = Rect(Point(0, TURTLE_LANE1),
- win.get_coord_width(), FROG_SIZE * 6,
- BLUE, true);
- Rect frog;
- vector<Rect> trucks(4);
- vector<Rect> cars(9);
- vector<Rect> logs(6);
- vector<Rect> turtles(9);
- vector<Rect> pads(4);
- reset(frog, trucks, cars, logs, turtles);
- init_pads(pads);
- Timer frog_tmr;
- frog_tmr.start();
- frog_tmr.add_time(FROG_WAIT);
- Timer frame_tmr;
- frame_tmr.start();
- win << background << river
- << pads[0] << pads[1] << pads[2] << pads[3];
- buffer_rectangles(trucks);
- buffer_rectangles(cars);
- buffer_rectangles(logs);
- buffer_rectangles(turtles);
- win << MSG_LIVES_LEFT << msg_num_lives << frog;
- int num_lives = LIVES;
- bool frog_on_pad = false;
- bool reset_objects = false;
- while (num_lives > 0 && !frog_on_pad)
- {
- if (reset_objects)
- {
- reset(frog, trucks, cars, logs, turtles);
- frog_tmr.reset();
- reset_objects = false;
- }
- win.clear_buffer();
- move_frog(frog, frog_tmr);
- move_trucks(trucks);
- move_cars(cars);
- move_logs(logs);
- move_turtles(turtles);
- //each frame occurs no faster than FRAME_WAIT milliseconds
- wait_for(FRAME_WAIT - frame_tmr.get_time());
- frame_tmr.reset();
- win << background << river << pads[0] << pads[1];
- buffer_rectangles(trucks);
- buffer_rectangles(cars);
- buffer_rectangles(logs);
- buffer_rectangles(turtles);
- win << MSG_LIVES_LEFT << msg_num_lives << frog;
- win.output_buffer();
- Point p_frog = frog.get_lower_left();
- double x_frog = p_frog.get_x();
- double y_frog = p_frog.get_y();
- if (y_frog >= TURTLE_LANE1 &&
- (x_frog + FROG_SIZE <= 0 || x_frog >= win.get_coord_width()))
- {
- reset_objects = true;
- msg_num_lives.set_text(--num_lives);
- }
- else if (collision(frog, logs))
- {
- if (y_frog == LOG_LANE1)
- frog.move(-LOG_LANE1_SPEED, 0);
- else if (y_frog == LOG_LANE2)
- frog.move(-LOG_LANE2_SPEED, 0);
- else if (y_frog == LOG_LANE3)
- frog.move(-LOG_LANE3_SPEED, 0);
- }
- else if (collision(frog, turtles))
- {
- if (y_frog == TURTLE_LANE1)
- frog.move(TURTLE_LANE1_SPEED, 0);
- else if (y_frog == TURTLE_LANE2)
- frog.move(TURTLE_LANE2_SPEED, 0);
- }
- else if (collision(frog, pads))
- {
- frog_on_pad = true;
- }
- else if (collision(frog, trucks) ||
- collision(frog, cars) ||
- y_frog >= TURTLE_LANE1)
- {
- reset_objects = true;
- msg_num_lives.set_text(--num_lives);
- }
- }
- win.clear_buffer();
- output_splat( frog.get_lower_left() );
- win << background << river << pads[0] << pads[1];
- buffer_rectangles(trucks);
- buffer_rectangles(cars);
- buffer_rectangles(logs);
- buffer_rectangles(turtles);
- win << MSG_LIVES_LEFT << msg_num_lives << frog;
- if (frog_on_pad)
- output_result(frog.get_lower_left(), "You Win!");
- else
- output_result(frog.get_lower_left(), "You Lose");
- win.output_buffer();
- return 0;
- }
- void output_splat( Point frog )
- {
- // x&y values of frog
- double x_frog = frog.get_x();
- double y_frog = frog.get_y();
- // x&y value and SPLAT message
- double x_splat = x_frog - 2;
- double y_splat = y_frog - 2;
- Point psplat = Point(x_splat, y_splat);
- Message splat = Message(psplat, "SPLAT", RED);
- win << splat;
- }
- void buffer_rectangles(vector<Rect> &rectangles)
- {
- for (int i = 0; i < rectangles.size(); i++)
- win << rectangles[i];
- }
- void output_result(Point frog, string text)
- {
- double frog_x = frog.get_x();
- double frog_y = frog.get_y();
- //Point pos_frog_msg = Point(frog_x, frog_y);
- Point pos_frog_msg = Point(frog_x - XPOS_OFFSET_FROG_MSG,
- frog_y - YPOS_OFFSET_FROG_MSG);
- Message msg = Message(pos_frog_msg, text, WHITE);
- win << msg;
- }
- //frog can't move faster than once per FROG_WAIT milliseconds
- void move_frog( Rect &frog, Timer &frog_tmr )
- {
- if (frog_tmr.get_time() >= FROG_WAIT)
- {
- if (win.is_key_down("up"))
- {
- frog.move(0, YMOVE);
- frog_tmr.reset();
- }
- else if (win.is_key_down("down"))
- {
- frog.move(0, -YMOVE);
- frog_tmr.reset();
- }
- else if (win.is_key_down("right"))
- {
- frog.move(XMOVE, 0);
- frog_tmr.reset();
- }
- else if (win.is_key_down("left"))
- {
- frog.move(-XMOVE, 0);
- frog_tmr.reset();
- }
- }
- }
- void move_trucks(vector<Rect> &trucks)
- {
- for (int i = 0; i < trucks.size(); i++)
- {
- Point p = trucks[i].get_lower_left();
- double x = p.get_x();
- double y = p.get_y();
- if (x + trucks[i].get_width() <= 0)
- trucks[i].move(win.get_coord_width() + trucks[i].get_width(), 0);
- if (i >= 2)
- trucks[i].move(-TRUCK_LANE2_SPEED, 0);
- else
- trucks[i].move(-TRUCK_LANE1_SPEED, 0);
- }
- }
- void move_cars(vector<Rect> &cars)
- {
- for (int i = 0; i < cars.size(); i++)
- {
- Point p = cars[i].get_lower_left();
- double x = p.get_x();
- double y = p.get_y();
- if (x >= win.get_coord_width())
- cars[i].move(-win.get_coord_width() - cars[i].get_width(), 0);
- if (i >= 6)
- cars[i].move(CAR_LANE3_SPEED, 0);
- else if (i >= 3)
- cars[i].move(CAR_LANE2_SPEED, 0);
- else
- cars[i].move(CAR_LANE1_SPEED, 0);
- }
- }
- void move_logs(vector<Rect> &logs)
- {
- for (int i = 0; i < logs.size(); i++)
- {
- Point p = logs[i].get_lower_left();
- double x = p.get_x();
- double y = p.get_y();
- if (x + logs[i].get_width() <= 0)
- logs[i].move(win.get_coord_width() + logs[i].get_width(), 0);
- if (i >= 4)
- logs[i].move(-LOG_LANE3_SPEED, 0);
- else if (i >= 2)
- logs[i].move(-LOG_LANE2_SPEED, 0);
- else
- logs[i].move(-LOG_LANE1_SPEED, 0);
- }
- }
- void move_turtles(vector<Rect> &turtles)
- {
- for (int i = 0; i < turtles.size(); i++)
- {
- Point p = turtles[i].get_lower_left();
- double x = p.get_x();
- double y = p.get_y();
- if (x >= win.get_coord_width())
- turtles[i].move(-win.get_coord_width() - turtles[i].get_width(), 0);
- if(i > 5)
- {
- turtles[i].move(TURTLE_LANE3_SPEED, 0);
- }
- else if(i > 2 && i < 6)
- {
- turtles[i].move(TURTLE_LANE2_SPEED, 0);
- }
- else
- turtles[i].move(TURTLE_LANE1_SPEED, 0);
- }
- }
- // returns true if the point is in rectangle r
- bool inside( Point p, Rect r )
- {
- Point r_lower_left = r.get_lower_left();
- double r_lower_x = r_lower_left.get_x();
- double r_lower_y = r_lower_left.get_y();
- double width = r.get_width();
- double height = r.get_height();
- double p_x = p.get_x();
- double p_y = p.get_y();
- if (p_x > r_lower_x && p_x < (r_lower_x + width))
- if (p_y > r_lower_y && p_y < (r_lower_y + height))
- return true;
- return false;
- }
- // function that returns true when a corner of r1 is in r2
- // or when two borders of r1 is overlapped with r2
- bool collision(Rect r1, vector<Rect> &rectangles)
- {
- Point p_r1_lower_left = r1.get_lower_left();
- double p_r1_lower_x = p_r1_lower_left.get_x();
- double p_r1_lower_y = p_r1_lower_left.get_y();
- double r1_width = r1.get_width();
- double r1_height = r1.get_height();
- Point p_r1_lower_right = Point(p_r1_lower_x + r1_width,
- p_r1_lower_y);
- Point p_r1_upper_left = Point(p_r1_lower_x,
- p_r1_lower_y + r1_height);
- Point p_r1_upper_right = Point(p_r1_lower_x + r1_width,
- p_r1_lower_y + r1_height);
- for (int i = 0; i < rectangles.size(); i++)
- {
- Rect r2 = rectangles[i];
- bool corner_inside = inside(p_r1_lower_left, r2) ||
- inside(p_r1_upper_left, r2) ||
- inside(p_r1_lower_right, r2) ||
- inside(p_r1_upper_right, r2);
- bool border_overlap =
- is_border_overlap(p_r1_lower_left, p_r1_upper_left, r2) ||
- is_border_overlap(p_r1_lower_right, p_r1_upper_right, r2) ||
- is_border_overlap(p_r1_upper_left, p_r1_upper_right, r2) ||
- is_border_overlap(p_r1_lower_left, p_r1_lower_right, r2);
- if (corner_inside || border_overlap)
- return true;
- }
- return false;
- }
- // function that returns true when two borders are overlapped
- bool is_border_overlap(Point p1, Point p2, Rect r)
- {
- Point r_lower_left = r.get_lower_left();
- double r_lower_x = r_lower_left.get_x();
- double r_lower_y = r_lower_left.get_y();
- double width = r.get_width();
- double height = r.get_height();
- double p1_x = p1.get_x();
- double p1_y = p1.get_y();
- double p2_x = p2.get_x();
- double p2_y = p2.get_y();
- bool p1_on_btm_border = false;
- bool p2_on_top_border = false;
- bool p1_on_left_border = false;
- bool p2_on_rght_border = false;
- if (p1_x > r_lower_x && p1_x < r_lower_x + width)
- if (p1_y == r_lower_y)
- p1_on_btm_border = true;
- if (p2_x > r_lower_x && p2_x < r_lower_x + width)
- if (p2_y == r_lower_y + height)
- p2_on_top_border = true;
- if (p1_y > r_lower_y && p1_y < r_lower_y + height)
- if (p1_x == r_lower_x)
- p1_on_left_border = true;
- if (p2_y > r_lower_y && p2_y < r_lower_y + height)
- if (p2_x == r_lower_x + width)
- p2_on_rght_border = true;
- return (p1_on_btm_border && p2_on_top_border) ||
- (p1_on_left_border && p2_on_rght_border);
- }
- Rect create_truck(double x, double y)
- {
- return Rect(Point(x, y), TRUCK_W, TRUCK_H, PURPLE, true);
- }
- Rect create_car(double x, double y)
- {
- return Rect(Point(x, y), CAR_W, CAR_H, YELLOW, true);
- }
- Rect create_log(double x, double y)
- {
- return Rect(Point(x, y), LOG_W, LOG_H, BROWN, true);
- }
- Rect create_turtle(double x, double y)
- {
- return Rect(Point(x, y), TURTLE_W, TURTLE_H, DARK_GREEN, true);
- }
- void init_frog(Rect &frog)
- {
- frog = Rect(FROG_START, FROG_SIZE, FROG_SIZE * .99, GREEN, true);
- }
- void init_trucks(vector<Rect> &trucks)
- {
- trucks[0] = create_truck(TRUCK_LANE1_START, TRUCK_LANE1);
- trucks[1] = create_truck(TRUCK_LANE1_START + TRUCK_GAP, TRUCK_LANE1);
- trucks[2] = create_truck(TRUCK_LANE2_START, TRUCK_LANE2);
- trucks[3] = create_truck(TRUCK_LANE2_START + TRUCK_GAP, TRUCK_LANE2);
- }
- void init_cars(vector<Rect> &cars)
- {
- cars[0] = create_car(CAR_LANE1_START, CAR_LANE1);
- cars[1] = create_car(CAR_LANE1_START + CAR_GAP, CAR_LANE1);
- cars[2] = create_car(CAR_LANE1_START + CAR_GAP * 2, CAR_LANE1);
- cars[3] = create_car(CAR_LANE2_START, CAR_LANE2);
- cars[4] = create_car(CAR_LANE2_START + CAR_GAP, CAR_LANE2);
- cars[5] = create_car(CAR_LANE2_START + CAR_GAP * 2, CAR_LANE2);
- cars[6] = create_car(CAR_LANE3_START, CAR_LANE3);
- cars[7] = create_car(CAR_LANE3_START + CAR_GAP, CAR_LANE3);
- cars[8] = create_car(CAR_LANE3_START + CAR_GAP * 2, CAR_LANE3);
- }
- void init_logs(vector<Rect> &logs)
- {
- logs[0] = create_log(LOG_LANE1_START, LOG_LANE1);
- logs[1] = create_log(LOG_LANE1_START + LOG_GAP, LOG_LANE1);
- logs[2] = create_log(LOG_LANE2_START, LOG_LANE2);
- logs[3] = create_log(LOG_LANE2_START + LOG_GAP, LOG_LANE2);
- logs[4] = create_log(LOG_LANE3_START + LOG_GAP, LOG_LANE3);
- logs[5] = create_log(LOG_LANE3_START + LOG_GAP, LOG_LANE3);
- }
- void init_turtles(vector<Rect> &turtles)
- {
- turtles[0] = create_turtle(TURTLE_LANE1_START, TURTLE_LANE1);
- turtles[1] = create_turtle(TURTLE_LANE1_START + TURTLE_GAP, TURTLE_LANE1);
- turtles[2] = create_turtle(TURTLE_LANE1_START + (2*TURTLE_GAP), TURTLE_LANE1);
- turtles[3] = create_turtle(TURTLE_LANE2_START, TURTLE_LANE2);
- turtles[4] = create_turtle(TURTLE_LANE2_START + TURTLE_GAP, TURTLE_LANE2);
- turtles[5] = create_turtle(TURTLE_LANE2_START + (2*TURTLE_GAP), TURTLE_LANE2);
- turtles[6] = create_turtle(TURTLE_LANE3_START, TURTLE_LANE3);
- turtles[7] = create_turtle(TURTLE_LANE3_START + TURTLE_GAP, TURTLE_LANE3);
- turtles[8] = create_turtle(TURTLE_LANE3_START + (2*TURTLE_GAP), TURTLE_LANE3);
- }
- void init_pads(vector<Rect> &pads)
- {
- pads[0] = Rect(Point(PAD_XPOS1, PAD_YPOS), PAD_W, PAD_H, RED, true);
- pads[1] = Rect(Point(PAD_XPOS2, PAD_YPOS), PAD_W, PAD_H, RED, true);
- }
- void reset(Rect &frog, vector<Rect> &trucks, vector<Rect> &cars,
- vector<Rect> &logs, vector<Rect> &turtles)
- {
- init_frog(frog);
- init_trucks(trucks);
- init_cars(cars);
- init_logs(logs);
- init_turtles(turtles);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement