|
Registered User
Join Date: Apr 2004
Posts: 2
|
Need help, C++ game
Well this is for a class, I don't have any more help room hours, I don't know anyone that is decent at programming that could help me, so sort of a last effort here.
I am getting to the point where the user enters in a power value, and that is happening alright, but the repaint() function is not working for some reason, and when I call it, it gives me a error code 128 in my compiler. (I think I am using a g++ compiler...I dunno how to tell really)
Anyways, here is the main.cxx
#include <winbgim.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <iomanip>
#include <fstream>
#include <string>
//#includes for the other classes
#include "constants.h"
#include "ground.h"
#include "tank.h"
//make control panel -- picture done, need it to be displayed when help button is pressed
//make new terrain files (diffuculty for each) -- using flat for testing, will make others once it works
//ready to play screen, pick level -- will make a picture screen and have different levels corrospond to different numbers
//winner screen -- pictures done, displayed when a tank gets hit
//make reset functions
using namespace std;
void getAngle (int mouseX, int mouseY, int tankY, int tankLeft, int tankRight, int slopeX, int slopeY);
void repaint (bool quitHit, bool helpHit, bool resetHit, Tank player1, Tank player2, Ground floor, bool turn_one);
bool nearby (int x, int y, int width, int height, int mouseX, int mouseY);
void drawPicture (int x, int y, int width, int height, char* pictureFileName);
int main ()
{
srand(time(NULL));
char inputChar = ' '; //default inputchar for the switch statement
bool pressed = false; //if a mouse button is pressed
int power = 0; //power of shot
int slopeX = 0; //used in getAngle
int slopeY = 0; //used it getAngle
bool turn_one = true; //player 1s turn or not
char affQuit = ' '; //confirm the quit
int whereShot = 0; //hold where the shot landed
char pow[2]; //store the power variable
//Initializing graphics
initwindow (WINDOW_SIZE, WINDOW_SIZE, "Cannon Game");
setbkcolor (WHITE);
setcolor (BLACK);
cleardevice ();
Ground floor;
//draw the ground
for(int g=0; g < WINDOW_SIZE; g++)
putpixel(floor.getx(g), floor.gety(g), BROWN);
//create tank objects
Tank player1(floor, turn_one);
Tank player2(floor, !turn_one);
//BUTTONS
//quit button
bool quitHit = false;
//help button
bool helpHit = false;
//reset button
bool resetHit = false;
//SWITCH STATEMENT
do
{
if (kbhit ())
inputChar = getch (); //catch when a character is pressed
if (ismouseclick (WM_LBUTTONDOWN)) //when left mouse button is pressed check if it is near any buttons
{
if (nearby (HELPX, HELPY, BUTTON_WIDTH, BUTTON_HEIGHT, mousex(), mousey()))
helpHit = true; //go to help screen
else if (nearby (RESETX, RESETY, BUTTON_WIDTH, BUTTON_HEIGHT, mousex(), mousey()))
resetHit = true; //reset the program
else if (nearby (QUITX, QUITY, BUTTON_WIDTH, BUTTON_HEIGHT, mousex(), mousey()))
{
quitHit = true;
repaint(quitHit, helpHit, resetHit, player1, player2, floor, turn_one);
bgiout << "Are you sure you want to quit?";
outstreamxy(200, 200);
do
{
if(kbhit())
affQuit = getch();
if(affQuit == 'y')
{
return EXIT_SUCCESS;
}
}while(affQuit != 'n');
}
}
if (ismouseclick (WM_RBUTTONDOWN)) //Once right mouse button pressed, find angle
{
getAngle (mousex(), mousey(), player1.getTop(), player1.getLeft(), player1.getRight(), slopeX, slopeY); //Find angle of shot, display angle on screen
pressed = true;
clearmouseclick (WM_RBUTTONDOWN);
}
if (ismouseclick (WM_MOUSEMOVE)) //While right mouse button pressed, continue finding angle
{
if (pressed)
getAngle (mousex(), mousey(), player1.getTop(), player1.getLeft(), player1.getRight(), slopeX, slopeY);
clearmouseclick (WM_MOUSEMOVE);
}
if (ismouseclick (WM_RBUTTONUP)) //Once right mouse button released, stop finding angle
{
pressed = false; //Key not pressed
clearmouseclick (WM_RBUTTONUP);
}
if (kbhit ())
inputChar = getch (); //catch when a character is pressed
switch (inputChar)
{
case '\n'://shoot
if(turn_one) //player 1 turn
{
whereShot = player1.shoot(slopeX, slopeY, power); //shoot(int, int int) will return 1 if player 1 wins, 2 if player 2 wins, and 0 if no hit
if(whereShot == 1)
{
bgiout << "Player One Wins";
outstreamxy(350, 200);
}
if(whereShot == 2)
{
bgiout << "Player Two Wins";
outstreamxy(350, 200);
}
}
else //player 2 turn
{
whereShot = player2.shoot(slopeX, slopeY, power);
if(whereShot == 1)
{
bgiout << "Player One Wins";
outstreamxy(350, 200);
}
if(whereShot == 2)
{
bgiout << "Player Two Wins";
outstreamxy(350, 200);
}
}
break;
} //End of switch statement
bgiout << "Please enter the power: ";
outstreamxy(100, 80);
pow[0]= getch();
if(pow[0] != '\n')
{
power = atoi(pow); //actual int power variable
pow[1] = getch();
if(pow[1] != '\n')
power = atoi(pow);
}
if(pow[0] == '\n')
power = 0;
bgiout << "Power: " << power; //output power onto the screen
outstreamxy(100, 100); //at 100,100
//paint the buttons, the tanks, and the ground
repaint(quitHit, helpHit, resetHit, player1, player2, floor, turn_one);
turn_one = !turn_one; //change to player 2 turn
}while (inputChar != 'q');
return EXIT_SUCCESS;
}
void repaint (bool quitHit, bool helpHit, bool resetHit, Tank player1, Tank player2, Ground floor, bool turn_one)
{
// static variables, allocated once, not
// every time the function is called
static int page = 0;
cout<<"1";
// switch between page 0 and page 1
page = 1 - page;
cout<<" 2";
setactivepage (page);
cout<<" 3";
cleardevice ();
//draw buttons
if(quitHit)
drawPicture (QUITX, QUITY, BUTTON_WIDTH, BUTTON_HEIGHT, "quitdown.gif");
else
drawPicture (QUITX, QUITY, BUTTON_WIDTH, BUTTON_HEIGHT, "quitup.gif");
if(resetHit)
drawPicture (RESETX, RESETY, BUTTON_WIDTH, BUTTON_HEIGHT, "resetdown.gif");
else
drawPicture (RESETX, RESETY, BUTTON_WIDTH, BUTTON_HEIGHT, "resetup.gif");
if(helpHit)
drawPicture (HELPX, HELPY, BUTTON_WIDTH, BUTTON_HEIGHT, "helpdown.gif");
else
drawPicture (HELPX, HELPY, BUTTON_WIDTH, BUTTON_HEIGHT, "helpup.gif");
//draw ground
for(int h=0; h < WINDOW_SIZE; h++)
putpixel(floor.getx(h), floor.gety(h), BROWN);
//draw tanks
player1.draw(turn_one);
player2.draw(!turn_one);
setvisualpage (page);
}
//Find if the mouse is close to an object
bool nearby (int x, int y, int width, int height, int mouseX, int mouseY)
{
if (mouseX > x + width / 2) return false;
if (mouseX < x - width / 2) return false;
if (mouseY > y + height / 2) return false;
if (mouseY < y - height / 2) return false;
return true;
}
//draw the button picture files
void drawPicture (int x, int y, int width, int height, char* pictureFileName)
{
readimagefile(
pictureFileName,
x - width / 2,
y - height / 2,
x - width / 2 + width - 1,
y - height / 2 + height - 1
);
}
//find the angle of the shot
void getAngle (int mouseX, int mouseY, int tankY, int tankLeft, int tankRight, int slopeX, int slopeY)
{
int middle = (tankLeft + tankRight ) / 2; //middle of tank image
slopeX = (middle - mouseX); //the X value of slope
slopeY = (tankY - mouseY); //the Y value of slope
}
Any help would be appreciated. It isn't getting to any of the cout statements in the repaint() function though.
If you need the other parts of the program to actually run and test it, hit me up on AIM, phait10101. Or irc.gamesurge.net as patcollins.
|