vary

Member
  • Content count

    4
  • Joined

  • Last visited

Community Reputation

100 Neutral

About vary

  • Rank
    Newbie
  1. If i remove, the return false, weird things come up, one of the things stop being rendered!! and only gets updated, ie becomes invisible on screen!! Though, square is rendered, but still the game does not pause when LeftCollision becomes true!!
  2. [quote name='Washu' timestamp='1330628548' post='4918296'] [code] else { return false; LeftCollision = false; } [/code] And you expect that to work...how? Else, statement is for, when ever the collision is not happening, things should go as they are supposed to be. If i'll remove that line, values wont revert back when left collision is not happening. [/quote] [quote name='Dawoodoz' timestamp='1330628065' post='4918289'] Try using breakpoints and look if it execute line 58 to 59 anyway. An access violation somewhere else can make the program pointer go nuts in C++. [/quote] 58/9 are responsible for Scrolling of Triangles and Squares from Right to Left, and they work all the time!!
  3. Problem is in the updation of LeftCollision, which is of bool type! It goes to true, when the condition is met in CheckCollision(), but the 'true' value is not reflected in the GameObject::Update() section, where if(LeftCollision==false).... is always false!! Why?? What could be the problem ?? [code]#include "GameObject.h" 2 3GameObject :: GameObject() 4{ 5 x = 0; 6 y = 0; 7 8 velX = 0; 9 velY = 0; 10 11 dirX = 0; 12 dirY = 0; 13 14 boundX = 0; 15 boundY = 0; 16 17 LeftCollision = false; //initial value (it is a Public: bool type variable) 18 19 20 maxFrame = 0; 21 curFrame = 0; 22 frameCount = 0; 23 frameDelay = 0; 24 frameWidth = 0; 25 frameHeight = 0; 26 animationColumns = 0; 27 animationDirection = 0; 28 29 image = NULL; 30 31 alive = true; 32 collidable = true; 33} 34 35void GameObject :: Destroy() 36{ 37 38} 39 40void GameObject :: Init(float x, float y, float velX, float velY, int dirX, int dirY, int boundX, int boundY) 41{ 42 GameObject :: x = x; 43 GameObject :: y = y; 44 45 GameObject :: velX = velX; 46 GameObject :: velY = velY; 47 48 GameObject :: dirX = dirX; 49 GameObject :: dirY = dirY; 50 51 GameObject :: boundX = boundX; 52 GameObject :: boundY = boundY; 53} 54void GameObject :: Update() 55{ 56 if(LeftCollision == false) //should stop when 'true', but never stops 57 { 58 x += velX * dirX; 59 y += velY * dirY; 60 } 61} 62void GameObject :: Render() 63{ 64} 65 66bool GameObject :: CheckCollisions(GameObject *otherObject) 67{ 68 float oX = otherObject->GetX(); 69 float oY = otherObject->GetY(); 70 71 int obX = otherObject->GetBoundX(); 72 int obY = otherObject->GetBoundY(); 73 74 75 if( x + boundX > oX - obX && 76 x - boundX < oX + obX && 77 y + boundY > oY - obY && 78 y - boundY < oY + obY && otherObject->GetID() == TRIANGLE) 79 { 80 return true; 81 } 82 else if(((oX < x + boundX + 14)&&(oX+ 40 >x - boundX))&&!((oY < y + boundY)&&(oY+40 > y - boundY)) 83 && otherObject->GetID() == SQUARE) 84 { 85 y = oY - boundX - 10; 86 //x = oX + 40; 87 return true; 88 } 89 else if((oX < x + boundX + 14) && (oX > x - boundX) && otherObject->GetID() == SQUARE) 90 { 91 LeftCollision = true; //LC updates to true, but does not reflect the value in the Update section above!! 92 return false; 93 } 94 else 95 { 96 return false; 97 LeftCollision = false; 98 } 99} 100void GameObject :: Collided(int objectID) 101{ 102 103} 104bool GameObject :: Collidable() 105{ 106 return alive && collidable; 107}[/code]
  4. Looking to code a 2-d side scroller, where in a ball rolls from left to right, and obstacles such as triangles and squares and combinations of the two are faced. Looking for : 1. In the code below, the jumping mechanism seems to be in some problem, as i made functions for Ball movement(previously it was working well). 2. The triangles and squares(randomly generated) seems to overlap sometimes. [attachment=7389:ball-tri2.JPG] 3. How shall i make scroll sideways, such as Mario, (you keep going towards right and things keep coming), i.e if i need to implement the camera type simulation, how shall i go ahead with it ? PS : I am new to game programming. Your help will be duly appreciated. [code]#include <allegro5/allegro.h> #include <allegro5/allegro_primitives.h> #include "objects.h" const int WIDTH = 1000; const int HEIGHT = 400; const int NUM_TRIANGLES = 6; enum KEYS{LEFT, RIGHT, SPACE}; //prototypes void InitBall(Ball &shapeB); void DrawBall(Ball &shapeB); void MoveLeftBall(Ball &shapeB); void MoveRightBall(Ball &shapeB); void JumpBall(Ball &shapeB); void InitTriangle(Triangle shapeT[], int size); void DrawTriangle(Triangle shapeT[], int size); void StartTriangle(Triangle shapeT[], int size); void UpdateTriangle(Triangle shapeT[], int size); int main(void) { bool done = false; bool redraw = true; // bool canJump = false; // float pos_x = 30; // float pos_y = 380; float speed =0; // float dx =1, dy = 1; bool keys[3] = {false,false,false}; int FPS = 60; Triangle shapeT[NUM_TRIANGLES]; Ball shapeB; ALLEGRO_DISPLAY *display = NULL; ALLEGRO_EVENT_QUEUE *event_queue = NULL; ALLEGRO_TIMER *timer; //Initialization Functions if(!al_init()) //initialize Allegro return -1; display = al_create_display(WIDTH, HEIGHT); //create our display object if(!display) //test display object return -1; al_init_primitives_addon(); al_install_keyboard(); srand(time(NULL)); InitTriangle(shapeT, NUM_TRIANGLES); InitBall(shapeB); event_queue = al_create_event_queue(); timer = al_create_timer(1.0/FPS); al_register_event_source(event_queue, al_get_keyboard_event_source()); al_register_event_source(event_queue, al_get_timer_event_source(timer)); al_start_timer(timer); while(!done) { ALLEGRO_EVENT ev; al_wait_for_event(event_queue, &ev); if(ev.type == ALLEGRO_EVENT_KEY_DOWN) { switch(ev.keyboard.keycode) { case ALLEGRO_KEY_LEFT: keys[LEFT] = true; break; case ALLEGRO_KEY_RIGHT: keys[RIGHT] = true; break; case ALLEGRO_KEY_SPACE: keys[SPACE] = true; break; } } else if(ev.type == ALLEGRO_EVENT_KEY_UP) { switch(ev.keyboard.keycode) { case ALLEGRO_KEY_LEFT: keys[LEFT] = false; break; case ALLEGRO_KEY_RIGHT: keys[RIGHT] = false; break; case ALLEGRO_KEY_ESCAPE: done=true; break; case ALLEGRO_KEY_SPACE: keys[SPACE] = false; break; } } else if(ev.type == ALLEGRO_EVENT_TIMER) { redraw = true; if(keys[LEFT]) MoveLeftBall(shapeB); if(keys[RIGHT]) MoveRightBall(shapeB); if (shapeB.pos_Y >= 380) { // means the ball is on the ground if(keys[SPACE]) { //JumpBall(shapeB); if (shapeB.canJump) { shapeB.dy= -10; shapeB.canJump = false; } } else shapeB.canJump = true; } if(shapeB.pos_Y < 380) shapeB.dy += 0.5; //JumpBall(shapeB); if (keys[SPACE] && shapeB.pos_Y <=380 && shapeB.dy > 0) shapeB.dy -= 0.1; // got to be LESS than 0.5 in this example if(shapeB.dy>5) shapeB.dy = 5; speed +=0.01; //shapeB.pos_X +=speed*1/2; shapeB.pos_Y +=shapeB.dy; if(shapeB.pos_Y > 380) shapeB.pos_Y = 380; StartTriangle(shapeT, NUM_TRIANGLES); UpdateTriangle(shapeT, NUM_TRIANGLES); } if(redraw && al_is_event_queue_empty(event_queue)) { redraw= false; /*al_draw_filled_circle(pos_x,pos_y,20,al_map_rgb(255,0,255));*/ //UpdateTriangle(shapeT,NUM_TRIANGLES); DrawBall(shapeB); DrawTriangle(shapeT,NUM_TRIANGLES); //al_draw_rounded_rectangle(180,340,260,400,5,5,al_map_rgb(255,255,255),3); al_flip_display(); al_clear_to_color(al_map_rgb(0,0,0)); } } //al_rest(5.0); al_destroy_display(display); //destroy our display object return 0; } void InitBall(Ball &shapeB) { shapeB.ID=BALL; shapeB.pos_X= 30; shapeB.pos_Y= 380; shapeB.lives = 3; shapeB.speed = 7; shapeB.score = 0; shapeB.boundX = 57; shapeB.boundY = 57; shapeB.dx = 1; shapeB.dy = 1; } void DrawBall(Ball &shapeB) { al_draw_filled_circle(shapeB.pos_X,shapeB.pos_Y,20,al_map_rgb(255,0,255)); } void MoveLeftBall(Ball &shapeB) { shapeB.pos_X -=shapeB.speed; if(shapeB.pos_X<20) shapeB.pos_X = 20; } void MoveRightBall(Ball &shapeB) { shapeB.pos_X += shapeB.speed; if(shapeB.pos_X>600) shapeB.pos_X = 600; } void JumpBall(Ball &shapeB) { if (shapeB.canJump) { shapeB.dy= -10; shapeB.canJump = false; } else { shapeB.canJump = true; } if(shapeB.pos_Y < 380) shapeB.dy += 0.5; } void InitTriangle(Triangle shapeT[], int size) { for(int i=0; i<size; i++) { shapeT[i].ID = TRIANGLE; shapeT[i].live = false; shapeT[i].speed = 2; shapeT[i].random = 1 + rand() % 10; } } void DrawTriangle(Triangle shapeT[], int size) { for(int i=0; i<size; i++) { if(shapeT[i].live) { //if(shapeT[i].pos_x3 == shapeT[i+1].pos_x1 || shapeT[i].pos_x3 +100<= shapeT[i+1].pos_x1) //{ /*&& shapeT[i].pos_x3 == shapeT[i].pos_rect_x1 || shapeT[i].pos_x3 + 400 <= shapeT[i].pos_rect_x1 || shapeT[i].pos_x1 >= shapeT[i].pos_rect_x2) */ if(shapeT[i].random < 5) al_draw_triangle(shapeT[i].pos_x1,shapeT[i].pos_y1,shapeT[i].pos_x2,shapeT[i].pos_y2, shapeT[i].pos_x3,shapeT[i].pos_y3,al_map_rgb(255,0,255),2); else al_draw_rounded_rectangle(shapeT[i].pos_rect_x1,shapeT[i].pos_rect_y1, shapeT[i].pos_rect_x2,shapeT[i].pos_rect_y2,5,5,al_map_rgb(255,255,255),3); //} } } } void StartTriangle(Triangle shapeT[], int size) { for (int i = 0 ; i < size; i++) { if(!shapeT[i].live) { if(rand() % 500 == 0) { shapeT[i].live = true; shapeT[i].pos_x1 = 780 + rand() % 20; shapeT[i].pos_y1 = 400; shapeT[i].pos_x2 = shapeT[i].pos_x1+10; shapeT[i].pos_y2 = 370; shapeT[i].pos_x3 = shapeT[i].pos_x1+20; shapeT[i].pos_y3 = 400; shapeT[i].pos_rect_x1 = shapeT[i].pos_x3 + 20; shapeT[i].pos_rect_y1 = 320; shapeT[i].pos_rect_x2 = shapeT[i].pos_rect_x1 + 80; shapeT[i].pos_rect_y2 = 400; break; } } } } void UpdateTriangle(Triangle shapeT[], int size) { for(int i=0; i<size; i++) { if(shapeT[i].live) { shapeT[i].pos_x1 -= shapeT[i].speed; shapeT[i].pos_x2 -= shapeT[i].speed; shapeT[i].pos_x3 -= shapeT[i].speed; shapeT[i].pos_rect_x1 -= shapeT[i].speed; shapeT[i].pos_rect_x2 -= shapeT[i].speed; if(shapeT[i].pos_x3 <0 || shapeT[i].pos_rect_x2 <0) shapeT[i].live = false; } } }[/code]