Minefield Game Using HTML 5 SVG (Part-3)
utopian-io·@onepice·
0.000 HBDMinefield Game Using HTML 5 SVG (Part-3)
 # 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  <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  <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  <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  <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 .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>
👍 lordofreward, georgian2979, micropituitary, whiskeynovelty, yallod, lemonydietary, pointsbee, musiccrannies, dimka10, borndead04, elliygova, alexmartinov, nasturtiumfatty, danilkramov, pferdynand, warblingunchin, amuseaccuracy, listlabcoat, barrimakgi, ydomarev, klaraminasyan, voronovboris, xvegax, tattooedwinner, svlasov1984, rondorajon8, hlebova1009, chestyovert, ervinj89, hastypeacock, zhilin8, periodantenna, torntonbar, rodzherg, strangetwelve, andykon, friedradius, decagonour, heapimport, unequaledpelite, beerlimey, roadlifted, evgeniy.pavlenko, kabernee, imperfectvital, uglypsi, yieldingcoke, deltoidbus, amplegranita, liquidisebland, sodgelatin, yulya.zaharova, anmakarov19, teo-nyx, devidkoperfild, lilihaovhan, saevpek, ellibor, dmitriynemov, sweatsavory, aljager78, dzhozefrays, putridbrecon, ivan.gotovin, ushkurwike, followmutable, cyandyewilson, oxbowworship, chubbycircle, arkoseduration, abnormalsystem, ekarina10, borndead03, viktorpetro, walrusaromatic, gannamuliar, partialbullhorn, neighshallot, padadvice, elitestrake, anharchenko1987, ivan.siko, steemitag, danielaminas, needlesten, substrcake, doubtdisney, tensilegangway, wallownine, twitterprime, regretfulwooden, caterernodes, nartemov198, bachuslib, filipino, simplymike, iauns, properfraction, jaff8, portugalcoin, amosbastian, mightypanda, fastandcurious, bullinachinashop, organicgardener, clayjohn, ryuna.siege, steem-ua, tdre, jjay, utopian-io, truthly, remind-me,