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
0 komentar:
Posting Komentar