Skip to main content

Membuat Game Sederhana Dengan JavaScript

Assalamualaikum Wr Wb

Javascript adalah Bahasa pemrograman yang di pakai di web, javascript kebanyakan di gunakan untuk kebutuhan membuat animasi, tapi tidak semata-mata javascript hanya bisa di pakai untuk membuat animasi, untuk kebutuhan operasi web pun sering di gunakan. Javascript bersifat Client Side, maksudnya adalah javascript berjalan di PC pengguna atau pengakses web, dan scriptnya pun bisa di lihat oleh user, berbeda dengan PHP yang scriptnya tidak bisa di lihat oleh user.

Selain di buat animasi, Javascript juga bisa di pakai untuk membuat aplikasi web, salah satunya adalah GAME, pada kesempatan kali ini saya akan memberikan tutorial bagaimana membuat GAME sederhana dengan Javascript, memanfaatkan event-event dalam javascript.

berikut screenshoot dari game sederhana :


Alur Game :

1. Klik button Start.
2. gambar monster akan bergerak cepat.
3. klik sebanyak mungkin gambar monsternya.
4. Apabila skor lebih dari 20, maka anda akan menang.

untuk sourcecode nya bisa di download disini atau copas dari box script di bawah.

Game ini memiliki 3 file, yaitu Html, Js dan Css.

index.html >

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/style.css" />
<title>
</title><script src="js/game.js">
</script></head>
<body><table>
<tr>
<td><span id="waktu">Time Left  </span></td><td>:  <input type="text" id="time" class="text"/></td></tr>
<tr><td><span id="angka">Score      </span></td><td>:  <input type="text" id="score" class="text" />  >= 20</td></tr>
<tr><td><input type="button" onclick="start()" value="start" id="start"/></td><td><select name="level">
<option value="level1">Level 1</option>
<option value="level2">Level 2</option>
<option value="level3">Level 3</option>
<option value="level4">Level 4</option>

</select></td></tr>
</table>
<div id="arena"><img src="img/1.jpg" id="gambar" onclick="nilai()" width="100px" height="100px"/>
</div>
</body>
</html>


game.js >

var angka=20;
var atas=5;
var kiri=10;
var score = 0;
//var waktu = 20;
var waktu_habis = 0;
function start(){
bill = new pindah();
bill2 = new timer();
//.getElementById("score").value = 0;
//document.getElementById("time").value = 0;

}
function nilai(){
document.getElementById("score").value =  score;
//document.getElementById("gambar").style.color = blue;
score++;
}
function timer(){
waktu_habis = document.getElementById("time").value = angka;
angka--;
var hasil = document.getElementById("score").value;
if(waktu_habis==0){
if(hasil<20){
alert("game over!!!");
window.location="index.html";
angka=0; document.getElementById("time").value= angka;
}else{
alert("you win!!");
window.location="index.html";
angka=0; document.getElementById("time").value= angka;
}
}
if(waktu_habis==0){
setTimeout("", 1000);
}else{
setTimeout("timer()", 1000);
}
}
var parameter_kiri = false;
var parameter_atas = false;
function pindah(){
var gambar = document.getElementById("gambar");
gambar.style.position = "fixed";
gambar.style.marginLeft = kiri+"px";
gambar.style.marginTop = atas+"px";
if(kiri==900){
parameter_kiri =true;
}else if(kiri==10){
parameter_kiri=false;
}
if(atas==250){
parameter_atas=true;
}else if(atas==100){
parameter_atas=false;
}
if(parameter_kiri){
kiri-=1;
}else{
kiri+=1;
}
if(parameter_atas){
if(kiri==900){
atas-=20;
}else if(kiri==10){
atas-=20;
}
}else{
if(kiri==900){
atas+=20;
}else if(kiri==10){
atas+=20;
}

}

setTimeout("pindah()", 1/1000);
}


style.css >

#cari { width:200px;height:300px;}
#arena{ width:1000px; height: 300px; background-color:black; padding:20px 20px 20px 20px; border-radius:20px;}
#bilik_kanan{ background-color:white; width:100px; height:100%; float:right; }
#bilik_kiri{ background-color:white; width:100px; height:100%; float:left; } #gambar{ border-radius:50%; }
#start{ width:100px; }
.teks { width:50px; }

Mau pasang iklan di blog?. klik Disini
Sekian artikel saya, Semoga Bermanfaat.

Stay fools to Stay Hungry
NB : Artikel ini di peruntukkan untuk pendidikan semata. hal-hal negatif yang di lakukan oleh pengguna tutorial , bukan tanggung jawab penulis.

Popular posts from this blog

Automatic API Documentation Swagger in Golang #GolangDev

pixabay.com In this article, I'll explain how to generate an API blueprint instantly using SwagGo in Golang. API blueprint is a document that contains a bunch of API endpoints, its slickly same as documentation but less description, it's allow another programmer to read and see all the available endpoint and try it out with sandbox feature. Swagger is one of the most used API blueprints right now, it's available in free but limited usage. if you wanna use the free credit, you need to understand YAML notation, you can read the example notation in swagger official documentation. but again, it's really hard and takes an expensive time to arranges all the notation to achieve a good API blueprint. fortunately, there are tools in Golang that allow us to generate the YAML notation and automatically generate the blueprint page with only using markup notation, and it's FREE unlimited for self-host, insane right? SwagGo tools are available here , the documentation is very cl...

Create MVC Pattern Using Echo Framework #GolangDev

pixabay.com Hi, Back again. today I am gonna share about Go Language, yes it is a new programming language that really fast growing this day, so many startup using it, especially in indonesia, there is a Gojek, Dana, Tiket and many more. Go Lang is a programming language found by google engineer, this language is a multi paradigm language, you could use it as functional programming or treat it like an OOP bases. Echo is an framework that use Golang as a main language, to use a fast and reliable backend/web apps, as you know Golang has a really fast performance than other language, one of the reason is because its compiled not interpreted, so the Echo claim that its 10x faster. insane right ? Actually I didn't test the performance yet, but yeah for newbie like me in golang its not the main thing that you must understand, so the thing that we should know is how we actually can do with Golang.  I am person with MVC bases, before jump to golang, I have experience with La...

Playing with Hooks in Gorm #GolangDev

Pixabay Hello everyone, I would like to share some tricks in gorm, the Idea of this tricks is to execute a command or code statement in the middle of an ORM operation, if you ever know about trigger in the query language, then hooks is just kinda like that. Hooks could execute statement in some events such as: beforeCreate, beforeUpdate, afterCreate,  and afterUpdate . those event is similar as a trigger,  when the trigger is executed in DBMS layer, hooks are executed in the application layer. In my real case, my current company has different convention of naming database column, so when I try to implement ORM, a lot of columns such as created_at, updated_at and deleted_at can't be generated automatically. also if you have a custom UUID you could pass the value UUID generator into hooks before the insert is executed. for more detail let's jump into code : # Explanation Above code containing model struct, and several methods from gorm to serve model, TableName method used to d...