create account

Minefield Game Using HTML 5 SVG (Part-3) by onepice

View this thread on: hive.blogpeakd.comecency.com
· @onepice ·
$30.34
Minefield Game Using HTML 5 SVG (Part-3)
![ezgif.com-crop.gif](https://cdn.steemitimages.com/DQmZ9UHZc58s6uYpkZDtWuoq6qrrVfe8y1c4DUsxZPhYAmg/ezgif.com-crop.gif)

# Repository

<a href="https://github.com/premasagar/pablo">PabloJs</a>

<a href="https://github.com/onepicesteem">My Github Profile</a>

<a href="https://github.com/onepicesteem/Minefield-Game-Using-HTML-5-SVG-Part3">Minefield Game Using HTML 5 SVG (Part-3)</a>

# What Will I Learn?

- You will learn how to build games using `HTML5 SVG`.
- You will learn to create svg objects more easily using `Pablojs`.
- You will learn to create a svg object as text on the screen.
- You will learn how to use the javascript `confirm()` method.
- You will learn the `click event` in pablojs.
- You will learn the `css()` and `attr()` methods in pablojs.

# Requirements

- <a href="https://github.com/Microsoft/vscode">Visual Studio Code in GitHub</a>

# Difficulty

- Basic

# Tutorial Contents

This article is the last piece of articles I prepared for the minefield game.

Now we complete our game. With this article we will close the boxes and we will print out the numbers when the boxes are clicked.

If the time we click on is a mine, we will bring the message that you lost the game. 

Let's start.

First, we must set all the boxes to the game start color. In earlier articles we set the color of the bombs black and the color of the other boxes is gray.

Let's set the color of all the boxes to gray so that you do not know which box is the bomb.

To do this we will set the color of the bombs to gray before we set the color of the bombs to black.
```
for (var x = 0; x < 5; x++) {
    for (var y = 0; y < 5; y++) {

      var square={
        value:0,
        obj:rectBuilder(pointX,pointY,'#dcdde1'),
        pointX:pointX,
        pointY:pointY
      }
        area[x][y]=square;

        for (var i = 0; i < bombs.length; i++) {
          if(x==bombs[i].x && y==bombs[i].y){
            var square={
              value:null,
              obj:rectBuilder(pointX,pointY,'#dcdde1'),//place of color of bombs
              pointX:pointX,
              pointY:pointY
            }
          area[x][y]=square;

          }
        }
        pointY=pointY+60;
    }
    pointX=pointX+60;
    pointY=10;
}
```
<br>
All we have to do is dye the bombs in gray color.

When we reopen the game we get an image like this.

#### Screenshot 1
![jquery1.jpg](https://cdn.steemitimages.com/DQmQARGtderW4CkNv5A196v6iaN1SZa8vPtPcEAQ6na1irv/jquery1.jpg)
<br>
So now it is not understood which bomb is the boxes.

### Box Click Operations

We need to know which box is clicked in order to catch the click event.

I will need the object of all the boxes and will use the nested for loop for this.
```
  for (var x = 0; x < 5; x++) {
      for (var y = 0; y < 5; y++) {
// click events will come
     }
}
```
<br>
The `object.on()` method is used in `Pablojs` to catch the click event.

We need to access the `obj` property of all the boxes because all the boxes are likely to be clicked.

I will access to all the boxes in the loop.
```
  //boxes member of svg
      var obj=Pablo(area[x][y].obj[0]);
      console.log(obj);
```
<br>
So we had access to all the rectangle objects as shown in the picture below.

#### Screenshot 2
![jquery2.jpg](https://cdn.steemitimages.com/DQmf2E7Xi84XZtBtozSJbmCzNnPSLLxUNCjFWmnVjRaGGvX/jquery2.jpg)
<br>
We can now catch the click event because we have reached the boxes as rectangle which is the svg element.

I've said we've got the events `on()` method. With the `css()` method we can give styles to svg elements.

I want to change the shape when I come to the boxes with the pointer. I can do with the `cursor: pointer` syntax.
```
  obj.on('click',function(e){
	//click events
   }).css({cursor:'pointer'});
```
<br>
So when the pointer is over the boxes, it looks like the picture below.

#### Screenshot 3
![jquery3.jpg](https://cdn.steemitimages.com/DQmaU7K68mcfRSsVvdefpAfXmZGyNvNEggnzxz5Pqxnjs1Y/jquery3.jpg)
<br>
We need to access the `value` property of the clicked box.

We need to follow a different path because there is no value in the object we clicked on.

If we can find the starting points of the clicked object, we can access the vlaue property using these points.

Let's examine the rectangle object to find the starting points.

#### Screenshot 4
 ![jquery4.jpg](https://cdn.steemitimages.com/DQmQZGmYDoNLJCN3tCwxfZJF1yoHoV3E6jpDDHcyt6JqmfQ/jquery4.jpg)
<br>
Where the `x` and `y` attributes indicate the starting points of the rectangle.

If I can access these attributes of the clicked object I can compare it with the `pointX` and `pointY` properties of the boxes.

In `Pablojs` we can access the attributes of the svg object with the `attr()` method.

We can access the value property by comparing the initial values.
```
  //access the x and y attributes
        var PointX=Pablo(this).attr('x');
        var PointY=Pablo(this).attr('y');
        var value;

        for (var x = 0; x < 5; x++) {
            for (var y = 0; y < 5; y++) {
              //equality of initial values is checked.
              if(area[x][y].pointX==PointX && area[x][y].pointY==PointY){
                value=area[x][y].value;
                PointX=area[x][y].pointX;
                PointY=area[x][y].pointY
              }
            }
          }
```
<br>
We performed the click event and reached the `value` property of the clicked box.

We need to do is catch the bomb click or the empty click.

If the value is null, it is clicked on the bomb.
```
  if(value!=null){

            textBuilder(PointX,PointY,'#1abc9c',value);

          }else{

            Pablo(this).attr('fill','#2c3e50');

            if (confirm('would you like to play again')) {
               window.location.reload(false)
            } else {

            }
          }
```
<br>
If the space is clicked, print the value using the `textBuilder()` method or If the bomb is clicked, I will paint the box black color and let the end user know that the game is over.

The player may want to replay so I use the `confirm()` method. The confirm method queries the end user and waits for a yes or no answer.

If the player's answer is yes, we need to restart the game. We only need to refresh the page to restart the game.

We can refresh the page with `window.location.reload(false)`.

So we have completed our game.

#### Below is animated version of our game
![ezgif.com-crop(2).gif](https://cdn.steemitimages.com/DQmZqmChm6e19Dj6BthNWc3QJNgrhFn3Zk5B1aKR2uTsZZS/ezgif.com-crop(2).gif)

# Curriculum

<a href="https://steemit.com/utopian-io/@onepice/minefield-game-using-html-5-svg-part-1">Minefield Game Using HTML 5 SVG (Part-1)</a>

<a href="https://steemit.com/utopian-io/@onepice/minefield-game-using-html-5-svg-part-2">Minefield Game Using HTML 5 SVG (Part-2)</a>

# Proof of Work Done

<a href="https://github.com/onepicesteem/Minefield-Game-Using-HTML-5-SVG-Part3">Minefield Game Using HTML 5 SVG (Part-3)</a>
πŸ‘  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 49 others
properties (23)
authoronepice
permlinkminefield-game-using-html-5-svg-part-3
categoryutopian-io
json_metadata{"tags":["utopian-io","tutorials","pablojs","html-game"],"image":["https://cdn.steemitimages.com/DQmZ9UHZc58s6uYpkZDtWuoq6qrrVfe8y1c4DUsxZPhYAmg/ezgif.com-crop.gif","https://cdn.steemitimages.com/DQmQARGtderW4CkNv5A196v6iaN1SZa8vPtPcEAQ6na1irv/jquery1.jpg","https://cdn.steemitimages.com/DQmf2E7Xi84XZtBtozSJbmCzNnPSLLxUNCjFWmnVjRaGGvX/jquery2.jpg","https://cdn.steemitimages.com/DQmaU7K68mcfRSsVvdefpAfXmZGyNvNEggnzxz5Pqxnjs1Y/jquery3.jpg","https://cdn.steemitimages.com/DQmQZGmYDoNLJCN3tCwxfZJF1yoHoV3E6jpDDHcyt6JqmfQ/jquery4.jpg","https://cdn.steemitimages.com/DQmZqmChm6e19Dj6BthNWc3QJNgrhFn3Zk5B1aKR2uTsZZS/ezgif.com-crop(2).gif"],"links":["https://github.com/premasagar/pablo","https://github.com/onepicesteem","https://github.com/onepicesteem/Minefield-Game-Using-HTML-5-SVG-Part3","https://github.com/Microsoft/vscode","https://steemit.com/utopian-io/@onepice/minefield-game-using-html-5-svg-part-1","https://steemit.com/utopian-io/@onepice/minefield-game-using-html-5-svg-part-2"],"app":"steemit/0.1","format":"markdown"}
created2018-09-04 13:51:45
last_update2018-09-04 13:51:45
depth0
children4
last_payout2018-09-11 13:51:45
cashout_time1969-12-31 23:59:59
total_payout_value22.965 HBD
curator_payout_value7.371 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length6,946
author_reputation9,626,549,398,383
root_title"Minefield Game Using HTML 5 SVG (Part-3)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id70,293,126
net_rshares26,229,833,701,862
author_curate_reward""
vote details (113)
@portugalcoin ·
$7.53
Thank you for your contribution.
After reviewing your tutorial we suggest the following:

- Cycles within cycles decreases code performance. Try another method to get array values without having to use cycles within cycles.

- In the game there is no possibility of winning. You must have a validation function if you have reached the end of the game without clicking on the bombs.

With the animation the HTML5 SVG game got better, well done.

Your contribution has been evaluated according to [Utopian policies and guidelines](https://join.utopian.io/guidelines), as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, [click here](https://review.utopian.io/result/8/21111324).

---- 
Need help? Write a ticket on https://support.utopian.io/. 
Chat with us on [Discord](https://discord.gg/uTyJkNm). 
[[utopian-moderator]](https://join.utopian.io/)
πŸ‘  , , , , , , , , , ,
properties (23)
authorportugalcoin
permlinkre-onepice-minefield-game-using-html-5-svg-part-3-20180904t225734700z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"links":["https://join.utopian.io/guidelines","https://review.utopian.io/result/8/21111324","https://support.utopian.io/","https://discord.gg/uTyJkNm","https://join.utopian.io/"],"app":"steemit/0.1"}
created2018-09-04 22:57:33
last_update2018-09-04 22:57:33
depth1
children1
last_payout2018-09-11 22:57:33
cashout_time1969-12-31 23:59:59
total_payout_value5.691 HBD
curator_payout_value1.837 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length935
author_reputation600,428,323,373,197
root_title"Minefield Game Using HTML 5 SVG (Part-3)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id70,337,039
net_rshares6,641,405,048,481
author_curate_reward""
vote details (11)
@utopian-io ·
Thank you for your review, @portugalcoin!

So far this week you've reviewed 19 contributions. Keep up the good work!
properties (22)
authorutopian-io
permlinkre-re-onepice-minefield-game-using-html-5-svg-part-3-20180904t225734700z-20180908t205611z
categoryutopian-io
json_metadata"{"app": "beem/0.19.42"}"
created2018-09-08 20:56:12
last_update2018-09-08 20:56:12
depth2
children0
last_payout2018-09-15 20:56:12
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length116
author_reputation152,955,367,999,756
root_title"Minefield Game Using HTML 5 SVG (Part-3)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id70,731,177
net_rshares0
@steem-ua ·
#### Hi @onepice!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
**Feel free to join our [@steem-ua Discord server](https://discord.gg/KpBNYGz)**
properties (22)
authorsteem-ua
permlinkre-minefield-game-using-html-5-svg-part-3-20180905t000114z
categoryutopian-io
json_metadata"{"app": "beem/0.19.54"}"
created2018-09-05 00:01:15
last_update2018-09-05 00:01:15
depth1
children0
last_payout2018-09-12 00:01:15
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length286
author_reputation23,214,230,978,060
root_title"Minefield Game Using HTML 5 SVG (Part-3)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id70,340,713
net_rshares0
@utopian-io ·
Hey, @onepice!

**Thanks for contributing on Utopian**.
We’re already looking forward to your next contribution!

**Get higher incentives and support Utopian.io!**
 Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via [SteemPlus](https://chrome.google.com/webstore/detail/steemplus/mjbkjgcplmaneajhcbegoffkedeankaj?hl=en) or [Steeditor](https://steeditor.app)).

**Want to chat? Join us on Discord https://discord.gg/h52nFrV.**

<a href='https://steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1'>Vote for Utopian Witness!</a>
πŸ‘  ,
properties (23)
authorutopian-io
permlinkre-minefield-game-using-html-5-svg-part-3-20180905t061536z
categoryutopian-io
json_metadata"{"app": "beem/0.19.42"}"
created2018-09-05 06:15:36
last_update2018-09-05 06:15:36
depth1
children0
last_payout2018-09-12 06:15:36
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length589
author_reputation152,955,367,999,756
root_title"Minefield Game Using HTML 5 SVG (Part-3)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id70,365,375
net_rshares16,275,861,412
author_curate_reward""
vote details (2)