-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
146 lines (125 loc) · 2.91 KB
/
game.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
function Game(canvas)
{
this.canvas = canvas;
this.context = canvas.getContext("2d");
this.width = window.innerWidth;
this.height = window.innerHeight;
this.paused = false;
this.muted = false;
this.fillColor = new Color(173, 216, 230);
this.bumpSound = new Audio("./bumpsound.wav"); // buffers automatically when created
this.missSound = new Audio("./missSound.wav"); // buffers automatically when created
// Keep track of key states
this.userInput = {};
this.userInterrupt = {};
var self = this;
$(canvas).on('keydown keyup', function(e) {
//Convert key code to key name
var keyName = Game.keys[e.which];
if (e.type === 'keydown') {
self.userInterrupt[keyName] = true;
}
//if keyState was changed
if(keyName)
{
e.preventDefault();
self.userInput[keyName] = e.type === 'keydown';
}
});
}
//Key codes mapping
Game.keys = {
32: 'togglePause',
37: 'moveLeft',
38: 'moveUp',
39: 'moveRight',
40: 'moveDown',
72: 'moveLeft',
74: 'moveDown',
75: 'moveUp',
76: 'moveRight',
}
Game.prototype.start = function() {
canvas.focus();
var self = this,
fps = 60,
interval = 1000/fps;
setInterval(function() {
self.draw();
self.update();
self.userInterrupt = {};
}
,interval);
};
Game.prototype.togglePause = function() {
this.paused = !this.paused;
if (this.paused) {
this.background.showMuteText();
}
else {
this.background.hideMuteText();
}
}
Game.prototype.update = function() {
if (this.userInterrupt.togglePause) {
this.togglePause();
}
if (this.paused) return;
this.entities.forEach(function(entity) {
if (entity.update)
{
entity.update();
}
});
}
Game.prototype.draw = function() {
if (this.paused) return;
var self = this;
this.entities.forEach(function(entity) {
if (entity.draw)
{
entity.draw(self.context);
}
});
}
Game.prototype.ballHitWall = function(paddle) {
this.playSound(this.bumpSound);
}
Game.prototype.ballHitPaddle = function(paddle) {
this.fillColor = randColor();
this.playSound(this.bumpSound);
}
Game.prototype.playerWonRound = function(player) {
//if bot wins make him slower if loses make him faster
if (player === this.bot) {
this.bot.wonRound();
}
else {
this.bot.lostRound();
}
player.score++;
this.ball.reset();
this.bot.reset();
this.player.reset();
this.playSound(this.missSound);
this.draw();
this.togglePause();
}
Game.prototype.playSound = function(sound) {
if (!this.muted) {
sound.play();
}
}
function randColor() {
mix = new Color(173, 216, 230);
var red = randomInt(255);
var green = randomInt(255);
var blue = randomInt(255);
red = Math.floor((mix.red + red) / 2);
green = Math.floor((mix.green + green) / 2);
blue = Math.floor((mix.blue + blue) / 2);
return new Color(red, green, blue);
}
function randomInt(n) {
return Math.floor(Math.random() * n) + 1;
}