#include
#include
#include
#include
#include
using namespace std;
class pongBall {
int x, y, length, height, screenx, screeny, color;
bool visible;
public:
int speedx, speedy;
pongBall(int bildschirmx, int bildschirmy, int posx = 0, int posy = 0, int laenge = 0, int hoehe = 0, int geschwindigkeit = 0, int farbe = 0x000000) {
if(posx == 0 && posy == 0 && laenge == 0 && hoehe == 0) {
visible = false;
}
else {
x = posx;
y = posy;
visible = true;
}
color = farbe;
length = laenge;
height = hoehe;
speedx = geschwindigkeit;
speedy = geschwindigkeit;
screenx = bildschirmx;
screeny = bildschirmy;
}
void print(BITMAP* bitmap) {
rectfill(bitmap, x, y, x + length, y + height, color);
}
void setPosition(int posx, int posy) {
x = posx,
y = posy;
}
int getPositionX() {
return x;
}
int getPositionY() {
return y;
}
void setSize(int laenge, int hoehe) {
length = laenge;
height = hoehe;
}
int getSizeX() {
return length;
}
int getSizeY() {
return height;
}
void overprint(BITMAP* bitmap, int farbe = 0x000000) {
rectfill(bitmap, x, y, x + length, y + height, farbe);
}
void setColor(int farbe) {
color = farbe;
}
int getColor() {
return color;
}
bool collide(rechteck recht2) {
if( (recht2.getPositionX() = x) && (recht2.getPositionY() = y) ) {
return true;
}
else {
return false;
}
}
short wand() {
if((y = screeny)) {
return 3;
}
else if(x return 1;
}
else if((x + length) >= screenx) {
return 2;
}
else {
return 0;
}
}
};
int main() {
//Bildschrimauflösung einlesen
int x = GetSystemMetrics(SM_CXSCREEN);
int y = GetSystemMetrics(SM_CYSCREEN);
//Allegro und Alfont initialisieren
allegro_init();
install_keyboard();
alfont_init();
set_color_depth(16);
set_gfx_mode(GFX_AUTODETECT, x, y, 0, 0);
BITMAP *buffer;
buffer = create_bitmap(x, y);
ALFONT_FONT *myfont;
myfont = alfont_load_font("digital.ttf");
alfont_set_font_size(myfont, 30);
rechteck schlaeger1(0, 0, 10, 40, 0xFFFFFF, true);
rechteck schlaeger2(x - 10, 400, 10, 40, 0xFFFFFF, true);
pongBall ball(x, y, x/2, y/2, 15, 15, 4, 0xFFFFFF);
int punkte1 = 0, punkte2 = 0;
while(!key[KEY_ESC]) {
clear(buffer);
//Schläger 1 bewegen
if(key[KEY_DOWN]) {
schlaeger1.setPosition(0, schlaeger1.getPositionY() + 6);
}
else if(key[KEY_UP]) {
schlaeger1.setPosition(0, schlaeger1.getPositionY() - 6);
}
//Schläger 2 bewegen
if(key[KEY_S]) {
schlaeger2.setPosition(x - schlaeger2.getSizeX(), schlaeger2.getPositionY() + 6);
}
else if(key[KEY_W]) {
schlaeger2.setPosition(x - schlaeger2.getSizeX(), schlaeger2.getPositionY() - 6);
}
//Kolisionsabfrage mit den Schlägern und dann mit den Wänden
if(ball.collide(schlaeger1) || ball.collide(schlaeger2)) {
ball.speedx *= -1;
ball.speedy *= -1;
}
else if(ball.wand() == 1) {
ball.setPosition(x/2, y/2);
punkte2++;
}
else if(ball.wand() == 2) {
ball.setPosition(x/2, y/2);
punkte1++;
}
else if(ball.wand() == 3) {
ball.speedy *= -1;
}
alfont_textprintf_centre_aa(buffer, myfont, x/2, 5, 0xFFFFFF, "%d : %d", punkte1, punkte2); //Punkte ausgeben
ball.setPosition(ball.getPositionX() + ball.speedx, ball.getPositionY() + ball.speedy); //Position des Balles neu setzen
//Schläger und Ball in den Buffer zeichnen
schlaeger2.print(buffer);
schlaeger1.print(buffer);
ball.print(buffer);
//Buffer auf Bildschirm ausgeben
blit(buffer, screen, 0, 0, 0, 0, x, y);
Sleep(5);
}
return 0;
}
END_OF_MAIN();