Donatur utama

Tampilkan postingan dengan label CPP. Tampilkan semua postingan
Tampilkan postingan dengan label CPP. Tampilkan semua postingan

Sabtu, 14 Maret 2009

Statemant Animasi pada CPP

Animasi dalam program sebenarnya bukan benar-benar mengerakkan sebuah objek, tetapi dengan menghapus-mencetak objek tersebut berulang-ulang dengan cepat sehingga terlihat oleh mata seakan-akan bergerak.
Berikut ini perintah-perintah atau fungsi yang sering digunakan dalam C++ untuk membuat suatu program animasi.

1. clrscr()
Digunakan untuk membersihkan layar dari tampilan sebelumnya, sehingga teks apapun yang ada dilayar sebelumnya menjadi tidak ditampilkan lagi.
2. delay()
Memerintahkan program untuk berhenti sejenak (menunda proses), sebelum melanjutkan eksekusi perintah berikutnya.
Perintah delay(1); artinya program akan berhenti selama 1/1000 detik atau 0,001 detik.
3. kbhit() dan getch()
Digunakan jika ingin program berhenti atau terus berjalan tergantung ada atau tidaknya penekanan tomboldari user.
4. random()
Digunakan jika menginginkan program untuk menghasilkan angka secara acak.



Kamis, 20 November 2008

Animasi Bola Pantul2

Ternyata banyak Game yang dapat dibuat menggunakan tampilan console, kini saya menyertakan sebuah game yang dapat dibuat menggunakan CPP dan VbNet console, Game tersebut adalah Game Bola Pantul dimana, Bola akan dipantul jika kena papan pantul yang posisinya dapat digeser kekiri dan ke kanan. Skor akan bertambah jika bola mengenai bidang pantulan di sisi berlawanan.


Ini nih kodenya, dan jika Mau langsung programnya silahkan aja di Belajar Tik dan Ziddu.


Kode Pada CPP:


#include<constream.h>

#include<dos.h>

#include<stdlib.h>

void main()

{

        textbackground(0);

        clrscr();

        int xb,yb,axb,ayb;

        int w[41];

        int v[15];

        int speed;

        int csk,t;

        t=4;        

        speed=100;

         axb=1;

        ayb=1;

        xb=20;

        yb=5;

        int xp,yp;


        xp=25;

        yp=23;

        int skor=0;

        int tombol;

        int p;

        for(p=1;p<=42;p++)

        {

                textbackground(3);

                gotoxy(p,1);

                cprintf(" ");

                gotoxy(p,24);

                cprintf(" ");

        }

        for(p=1;p<=24;p++)

        {

                textbackground(3);

                gotoxy(1,p);

                cprintf(" ");

                gotoxy(42,p);

                cprintf(" ");

        }

        textbackground(0);

        int pj;

        for(p=5;p<=36;p++)

                w[p]=1;

                for(p=2;p<=t;p++)

                v[p]=p;

                int br;

        for(br=2;br<=t;br++)

        {

                for(p=5;p<=36;p++)

                { gotoxy(p,br);

                   cprintf("ß");}

        }

        _setcursortype(_NOCURSOR);


        do

        {

                // textbackground(CYAN);

                gotoxy(10,1);

                cout<<"skor = "<<skor;

                gotoxy(30,1);

                cout<<"speed = "<<speed;

                pj=xp+8;

                for(p=xp;p<=pj;p++)

                {

                        gotoxy(p,yp);

                        cprintf("ß");

                }

                 gotoxy(xb,yb);

               cprintf("O");


                delay(speed);


                gotoxy(xb,yb);

                cprintf(" ");

                for(p=xp;p<=pj;p++)

                {

                        gotoxy(p,yp);

                        cprintf(" ");

                }

                if(kbhit())

                {

                        tombol=getch();

                        if(tombol==75)

                        {

                                if(xp>10)

                                        xp=xp-6;

                                else

                                        xp=2;

                        }


                        else if(tombol==77)

                        {

                                if(xp<30)

                                        xp=xp+6;

                                else

                                        xp=33;

                        }

               }

                        xb=xb+axb;

                        yb=yb+ayb;


                if(xb==2)

                        axb=1;

                else if(xb==41)

                        axb=-1;

                if(yb==2)

                        ayb=1;


                if(yb==22 && (xb>=xp-1 && xb<=pj+1))

                {

                        ayb=-1;

                }

                else if(yb==23)

                {

                        gotoxy(30,12);

                        cout<<"Game Over";

                        tombol=27;

                }

                for(csk=t;csk>=3;csk--)

                {

                        if(yb==csk && w[xb]==1)

                        {

                                w[xb]=0;

                                ayb=-ayb;

                                skor=skor+10;

                                if(skor%50==0)

                                {

                                        if(speed>30)

                                        speed=speed-5;

                                }

                        }

                }


        }while(tombol!=27);

        getch();

}

Game Snack Versi3

Setelah Game Snack Versi 1 dan 2 keluar ini versi tiga saya keluarkan, anda bisa menggunakan menggunakan game snack menggunakan versi CPP atau menggunakan Versi VB.net atau mau coba programnya ambil aja di Belajar Tik


Kode program CPP


#include<constream.h>

#include<dos.h>

#include<stdlib.h>

int p,tombol,pj;

int x[10],y[10];

int badan[10];

int xm,ym;

int skor,speed;

int arah;

int ax,ay;

int waktu,w,level;

void cetaku();

void bentukarah();

void skors();

void main()

{

         textbackground(0);

         clrscr();

         waktu=0;

         arah=0;

         w=0;

         speed=200;

         randomize();

         level=0;

         skor=0;

         _setcursortype(_NOCURSOR);

         ax=1;

         ay=0;

         pj=2;

         xm=random(45)+5;

         ym=random(15)+5;

         skor=0;

         for(p=0;p<=pj;p++)

         {

                  x[pj-p]=p+2;

                  y[p]=12;

         }

         int ss;

         textbackground(3);

         for(ss=1;ss<=51;ss++)

         {

                  gotoxy(ss,1);cprintf(" ");

                  gotoxy(ss,24);cprintf(" ");

         }

         for(ss=1;ss<=24;ss++)

         {

                  gotoxy(1,ss);cprintf(" ");

                  gotoxy(51,ss);cprintf(" ");

         }


         textbackground(0);

         do

         {

                  skors();

                  gotoxy(xm,ym);

                  cprintf("$");

                  cetaku();

                  bentukarah();

                  if(kbhit())

                  {

                           tombol=getch();

                           if(tombol==0)

                           {

                                    tombol=getch();

                                    if(tombol==75)

                                    {

                                             if(arah==3 || arah==4 || arah==0)

                                                      arah=2;

                                    }

                                    else if(tombol==77)

                                    {

                                             if(arah==3 || arah==4 || arah==0)

                                                      arah=1;

                                    }

                                    else if(tombol==72)

                                    {

                                             if(arah==1 || arah==2)

                                                      arah=3;

                                    }

                                    else if(tombol==80)

                                    {

                                             if(arah==1 || arah==2)

                                                      arah=4;

                                    }

                           }

                  }

                  if(x[0]==2 || x[0]==50 || y[0]==1 || y[0]==24)

                  {

                           gotoxy(20,12);

                           cout<<"Game Over";

                           tombol=27;

                  }

                  if(x[0]==xm && y[0]==ym && arah!=0)

                  {

                           skor=skor+10;

                           x[pj+1]=x[pj];

                           y[pj+1]=y[pj];

                           pj=pj+1;

                           xm=random(45)+5;

                           ym=random(15)+5;

                  }

                  waktu++;

                  if(waktu%(1000/speed)==0)

                           w=w+1;

                  if(pj==9)

                  {

                           pj=3;

                           if(speed>=60)

                                    speed=speed-10;

                           level=level+1;

                  }

         }while(tombol!=27);

         getch();


}


void cetaku()

{

         for(p=0;p<=pj;p++)

         {

                  gotoxy(x[p],y[p]);

                  cprintf("x");

         }

         delay(100);

         for(p=0;p<=pj;p++)

         {

                  gotoxy(x[p],y[p]);

                  cprintf(" ");

         }

}


void bentukarah()

{

          for(p=pj;p>0;p--)

         {

                  x[p]=x[p-1];

                  y[p]=y[p-1];

         }

         if(arah==1)

                  x[0]=x[0]+1;

         else if(arah==2)

                  x[0]=x[0]-1;

         else if(arah==3)

                  y[0]=y[0]-1;

         else if(arah==4)

                  y[0]=y[0]+1;

 }


void skors()

{

         gotoxy(53,3);

         cout<<"Skor : "<<skor;

         gotoxy(53,5);

         cout<<"Time : "<<w;

         gotoxy(53,7);

         cout<<"Level : "<<level;

}


 


Untuk VB.Net Inini kodenya


Module Module1


Dim p, tombol, pj

Dim x(10), y(10)

Dim badan(10)

Dim xm, ym

Dim skor, speed

Dim arah

Dim ax, ay

Dim waktu, w, level


Sub main()


         Console.BackgroundColor = ConsoleColor.Black

         Console.Clear()

         waktu = 0

         arah = 0

         w = 0

         speed = 200

         Randomize()

         level = 0

         skor = 0

         Console.CursorVisible = False

         ax = 1

         ay = 0

         pj = 2

         xm = CInt(Rnd() * 45) + 5

         ym = CInt(Rnd() * 15) + 5

         skor = 0

         For p = 0 To pj

                   x(pj - p) = p + 2

                  y(p) = 12

         Next

         Dim ss

         Console.BackgroundColor = ConsoleColor.Cyan

         For ss = 1 To 51

                  Console.CursorTop = 1

                  Console.CursorLeft = ss

                  Console.Write(" ")

                  Console.CursorTop = 24

                  Console.CursorLeft = ss

                  Console.Write(" ")

         Next

         For ss = 1 To 24

                  Console.CursorTop = ss

                  Console.CursorLeft = 1

                  Console.Write(" ")

                  Console.CursorTop = ss

                  Console.CursorLeft = 51

                  Console.Write(" ")

         Next

         Console.BackgroundColor = ConsoleColor.Black

         Do

                   skors()

                  Console.CursorTop = ym

                  Console.CursorLeft = xm

                  Console.Write("$")

                  cetaku()

                  bentukarah()

                  If (Console.KeyAvailable) Then

                           tombol = Console.ReadKey.Key

                           If (tombol = 37) Then

                                    If (arah = 3 Or arah = 4 Or arah = 0) Then arah = 2

                           ElseIf (tombol = 39) Then

                                    If (arah = 3 Or arah = 4 Or arah = 0) Then arah = 1

                           ElseIf (tombol = 38) Then

                                    If (arah = 1 Or arah = 2) Then arah = 3

                           ElseIf (tombol = 40) Then

                                    If (arah = 1 Or arah = 2) Then arah = 4

                           End If

               End If

         If (x(0) = 2 Or x(0) = 50 Or y(0) = 1 Or y(0) = 24) Then

                  Console.CursorTop = 12

                  Console.CursorLeft = 20

                  Console.Write("Game Over")

                  tombol = 27

         End If

         If (x(0) = xm And y(0) = ym And arah <> 0) Then

                  skor = skor + 10

                  x(pj + 1) = x(pj)

                  y(pj + 1) = y(pj)

                  pj = pj + 1

                  xm = CInt(Rnd() * 45) + 5

                  ym = CInt(Rnd() * 15) + 5

         End If

         waktu = waktu + 1

         If (waktu Mod (1000 / speed) = 0) Then

                  w = w + 1

                  If (pj = 9) Then

                            pj = 3

                          If (speed >= 60) Then speed = speed - 10

                           level = level + 1

                    End If

           End If


         Loop While (tombol <> 27)

         Console.ReadKey()

End Sub



Sub cetaku()

         For p = 0 To pj

                   Console.CursorTop = y(p)

                  Console.CursorLeft = x(p)

                  Console.Write("x")

         Next

         System.Threading.Thread.Sleep(100)

         For p = 0 To pj

                  Console.CursorTop = y(p)

                  Console.CursorLeft = x(p)

                  Console.Write(" ")

         Next

End Sub


Sub bentukarah()

         For p = pj To 1 Step -1

                  x(p) = x(p - 1)

                  y(p) = y(p - 1)

         Next

         If (arah = 1) Then

                  x(0) = x(0) + 1

         ElseIf (arah = 2) Then

                  x(0) = x(0) - 1

         ElseIf (arah = 3) Then

                  y(0) = y(0) - 1

         ElseIf (arah = 4) Then

                  y(0) = y(0) + 1

         End If

End Sub



sub skors()

         Console.CursorTop = 3

         Console.CursorLeft = 53

         Console.Write("Skor : " & skor)

         Console.CursorTop = 5

         Console.CursorLeft = 53

         Console.Write("Time : " & w)

         Console.CursorTop = 7

         Console.CursorLeft = 53

         Console.Write("Level : " & level)

End Sub



End Module

Kamis, 13 November 2008

Game: Snack Versi 1 dan versi 2

Snack versi 1 adalah snack versi saya sendiri dimana game ini berupa sebuah objek yang dapat digerakkan sesuai dengan arah panah dan ada sebuah objek yang posisinya dirandom oleh komputer, tugas pemain adalah menggerakkan objek pertama sampai ketemua dengan objek kedua dan jika ketemu maka skornya akan bertambah 10 dan posisi objek kedua akan diubah atau dirandom oleh komputer

nah ini nih kode programnya
#include
#include
void main()
{
clrscr();
int x,y;
x=40;
y=12;
randomize();
int x1,y1;
x1=random(78)+1;
y1=random(24)+1;
long skor;
skor=0;

int tombol;
do
{
clrscr();
gotoxy(x1,y1);
cprintf("$");
gotoxy(1,5);
cout<<"Skor : "< gotoxy(x,y);
cprintf("*");
tombol=getch();
if(tombol==0)
{
tombol=getch();
if(tombol==72)
{
if(y>1)
y=y-1;
else
y=24;
}
else if(tombol==80)
{
if(y<24)
y=y+1;
else
y=1;
}
else if(tombol==75)
{
if(x>1)
x=x-1;
else
x=79;
}
else if(tombol==77)
{
if(x<79)
x=x+1;
else
x=1;
}
}
if(x==x1 && y==y1)
{
skor=skor+10;
x1=random(78)+1;
y1=random(24)+1;
}
}while(tombol!=27);
}


Game Snack Versi 2 silahkan Tik
#include
#include
#include
void main()
{
clrscr();
int x,y;
x=40;
y=12;
randomize();
int x1,y1;
int ax,ay;
ax=0;
ay=0;

x1=random(78)+1;
y1=random(24)+1;
long skor;
skor=0;

int tombol;
do
{
clrscr();

gotoxy(x1,y1);
cprintf("$");
gotoxy(1,5);
cout<<"Skor : "< gotoxy(x,y);
cprintf("*");
delay(100);
if(kbhit())
{
tombol=getch();
if(tombol==0)
{
tombol=getch();
if(tombol==72)
{
ax=0;
ay=-1;
}
else if(tombol==80)
{
ax=0;
ay=1;
}
else if(tombol==75)
{
ax=-1;
ay=0;
}
else if(tombol==77)
{
ax=1;
ay=0;
}
}
}
x=x+ax;
y=y+ay;
if(x==78)
x=1;
else if(x==1)
x=78;
if(y==1)
y=24;
else if(y==24)
y=1;

if(x==x1 && y==y1)
{
skor=skor+10;
x1=random(78)+1;
y1=random(24)+1;
}
}while(tombol!=27);
}

Tombol pada CPP

Pada pemograman cpp kita dapat membaca tombol yang ditekan oleh user, dimana perintah yang kita gunakan untuk membaca tombol tersebut adalah getch().
dimana beberapa nilai tombol
panah atas = 72
panah bawah = 80
panah kiri = 75
panah kanan 77
ESC = 27
enter = 13
spasi = 32
Nah ini ada sebuah kode program yang akan menggerakkan objek sesuai dengan arah panah yang kita tekan dan akan keluar ketika ditekan tombol ESC. untuk coba programnya tik aja
#include
#include
#include
void main()
{
clrscr();
int x,y;
x=40;
y=12;
int tombol;
do
{
clrscr();
gotoxy(x,y);
cprintf("*");
tombol=getch();
if(tombol==0)
{
tombol=getch();
if(tombol==72)
y=y-1;
else if(tombol==80)
y=y+1;
else if(tombol==75)
x=x-1;
else if (tombol==77)
x=x+1;
}
}while(tombol!=27);
}

Kamis, 23 Oktober 2008

Kode Untuk Program Ular Sederhana

# include
# include
# include

struct segment
{
int x;
int y;
}tembok[240],makanan;


main()
{
char tekan,arah='d',counter='a';
struct segment snake[500];
int i,j,k,kena=0,benar=0,panjang=10,posisi=1,score=0,sp=5;

randomize();

for(i=0;i<10;i++)
{
tembok[i].y=1;
tembok[i].x=1+i;
}

for (i=0;i{
snake[i].x=40+i;
snake[i].y=12;
}

clrscr();

do
{

for(i=0;i<204;i++)
{
if((i%20==0 && i!=0) || kena==1)
{
if(kena!=1)
{gotoxy(makanan.x,makanan.y);
printf(" ");
}

do
{
makanan.x=random(77)+2;
makanan.y=random(21)+2;
for (j=0;jif (snake[j].x==makanan.x && snake[j].y==makanan.y) benar=1;

}
while(benar==1);
benar=0;
gotoxy(makanan.x,makanan.y);
printf("@");
kena=0;
}


for(j=0;j{
gotoxy (snake[j].x,snake[j].y); printf("*");
}

for(j=0;j{
gotoxy(tembok[j].x,tembok[j].y);
printf("O");
}

for(j=0;j<3;j++)
delay(30000);

if (snake[sp-1].x==makanan.x && snake[sp-1].y==makanan.y) {score+=10;sp++;}

for(j=0;jfor (k=0;k{
if(tembok[j].x==snake[k].x && tembok[j].y==snake[k].y)
{
gotoxy(30,12);
printf("Game Over (score : %d)",score);
tekan=27;
getch();
}
}

gotoxy(tembok[0].x,tembok[0].y); printf(" ");
gotoxy(snake[0].x,snake[0].y); printf(" ");


if (kbhit()) tekan=getch();

if ((tekan =='w' || tekan=='a' || tekan=='s' || tekan=='d') && tekan!=counter) arah=tekan;
{switch (arah)
{
case 'w' :
{snake[sp].x=snake[sp-1].x;
snake[sp].y=snake[sp-1].y-1;
counter='s';
break;}
case 's' :
{snake[sp].x=snake[sp-1].x;
snake[sp].y=snake[sp-1].y+1;
counter='w';
break;}
case 'a' :
{snake[sp].x=snake[sp-1].x-1;
snake[sp].y=snake[sp-1].y;
counter='d';
break;}
case'd':
{snake[sp].x=snake[sp-1].x+1;
snake[sp].y=snake[sp-1].y;
counter='a';
break;}
}
}
if(tekan==27) break;

if (snake[sp].x==81) snake[sp].x=1;
if (snake[sp].x==0) snake[sp].x=80;
if (snake[sp].y==25) snake[sp].y=1;
if (snake[sp].y==0) snake[sp].y=24;


if (posisi==1)
{ tembok[panjang].x=tembok[panjang-1].x+1;
tembok[panjang].y=tembok[panjang-1].y;
if (tembok[panjang].x==80) posisi=2;
}
else if (posisi==2)
{
tembok[panjang].x=tembok[panjang-1].x;
tembok[panjang].y=tembok[panjang-1].y+1;
if (tembok[panjang].y==24) posisi=3;
}
else if (posisi==3)
{
tembok[panjang].x=tembok[panjang-1].x-1;
tembok[panjang].y=tembok[panjang-1].y;
if (tembok[panjang].x==1) posisi=4;
}
else
{
tembok[panjang].x=tembok[panjang-1].x;
tembok[panjang].y=tembok[panjang-1].y-1;
if (tembok[panjang].y==1) {posisi=1;}
}

for (j=0;jtembok[j]=tembok[j+1];

for (j=0;jsnake[j]=snake[j+1];

delay(200);

}
panjang++;
}
while(tekan!=27);
}

Kamis, 16 Oktober 2008

Penggunaan IF

#include
void main()
{

clrscr();
cout<<"\tKantin xx 1 Medan";
cout<<"\n Jln.yamin ginting no.7";
cout<<"\n ===============================";
cout<<"\n DAFTAR MENU : ";
cout<<"\n 1.MENU MAKANAN";
cout<<"\n 2.MENU MINUMAN";
cout<<"\n PILIHAN MENU:";
int pil;
cin>>pil;
if(pil==1)
{
cout <<"1.Nasi Goreng";
cout <<"\n2.Martabak";
cout <<"\n3.Lontong";
cout <<"\n4.Nasi Ayam";
cout <<"\n5.Indomie Goreng";

}
else if(pil==2)
{
cout <<"1.Air Mineral";
cout <<"\n2.Aneka Jus ";
cout <<"\n3.Minuman Soda";


}
cout <<"\nPilihan Anda:";
int jenis;
cin>>jenis;
cout <<"\nBerapa Banyak:";
int jlh;
cin>>jlh;
if(pil==1 && jenis==1)
{
cout<<"anda memilih nasi goreng";
cout<<"\nanda memesan sebanyak"< cout<<"\nTotal Pembayaran="< }
if(pil==1 && jenis==2)
{
cout<<"anda memilih martabak";
cout<<"\nanda memesan sebanyak"< cout<<"\nTotal Penbayaran="< }
if (pil==1 && jenis==3 )
{
cout<<"anda memilih lontong";
cout<<"\nanda memesan sebanyak"< cout<<"\nTotal Pembayaran="< }
if(pil==1 && jenis==4)
{
cout<<"anda memilih nasi ayam";
cout<<"\nanda memesan sebanyak"< cout<<"\nTotal Pembayaran="< }
if(pil==1 && jenis==5)
{
cout<<"anda memilih indomie goreng";
cout<<"\nanda memesan sebanyak"< cout<<"\nTotal Pembayaran="< }
if (pil==2 && jenis==1)
{
cout<<"anda memilih Air Mineral";
cout<<"\nanda memesan sebanyak"< cout<<"\nTotal Pembayaran="< }
if (pil==2 && jenis==2)
{
cout<<"anda memeilih Aneka Jus";
cout<<"\nanda memesan sebanyak"< cout<<"\nTotal Pembayaran="< }
if(pil==2 && jenis==3)
{
cout<<"anda memilih Minuman soda";
cout<<"\n anda memesan sebanyak"< cout<<"\n Total Pembayaran="< }
getch();
}

Penggunaan Nested For

Contoh tampilan
1
12
123
1234
12345
Program :
for(i=1;i<=5;i++)
{
for(a=1;a<=x;a++)
{
cout<<<"\n";
}
Tampilan
54321
4321
321
21
1
Programnya : for(i=5;i>=1;i--)
{
for(a=i;a>=1;a--)
{
cout<<<"\n"; }
 

Berlangganan

Enter your email address:

Delivered by FeedBurner

Belajar Tik


Pengikut

Komentar Terbaru

Belajar Tik
Tempatnya Belajar Teknologi Informasi dan Komunikasi

Administrator By : 3eta