I am currently working on coding a simple Risk game where players can click on countries to change their color from red to green, yellow, purple, and back to white.
The game starts with an "all-white" map. I am looking to create different save slots so that players can save the game in one slot and then reload that slot later to continue playing. How can I implement this feature for 4 different save slots? The world map is an SVG file, and I am changing each region's color using event.target.style.fill
.
Consider the following code:
HTML
<svg width="100%" version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 701 300" style="enable-background:new 0 0 701 300;" xml:space="preserve">
<g onclick="changeColor(event)">
<path class="st1" d="M308.6,112.3l-0.1,0.2l-0.1,0.2l-0.1-0.1h-0.1l-0.1-0.1l0.1-0.1h0.1l0.1-0.1l0.1-0.1L308.6,112.3L308.6,112.3z
M313.4,111.6v0.2l0.1,0.1l-0.1,0.2v0.1l-0.1,0.1l-0.2,0.1l-0.1,0.1l-0.2-0.1l-0.2-0.2l-0.1-0.1v-0.2l0.2-0.1v-0.2v-0.1h0.2h0.1h0.1
L313.4,111.6z M309.9,111.8L309.9,111.8l-0.2-0.1l-0.1-0.1v-0.1v-0.1h0.1h0.1l0.2,0.1v0.1L309.9,111.8L309.9,111.8z M312,110.6
L312,110.6l-0.2,0.2l-0.1,0.2l-0.1,0.1v0.1l-0.1,0.2v0.1l-0.2,0.2v0.1h-0.1l-0.2,0.1l0,0v-0.1l-0.1-0.1v-0.1l-0.1-0.1v-0.1l-0.1-0.2
l0.2-0.1l0.1,0.1l0.2-0.1h0.1l0.2-0.1l0.2-0.1v-0.1l0.2-0.1h0.2L312,110.6L312,110.6z M315.6,111.6l-0.1,0.1h-0.1h-0.1h-0.1v-0.1
h0.1l0.2-0.1l0.1-0.1l0.1-0.1v-0.2V111l0.1-0.2l0.1-0.1l0.1-0.2l0.1-0.3l0.1-0.1h0.1l0.1,0.1v0.2v0.2v0.2v0.2l0,0l-0.1,0.2l-0.1,0.1
H316L315.6,111.6L315.6,111.6z M308.8,110h0.1l0.1,0.1l0.1,0.2l-0.1,0.1v0.1l-0.2,0.3l0,0v-0.1l-0.1-0.3v-0.1v-0.1L308.8,110h0.1
H308.8z M317.1,109.3L317.1,109.3l-0.1,0.3l-0.2,0.2h-0.2l-0.1,0.2l-0.2-0.1l0,0l0.1-0.1v-0.1l0.1-0.1l0.1-0.1h0.1l0.1-0.1h0.2l0,0
L317.1,109.3l0.1-0.1l0...
JS
function changeColor(event) {
clicks++
if (clicks == 1) {
event.target.style.fill = "#DA4567";
}
if (clicks == 2) {
event.target.style.fill = "#7aeb34";
}
if (clicks == 3) {
event.target.style.fill = "#ffe70a";
}
if (clicks == 4) {
event.target.style.fill = "#ba0afa";
}
if (clicks == 5) {
event.target.style.fill = "#FFFFFF";
clicks = 0;
}
}
CSS (INITIAL COLOR = WHITE)
.st1{fill:#FFFFFF;stroke:#000000;stroke-linejoin:round;}
You can see a live demo of the game and access the website by clicking on the link below. I have also added a user interface for saving and loading games which can be accessed through the "Load/Save game" button.
Visit the Risk Game Demo here.
Thank you for your support!