Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Go Back   Code Forums > Application and Web Development > Standard C, C++
User Name
Password

Reply
 
LinkBack Thread Tools Display Modes
Old 04-26-2004, 02:52 PM   #1 (permalink)
patcollins
Registered User
 
Join Date: Apr 2004
Posts: 2
patcollins is on a distinguished road
Unhappy 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.
__________________
patcollins is offline   Reply With Quote
Old 04-27-2004, 05:23 PM   #2 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
Crappa duck.
I need to reformat your program to read it.
What's the deadline for this project?
__________________
Valmont is offline   Reply With Quote
Old 04-27-2004, 07:53 PM   #3 (permalink)
patcollins
Registered User
 
Join Date: Apr 2004
Posts: 2
patcollins is on a distinguished road
Ah due today, I got most of it sorted out I think in the lab today.
Thanks though =o)
__________________
patcollins is offline   Reply With Quote
Old 04-28-2004, 03:43 AM   #4 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
Ok. I was so sleepy at 3.23 am... I wasn't in the mood.
__________________
Valmont is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
VB or C#, or something else Apodysophilia MS Technologies ( ASP, VB, C#, .NET ) 6 10-15-2004 09:48 AM
For those who have Learned C from "The C Programming Lanuage" DemosthenesB Standard C, C++ 5 07-13-2003 12:22 AM
Spring Break Game Fest saline Saline's Brain Teasers 5 03-14-2003 03:48 PM
Starting out with C anon C 0 02-24-2003 01:06 PM
edit? anon Lounge 10 11-21-2002 03:02 PM


All times are GMT -8. The time now is 12:03 AM.


Powered by vBulletin Version 3.6.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle