-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_sp.js
142 lines (126 loc) · 3.06 KB
/
game_sp.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
canvas = document.getElementById("c");
console.log(canvas.width)
console.log(canvas.height)
ctx = canvas.getContext("2d")
player = {
pos_x: 0,
pos_y: 0,
size: 30,
velocity: 30,
color: "#000099",
'ArrowUp': up_move,
'ArrowDown': down_move,
'ArrowRight': right_move,
'ArrowLeft': left_move,
draw: function(){
ctx.fillStyle = this['color']
ctx.fillRect(this['pos_x'], this['pos_y'], this['size'], this['size'])
}
}
block = {
pos_x: -10,
pos_y: -10,
size: 30,
color: "#ffcc00",
draw: function(){
ctx.fillStyle = this['color']
ctx.fillRect(this['pos_x'], this['pos_y'], this['size'], this['size'])
}
}
game = {
score: 0,
time: 60,
over: false,
keys_pressed: 0
}
function init(){
player.draw()
refresh_block()
block.draw()
}
function start(){
ctx.fillStyle = "#fff"
ctx.fillRect(0, 0, canvas.width, canvas.height)
player.draw()
block.draw()
colision_detect()
refresh()
}
function refresh(){
if(!game['over']){
window.requestAnimationFrame(start)
}else{
draw_game_over()
}
}
function refresh_time(){
loop = setInterval(function(){
game['time']--
document.getElementById("tempo").innerHTML = game['time']
if(game['time'] <= 0){
game['over'] = true
clearInterval(loop)
}
}, 1000)
}
function draw_game_over(){
ctx.fillStyle = "#000"
ctx.font = "40px serif"
ctx.fillText("Game Over!", canvas.width/2.5, canvas.height/2)
ctx.fillText("Score: " + game['score'], canvas.width/2.5, canvas.height/1.7)
}
function right_move(){
if(this['pos_x'] < canvas.width - this['size']){
this['pos_x'] += this['velocity']
}else{
this['pos_x'] = canvas.width - this['size']
}
}
function up_move(){
if(this['pos_y'] > 0){
this['pos_y'] -= this['velocity']
}else{
this['pos_y'] = 0
}
}
function left_move(){
if(this['pos_x'] > 0){
this['pos_x'] -= this['velocity']
}else{
this['pos_x'] = 0
}
}
function down_move(){
if(this['pos_y'] < canvas.height - this['size']){
this['pos_y'] += this['velocity']
}else{
this['pos_y'] = canvas.height - this['size']
}
}
function refresh_block(){
do{
block['pos_x'] = Math.floor(Math.random()*(canvas.width - 10))
}while(block['pos_x']%block['size'] != 0)
do{
block['pos_y'] = Math.floor(Math.random()*(canvas.height - 10))
}while(block['pos_y']%block['size'] != 0)
}
function colision_detect(){
if (player['pos_x'] == block['pos_x'] && player['pos_y'] == block['pos_y']){
refresh_block()
game['score']++
game['time']++
document.getElementById("tempo").innerHTML = game['time']
document.getElementById('score').innerHTML = game['score']
}
}
document.addEventListener("keydown", function(e){
if(game['keys_pressed'] == 0){
start()
refresh_time()
game['keys_pressed']++
}
if(player[e.key]){
player[e.key]()
}
})