Here is the code:
Code: Select all
#define USE_CONSOLE
#include <allegro.h>
#include <mvTIMER_a.h>
#include <stdio.h>
#include <stdlib.h>
const static int screen_x=1440;
const static int screen_y=900;
class ball
{
public:
void physics(int x, int y1, int y2, int test);
void draw();
void move();
ball(int rad);
bool gameon;
int x; //x coordinate
int y; //y coordinate
int r; //radius
int oldx;
int oldy;
int dirx; //x's direction
int diry; //y's direction
};
ball::ball(int rad)
{
r=rad;
x=700;
y=500;
dirx=-1;
gameon=true;
diry=(rand()%1);
if(dirx==0)
{
dirx=-1;
}
if(diry==0)
{
diry=-1;
}
}
void ball::move()
{
oldx=x;
oldy=y;
x+=r*dirx;
y+=r*diry;
}
void ball::physics(int px, int y1, int y2, int test)
{
if(test==1&&x-(r)==px&&(y<=y2+r&&y>=y1-r))
{
dirx=1;
}
if(test==2&&x-(r)==px&&(y<=y2+r&&y>=y1-r)) //this should check the collision for the computer player. This also might be whats wrong.
{
dirx=-1;
}
if(x>=screen_x-r)
{
dirx=-1;
}
if(x<=0+r)
{
dirx=1;
}
if(y>=screen_y-r)
{
diry=-1;
}
if(y<=0+r)
{
diry=1;
}
}
void ball::draw()
{
circlefill(screen,oldx,oldy,r,makecol(0,0,0));
circlefill(screen,x,y,r,makecol(255,255,255));
}
int main()
{
allegro_init();
install_keyboard();
set_gfx_mode(GFX_AUTODETECT,screen_x,screen_y,0,0);
//AI
int aix=1240;
int aiy=100;
int aixx=aix-25;
int aiyy=aiy-150;
//Player
int x=200;
int y=100;
int xx=x-25;
int yy=y-150;
ball b(25);
acquire_screen();
while(!key[KEY_ESC])
{
if(b.y>aiy)
{
rectfill(screen,aix,aiy,aixx,aiyy,makecol(0,0,0));
aiy+=25;
aiyy=aiy-150;
rectfill(screen,aix,aiy,aixx,aiyy,makecol(255,255,255));
}
if(b.y<aiy)
{
rectfill(screen,aix,aiy,aixx,aiyy,makecol(0,0,0));
aiy-=25;
aiyy=aiy-150;
rectfill(screen,aix,aiy,aixx,aiyy,makecol(255,255,255));
}
if(key[KEY_DOWN])
{
rectfill(screen,x,y,xx,yy,makecol(0,0,0));
y+=30;
yy=y-150;
rectfill(screen,x,y,xx,yy,makecol(255,255,255));
}
if(key[KEY_UP])
{
rectfill(screen,x,y,xx,yy,makecol(0,0,0));
y-=30;
yy=y-150;
rectfill(screen,x,y,xx,yy,makecol(255,255,255));
}
b.move();
b.physics(x,yy,y,1); //check for collision against player
b.physics(aix,aiy,aiyy,2); //check for collision agains opponent. I have no idea why this isnt working.
rectfill(screen,x,y,xx,yy,makecol(255,255,255));
b.draw();
if(b.gameon==false)
{
return 0;
}
timer(30);
}
}
END_OF_MAIN();
Oh, if you plan on running this on your computer, you should probably change the:
Code: Select all
const static int screen_x=1440; //to your screen res
const static int screen_y=900;
int aix=1240; // and make this lower