Example #1
0
void getInput(window &w, color skycolor, tank &left, tank &right, bool &leftTurn) { // gets input for 2 parameters. returns when enter key (shoot) is pressed
    char k = '#';  // for key, set to a known, but useless value
    tank &currentTank = leftTurn ? left : right;
    int angle = currentTank.getAngle();
    int velocity = currentTank.getSpeed();
    w.SetFont(50, PLAIN, ROMAN);
    setBrushAndPenColor(w, BLACK);
    while (k != 13) { // return key
        setBrushAndPenColor(w, skycolor); // erasing up top
        w.DrawRectangle(0, 0, w.GetWidth(), 200, FILLED);
        w.SetFont(50, PLAIN, ROMAN); // redrawing up top
        setBrushAndPenColor(w, BLACK);
        w.DrawString(w.GetWidth() / 2 - 60, 100, "angle: " + to_string(angle));
        w.DrawString(w.GetWidth() / 2 - 60, 150, "speed: " + to_string(velocity));
        w.UpdateBuffer();

        // redraw tank
        setBrushAndPenColor(w, skycolor); // erasing around tank
        if (leftTurn) w.DrawRectangle(currentTank.getXstart() - 3, currentTank.getYstart() + 3, currentTank.getXend() + 3, currentTank.getYend() - 3, FILLED);
        else w.DrawRectangle(currentTank.getXstart() + 3, currentTank.getYstart() + 3, currentTank.getXend() - 3, currentTank.getYend() - 3, FILLED);

        // letting user change parameters (w/ limits)
        w.WaitKeyPress(k);
        if (k == 8 && angle < 90) angle++; // up
        if (k == 2 && angle > 1) angle--; // down
        if (k == 6 && velocity < 35) velocity++; // right
        if (k == 4 && velocity > 1) velocity--; // left

        currentTank.setAngle(angle);
        currentTank.setSpeed(velocity);
        currentTank.drawTank(w); // changes pen color
        w.UpdateBuffer();
    }
}
Example #2
0
color bullet(window &w, color skycolor, tank &left, tank &right, bool &leftTurn) { // goes through bullets path. returns color of what is hit.
    setBrushAndPenColor(w, skycolor); // erasing up top to make sure it doesnt count hitting the text
    w.DrawRectangle(0, 0, w.GetWidth(), 200, FILLED);
    double dtime = 0.007;
    int bulletRadius = 4;
    int bulletX, bulletY;
    double radianAngle;
    tank &currentTank = leftTurn ? left : right;
    int angle = currentTank.getAngle();
    if (leftTurn) {
        radianAngle = angle * cdPi / 180;
        bulletX = currentTank.getXend() + 3;
    } else {
        radianAngle = ((180 - angle) * cdPi / 180);
        bulletX = currentTank.getXend() - 3;
    }
    int velocity = currentTank.getSpeed();
    bulletY = currentTank.getYend() - 3;
    leftTurn = !leftTurn;
    double xVel = cos(radianAngle) * velocity;
    double yVel = sin(radianAngle) * velocity;
    double time = 0;
    while (w.GetColor(bulletX, bulletY) == skycolor) {
        setBrushAndPenColor(w, BLACK);
        w.DrawCircle(bulletX, bulletY, bulletRadius, FILLED);
        w.UpdateBuffer();
        Sleep(1);
        setBrushAndPenColor(w, skycolor);
        w.DrawCircle(bulletX, bulletY, bulletRadius + 1, FILLED);
        time += dtime;
        bulletX += xVel * time;
        bulletY += -yVel * time + 40 * time * time;
    }
    return w.GetColor(bulletX, bulletY);
}