Apologies, sometimes I can just get so frustrated that I give up and ask for people to "give me da codz" but I couldnt get your loop code to work so I just went with the built in one and changed my code around a bit... and it works fine now so thanks everyone, but just wondering if there is anything ineficient about my code as it just doesnt feel right to me, do I have to declare one iterator for every vector? and I think my class member functions may be a bit much...
Im just wondering if my code is repetative at all, if you dont want to look over my code and tell me noob mistakes dont worry about it... I just dont want to learn the wrong way to do something as I had a major incident with that the first language I learned...
#include <allegro.h>#include <vector> // To use the vector class#include <algorithm> // To use for_each#include <functional> // To use mem_fun_ref#include "siheader.h"using namespace std;void init();void deinit();int main() { init(); vector<Ship> ships; vector<bullet> bullets; // Create a new ship and add it to the end of the vector for (int x=50; x<750; x=x+35) { for (int y=20; y<200; y=y+35) { ships.push_back( Ship(x, y) ); } } vector<Ship>::iterator shipIter; vector<bullet>::iterator bulletIter; //playervariables int player_x=400; int player_y=500; BITMAP *playerImage; playerImage = load_bitmap( "test.bmp", NULL); BITMAP *backGround; backGround = load_bitmap( "back.bmp", NULL); while (!key[KEY_ESC]) { acquire_screen(); draw_sprite(screen, backGround, 0,0); if (key[KEY_RIGHT]) { player_x=player_x+4; } if (key[KEY_LEFT]){ if (player_x > 0){ player_x=player_x-4; } } if (key[KEY_SPACE]) { bullets.push_back( bullet(player_x, player_y) ); } for (shipIter = ships.begin(); shipIter != ships.end();shipIter++) { shipIter->update(); } for (int i = 0; i < bullets.size(); i++) { bullets.update(); } for (bulletIter = bullets.begin(); bulletIter != bullets.end(); ++bulletIter) { for (shipIter = ships.begin(); shipIter != ships.end(); ++shipIter) { if (shipIter->isHit(bulletIter->returnX(),bulletIter->returnY())) { bulletIter->setDestroyed(); shipIter->setDestroyed(); } } } bullets.erase(remove_if(bullets.begin(), bullets.end(), mem_fun_ref(&bullet::isDestroyed)), bullets.end()); ships.erase(remove_if(ships.begin(), ships.end(), mem_fun_ref(&Ship::isDestroyed)), ships.end()); rotate_sprite(screen, playerImage, player_x+100, player_y-100, itofix(player_x)); draw_sprite(screen, playerImage, player_x, player_y); release_screen(); } deinit(); return 0;}END_OF_MAIN()void init() { int depth, res; allegro_init(); depth = desktop_color_depth(); if (depth == 0) depth = 32; set_color_depth(depth); res = set_gfx_mode(GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0); if (res != 0) { allegro_message(allegro_error); exit(-1); } install_timer(); install_keyboard(); install_mouse(); /* add other initializations here */}void deinit() { clear_keybuf(); /* add other deinitializations here */}
and the header...
class Ship {private: // We don't want external code to access the coordinates directly int x, y; bool destroy;public: // We do want external code to be able to call these functions // This creates a Ship object with the appropriate coordinates Ship(int xpos,int ypos) : x(xpos), // Sets the ship's x to xpos y(ypos) // Sets the ship's y to ypos { // No code is currently required here. // Things may change as your class grows. } void update() { circlefill( screen, x, y, 15, makecol(255,255,255)); if (mouse_b & 1) { y=y+1; } } bool isHit(int bulletX, int bulletY) { if (bulletX < x + 25){ if (bulletX > x - 25) { if (bulletY == y+10) { return true; } } } return false; } int returnX() { return x; } int returnY() { return y; } void setDestroyed() { destroy = true; } bool isDestroyed() { if (destroy == true) { return true; } return false; }};class bullet {private: // We don't want external code to access the coordinates directly int x, y; bool destroy;public: // We do want external code to be able to call these functions // This creates a Ship object with the appropriate coordinates bullet(int xpos,int ypos) : x(xpos), y(ypos) { // No code is currently required here. // Things may change as your class grows. } void update() { circlefill( screen, x, y, 15, makecol(255,255,255)); y=y-5; if (y<40) { destroy = true; } } int returnX() { return x; } int returnY() { return y; } void setDestroyed() { destroy = true; } bool isDestroyed() { if (destroy == true) { return true; } return false; }};
As I said, something just feels wierd with my code...