Pages

ini adalah project kami........... project ini menggantikan nilai uas kami.... tidak banyak yang berbeda pada game pong yang lain... hanya saja kami menambahkan suara

lihat syntax :

/* program game pong */
#include
#include
#include
#include

/*pendeklarasian variabel awal*/
BITMAP *pintro=NULL;
BITMAP *pmulai=NULL;
BITMAP *buffer; //This will be our temporary bitmap for double buffering
MIDI *music;
MIDI *music_awal;
MIDI *music_akhir;

/*pendeklarasian koordinat awal*/
int ball_x = 320;
int ball_y = 240;

int ball_tempX = 320;
int ball_tempY = 240;

int p1_x = 20;
int p1_y = 210;

int p1_tempX = 20;
int p1_tempY = 210;

int p2_x = 620;
int p2_y = 210;

int p2_tempX = 620;
int p2_tempY = 210;

int point_p1=0, point_p2=0;

time_t secs; //The seconds on the system clock will be stored here
//this will be used as the seed for srand()

int dir; //This will keep track of the circles direction
//1= up and left, 2 = down and left, 3 = up and right, 4 = down and right

/*fungsi perpindahan bola
- dir1 : bola menuju koordinat kiri atas,
- dir2 : bola menuju koordinat kiri bawah,
- dir3 : bola menuju koordinat kanan atas,
- dir4 : bola menuju koordinat kanan bawah */
void moveBall(){

ball_tempX = ball_x;
ball_tempY = ball_y;
if (dir == 1 && ball_x > 5 && ball_y > 5){
if( ball_x == p1_x + 15 && ball_y >= p1_y && ball_y <= p1_y + 60){
dir = rand()% 2 + 3;
}else{
--ball_x;
--ball_y;
}
} else if (dir == 2 && ball_x > 5 && ball_y < 475){
if( ball_x == p1_x + 15 && ball_y >= p1_y && ball_y <= p1_y + 60){
dir = rand()% 2 + 3;
}else{
--ball_x;
++ball_y;
}
} else if (dir == 3 && ball_x < 635 && ball_y > 5){
if( ball_x + 5 == p2_x && ball_y >= p2_y && ball_y <= p2_y + 60){
dir = rand()% 2 + 1;
}else{
++ball_x;
--ball_y;
}
} else if (dir == 4 && ball_x < 635 && ball_y < 475){
if( ball_x + 5 == p2_x && ball_y >= p2_y && ball_y <= p2_y + 60){
dir = rand()% 2 + 1;
}else{
++ball_x;
++ball_y;
}
} else {
if (dir == 1 || dir == 3)
++dir;
else if (dir == 2 || dir == 4)
--dir;
}
/*mulai merancang screen*/
acquire_screen();
circlefill ( buffer, ball_tempX, ball_tempY, 5, makecol( 0, 0, 0));
circlefill ( buffer, ball_x, ball_y, 5, makecol( 128, 255, 0));
draw_sprite( screen, buffer, 0, 0);
release_screen();
rest(5);
}

/*fungsi pemindahan palang1
- tombol W: untuk arah atas,
- tombol S: untuk arah bawah*/
void p1Move(){
p1_tempY = p1_y;
if( key[KEY_W] && p1_y > 0){
--p1_y;
} else if( key[KEY_S] && p1_y < 420){
++p1_y;
}
acquire_screen();
rectfill( buffer, p1_tempX, p1_tempY, p1_tempX + 10, p1_tempY + 60, makecol ( 0, 0, 0));
rectfill( buffer, p1_x, p1_y, p1_x + 10, p1_y + 60, makecol ( 0, 0, 255));
release_screen();
}

/*fungsi pemindahan palang2
- tombol up: untuk arah atas,
- tombol down: untuk arah bawah*/
void p2Move(){
p2_tempY = p2_y;
if( key[KEY_UP] && p2_y > 0){
--p2_y;
} else if( key[KEY_DOWN] && p2_y < 420){
++p2_y;
}
acquire_screen();
rectfill( buffer, p2_tempX, p2_tempY, p2_tempX + 10, p2_tempY + 60, makecol ( 0, 0, 0));
rectfill( buffer, p2_x, p2_y, p2_x + 10, p2_y + 60, makecol ( 0, 0, 255));
release_screen();
}
/*fungsi untuk mengembalikan posisi awal bola dan pemain*/
void startNew(){
clear_keybuf();
readkey();
clear_to_color( buffer, makecol( 0, 0, 0));
ball_x = 320;
ball_y = 240;

p1_x = 20;
p1_y = 210;

p2_x = 620;
p2_y = 210;
}
/*fungsi untuk mengecek pertambahan point*/
void checkWin(){
if ( ball_x < p1_x){
textout_ex( screen, font, "Player 2 Wins!", 320, 240, makecol( 255, 0, 0), makecol( 0, 0, 0));
point_p2=point_p2+100;
startNew();
}
else if ( ball_x > p2_x){
textout_ex( screen, font, "Player 1 Wins!", 320, 240, makecol( 255, 0, 0), makecol( 0, 0, 0));
point_p1=point_p1+100;
startNew();
}
}
/*fungsi untuk menampilkan kemenangan*/
void cekmenang(){
acquire_screen();
if(point_p1==500)
{
textprintf_ex(screen, font,250, 250, makecol(255,0,0), 0,"P1 MENANG!!");
}
if(point_p2==500)
{
textprintf_ex(screen, font,250, 250, makecol(255,0,0), 0,"P2 MENANG!!");
}
release_screen();
}

/*fungsi untuk menyimpan dalam file*/
void operasifile(){
FILE *pfolder;
pfolder=fopen("nilai.txt","w");
if(pfolder!=NULL){
fprintf(pfolder,"\nPOINT P1 : %d\n",point_p1);
fprintf(pfolder,"\nPOINT P2 : %d\n",point_p2);
fclose(pfolder);
}
}

/*fungsi untuk menampilkan point*/
void cekpoint(){
acquire_screen();
textprintf_ex(screen, font,250, 200, makecol(255,0,0), 0,"POINT P1 : %d ", point_p1);
textprintf_ex(screen, font,250, 210, makecol(255,0,0), 0,"POINT P2 : %d ", point_p2);
textprintf_ex(screen, font,250, 265, makecol(255,0,0), 0,"tekan x untuk kembali ke menu utama");
release_screen();
}

/*fungsi untuk memulai permainan*/
void setupGame(){
acquire_screen();
music=load_midi("game_main.mid");
play_midi (music,FALSE);
draw_sprite( screen, buffer, 0, 0);
rectfill( buffer, p1_x, p1_y, p1_x + 20, p1_y + 60, makecol ( 0, 255, 255));
rectfill( buffer, p2_x, p2_y, p2_x + 30, p2_y + 60, makecol ( 0, 255, 255));
circlefill ( buffer, ball_x, ball_y, 255, makecol( 128, 255, 0));
release_screen();

time(&secs);
srand( (unsigned int)secs);
dir = rand() % 4 + 1;
}

/*fungsi untuk menampilkan menu dan isinya
tombol 1 untuk menampilkan credit
tombol 2 untuk memulai permainan
tombol 3 untuk keluar dari permainan
*/
void intro(){
music_awal=load_midi("mintro.mid");
play_midi (music_awal,FALSE);
while(!key[KEY_1] || !key[KEY_2] || !key[KEY_3])
{
pmulai=load_bitmap("gambar_awal.bmp",NULL);
draw_sprite( screen, pmulai, 2, 2);
if(key[KEY_1])
{ clear(screen);
pintro=load_bitmap("INTRO.bmp",NULL);
draw_sprite( screen, pintro, 7, 6);
rest(10000);
intro();
}
if(key[KEY_2])
{ destroy_midi(music_awal);
point_p1=0, point_p2=0;
setupGame();

while(!key[KEY_ESC]){
p1Move();
p2Move();
moveBall();
checkWin();
clear(buffer);
if(point_p1==500 || point_p2==500)break;
}
destroy_midi(music);
clear(screen);
music_akhir=load_midi("point.mid");
play_midi (music_akhir,FALSE);
while(!key[KEY_X])
{
cekpoint();
cekmenang();
}
destroy_midi(music_akhir);
operasifile();
intro();
}
if(key[KEY_3])
{ destroy_midi(music_awal);
clear(pmulai);
exit(1);
}
}
}

/*fungsi utama*/
int main(){

allegro_init();
install_keyboard();
/*menginstal penggunaan sound*/
install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, NULL);
/*mengeset kedalaman warna*/
set_color_depth(16);
set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0);
buffer = create_bitmap(640,480);
intro();
return 0;
}
END_OF_MAIN();