-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
156 lines (126 loc) · 4.56 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
$(document).ready(function() {
const ourCanvus = $("#ourCanvus")[0] // array 0 is the initial view of the canvus
const ctx = ourCanvus.getContext("2d") ; // will create/transform ourCanvus into 2D view
const wed = $("#ourCanvus").width(); // the width of the canvus, taken from the canvus html tag
const high = $("#ourCanvus").height();// the height of the canvus, height taken from the html tag
const playerBorder = 5;
// the food's name
var victim = {};
// the player's name
var predetor = {};
// directions storage
var directions = ' ';
var counter = 0 ;
// the player name goes here!!
var playerName = prompt("Enter thy name") ;
$("#player1").append("<source id='playerSrc' type='audio/mpeg' src='/sounds/boulders,boulderdash.mp3'>")
var playerScore ;
var loop = setInterval( movement, 100);
function movement(){
if (directions == "RIGHT"){
predetor.x = predetor.x + 1
}else if (directions == "LEFT"){
predetor.x = predetor.x - 1
}else if (directions == "UP"){
predetor.y = predetor.y - 1
}else if( directions == "DOWN"){
predetor.y = predetor.y + 1
}
edgeOfMap();
paintBlank();
victimaizble();
shapes(predetor.x , predetor.y , "#830303");
shapes(victim.x , victim.y , '#00ff00');
}
// spawn the victim
function spawnVictim (){
victim = {
x : Math.floor(Math.random() * (wed - playerBorder)/playerBorder) ,
y : Math.floor(Math.random() * (high - playerBorder)/playerBorder)
}
}
// spawn predetor
function spawnPredetor (){
predetor = {
x : Math.floor(Math.random()* (wed - playerBorder)/playerBorder),
y : Math.floor(Math.random()* (high - playerBorder)/playerBorder)
}
}
// Paint the Canvas
function paintBlank () {
ctx.fillStyle = "wheat";
ctx.fillRect(0,0,wed,high);
ctx.strokeStyle = "black"
ctx.strokeRect(0,0,wed, high);
var max = 0
}
// Game Over function
function gameOver (){
var result = parseInt($('#over').text())
$("#finalDestination").text(result)
$("#gOver").fadeIn();
$("#player1").last().remove()
$("#player2").append("<source id='playerSrcc' type='audio/mpeg' src='/sounds/woah.mp3'>")
$("#mena").text("Is that your best ?!? " + playerName)
setTimeout(function(){$("#player2").last().remove()},700) //
}
// will check if the predetor is In any of the egdes of the map we define
function edgeOfMap () {
if (predetor.x < 0 || predetor.x > (wed-playerBorder)/playerBorder || predetor.y < 0 || predetor.y > (high-playerBorder) / playerBorder){
clearInterval(loop);
gameOver();
}
}
// paints the predetor dependin and the victim on what the player enters
function shapes (x,y, color){
ctx.fillStyle = color;
ctx.fillRect(x*playerBorder, y*playerBorder , playerBorder , playerBorder);
ctx.strokeStyle = color;//this is the border of either the predetor and the victim
ctx.strokeRect(x*playerBorder , y*playerBorder , playerBorder , playerBorder);
}
// check if the victim is victimiazable
function victimaizble (){
if(victim.x == predetor.x && victim.y == predetor.y){
var current = parseInt($('#over').text())
current++
$('#over').text(current)
counter++
// $("#effect3").append("<source id='playerSrcc' type='audio/mpeg' src='/sounds/woah.mp3'>")
// setTimeout(function(){$("#effect3").last().remove()},700)
eatTime()
spawnVictim()
}
}
function eatTime(){
$("#effect3").append("<source id='playerSrcc' type='audio/wav' src='/sounds/Die.wav'>")
setTimeout(function(){$("#effect3").last().remove()},900)
}
spawnVictim();
shapes(predetor.x , predetor.y , "#830303");
spawnPredetor();
shapes(victim.x , victim.y , '#00ff00');
// keyboard controller
$(document).keydown(function(){
var key = event.which;
if(key == "37" || key == "65"){
predetor.x = predetor.x - 1 ;
directions = "LEFT";
} else if(key == "38" || key == "87"){
predetor.y = predetor.y - 1 ;
directions = "UP";
} else if(key == "39" || key == "68"){
predetor.x = predetor.x + 1 ;
directions = "RIGHT";
}else if(key == "40" || key == "83"){
predetor.y = predetor.y + 1 ;
directions = "DOWN";
};
victimaizble();
paintBlank();
color = "red";
shapes(victim.x , victim.y)
color = "green"
shapes(predetor.x , predetor.y)
});
// $("#restartTheGame").on("click",function(){window.location.reload()})
});