C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old December 4th, 2008, 06:40 PM
duhduhdan duhduhdan is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2008
Posts: 1 duhduhdan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 m 27 sec
Reputation Power: 0
General - Basic Space Invaders Question...

Hello all, I am new to the forums, and new to the programming world of C++ as well.

So far, everything is working out great, I love C++ and how simple it actually is in hindsight.

I wrote a Space Invaders program (I'm sure everyone here has) and have a question:

Everytime I compile and run the program, an MS error shows up and says it has encountered a problem and can't run the program.

It's probably a simple solution, but if looking at my code helps at all here it is:

Obviously, I started with the basic main.cpp

#include <allegro.h>
#include <cstdlib>
#include <time.h>
#include "Character.h"
#include "Ship.h"
#include "Laser.h"


Ship myShip;
Laser myLaser;
Character myEnemies[10];
Laser enemyLasers[10];

BITMAP* buffer;
BITMAP* ship;
BITMAP* enemy;
BITMAP* laser;
BITMAP* enemyLaser;

bool update = true;

void Draw(){

acquire_screen();
draw_sprite( screen, buffer, 0, 0);
release_screen();

update = false;

rest(50);

}

void SetRandSeed(){

time_t secs;
time(&secs);
srand( (unsigned int)secs);

}

void updateLives(){

char tempStr[2];
itoa(myShip.GetLives(), tempStr, 10);
textout_ex( buffer, font, tempStr, 70, 460, makecol( 255, 0, 0), makecol( 0, 0, 0));

}

void SetupGame(){

SetRandSeed();

myLaser.SetDead( true, buffer);

for( int i= 0; i <=9; i++){
if( i < 5)
myEnemies[i].SetY( 20, buffer, enemy);
else
myEnemies[i].SetY( 40, buffer, enemy);
if( i < 5)
myEnemies[i].SetX( ((20* i) + 240), buffer, enemy);
else
myEnemies[i].SetX( ((20* (i - 5)) + 240), buffer, enemy);
}

for( int i= 0; i <=9; i++){

enemyLasers[i].SetDead(true, buffer);

}

myShip.SetX( ((640/2) - 40), buffer, ship);
myShip.SetY( (480 - 40), buffer, ship);
myShip.SetLives( 4);
textout_ex( buffer, font, "Lives: ", 10, 460, makecol( 255, 0, 0), makecol( 0, 0, 0));
updateLives();


Draw();


}

void movePlayer(){

if( key[KEY_RIGHT]){

myShip.SetX( (myShip.GetX() + 20), buffer, ship);
update = true;

} else if( key[KEY_LEFT]){

myShip.SetX( (myShip.GetX() - 20), buffer, ship);
update = true;

} else if( key[KEY_SPACE]){

if( myLaser.IsDead()){

myLaser.SetDead(false, buffer);
myLaser.SetX( myShip.GetX(), buffer, laser);
myLaser.SetY( (myShip.GetY() - 20), buffer, laser);
update = true;

}

}

}

void moveLaser(){

if( !(myLaser.IsDead()) ){

myLaser.SetY( (myLaser.GetY() - 20), buffer, laser);

myLaser.CheckCollision(myEnemies, buffer);

update = true;

}

}

void moveEnemy(){

int shoot;

for( int i = 0; i <= 9; i++){

if(!myEnemies[i].IsDead()){

shoot = rand() % 1000 + 1;

if( shoot == 25){

enemyLasers[i].SetDead( false, buffer);
enemyLasers[i].SetX( myEnemies[i].GetX(),buffer, enemyLaser);
enemyLasers[i].SetY( 60, buffer, enemyLaser);
update = true;

}

}

}

}

void moveEnemyLaser(){

for( int i = 0; i <= 9; i++){

if( !enemyLasers[i].IsDead()){

enemyLasers[i].SetY( (enemyLasers[i].GetY() + 20), buffer, enemyLaser);
if( (enemyLasers[i].CheckCollision(myShip, buffer))){

myShip.SetLives(myShip.GetLives() - 1);
updateLives();


}
update = true;

}

}

}

bool checkPlayerLives(){

if ( myShip.GetLives() <= 0){

textout_ex( screen, font, "GAME OVER", 320, 240, makecol( 255, 0, 0), makecol( 0, 0, 0));

while(!key[KEY_ESC]){}

return true;
}

return false;

}

bool checkEnemyLives(){

int enemiesDead = 0;

for( int i = 0; i <= 9; i++){

if(myEnemies[i].IsDead())
++enemiesDead;

}

if(enemiesDead >= 10){

textout_ex( screen, font, "YOU WIN!!!!", 320, 240, makecol( 255, 0, 0), makecol( 0, 0, 0));

while(!key[KEY_ESC]){}

return true;
}

return false;

}

int main(){

allegro_init();
install_keyboard();
set_color_depth(16);
set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0);

buffer = create_bitmap( 640, 480);
ship = load_bitmap( "ship.bmp", NULL);
enemy = load_bitmap( "enemy.bmp", NULL);
laser = load_bitmap( "laser.bmp", NULL);
enemyLaser = load_bitmap( "enemyLaser.bmp", NULL);

SetupGame();

while(!key[KEY_ESC]){

movePlayer();
moveLaser();
moveEnemy();
moveEnemyLaser();
if(checkPlayerLives())
break;
if(checkEnemyLives())
break;

if(update)
Draw();

}

delete [] myEnemies;

destroy_bitmap( buffer);
destroy_bitmap( ship);
destroy_bitmap( enemy);

return 0;

}
END_OF_MAIN();

There's really no need to show the other .cpp and .h files I think, but if anyone could answer my question that would be great!


Thanks,
Dan

Reply With Quote
  #2  
Old December 6th, 2008, 04:59 PM
reaper7861 reaper7861 is offline
Contributing User
Click here for more information.
 
Join Date: Oct 2008
Posts: 54 reaper7861 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 56 m 22 sec
Reputation Power: 1
Can you show us the error that it is giving you?

Reply With Quote
  #3  
Old December 7th, 2008, 05:44 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Click here for more information.
 
Join Date: Sep 2005
Posts: 790 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 4 Days 38 m 39 sec
Reputation Power: 4
Try running it in the debugger to see if you get some more information. Try to remove (comment) code to find out which part is causing the problem, i.e., does the program start at all?

Try to put a return statement in main after set_gfx_mode() to see if allegro starts up properly. If it does, put the return statement after SetupGame() to see if the problem is around there. Otherwise you might want to post the remaining code.

If you think C++ is simple then you have not used it long enough yet, expect surprises Your code has a lot of C-style in it by the way. (C != C++, C/C++ is a misnomer)
__________________
Current project: roborally

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > General - Basic Space Invaders Question...


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT