Contoh Program C++ || Snake game consolle

Klik disini, jika kalian ingin belajar tentang bhs pemrograman termasuk C++
jangan lupa untuk SUBSCRIBE jika kalian ingin lebih pro dalam bhs pemrograman

Nih bagi kalian yang sudah penasaran sama game Snake nya. Cara bikin nya sih sama seperti di Tutorial C dan C++. Create new file, beri nama, lalu klik OK. Nah oe ada source code nya, kalian bisa copy dibawah ini, untuk game Snake nya.

 

#include < iostream >
#include < conio.h >
#include < windows.h >
 

 

#define batas_atas 2
#define batas_bawah 23
#define batas_samping 2
#define batas_samping_k 78
 

 

#define caption "\t\t Ulo-Ulo \t\t\n\n"
 

 

using namespace std;
 

 

int iX(5), iY(5), x1 (4), y1 (4), xHapus(3), yHapus(3), veloX(1), veloY(0),ekorsX[200], ekorsY[200], nEkor = 2, start = 0, makX, makY ,level = 1 ,username;
 

 

void gotoxy(int x, int y)
{
    COORD pos = {x, y};
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
 

 

void naik()
{
    veloY = -1;
    veloX = 0;
}
 

 

void turun()
{
    veloY = 1;
    veloX = 0;
}
 

 

void samping()
{
    veloX = -1;
    veloY = 0;
}
 

 

void samping_k()
{
    veloX = 1;
    veloY = 0;
}
 

 

void hapus()
{
    gotoxy(xHapus, yHapus);
    cout << " ";
}
 

 

void ekor()
{
    for(int j = 0; j <= 2; j++)
    {
        gotoxy(ekorsX[j], ekorsY[j]);
        cout << char(02);
    }
}
 

 

void tampil()
{
    gotoxy(iX, iY);
    cout << char(01);

    gotoxy(makX, makY);
    cout << char(06);
}
 

 

void ganti_posisi()
{
    xHapus = ekorsX[nEkor - 1];
    yHapus = ekorsY[nEkor - 1];
    for(int j = nEkor - 1; j >= 1; j--)
    {
        ekorsX[j] = ekorsX[j-1];
        ekorsY[j] = ekorsY[j-1];
    }
    ekorsX[0] = iX;
    ekorsY[0] = iY;
}
 

 

void velo()
{
    ganti_posisi();
    hapus();
    iX += veloX;
    iY += veloY;
    tampil();
    ekor();
}
 

 

bool isDestroy()
{
    if(iX == batas_samping_k) { iX = 3; gotoxy(78, iY); cout << " "; }
    if(iX == batas_samping)  { iX = 77; gotoxy(2, iY); cout << " "; }
    if(iY == batas_atas) { iY = 22; gotoxy(iX, 2); cout << " "; }
    if(iY == batas_bawah) { iY = 3; gotoxy(iX, 23); cout << " "; }
    for(int j = 0; j <= nEkor - 1; j++)
    if(ekorsX[j] == iX && ekorsY[j] == iY) return true;
    return false;
}
 

 

char getkey()
{
    for(int i = 8; i <= 222; i++)
    {
        if(GetAsyncKeyState(i) == -32767)
        {
            switch(i)
            {
            case 38 : if(veloY != 1)naik();
                        break;
            case 40 : if(veloY != -1)turun();
                        break;
            case 37 : if(veloX != 1)samping();
                        break;
            case 39 : if(veloX != -1)samping_k();
                        break;
            }
        }
    }
}
 

 

void random_makanan()
{
    makX = rand()%(batas_samping_k - 1);
    if(makX < 4) makX += 3 + (4-makX);
    makY = rand()%(batas_bawah - 1);
    if(makY < 4) makY += 3 + (4-makY);
    gotoxy(makX, makY);
    cout << char(3);
    if (nEkor%10==0)
 level=level+1;
}
 

 

bool isEaten()
{
    if(iX == makX && iY == makY)
        return true;
    else
        return false;
}
 

 

void cBorder()
{
    for(int i = 1; i <= 78; i++)
    {
        for(int j = 1; j <= 24; j += 23)
        {
            if(j > 10 || i >= 2)
            {
                gotoxy(i, j);cout << "-";
            }
        }
    }
    for(int i = 1; i <= 24; i++)
    {
        for(int k = 1; k <= 80; k += 78)
        {
            gotoxy(k, i); cout << "|";
        }
    }
}
 

 

void skor()
{
    gotoxy(3,1); cout << "Skor : ";
    gotoxy(18,1); cout << "Panjang : ";
    gotoxy(34,1); cout << "level : ";
    gotoxy(47,1); cout << "twitter : @thomzgunz";
    gotoxy(3,2); cout << "Nama : ";
    gotoxy(47,2); cout << "Instagram : @thomas_gunawan";
}
 

 

void tulis_skor()
{
    gotoxy(11,1); cout << (nEkor) * 10;
    gotoxy(28,1); cout << nEkor;
    gotoxy(41,1); cout << level;
}
 

 

void inisialisasi()
{
    cBorder();
    random_makanan();
    skor();
    tulis_skor();
}
 

 

void tulisTengah(string s, int y)
{
    gotoxy(40 - (s.length() / 2), y);
    cout << s;
}
 

 

int main()
{
    system("color f0");
    int pilih;
 char user[20];

    pilih:
    system("cls");
    cout<<(caption);
    tulisTengah("Selamat Datang",5);
    tulisTengah("================================", 6);
    tulisTengah("GAME Inkor by Qowi17", 7);
    tulisTengah("================================", 8);
    tulisTengah("Follow Me :",10);
    tulisTengah("Twitter : @QBusthomi | Fb : thomas gunawan part I | WA : 085856403947", 11);
    tulisTengah("Blog : akungamenow.blogspot.com | IG : mas_thom_17 | Channel YT : Qowi17 Official", 12);
    tulisTengah("pilih kode nya : 1. untuk bermain , 2. untuk keluar", 14);
    tulisTengah("1.main",15);
    tulisTengah("2.keluar",16);
    tulisTengah("anda pilih yang mana : ",18);
    cin>>pilih;

    switch (pilih)
    {
        case 1:
                goto main;
        case 2:
                goto exit;
        default : tulisTengah("maaf tidak tersedia",25);
                goto pilih;
    }

    main:
    system("cls");
    tulisTengah("Selamat Datang",5);
    tulisTengah("================================", 6);
    tulisTengah("GAME Inkor by Qowi17", 7);
    tulisTengah("================================", 8);
    tulisTengah("Follow Me :",9);
    tulisTengah("Twitter : @QBusthomi | Fb : thomas gunawan part I | WA : 085856403947", 10);
    tulisTengah("Blog : akungamenow.blogspot.com | IG : mas_thom_17 | Channel YT : Qowi17 Official", 11);

    tulisTengah("Masukkan Nama : ",13);
    cin >> user;
    system("cls");
    gotoxy(11,2); cout << user;
    inisialisasi();

    while(!(isDestroy()))
    {
        velo();
        getkey();
        if(isEaten())
        {
            nEkor += 2;
            random_makanan();
            tulis_skor();
        }
        Sleep(100 - (nEkor * 3));
    }
    system("cls");
    gotoxy(32,12); cout << "Skor "<< user<<":" << (nEkor - 1) * 10;
    gotoxy(25,13);
    exit:
    system("pause");
}


Nih contoh codingan yang full nya, bisa kalian copy.


#include <iostream>
#include <conio.h>
#include <windows.h>

#define batas_atas 2
#define batas_bawah 23
#define batas_samping 2
#define batas_samping_k 78

#define caption "\t\t Inkor by Qowi17 \t\t\n\n"

using namespace std;

int iX(5), iY(5), x1 (4), y1 (4), xHapus(3), yHapus(3), veloX(1), veloY(0),ekorsX[200], ekorsY[200], nEkor = 2, start = 0, makX, makY ,level = 1 ,username;

void gotoxy(int x, int y)
{
    COORD pos = {x, y};
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}

void naik()
{
    veloY = -1;
    veloX = 0;
}

void turun()
{
    veloY = 1;
    veloX = 0;
}

void samping()
{
    veloX = -1;
    veloY = 0;
}

void samping_k()
{
    veloX = 1;
    veloY = 0;
}

void hapus()
{
    gotoxy(xHapus, yHapus);
    cout << " ";
}

void ekor()
{
    for(int j = 0; j <= 2; j++)
    {
        gotoxy(ekorsX[j], ekorsY[j]);
        cout << char(02);
    }
}

void tampil()
{
    gotoxy(iX, iY);
    cout << char(01);

    gotoxy(makX, makY);
    cout << char(06);
}

void ganti_posisi()
{
    xHapus = ekorsX[nEkor - 1];
    yHapus = ekorsY[nEkor - 1];
    for(int j = nEkor - 1; j >= 1; j--)
    {
        ekorsX[j] = ekorsX[j-1];
        ekorsY[j] = ekorsY[j-1];
    }
    ekorsX[0] = iX;
    ekorsY[0] = iY;
}

void velo()
{
    ganti_posisi();
    hapus();
    iX += veloX;
    iY += veloY;
    tampil();
    ekor();
}

bool isDestroy()
{
    if(iX == batas_samping_k) { iX = 3; gotoxy(78, iY); cout << " "; }
    if(iX == batas_samping)  { iX = 77; gotoxy(2, iY); cout << " "; }
    if(iY == batas_atas) { iY = 22; gotoxy(iX, 2); cout << " "; }
    if(iY == batas_bawah) { iY = 3; gotoxy(iX, 23); cout << " "; }
    for(int j = 0; j <= nEkor - 1; j++)
    if(ekorsX[j] == iX && ekorsY[j] == iY) return true;
    return false;
}

char getkey()
{
    for(int i = 8; i <= 222; i++)
    {
        if(GetAsyncKeyState(i) == -32767)
        {
            switch(i)
            {
            case 38 : if(veloY != 1)naik();
                        break;
            case 40 : if(veloY != -1)turun();
                        break;
            case 37 : if(veloX != 1)samping();
                        break;
            case 39 : if(veloX != -1)samping_k();
                        break;
            }
        }
    }
}

void random_makanan()
{
    makX = rand()%(batas_samping_k - 1);
    if(makX < 4) makX += 3 + (4-makX);
    makY = rand()%(batas_bawah - 1);
    if(makY < 4) makY += 3 + (4-makY);
    gotoxy(makX, makY);
    cout << char(3);
    if (nEkor%10==0)
 level=level+1;
}

bool isEaten()
{
    if(iX == makX && iY == makY)
        return true;
    else
        return false;
}

void cBorder()
{
    for(int i = 1; i <= 78; i++)
    {
        for(int j = 1; j <= 24; j += 23)
        {
            if(j > 10 || i >= 2)
            {
                gotoxy(i, j);cout << "-";
            }
        }
    }
    for(int i = 1; i <= 24; i++)
    {
        for(int k = 1; k <= 80; k += 78)
        {
            gotoxy(k, i); cout << "|";
        }
    }
}

void skor()
{
    gotoxy(3,1); cout << "Skor : ";
    gotoxy(18,1); cout << "Panjang : ";
    gotoxy(34,1); cout << "level : ";
    gotoxy(47,1); cout << "twitter : @QBusthomi";
    gotoxy(3,2); cout << "Nama : ";
    gotoxy(47,2); cout << "Instagram : mas_thom_17";

}

void tulis_skor()
{
    gotoxy(11,1); cout << (nEkor) * 10;
    gotoxy(28,1); cout << nEkor;
    gotoxy(41,1); cout << level;
}

void inisialisasi()
{
    cBorder();
    random_makanan();
    skor();
    tulis_skor();
}

void tulisTengah(string s, int y)
{
    gotoxy(40 - (s.length() / 2), y);
    cout << s;
}

int main()
{
    system("color f0");
    int pilih;
 char user[20];

    pilih:
    system("cls");
    cout<<(caption);
    tulisTengah("Selamat Datang",5);
    tulisTengah("================================", 6);
    tulisTengah("GAME Inkor by Qowi17", 7);
    tulisTengah("================================", 8);
    tulisTengah("Follow Me :",10);
    tulisTengah("Twitter : @QBusthomi | Fb : thomas gunawan part I | WA : 085856403947", 11);
    tulisTengah("Blog : akungamenow.blogspot.com | IG : mas_thom_17 | Channel YT : Qowi17 Official", 12);
    tulisTengah("pilih kode nya : 1. untuk bermain , 2. untuk keluar", 14);
    tulisTengah("1.main",15);
    tulisTengah("2.keluar",16);
    tulisTengah("anda pilih yang mana : ",18);
    cin>>pilih;

    switch (pilih)
    {
        case 1:
                goto main;
        case 2:
                goto exit;
        default : tulisTengah("maaf tidak tersedia",25);
                goto pilih;
    }

    main:
    system("cls");
    tulisTengah("Selamat Datang",5);
    tulisTengah("================================", 6);
    tulisTengah("GAME Inkor by Qowi17", 7);
    tulisTengah("================================", 8);
    tulisTengah("Follow Me :",9);
    tulisTengah("Twitter : @QBusthomi | Fb : thomas gunawan part I | WA : 085856403947", 10);
    tulisTengah("Blog : akungamenow.blogspot.com | IG : mas_thom_17 | Channel YT : Qowi17 Official", 11);

    tulisTengah("Masukkan Nama : ",13);
    cin >> user;
    system("cls");
    gotoxy(11,2); cout << user;
    inisialisasi();

    while(!(isDestroy()))
    {
        velo();
        getkey();
        if(isEaten())
        {
            nEkor += 2;
            random_makanan();
            tulis_skor();
        }
        Sleep(100 - (nEkor * 3));
    }
    system("cls");
    gotoxy(32,12); cout << "Skor "<< user<<":" << (nEkor - 1) * 10;
    gotoxy(25,13);
    exit:
    system("pause");
}

Bonus Code : Bouncing Ball
  1. #include <windows.h>
  2. #define Show(Window) RedrawWindow(Window,0,0,0);ShowWindow(Window,SW_SHOW);
  3. #define AppName "BouncingBall1"
  4. #define Caption "Bouncing Ball ..."
  5. char BCX_STR [1024*1024];
  6. static int BCX_GetDiaUnit;
  7. static int BCX_cxBaseUnit;
  8. static int BCX_cyBaseUnit;
  9. static int BCX_ScaleX;
  10. static int BCX_ScaleY;
  11. static HANDLE Form1;
  12. double MIN (double,double);
  13. double MAX (double,double);
  14. int WINAPI WinMain (HINSTANCE, HINSTANCE, LPSTR, int);
  15. void FormLoad (HANDLE);
  16. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
  17. double MAX (double a, double b)
  18. {
  19. if (a > b)
  20. {
  21. return a;
  22. }
  23. return b;
  24. }
  25. double MIN (double a, double b)
  26. {
  27. if (a < b)
  28. {
  29. return a;
  30. }
  31. return b;
  32. }
  33. // standard main for Windows GUI
  34. int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPSTR CmdLine, int CmdShow)
  35. {
  36. static WNDCLASS Wc;
  37. memset(&Wc,0,sizeof(Wc));
  38. static MSG Msg;
  39. memset(&Msg,0,sizeof(Msg));
  40. Wc.style=CS_HREDRAW | CS_VREDRAW;
  41. Wc.lpfnWndProc=WndProc;
  42. Wc.cbClsExtra=0;
  43. Wc.cbWndExtra=0;
  44. Wc.hInstance=hInst;
  45. Wc.hIcon=LoadIcon(NULL,IDI_WINLOGO);
  46. Wc.hCursor=LoadCursor(NULL,IDC_ARROW);
  47. Wc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
  48. Wc.lpszMenuName=NULL;
  49. Wc.lpszClassName=AppName;
  50. RegisterClass(&Wc);
  51. FormLoad(hInst);
  52. // 50ms here, lower value gives higher speed
  53. SetTimer((HWND)Form1,1,50,NULL);
  54. // ye olde event message loop
  55. while(GetMessage(&Msg,NULL,0,0))
  56. {
  57. if (!IsWindow((HWND)Form1)||!IsDialogMessage((HWND)Form1,&Msg))
  58. {
  59. TranslateMessage(&Msg);
  60. DispatchMessage(&Msg);
  61. }
  62. }
  63. return Msg.wParam;
  64. }
  65. // create the form and show it (somewhat older style)
  66. void FormLoad (HANDLE hInst)
  67. {
  68. // get the scale factors
  69. BCX_GetDiaUnit = GetDialogBaseUnits();
  70. BCX_cxBaseUnit = LOWORD(BCX_GetDiaUnit);
  71. BCX_cyBaseUnit = HIWORD(BCX_GetDiaUnit);
  72. BCX_ScaleX = BCX_cxBaseUnit/4;
  73. BCX_ScaleY = BCX_cyBaseUnit/8;
  74. // now the form
  75. Form1=CreateWindow(AppName,Caption,
  76. DS_MODALFRAME|WS_POPUP|WS_CAPTION|WS_SYSMENU,
  77. 10*BCX_ScaleX,20*BCX_ScaleY,250*BCX_ScaleX,175*BCX_ScaleY,NULL,
  78. (HMENU)NULL,(HINSTANCE)hInst,NULL);
  79. Show((HWND)Form1);
  80. }
  81. // event message handler
  82. LRESULT CALLBACK WndProc (HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
  83. {
  84. static HANDLE hBitmap;
  85. static HBRUSH hBrush;
  86. static HDC hdc;
  87. static HDC hdcMem;
  88. static int cxClient;
  89. static int cyClient;
  90. static int xCenter;
  91. static int yCenter;
  92. static int cxTotal;
  93. static int cyTotal;
  94. static int cxRadius;
  95. static int cyRadius;
  96. static int cxMove;
  97. static int cyMove;
  98. static int xPixel;
  99. static int yPixel;
  100. static int nScale;
  101. while(1)
  102. {
  103. if (Msg == WM_CREATE)
  104. {
  105. hdc = GetDC(hWnd);
  106. xPixel = GetDeviceCaps(hdc,ASPECTX);
  107. yPixel = GetDeviceCaps(hdc,ASPECTY);
  108. ReleaseDC(hWnd,hdc);
  109. return 0;
  110. break;
  111. }
  112. // draw the ball
  113. if (Msg == WM_SIZE)
  114. {
  115. xCenter = (cxClient=LOWORD(lParam))/2;
  116. yCenter = (cyClient=HIWORD(lParam))/2;
  117. nScale = (int)MIN(cxClient*xPixel,cyClient*yPixel)/16;
  118. cxRadius = nScale/xPixel;
  119. cyRadius = nScale/yPixel;
  120. cxMove = (int)MAX(1,cxRadius/4);
  121. cyMove = (int)MAX(1,cyRadius/4);
  122. cxTotal = 2*(cxRadius+cxMove);
  123. cyTotal = 2*(cyRadius+cyMove);
  124. if (hBitmap)
  125. {
  126. DeleteObject(hBitmap);
  127. }
  128. hdc = GetDC(hWnd);
  129. hdcMem = CreateCompatibleDC(hdc);
  130. hBitmap = CreateCompatibleBitmap(hdc,cxTotal,cyTotal);
  131. ReleaseDC(hWnd,hdc);
  132. SelectObject(hdcMem,hBitmap);
  133. Rectangle(hdcMem,-1,-1,cxTotal+1,cyTotal+1);
  134. hBrush = CreateHatchBrush(HS_DIAGCROSS,0);
  135. SelectObject(hdcMem,hBrush);
  136. SetBkColor(hdcMem,RGB(0,127,255));
  137. Ellipse(hdcMem,cxMove,cyMove,cxTotal-cxMove,cyTotal-cyMove);
  138. DeleteDC(hdcMem);
  139. DeleteObject(hBrush);
  140. return 0;
  141. break;
  142. }
  143. // move the ball
  144. if (Msg == WM_TIMER)
  145. {
  146. if (!hBitmap)
  147. {
  148. break;
  149. }
  150. hdc = GetDC(hWnd);
  151. hdcMem = CreateCompatibleDC(hdc);
  152. SelectObject(hdcMem,hBitmap);
  153. BitBlt(hdc,xCenter-cxTotal/2,yCenter-cyTotal/2,cxTotal,cyTotal,hdcMem,0,0,SRCCOPY);
  154. ReleaseDC(hWnd,hdc);
  155. DeleteDC(hdcMem);
  156. xCenter += cxMove;
  157. yCenter += cyMove;
  158. if (xCenter+cxRadius>=cxClient||xCenter-cxRadius<=0)
  159. {
  160. cxMove = -cxMove;
  161. }
  162. if (yCenter+cyRadius >= cyClient || yCenter-cyRadius <= 0)
  163. {
  164. cyMove = -cyMove;
  165. }
  166. return 0;
  167. break;
  168. }
  169. // clean up and exit program
  170. if (Msg == WM_DESTROY)
  171. {
  172. if (hBitmap)
  173. {
  174. DeleteObject(hBitmap);
  175. }
  176. KillTimer((HWND)Form1,1);
  177. PostQuitMessage(0);
  178. return 0;
  179. }
  180. break;
  181. }
  182. return DefWindowProc(hWnd, Msg, wParam, lParam);
  183. }
Share:
spacer

No comments:

Post a Comment