-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
199 lines (174 loc) · 4.98 KB
/
index.js
File metadata and controls
199 lines (174 loc) · 4.98 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
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
188
189
190
191
192
193
194
195
196
197
198
199
let game = false
let fezPonto = false
let bola
let players = []
let winner = null
var tempo
//diametro da bola
const diametro = 16
const raio = diametro / 2
//dimensoes do jogador
const largura = 20
const altura = 60
const tempoAnimacao = 1000
function preload(){
loadFont('fonts/Ubuntu-Regular.ttf');
}
function setup(){
createCanvas(1000,400)
bola = new Bola(width/2, height/2)
players.push(new Player(largura, width/2, "Jogador 1"))
players.push(new Player(width - largura*2, width/2, "Bot"))
background(0)
fill(255)
stroke(255)
tempo = millis()
bola.start()
}
function draw(){
if(game) {
background(0)
for(let i = 0; i < players.length; i++) {
rect(players[i].position.x, players[i].position.y, largura, altura)
detectaColisao(players[i], i)
}
fill(255)
stroke(255)
textStyle(NORMAL)
if(millis() - tempo >= tempoAnimacao){
tempo = millis()
fezPonto = false
}
escrevePontos(players)
line(width/2, height - largura, width/2, largura)
circle(bola.position.x, bola.position.y, diametro) //bola
updateBot(players[1])
bola.update()
updatePlayer(players[0])
} else {
if(winner != null){
winnerText(winner)
} else {
startText()
}
}
}
function mouseMoved(){
players[0].velocity = 5
players[0].goto = mouseY - altura/2
if(players[0].position.y - players[0].goto < 0){
players[0].velocity = abs(players[0].velocity)
return true
}else if(players[0].position.y - players[0].goto > 0){
players[0].velocity = abs(players[0].velocity) * -1
return true
}else{
return false
}
}
function mouseClicked() {
if(!game) {
winner = null
game = true
bola.start()
players[1].pontos.val = 0
players[0].pontos.val = 0
}
}
function updateBot(bot){
bot.goto = bola.position.y - altura/2
if(bot.position.y - bot.goto < 0){
bot.velocity = abs(bot.velocity)
}else if(bot.position.y - bot.goto > 0){
bot.velocity = abs(bot.velocity) * -1
}
if(abs(bola.position.x - bot.position.x) < 150){
bot.position.y += bot.velocity
}
if(bot.position.y < 20)
bot.position.y = 20
if(bot.position.y > height - (altura + 20))
bot.position.y = height - (altura + 20)
}
function updatePlayer(player){
if(abs(player.goto - player.position.y) < 10) {
player.velocity = 0
}
player.position.y += player.velocity
if(player.position.y < 20)
player.position.y = 20
if(player.position.y > height - (altura + 20))
player.position.y = height - (altura + 20)
}
function detectaColisao(player, i) {
let bordaEsquerda = player.position.x
let bordaDireita = player.position.x + largura;
let bordaTop = player.position.y
let bordaBottom = player.position.y + altura;
let r2 = [3, 4, 5, 6, 7, 8]
let r = map(random(), 0, 1, 0, 5)
r = Math.ceil(r)
r = r2[r]
if(bola.position.x + raio > bordaEsquerda)
if(bola.position.x - raio < bordaDireita)
if(bola.position.y + raio > bordaTop)
if(bola.position.y - raio < bordaBottom) {
bola.velocity.x *= -1
if(i == 0)
bola.velocity.x = r
if(i == 1)
bola.velocity.x = r * -1
bola.update()
}
}
function escrevePontos() {
textSize(32)
fill(255)
for(let i = 0; i < players.length; i++) {
let pontos = players[i]["pontos"]
if(i == 0)
if(fezPonto) {
//tempo = millis()
pontos["posX"] = width/4 + random(-3, 3)
pontos["size"] = 40
} else {
pontos["posX"] = width/4
pontos["size"] = 32
}
if(i == 1)
if(fezPonto) {
//tempo = millis()
pontos["posX"] = (width - width/4) + random(-3, 3)
pontos["size"] = 40
} else {
pontos["posX"] = width - width/4
pontos["size"] = 32
}
text(pontos["val"].toString(), pontos["posX"], pontos["size"])
}
}
function startText(){
background(120)
fill(255)
stroke(0)
strokeWeight(1)
textFont("Ubuntu-Regular")
textSize(96)
textStyle(ITALIC)
text("PONG", width / 3 , height / 4)
textSize(24)
text("Click anywhere to start", width / 3, height / 2)
}
function winnerText(name) {
background(120)
fill(255)
stroke(80)
strokeWeight(1)
textFont("Ubuntu-Regular")
textSize(96)
text("WINNER \n\t", largura, 100)
textSize(72)
text(name, largura, 200)
textSize(24)
text("Click anywhere to restart", largura, height / 1.5)
}