-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
117 lines (111 loc) · 2.4 KB
/
script.js
File metadata and controls
117 lines (111 loc) · 2.4 KB
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
// Game code
//FPS
var FPS = 30;
var RArrow = 39;
var LArrow = 37;
//get the canvas
var canvas = document.getElementById("canvas");
//get context
var context = canvas.getContext("2d");
// Array of key flags
var KeyDownA = [];
onkeydown = function(e) {
e = e || event;
KeyDownA [e.keyCode] = true;
};
onkeyup = function(e) {
e = e || event;
KeyDownA [e.keyCode] = undefined;
};
//shooting key flags
var KeyDownX = [];
onkeydown2 = function(e) {
e = e || event;
KeyDownX [e.keyCode] = true;
};
onkeyup2 = function(e) {
e = e || event;
KeyDownX [e.keyCode] = undefined;
};
//Target
var target = {
color: 'red',
x: 250,
y:150,
vx: 0,
vy: 0,
ax: 0,
ay:0,
targetRadius: 20,
//draw function
draw: function() {
context.beginPath();
context.fillStyle = this.color;
context.arc( this.x, this.y, this.targetRadius, 0, 2 * Math.PI );
context.fill();
}
};
//end of target
//Crosshair Thing
var crosshair = {
color: 'black',
x: 150,
y: 150,
vx: 0,
vy: 0,
ax: 0,
ay: 0,
crossHairSize:10,
//draw function
draw: function() {
context.beginPath();
context.fillStyle = this.color;
context.moveTo(this.x-this.crossHairSize,this.y);
context.lineTo(this.x+this.crossHairSize,this.y);
context.moveTo(this.x,this.y-this.crossHairSize);
context.lineTo(this.x,this.y+this.crossHairSize);
context.stroke();
context.closePath();
},
// Update function
update: function() {
this.vx += this.ax / FPS;
this.vy += this.ay / FPS;
this.x += this.vx / FPS;
this.y += this.vy / FPS;
},
keymove: function() {
if (KeyDownA[RArrow] &&
this.x + this.crossHairSize < canvas.width)
this.x += 10;
if (KeyDownA[LArrow] &&
this.x - this.crossHairSize > 0)
this.x -= 10;
}
};
//end of crosshairs
//this is supposed to do the shooting
//shoot
function shoot() {
if (keyDownX[F] && crosshair.x >= 260 && crosshair.x <= 240) {
alert("You hit the target!");
}
//this is supposed to make the canvas explode with jQuery if you miss the target
/*
if (keyDownX[F] && crosshair.x > 260 && crosshair.x < 140) {
$(document).ready(function() {
$('#canvas').animate('explode', slow);
});
}
*/
}
//game tick
function tick() {
context.clearRect(0,0,canvas.width,canvas.height);
crosshair.keymove();
crosshair.draw();
target.draw();
}
//fps thing
setInterval(tick, 1000/FPS);
// End Game Code