-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsquash-game.js
187 lines (164 loc) · 4.57 KB
/
squash-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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright 2015 David Corking, Chris Mills,
// Mozilla Development Network and individual MDN contributors.
// Unless otherwise indicated, the content is available under the
// terms of the Creative Commons Attribution-ShareAlike license
// (CC-BY-SA), v2.5 or any later version.
// http://creativecommons.org/licenses/by-sa/2.5/
// Adapted from the tutorial at
// https://developer.mozilla.org/en-US/docs/Games/Workflows/2D_Breakout_game_pure_JavaScript
// ********************************
// *****1 PLAYER SQUASH GAME*******
// ********************************
// global singleton to contain game
var sg = {
rightPressed: false,
leftPressed: false
};
// event handlers must be defined before they are registered
sg.keyDownHandler = function (e) {
if(e.keyCode == 39) {
sg.rightPressed = true;
}
else if(e.keyCode == 37) {
sg.leftPressed = true;
}
else if(e.keyCode == 32) {
sg.paused = ! sg.paused;
};
};
sg.keyUpHandler = function (e) {
if(e.keyCode == 39) {
sg.rightPressed = false;
}
else if(e.keyCode == 37) {
sg.leftPressed = false;
};
};
sg.canvas = document.getElementById("myCanvas");
sg.ctx = sg.canvas.getContext("2d");
// constants
sg.paddle = {
height: 10,
width: 75,
speed: 3};
sg.ball = {
radius: 10};
//initialize game
sg.score = 0;
sg.paused = false;
sg.gameOver = false; // the game never ends
// initialize ball
sg.ball.initialize = function () {
// velocity
sg.ball.dx = 2; // rightwards
sg.ball.dy = -2; // upwards
// position
sg.ball.x = sg.canvas.width / 2;
sg.ball.y = sg.canvas.height - sg.paddle.height - sg.ball.radius + sg.ball.dy;
};
sg.ball.initialize();
// paddle starts in middle
sg.paddle.x = (sg.canvas.width - sg.paddle.width ) / 2;
sg.ball.draw = function (ctx) {
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI*2);
ctx.fillStyle = "#0095DD";
ctx.fill();
ctx.closePath();
};
sg.ball.step = function () {
// TODO poss. refactor to avoid referring
// directly to sg
//////////////////////////////
// collision with top
if (this.y - this.radius + this.dy < 0) {
this.dy = - this.dy;
};
// collision with paddle - y too big
if ((this.y + this.radius + this.dy + sg.paddle.height >
sg.canvas.height) &&
this.x > sg.paddle.x &&
this.x - sg.paddle.x < sg.paddle.width) {
this.dy = - this.dy;
};
// collision with sides
if (this.x - this.radius + this.dx < 0 ||
this.x + this.radius + this.dx > sg.canvas.width ) {
this.dx = - this.dx;
};
// lose the ball and lose a point
if (this.y + this.radius + this.dy > sg.canvas.height ) {
sg.score -= 1;
// TODO pause
// restart ball
this.initialize();
};
//move
this.x += this.dx;
this.y += this.dy;
};
sg.paddle.draw = function (ctx) {
ctx.beginPath();
ctx.rect(this.x,
sg.canvas.height - this.height,
this.width, this.height);
ctx.fillStyle = "#0095DD";
ctx.fill();
ctx.closePath();
};
sg.paddle.step = function () {
if (sg.rightPressed && this.x + this.width < sg.canvas.width ) {
this.x += this.speed;
};
if (sg.leftPressed && this.x > 0) {
this.x -= this.speed;
};
};
sg.walls = {
width: 10
};
sg.walls.draw = function (ctx) {
ctx.beginPath();
ctx.rect(0, 0, this.width, sg.canvas.height);
ctx.rect(0, 0, sg.canvas.width, this.width);
ctx.rect(sg.canvas.width - this.width, 0,
sg.canvas.width, sg.canvas.height);
ctx.fillStyle = "#0095DD";
ctx.fill();
ctx.closePath();
};
// Draw the world, then update it
sg.draw = function () {
// When setInterval invokes sg.draw, 'this' is the global object
// so this doesn't refer to my game object
// When I wrap the callback in setInterval to an anonymous function, so:
// function(){sg.draw()}
// then 'this' does indeed become the game object.
if (! sg.gameOver) {
sg.ctx.clearRect(0, 0, sg.canvas.width, sg.canvas.height);
sg.walls.draw(sg.ctx);
sg.ball.draw(sg.ctx);
(! sg.paused) && sg.ball.step();
sg.paddle.draw(sg.ctx);
sg.paddle.step();
}
else {
window.clearInterval(sg.drawAction); // stop the world
document.write ("<p>Game over!</p><p>Reload page to play again.</p>");
};
};
var displayScore = function () {
// TODO
};
////////////////////////////////////////////////////////
// Main program
////////////////////////////////////////////////////////
document.addEventListener("keydown", sg.keyDownHandler, false);
document.addEventListener("keyup", sg.keyUpHandler, false);
// repeatedly draw and update the world
sg.drawAction = window.setInterval(
function(){
sg.draw();
displayScore();
},
5);