create account

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

View this thread on: hive.blogpeakd.comecency.com
· @onepice ·
$29.32
Minefield Game Using HTML 5 SVG (Part-2)
![jquery0.fw.png](https://cdn.steemitimages.com/DQmXZmWSGqMeE3VmtYCFPxRMHoneKnAS88db8rcfRDo7iSP/jquery0.fw.png)

# 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-Part2">Minefield Game Using HTML 5 SVG (Part-2)</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.

# Requirements

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

# Difficulty

- Basic

# Tutorial Contents

In the previous article we created minefields for game and set mines at random. Mines were randomly settled in different places when we refresh the page.

Now we can place the numbers in the boxes next to the bombs.

In this article I will show the number of bombs that are placed next to the boxes that are not bombs. I'll use `svg.text()` to do this.

Let's start.

I created a object named `square` to create a box so that I could keep the properties of the boxes.

I could keep the value, appearance and starting points of this box with this object.

#### Let's make a few edits in our code

Change the `rect` property to `obj` before you start placing numbers. I also return the rectangle drawn in the `rectBuilder()` function.

So we can click on the boxes with the help of the pointer and play the game.

Let's arrange the `obj` property into the `square` object.

```
var square={
        value:0,
        obj:rectBuilder(pointX,pointY,'#dcdde1'),
        pointX:pointX,
        pointY:pointY
      }
```
<br>

Let's edit the `rectBuilder()` function.

```
function rectBuilder(x,y,color){
  return arc=svg.rect({
    x:x,
    y:y,
    width:50, height:50,
    fill: color,
});

```
<br>
We can start to place numbers according to what we made the necessary changes.

#### Set Value Property

We created the `value property` to hold the values of the bombs and the numbers to be written next to it.

We set the value to null when creating bombs so we can add values next to this box when `value = null`.

When we refresh the page, the `area` object having random bombs is as follows.

#### Screenshot 1
![jquery1.jpg](https://cdn.steemitimages.com/DQmU4meZSAPzu9tMPn1m4hnszjiBZxpgLJMCyrZpz7NgeaK/jquery1.jpg)
<br>

If we increase the value of the boxes next to the bombs by one and we repeat it for all the bombs, we will place the numbers correctly.

I will use the nested for loop for minefield fields so I will have access to x-y coordinates.

When the value attribute in the area array is null, we can catch what we are on the bomb.

```
for (var x = 0; x < 5; x++) {
    for (var y = 0; y < 5; y++) {

      if(area[x][y].value==null){
       
        }

      }

    }
```
<br>
We need to access the sides of the bomb.

#### Screenshot 2
![jquery1.fw.png](https://cdn.steemitimages.com/DQmPztXvwRF7RnMwDySA55fBynKLc94v9gefiw3kpQh4bRW/jquery1.fw.png)
<br>
With the nested for loop we can find the boxes on the sides of the bomb. Increase the value properties of the boxes next to it.

```
for (var x = 0; x < 5; x++) {
    for (var y = 0; y < 5; y++) {

      if(area[x][y].value==null){
        console.log(x+','+y);

        for (var i = x-1; i <=x+1; i++) {
          for (var j = y-1; j <=y+1; j++) {

                area[i][j].value=area[i][j].value+1;
              
              }
            }

      }

    }
  }
```
<br>
When we create our algorithm like this, we will encounter an error when we refresh the page.

This error occurs because we went out of the minefield area.

If the bomb is placed on the edges, we can not find such an area because we have adjusted the values of the boxes on the sides of the bomb.

If we place the values by controlling the edges of the minefield, we will not have such a problem.

```
for (var x = 0; x < 5; x++) {
    for (var y = 0; y < 5; y++) {

      if(area[x][y].value==null){
        console.log(x+','+y);

        for (var i = x-1; i <=x+1; i++) {
          for (var j = y-1; j <=y+1; j++) {

           if(i<0||i>4){//We are checking bounds in x coordinate.

            }else{
             if(j<0||j>4){//We are checking bounds in y coordinate.

              }
             else{
                if(area[i][j].value!=null){
                area[i][j].value=area[i][j].value+1;
              }

              }
            }
         }
        }


      }

    }
  }
```
<br>
So we set the value property to the box next to the bomb.

#### Screenshot 3
![jquery3.jpg](https://cdn.steemitimages.com/DQmcpe3Q8kCogZmHDHfGnjPp3MJbhK1gF5qX7njpLqayihA/jquery3.jpg)
<br>
The values next to the bomb were set.

### Drawing Numbers On The Screen

I'll define one function to draw the numbers on the screen.

I can create a drawing with the `text()` function from the svg object we created. With the `text() function`, we can set the color, size and starting point of the text to be written on the screen.

The text to be written to the screen, we will send it as a parameter to the `content()` function.

Let's create the function.

```
function textBuilder(x,y,color,value){
    text = svg.text({
            x:x, y:y,
            fill:color,
            'font-size':'15px',
            'font-family':'sans-serif'
        });
    text.content(value);
  }
```
<br>
We send to this function the starting points of the text, the color and the value to be written.

Now, we need to do this in a 5x5 nested for loop using this function to draw value values.

```
  for (var x = 0; x < 5; x++) {
      for (var y = 0; y < 5; y++) {

        if(area[x][y].value!=null){
         textBuilder(area[x][y].pointX,area[x][y].pointY,'#1abc9c',area[x][y].value);
       }
     }
   }
```
<br>
I printed the values in the boxes outside the bomb box.

I started the starting points of the boxes and the starting points of the texts from the same place.

#### Screenshot 4
![jquery4.jpg](https://cdn.steemitimages.com/DQmP2WiS6FV6QEKNzYBQb3gz8qk4Br1UGX9odCYDiMhkrAB/jquery4.jpg)
<br>
The mine field was like the one pictured above because we made such an encoding.

The problem will be solved if we shift the starting points of the text a bit.

The new `textBuilder()` function is below.
```
function textBuilder(x,y,color,value){
    text = svg.text({
             x:x+20, y:y+35,
            fill:color,
            'font-size':'15px',
            'font-family':'sans-serif'
        });
    text.content(value);
  }
```
<br>
The new version of the minefield is below.

#### Screenshot 5
![jquery5.jpg](https://cdn.steemitimages.com/DQmbTa8Gpb6YvrF3B4kWwoyDm2NQSFsd3LV9539CEUxph7R/jquery5.jpg)
<br>
So we set the numbers and when the page is refreshed it is rearranged according to the position of the bombs.

#### Screenshot 6
![jquery6.JPG](https://cdn.steemitimages.com/DQmSRWpv8Zh9FcRbPqy2qeAN9djLda8yqudoz1muiaBa1yH/jquery6.JPG)

### What Is Next ?

We put bombs and numbers in the minefield. Must be invisible and clickable to become a game

# Curriculum

- [Minefield Game Using HTML 5 SVG (Part-1)](https://steemit.com/utopian-io/@onepice/minefield-game-using-html-5-svg-part-1)

# Proof of Work Done

https://github.com/onepicesteem/Minefield-Game-Using-HTML-5-SVG-Part2
πŸ‘  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 52 others
properties (23)
authoronepice
permlinkminefield-game-using-html-5-svg-part-2
categoryutopian-io
json_metadata{"tags":["utopian-io","tutorials","jquery","pablojs","html5-game"],"image":["https://cdn.steemitimages.com/DQmXZmWSGqMeE3VmtYCFPxRMHoneKnAS88db8rcfRDo7iSP/jquery0.fw.png","https://cdn.steemitimages.com/DQmU4meZSAPzu9tMPn1m4hnszjiBZxpgLJMCyrZpz7NgeaK/jquery1.jpg","https://cdn.steemitimages.com/DQmPztXvwRF7RnMwDySA55fBynKLc94v9gefiw3kpQh4bRW/jquery1.fw.png","https://cdn.steemitimages.com/DQmcpe3Q8kCogZmHDHfGnjPp3MJbhK1gF5qX7njpLqayihA/jquery3.jpg","https://cdn.steemitimages.com/DQmP2WiS6FV6QEKNzYBQb3gz8qk4Br1UGX9odCYDiMhkrAB/jquery4.jpg","https://cdn.steemitimages.com/DQmbTa8Gpb6YvrF3B4kWwoyDm2NQSFsd3LV9539CEUxph7R/jquery5.jpg","https://cdn.steemitimages.com/DQmSRWpv8Zh9FcRbPqy2qeAN9djLda8yqudoz1muiaBa1yH/jquery6.JPG"],"links":["https://github.com/premasagar/pablo","https://github.com/onepicesteem","https://github.com/onepicesteem/Minefield-Game-Using-HTML-5-SVG-Part2","https://github.com/Microsoft/vscode","https://steemit.com/utopian-io/@onepice/minefield-game-using-html-5-svg-part-1"],"app":"steemit/0.1","format":"markdown"}
created2018-09-03 10:37:51
last_update2018-09-03 10:37:51
depth0
children5
last_payout2018-09-10 10:37:51
cashout_time1969-12-31 23:59:59
total_payout_value22.226 HBD
curator_payout_value7.098 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length7,354
author_reputation9,626,549,398,383
root_title"Minefield Game Using HTML 5 SVG (Part-2)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id70,172,617
net_rshares24,020,336,115,663
author_curate_reward""
vote details (116)
@nadir1122 ·
Oh my God....!
properties (22)
authornadir1122
permlinkre-onepice-minefield-game-using-html-5-svg-part-2-20180909t041015640z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit/0.1"}
created2018-09-09 04:10:21
last_update2018-09-09 04:10:21
depth1
children0
last_payout2018-09-16 04:10:21
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_length14
author_reputation5,443,528,914
root_title"Minefield Game Using HTML 5 SVG (Part-2)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id70,755,009
net_rshares0
@portugalcoin ·
$7.68
Thank you for your contribution.
After reviewing your tutorial we suggest the following:

- Nice work on the explanations of your code, although adding a bit more comments to the code can be helpful as well.

We are waiting to see the game with animation. Good job!

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/21113314).

---- 
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-2-20180903t222618295z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"links":["https://join.utopian.io/guidelines","https://review.utopian.io/result/8/21113314","https://support.utopian.io/","https://discord.gg/uTyJkNm","https://join.utopian.io/"],"app":"steemit/0.1"}
created2018-09-03 22:26:18
last_update2018-09-03 22:26:18
depth1
children1
last_payout2018-09-10 22:26:18
cashout_time1969-12-31 23:59:59
total_payout_value5.806 HBD
curator_payout_value1.873 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length757
author_reputation598,828,312,571,988
root_title"Minefield Game Using HTML 5 SVG (Part-2)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id70,227,617
net_rshares6,651,943,769,385
author_curate_reward""
vote details (12)
@utopian-io ·
Thank you for your review, @portugalcoin!

So far this week you've reviewed 14 contributions. Keep up the good work!
properties (22)
authorutopian-io
permlinkre-re-onepice-minefield-game-using-html-5-svg-part-2-20180903t222618295z-20180907t224011z
categoryutopian-io
json_metadata"{"app": "beem/0.19.42"}"
created2018-09-07 22:40:12
last_update2018-09-07 22:40:12
depth2
children0
last_payout2018-09-14 22:40: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-2)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id70,647,910
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-2-20180903t224016z
categoryutopian-io
json_metadata"{"app": "beem/0.19.54"}"
created2018-09-03 22:40:18
last_update2018-09-03 22:40:18
depth1
children0
last_payout2018-09-10 22:40:18
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-2)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id70,228,546
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-2-20180904t031511z
categoryutopian-io
json_metadata"{"app": "beem/0.19.42"}"
created2018-09-04 03:15:12
last_update2018-09-04 03:15:12
depth1
children0
last_payout2018-09-11 03:15: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_length589
author_reputation152,955,367,999,756
root_title"Minefield Game Using HTML 5 SVG (Part-2)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id70,246,285
net_rshares15,679,266,296
author_curate_reward""
vote details (2)