-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavascript
More file actions
139 lines (128 loc) · 3.35 KB
/
Javascript
File metadata and controls
139 lines (128 loc) · 3.35 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
* { padding: 0; margin: 0;}
canvas { background: #eee; display: block; margin: 0 auto; }
</style>
</head>
<body>
<canvas id="canv" width="480" height="320"></canvas>
<script>
var canvas = document.getElementById("canv");
var ctx = canvas.getContext("2d");
//공
var ballRadius = 10;
var x = canvas.width/2;
var y = canvas.height-30;
var dx = 2;
var dy = -2;
//패들
var paddleHeight = 10;
var paddleWidth = 75;
var paddleX = (canvas.width-paddleWidth)/2;
//AI
var AipH = 10;
var AipW = 75;
var AipX = (canvas.width-AipW)/2;
//키보드
var rightPressed = false;
var leftPressed = false;
document.addEventListener("keydown", keyDownHandler, false);
document.addEventListener("keyup", keyUpHandler, false);
function keyDownHandler(e) {
if(e.keyCode == 39) {
rightPressed = true;
}
else if(e.keyCode == 37) {
leftPressed = true;
}
}
function keyUpHandler(e) {
if(e.keyCode == 39) {
rightPressed = false;
}
else if(e.keyCode == 37) {
leftPressed = false;
}
}
function drawAip() {
ctx.beginPath();
ctx.rect(AipX, 0, AipW, AipH);
ctx.fillStyle = "red";
ctx.fill();
ctx.closePath();
}
function drawBall() {
ctx.beginPath();
ctx.arc(x, y, ballRadius, 0, Math.PI*2);
ctx.fillStyle = "#0095DD";
ctx.fill();
ctx.closePath();
}
function drawPaddle() {
ctx.beginPath();
ctx.rect(paddleX, canvas.height-paddleHeight, paddleWidth, paddleHeight);
ctx.fillStyle = "#0095DD";
ctx.fill();
ctx.closePath();
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawBall();
drawPaddle();
drawAip();
if(x + dx > canvas.width-ballRadius || x + dx < ballRadius) {
dx = -dx;
}
if(y + dy < ballRadius) {
if (x > AipX && x < AipX + AipW){
dy = -dy;
}
else {
alert("GAME OVER");
x = canvas.width/2;
y = canvas.height-30;
document.location.reload();
}
}
else if(y + dy > canvas.height-ballRadius) {
if(x > paddleX && x < paddleX + paddleWidth) {
dy = -dy;
if (rightPressed){
dx = dx+1;
}
else if (leftPressed){
dx = dx-1;
}
}
else {
alert("GAME OVER");
x = canvas.width/2;
y = canvas.height-30;
document.location.reload();
}
}
if(rightPressed && paddleX < canvas.width-paddleWidth) {
paddleX += 7;
}
else if(leftPressed && paddleX > 0) {
paddleX -= 7;
}
x += dx;
y += dy;
AipX += dx*0.85;
if (AipX > canvas.width-AipW ){
AipX = canvas.width-AipW;
}
else if (AipX < 0){
AipX = 0;
}
requestAnimationFrame(draw);
}
draw();
</script>
</body>
</html>