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.
Nih contoh codingan yang full nya, bisa kalian copy.
Bonus Code : Bouncing Ball
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
#include <windows.h>
#define Show(Window) RedrawWindow(Window,0,0,0);ShowWindow(Window,SW_SHOW);
#define AppName "BouncingBall1"
#define Caption "Bouncing Ball ..."
char BCX_STR [1024*1024];
static int BCX_GetDiaUnit;
static int BCX_cxBaseUnit;
static int BCX_cyBaseUnit;
static int BCX_ScaleX;
static int BCX_ScaleY;
static HANDLE Form1;
double MIN (double,double);
double MAX (double,double);
int WINAPI WinMain (HINSTANCE, HINSTANCE, LPSTR, int);
void FormLoad (HANDLE);
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
double MAX (double a, double b)
{
if (a > b)
{
return a;
}
return b;
}
double MIN (double a, double b)
{
if (a < b)
{
return a;
}
return b;
}
// standard main for Windows GUI
int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPSTR CmdLine, int CmdShow)
{
static WNDCLASS Wc;
memset(&Wc,0,sizeof(Wc));
static MSG Msg;
memset(&Msg,0,sizeof(Msg));
Wc.style=CS_HREDRAW | CS_VREDRAW;
Wc.lpfnWndProc=WndProc;
Wc.cbClsExtra=0;
Wc.cbWndExtra=0;
Wc.hInstance=hInst;
Wc.hIcon=LoadIcon(NULL,IDI_WINLOGO);
Wc.hCursor=LoadCursor(NULL,IDC_ARROW);
Wc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
Wc.lpszMenuName=NULL;
Wc.lpszClassName=AppName;
RegisterClass(&Wc);
FormLoad(hInst);
// 50ms here, lower value gives higher speed
SetTimer((HWND)Form1,1,50,NULL);
// ye olde event message loop
while(GetMessage(&Msg,NULL,0,0))
{
if (!IsWindow((HWND)Form1)||!IsDialogMessage((HWND)Form1,&Msg))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
}
return Msg.wParam;
}
// create the form and show it (somewhat older style)
void FormLoad (HANDLE hInst)
{
// get the scale factors
BCX_GetDiaUnit = GetDialogBaseUnits();
BCX_cxBaseUnit = LOWORD(BCX_GetDiaUnit);
BCX_cyBaseUnit = HIWORD(BCX_GetDiaUnit);
BCX_ScaleX = BCX_cxBaseUnit/4;
BCX_ScaleY = BCX_cyBaseUnit/8;
// now the form
Form1=CreateWindow(AppName,Caption,
DS_MODALFRAME|WS_POPUP|WS_CAPTION|WS_SYSMENU,
10*BCX_ScaleX,20*BCX_ScaleY,250*BCX_ScaleX,175*BCX_ScaleY,NULL,
(HMENU)NULL,(HINSTANCE)hInst,NULL);
Show((HWND)Form1);
}
// event message handler
LRESULT CALLBACK WndProc (HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
static HANDLE hBitmap;
static HBRUSH hBrush;
static HDC hdc;
static HDC hdcMem;
static int cxClient;
static int cyClient;
static int xCenter;
static int yCenter;
static int cxTotal;
static int cyTotal;
static int cxRadius;
static int cyRadius;
static int cxMove;
static int cyMove;
static int xPixel;
static int yPixel;
static int nScale;
while(1)
{
if (Msg == WM_CREATE)
{
hdc = GetDC(hWnd);
xPixel = GetDeviceCaps(hdc,ASPECTX);
yPixel = GetDeviceCaps(hdc,ASPECTY);
ReleaseDC(hWnd,hdc);
return 0;
break;
}
// draw the ball
if (Msg == WM_SIZE)
{
xCenter = (cxClient=LOWORD(lParam))/2;
yCenter = (cyClient=HIWORD(lParam))/2;
nScale = (int)MIN(cxClient*xPixel,cyClient*yPixel)/16;
cxRadius = nScale/xPixel;
cyRadius = nScale/yPixel;
cxMove = (int)MAX(1,cxRadius/4);
cyMove = (int)MAX(1,cyRadius/4);
cxTotal = 2*(cxRadius+cxMove);
cyTotal = 2*(cyRadius+cyMove);
if (hBitmap)
{
DeleteObject(hBitmap);
}
hdc = GetDC(hWnd);
hdcMem = CreateCompatibleDC(hdc);
hBitmap = CreateCompatibleBitmap(hdc,cxTotal,cyTotal);
ReleaseDC(hWnd,hdc);
SelectObject(hdcMem,hBitmap);
Rectangle(hdcMem,-1,-1,cxTotal+1,cyTotal+1);
hBrush = CreateHatchBrush(HS_DIAGCROSS,0);
SelectObject(hdcMem,hBrush);
SetBkColor(hdcMem,RGB(0,127,255));
Ellipse(hdcMem,cxMove,cyMove,cxTotal-cxMove,cyTotal-cyMove);
DeleteDC(hdcMem);
DeleteObject(hBrush);
return 0;
break;
}
// move the ball
if (Msg == WM_TIMER)
{
if (!hBitmap)
{
break;
}
hdc = GetDC(hWnd);
hdcMem = CreateCompatibleDC(hdc);
SelectObject(hdcMem,hBitmap);
BitBlt(hdc,xCenter-cxTotal/2,yCenter-cyTotal/2,cxTotal,cyTotal,hdcMem,0,0,SRCCOPY);
ReleaseDC(hWnd,hdc);
DeleteDC(hdcMem);
xCenter += cxMove;
yCenter += cyMove;
if (xCenter+cxRadius>=cxClient||xCenter-cxRadius<=0)
{
cxMove = -cxMove;
}
if (yCenter+cyRadius >= cyClient || yCenter-cyRadius <= 0)
{
cyMove = -cyMove;
}
return 0;
break;
}
// clean up and exit program
if (Msg == WM_DESTROY)
{
if (hBitmap)
{
DeleteObject(hBitmap);
}
KillTimer((HWND)Form1,1);
PostQuitMessage(0);
return 0;
}
break;
}
return DefWindowProc(hWnd, Msg, wParam, lParam);
}
No comments:
Post a Comment