problem with my first game in allegro 5

Started by
3 comments, last by RulerOfNothing 12 years, 3 months ago
[color=#0000FF]Hi guys
[color=#0000FF]this is my first time using allegro 5 and creating a game..

[color=#0000FF]my game is about a ball moving in the screen and it should change its direction when it hits the walls or when it hits the bar witch is moving by the user


[color=#0000FF]my problem is when the ball comes closer to the bar something goes wrong...

[color=#0000FF]so if someone can check my code and help me I will be thankful





#include <stdio.h>
#include <iostream>
#include <allegro5/allegro.h>
#include <allegro5/allegro_primitives.h>
using namespace std;
#include<math.h>

class line { public:
int x1,x2,y1,y2;
line(int x1,int y1,int x2,int y2)
{

this->x1=x1;
this->y1=y1;
this->x2=x2;
this->y2=y2;
}

void drow()
{
al_draw_line(x1, y1, x2,y2, al_map_rgb(0, 0, 255), 30.0);

}
void move(int i){
if(i==1&&x2<640)
{
x2+=50;
x1+=50;
}
if(i==2&&x1>0)
{
x1-=50;
x2-=50;
}



}

};

class Ball{
public:

int x, y,i;
int R;
int r,b,g;
int dx,dy;


Ball(int x, int y, int R, int r,int b , int g){
this->x=x;
this->y=y;
this->R= R;
this->r=r;
this->b=b;
this->g=g;
i=0;

dx=dy=4;
}
void draw(){

al_draw_filled_circle(x,y,R,al_map_rgb(r,b,g));
}
void move(){
x+=dx;
y+=dy;
}
int collid(int w, int h,line *l){
if(((y+R)-l->y1==0)||((x+R)>=l->x1&&(x+R)<=l->x2)){
dx*=-1;
dy*=-1;} // I know there is something wrong here....

else{

if ((x+R >= w)||(x-R<= 0)) dx*=-1;
if ((y-R<= 0)||(y+R >= h)) dy*=-1;

}
}




};



Advertisement
Be more specific.

[color=#0000FF]my problem is when the ball comes closer to the bar something goes wrong...

What goes wrong?
Show us the part of the code where this happens. Nodoby is going to try to read and understand your code :)
Well, something that immediately strikes me as a bit off is that you go back on both x and y directions when your ball hits the line. Since the line is vertical, you usually only want to reflect on the y velocity.

Be more specific.

[color=#0000FF]my problem is when the ball comes closer to the bar something goes wrong...

What goes wrong?
Show us the part of the code where this happens. Nodoby is going to try to read and understand your code smile.png




[color=#000088]int[color=#000000] collid[color=#666600]([color=#000088]int[color=#000000] w[color=#666600],[color=#000000] [color=#000088]int[color=#000000] h[color=#666600],[color=#000000]line [color=#666600]*[color=#000000]l[color=#666600]){
[color=#000000] [color=#000088]if[color=#666600]((([color=#000000]y[color=#666600]+[color=#000000]R[color=#666600])-[color=#000000]l[color=#666600]->[color=#000000]y1[color=#666600]==[color=#006666]0[color=#666600])||(([color=#000000]x[color=#666600]+[color=#000000]R[color=#666600])>=[color=#000000]l[color=#666600]->[color=#000000]x1[color=#666600]&&([color=#000000]x[color=#666600]+[color=#000000]R[color=#666600])<=[color=#000000]l[color=#666600]->[color=#000000]x2[color=#666600])){[color=#000000] [color=#880000]// I know there is something wrong here....

[color=#000000] dx[color=#666600]*=-[color=#006666]1[color=#666600];
[color=#000000] dy[color=#666600]*=-[color=#006666]1[color=#666600];}[color=#000000]
[color=#000000]
[color=#000088]else[color=#666600]{
[color=#000000]
[color=#000088]if[color=#000000] [color=#666600](([color=#000000]x[color=#666600]+[color=#000000]R [color=#666600]>=[color=#000000] w[color=#666600])||([color=#000000]x[color=#666600]-[color=#000000]R[color=#666600]<=[color=#000000] [color=#006666]0[color=#666600]))[color=#000000] dx[color=#666600]*=-[color=#006666]1[color=#666600];
[color=#000000] [color=#000088]if[color=#000000] [color=#666600](([color=#000000]y[color=#666600]-[color=#000000]R[color=#666600]<=[color=#000000] [color=#006666]0[color=#666600])||([color=#000000]y[color=#666600]+[color=#000000]R [color=#666600]>=[color=#000000] h[color=#666600]))[color=#000000] dy[color=#666600]*=-[color=#006666]1[color=#666600];
[color=#000000]
[color=#666600]}
[color=#000000] [color=#666600]}
[color=#000000]

[color=#0000cd]I mean I don't know when the ball will hit the bar ,
[color=#0000cd]I took this (y+R)-l->y1==0)||((x+R)>=l->x1&&(x+R)<=l->x2 from my friend, but it doesn't give me the right position
To clarify with my suggestion, you should get rid of the dy*=-1 line from the main "line collision" test and possibly put in a secondary if block which tests to see if the ball hit the edge of the line.

This topic is closed to new replies.

Advertisement