Il mio obbiettivo è fare interagire le classi tra loro. Qualsiasi oggetto di quella classe si deve comportare in quel modo con quell'altra determinata classe. Comunque posto il codice completo (sto utilizzando allegro).
main.cpp
codice:
#include <allegro.h>
#include "header.h"
const int W_W=640, W_H=480;
BITMAP *buffer;
Player player;
Player nemico;
Palla palla;
int main(){
allegro_init();
install_keyboard();
install_mouse();
set_gfx_mode(GFX_AUTODETECT_WINDOWED, W_W, W_H, 0, 0);
set_color_depth(32);
buffer=create_bitmap(W_W, W_H);
iniscr=create_bitmap(W_W, W_H);
player.x=0;player.giocatore=true;
nemico.x=SCREEN_W-15;nemico.giocatore=false;
while(!key[KEY_ESC]){
vsync();
blit(buffer, screen, 0, 0, 0, 0, W_W, W_H);
clear(buffer);
palla.muovi();
player.muovi();
}
return 0;
}
END_OF_MAIN();
player.cpp
codice:
#include "header.h"
#include <allegro.h>
extern BITMAP *buffer;
extern int W_H, W_W;
void Player::muovi(){
rectfill(buffer, x, y, x+15, y+60, 0xffffff);
if(giocatore){
if(key[KEY_DOWN]) y+=4;
if(key[KEY_UP]) y-=4;
if(y<=0) y=0;
if(y+60>=W_H) y=W_H-60;
}
}
Player::Player(){
y=SCREEN_W/2-30;
}
palla.cpp
codice:
#include "header.h"
#include <allegro.h>
extern BITMAP *buffer;
extern int W_W, W_H; ---> questo non so perchè ma non funziona
void Palla::muovi(){
circle(buffer, x, y, 10, 0xffffff);
if(x>=W_W-1) v[0]-=1;
if(x<=1) v[0]+=1;
if(y<=1) v[1]+=1;
if(y>=W_H-1) v[1]-=1;
x+=v[0];
y+=v[1];
}
Palla::Palla(){
x=SCREEN_W/2;
y=SCREEN_H/2;
v[0]=8;
v[1]=8;
}
header.h
codice:
class Player{public:
Player();
int x, y;
bool giocatore;
void muovi();
void azzera();
};
class Palla{
public:
Palla();
int x,y,v[2];
void muovi();
};