Live chat
Archive for April 2019
- helo assalamu'alikum teman teman :)
- #kambali lagi dengan saya Mr.CodenZa akan memberikan tutorial bikin game tebak tentara :) #Mr.CodenZa
Pada kesempatan ini, saya akan membuat tutorial tentang cara membuat game tebak tentara. Game ini berbasis console jadi, tidak ada graphics yang cantik seperti yang Anda bayangkan. Namun, pada game ini mungkin bisa melatih atau meningkatkan pola pikir algoritma pemrograman Anda. Tutorial ini lumayan panjang jadi, saya akan membuatnya secara bertahap. Untuk part 1, saya akan membuatnya sampai sebatas tampilannya saja. Dan mudah-mudahan tutorial ini bisa cepat selesai. Pada tutorial ini, saya menggunakan bahasa Pascal. Mungkin bagi Anda yang belum bisa Pascal bisa mencobanya di bahasa pemrograman lainnya yang bisa Anda kuasai.
Dalam pembuatan game ini beberapa teknik yang akan kita pakai ialah, If Else, Prosedur(disebut method dalam Java), Perulangan(Looping), Array 1 dan 2 dimensi. Untuk mempersingkat waktu langsung saja ikuti tutorial berikut secara bertahap dan pelan – pelan.
- Pertama, buat coding seperti berikut. Pada coding ini, kita hanya
mendeklarasikan variable dan array yang akan kita pakai nantinya dalam
pembuatan game ini.
12345678910111213141516171819{Deklarasi Variable dan Array}varnama_user :string;MyArmy_X :array[1..4]ofinteger;MyArmy_Y :array[1..4]ofinteger;ComArmy_X :array[1..4]ofinteger;ComArmy_Y :array[1..4]ofinteger;Kondisi_MyArmy_XY :array[1..4,1..4]ofstring;Kondisi_ComArmy_XY :array[1..4,1..4]ofstring;user, com :string;tebak_x_user :integer;tebak_y_user :integer;tebak_x_com :integer;tebak_y_com :integer;benar :boolean;again :boolean;skor_user :integer;skor_com :integer;x, y, z :integer; - Setelah itu, buatlah prosedur berikut untuk menginisialisasikan
nilai Array yang akan kita pakai nanti untuk mengidentifikasi apakah
posisi tentara sudah dibuka atau belum.
1234567891011121314{Prosedur Inisialisasi Kondisi MyArmy XY dan Kondisi ComArmy XY}procedureInitKondisiMyArmy;vara, b, c :integer;beginfora :=1to4dobeginforb :=1to4dobeginKondisi_MyArmy_XY[a, b] :='tutup';Kondisi_ComArmy_XY[a, b] :='tutup';end;end;end; - Dan ketiga, tambahkan coding berikut ke dalam Main. Dimana, coding ini akan menampilkan interface yang akan menanyakan Nama user.
12(*Tanya nama si user*)write('Masukkan Nama Anda : '); readln(nama_user); - Keempat, tambahkan coding berikut setelah coding diatas. Dimana,
coding berikut akan menanyakan si user dimana posisi tentara si user
akan diletakkan.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115(*Buat inputan untuk menanyakan mau diposisikan dimana tentara si user*)gotoxy(1, wherey+1);write('Posisikan tentara kamu pada Koordinat berikut');(*Posisi semua tentara tidak boleh ada yang sama*)(*Tentara 1*)again :=false;repeatbegingotoxy(1, wherey+1);write('Tentara 1, Koordinat X [1..4] : '); readln(MyArmy_X[1]);write('Tentara 1, Koordinat Y [1..4] : '); readln(MyArmy_Y[1]);again :=true;(*Posisi tentara harus berada diantara 1 sampai 4*)if(MyArmy_X[1] <1)or(MyArmy_Y[1] <1)or(MyArmy_X[1] >4)or(MyArmy_Y[1] >4)thenbeginwriteln('Koordinat harus diantara 1 - 4 !!!');again :=false;end;enduntilagain =true;(*Tentara 2*)again :=false;repeatbegingotoxy(wherex, wherey+1);write('Tentara 2, Koordinat X [1..4] : '); readln(MyArmy_X[2]);write('Tentara 2, Koordinat Y [1..4] : '); readln(MyArmy_Y[2]);again :=true;if(MyArmy_X[2] <1)or(MyArmy_Y[2] <1)or(MyArmy_X[2] >4)or(MyArmy_Y[2] >4)thenbeginwriteln('Koordinat harus diantara 1 - 4 !!!');again :=false;end;(*Cek apakah posisi tentara 2 sudah ditempati oleh tentara 1*)if(MyArmy_X[2] = MyArmy_X[1])and(MyArmy_Y[2] = MyArmy_Y[1])thenbeginwriteln('Cari tempat lain...');writeln('Posisi telah ditempati oleh tentara sebelumnya !!!');again :=false;end;enduntilagain =true;(*Tentara 3*)again :=false;repeatbegingotoxy(wherex, wherey+1);write('Tentara 3, Koordinat X [1..4] : '); readln(MyArmy_X[3]);write('Tentara 3, Koordinat Y [1..4] : '); readln(MyArmy_Y[3]);again :=true;if(MyArmy_X[3] <1)or(MyArmy_Y[3] <1)or(MyArmy_X[3] >4)or(MyArmy_Y[3] >4)thenbeginwriteln('Koordinat harus diantara 1 - 4 !!!');again :=false;end;(*Cek apakah posisi tentara 3 sudah ditempati oleh tentara 1 atau 2*)if(MyArmy_X[3] = MyArmy_X[1])and(MyArmy_Y[3] = MyArmy_Y[1])thenbeginwriteln('Cari tempat lain...');writeln('Posisi telah ditempati oleh tentara sebelumnya !!!');again :=false;endelseif(MyArmy_X[3] = MyArmy_X[2])and(MyArmy_Y[3] = MyArmy_Y[2])thenbeginwriteln('Cari tempat lain...');writeln('Posisi telah ditempati oleh tentara sebelumnya !!!');again :=false;end;enduntilagain =true;(*Tentara 4*)again :=false;repeatbegingotoxy(wherex, wherey+1);write('Tentara 4, Koordinat X[1..4] : '); readln(MyArmy_X[4]);write('Tentara 4, Koordinat Y[1..4] : '); readln(MyArmy_Y[4]);again :=true;if(MyArmy_X[4] <1)or(MyArmy_Y[4] <1)or(MyArmy_X[4] >4)or(MyArmy_Y[4] >4)thenbeginwriteln('Koodinat harus diantara 1 - 4 !!!');again :=false;end;(*Cek apakah posisi tentara sudah ditempati oleh tentara 1, 2 atau 3*)if(MyArmy_X[4] = MyArmy_X[1])and(MyArmy_Y[4] = MyArmy_Y[1])thenbeginwriteln('Cari tempat lain...');writeln('Posisi telah ditempati oleh tentara sebelumnya !!!');again :=false;endelseif(MyArmy_X[4] = MyArmy_X[2])and(MyArmy_Y[4] = MyArmy_Y[2])thenbeginwriteln('Cari tempat lain...');writeln('Posisi telah ditempati oleh tentara sebelumnya !!!');again :=false;endelseif(MyArmy_X[4] = MyArmy_X[3])and(MyArmy_Y[4] = MyArmy_Y[3])thenbeginwriteln('Cari tempat lain...');writeln('Posisi telah ditempati oleh tentara sebelumnya !!!');again :=false;end;enduntilagain =true; - Kelima, coding berikut akan mencetak layout Area User dan layout Area Musuh.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192(*Cetak layout area user dan area musuh*)while((user <>'win')and(com <>'win'))dobeginclrscr;writeln('Tebak posisi koordinat tentara musuh yang bersembunyi');(*Cetak Header Layout area user dan area musuh*)gotoxy(wherex, wherey+1);write('==Area User==');gotoxy(20, wherey);write('=Area Musuh=');(*Cetak Angka Koordinat X di Area User*)gotoxy(3, wherey+1);forx :=1to4dobeginwrite(x);gotoxy(wherex+2, wherey);end;(*Cetak Angka Koordinat X di Area Musuh*)gotoxy(22, wherey);forx :=1to4dobeginwrite(x);gotoxy(wherex+2, wherey);end;(*Cetak Angka Koordinat Y di Area User*)gotoxy(1, wherey+2);forx :=1to4dobeginwrite(x);gotoxy(1, wherey+2);end;(*Cetak Angka Koordinat Y di Area Musuh*)gotoxy(20, wherey-8);forx :=1to4dobeginwrite(x);gotoxy(20, wherey+2);end;(*Cetak Kotak Persembunyian di Area User*)gotoxy(3, wherey-8);forx :=1to4dobeginfory :=1to4dobegin(*Cek status tiap kotak persembunyian untuk Area User*)if(Kondisi_MyArmy_XY[y, x] ='tutup')thenbeginwrite(chr(219));endelseif(Kondisi_MyArmy_XY[y, x] ='buka_salah')thenbeginwrite('X');endelseif(Kondisi_MyArmy_XY[y, x] ='buka_benar')thenbeginwrite(chr(258));end;gotoxy(wherex+2, wherey);end;gotoxy(3, wherey+2);end;(*Cetak Kotak Persembunyian di Area Musuh*)gotoxy(22, wherey-8);forx :=1to4dobeginfory :=1to4dobegin(*Cek status tiap kotak persembunyian untuk Area Musuh*)if(Kondisi_ComArmy_XY[y, x] ='tutup')thenbeginwrite(chr(219));endelseif(Kondisi_ComArmy_XY[y, x] ='buka_salah')thenbeginwrite('X');endelseif(Kondisi_ComArmy_XY[y, x] ='buka_benar')thenbeginwrite(chr(258));end;gotoxy(wherex+2, wherey);end;gotoxy(22, wherey+2);end;end; - Dan coding berikut, akan menampilkan skor si user dan si computer.
Letakkan setelah coding diatas dan di dalam blok statement looping
repeat - until.
1234567(*Cetak Skor si User*)gotoxy(1, wherey+1);write('Skor User : ', skor_user);(*Cetak Skor si Com*)gotoxy(20, wherey);write('Skor Musuh : ', skor_com); - Dan yang terakhir, coding ini akan menanyakan si user untuk menebak kira - kira dimana posisi tentara musuh bersembunyi.
12345(*Masukan untuk menebak Koordinat dimana posisi tentara musuh berada*)gotoxy(1, wherey+2);write('Masukkan Koordinat X untuk Musuh [1..4] : '); readln(tebak_x_user);gotoxy(1, wherey+1);write('Masukkan Koordinat Y untuk Musuh [1..4] : '); readln(tebak_y_user);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
| (* Author Yudi Setiawan*)program TebakTentara;uses crt;{Deklarasi Variable dan Array}var nama_user :string; MyArmy_X :array[1..4] of integer; MyArmy_Y :array[1..4] of integer; ComArmy_X :array[1..4] of integer; ComArmy_Y :array[1..4] of integer; Kondisi_MyArmy_XY :array[1..4, 1..4] of string; Kondisi_ComArmy_XY :array[1..4, 1..4] of string; user, com :string; tebak_x_user :integer; tebak_y_user :integer; tebak_x_com :integer; tebak_y_com :integer; benar :boolean; again :boolean; skor_user :integer; skor_com :integer; x, y, z :integer; {Prosedur Inisialisasi Kondisi MyArmy XY dan Kondisi ComArmy XY}procedure InitKondisiMyArmyComArmy;var a, b, c :integer;begin for a := 1 to 4 do begin for b := 1 to 4 do begin Kondisi_MyArmy_XY[a, b] := 'tutup'; Kondisi_ComArmy_XY[a, b] := 'tutup'; end; end;end;{Main}begin (*Panggil prosedur*) InitKondisiMyArmyComArmy; (*Clear Screen*) clrscr; (*Set status si user dan com masih natural*) user := 'natural'; com := 'natural'; (*Set Skor user dan Skor com masih nol*) skor_user := 0; skor_com := 0; (*Tanya nama si user*) write('Masukkan Nama Anda : '); readln(nama_user); (*Buat inputan untuk menanyakan mau diposisikan dimana tentara si user*) gotoxy(1, wherey+1); write('Posisikan tentara kamu pada Koordinat berikut'); (*Posisi semua tentara tidak boleh ada yang sama*) (*Tentara 1*) again := false; repeat begin gotoxy(1, wherey+1); write('Tentara 1, Koordinat X [1..4] : '); readln(MyArmy_X[1]); write('Tentara 1, Koordinat Y [1..4] : '); readln(MyArmy_Y[1]); again := true; (*Posisi tentara harus berada diantara 1 sampai 4*) if(MyArmy_X[1] < 1) or (MyArmy_Y[1] < 1) or (MyArmy_X[1] > 4) or (MyArmy_Y[1] > 4) then begin writeln('Koordinat harus diantara 1 - 4 !!!'); again := false; end; end until again = true; (*Tentara 2*) again := false; repeat begin gotoxy(wherex, wherey+1); write('Tentara 2, Koordinat X [1..4] : '); readln(MyArmy_X[2]); write('Tentara 2, Koordinat Y [1..4] : '); readln(MyArmy_Y[2]); again := true; if(MyArmy_X[2] < 1) or (MyArmy_Y[2] < 1) or (MyArmy_X[2] > 4) or (MyArmy_Y[2] > 4) then begin writeln('Koordinat harus diantara 1 - 4 !!!'); again := false; end; (*Cek apakah posisi tentara 2 sudah ditempati oleh tentara 1*) if(MyArmy_X[2] = MyArmy_X[1]) and (MyArmy_Y[2] = MyArmy_Y[1]) then begin writeln('Cari tempat lain...'); writeln('Posisi telah ditempati oleh tentara sebelumnya !!!'); again := false; end; end until again = true; (*Tentara 3*) again := false; repeat begin gotoxy(wherex, wherey+1); write('Tentara 3, Koordinat X [1..4] : '); readln(MyArmy_X[3]); write('Tentara 3, Koordinat Y [1..4] : '); readln(MyArmy_Y[3]); again := true; if(MyArmy_X[3] < 1) or (MyArmy_Y[3] < 1) or (MyArmy_X[3] > 4) or (MyArmy_Y[3] > 4) then begin writeln('Koordinat harus diantara 1 - 4 !!!'); again := false; end; (*Cek apakah posisi tentara 3 sudah ditempati oleh tentara 1 atau 2*) if(MyArmy_X[3] = MyArmy_X[1]) and (MyArmy_Y[3] = MyArmy_Y[1]) then begin writeln('Cari tempat lain...'); writeln('Posisi telah ditempati oleh tentara sebelumnya !!!'); again := false; end else if(MyArmy_X[3] = MyArmy_X[2]) and (MyArmy_Y[3] = MyArmy_Y[2]) then begin writeln('Cari tempat lain...'); writeln('Posisi telah ditempati oleh tentara sebelumnya !!!'); again := false; end; end until again = true; (*Tentara 4*) again := false; repeat begin gotoxy(wherex, wherey+1); write('Tentara 4, Koordinat X[1..4] : '); readln(MyArmy_X[4]); write('Tentara 4, Koordinat Y[1..4] : '); readln(MyArmy_Y[4]); again := true; if(MyArmy_X[4] < 1) or (MyArmy_Y[4] < 1) or (MyArmy_X[4] > 4) or (MyArmy_Y[4] > 4) then begin writeln('Koodinat harus diantara 1 - 4 !!!'); again := false; end; (*Cek apakah posisi tentara sudah ditempati oleh tentara 1, 2 atau 3*) if(MyArmy_X[4] = MyArmy_X[1]) and (MyArmy_Y[4] = MyArmy_Y[1]) then begin writeln('Cari tempat lain...'); writeln('Posisi telah ditempati oleh tentara sebelumnya !!!'); again := false; end else if(MyArmy_X[4] = MyArmy_X[2]) and (MyArmy_Y[4] = MyArmy_Y[2]) then begin writeln('Cari tempat lain...'); writeln('Posisi telah ditempati oleh tentara sebelumnya !!!'); again := false; end else if(MyArmy_X[4] = MyArmy_X[3]) and (MyArmy_Y[4] = MyArmy_Y[3]) then begin writeln('Cari tempat lain...'); writeln('Posisi telah ditempati oleh tentara sebelumnya !!!'); again := false; end; end until again = true; (*Cetak layout area user dan area musuh*) while((user <> 'win') and (com <> 'win')) do begin clrscr; writeln('Tebak posisi koordinat tentara musuh yang bersembunyi'); (*Cetak Header Layout area user dan area musuh*) gotoxy(wherex, wherey+1); write('==Area User=='); gotoxy(20, wherey); write('=Area Musuh='); (*Cetak Angka Koordinat X di Area User*) gotoxy(3, wherey+1); for x := 1 to 4 do begin write(x); gotoxy(wherex+2, wherey); end; (*Cetak Angka Koordinat X di Area Musuh*) gotoxy(22, wherey); for x := 1 to 4 do begin write(x); gotoxy(wherex+2, wherey); end; (*Cetak Angka Koordinat Y di Area User*) gotoxy(1, wherey+2); for x := 1 to 4 do begin write(x); gotoxy(1, wherey+2); end; (*Cetak Angka Koordinat Y di Area Musuh*) gotoxy(20, wherey-8); for x := 1 to 4 do begin write(x); gotoxy(20, wherey+2); end; (*Cetak Kotak Persembunyian di Area User*) gotoxy(3, wherey-8); for x := 1 to 4 do begin for y := 1 to 4 do begin (*Cek status tiap kotak persembunyian untuk Area User*) if(Kondisi_MyArmy_XY[y, x] = 'tutup') then begin write(chr(219)); end else if(Kondisi_MyArmy_XY[y, x] = 'buka_salah') then begin write('X'); end else if(Kondisi_MyArmy_XY[y, x] = 'buka_benar') then begin write(chr(258)); end; gotoxy(wherex+2, wherey); end; gotoxy(3, wherey+2); end; (*Cetak Kotak Persembunyian di Area Musuh*) gotoxy(22, wherey-8); for x := 1 to 4 do begin for y := 1 to 4 do begin (*Cek status tiap kotak persembunyian untuk Area Musuh*) if(Kondisi_ComArmy_XY[y, x] = 'tutup') then begin write(chr(219)); end else if(Kondisi_ComArmy_XY[y, x] = 'buka_salah') then begin write('X'); end else if(Kondisi_ComArmy_XY[y, x] = 'buka_benar') then begin write(chr(258)); end; gotoxy(wherex+2, wherey); end; gotoxy(22, wherey+2); end; (*Cetak Skor si User*) gotoxy(1, wherey+1); write('Skor User : ', skor_user); (*Cetak Skor si Com*) gotoxy(20, wherey); write('Skor Musuh : ', skor_com); (*Masukan untuk menebak Koordinat dimana posisi tentara musuh berada*) gotoxy(1, wherey+2); write('Masukkan Koordinat X untuk Musuh [1..4] : '); readln(tebak_x_user); gotoxy(1, wherey+1); write('Masukkan Koordinat Y untuk Musuh [1..4] : '); readln(tebak_y_user); end; readln;end. |
Berikut hasil dari tahap pertama ini. Untuk latar belakangnya bisa berwarna, itu saya menggunakan textBackground. Bisa Anda cari di google. Dan mungkin untuk tahap pertama ini Anda bisa bereksperimen sendiri untuk mewarnai latar belakangnya ataupun warna teksnya.
![]() |
| Mr.Codenza |
