-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
219 lines (179 loc) · 4.21 KB
/
sketch.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//variaveis da Bolinha
let xBolinha = 300;
let yBolinha = 200;
let diametro = 20;
let raio = diametro / 2;
//variaveis da velocidade de Bolinha
let velocidadexBolinha = 6;
let velocidadeyBolinha = 6;
let raqueteComprimento = 10;
let raqueteLargura = 90;
//variaveis da raquete
let xRaquete = 5;
let yRaquete = 150;
let velocidadeyOponente;
//variaveis da raquete do oponente
let xRaqueteOponente = 585;
let yRaqueteOponente = 150;
let colidiu = false;
//placar do jogo
let meusPontos = 0;
let pontosOponente = 0;
//sons do jogos
let raquetada;
let ponto;
let trilha;
let ceu;
let chanceDeErrar = 0;
function preload() {
trilha = loadSound("trilha.mp3");
ponto = loadSound("ponto.mp3");
raquetada = loadSound("raquetada.mp3")
ceu = loadImage("ceu.webp")
}
function setup() {
createCanvas(600, 400);
trilha.loop();
}
function draw() {
background(ceu);
mostraBolinha();
movimentaBolinha();
verificaColisaoBorda();
mostraRaquete(xRaquete,yRaquete);
movimentaMinhaRaquete();
verificaColisaoRaquete(xRaquete,yRaquete);
mostraRaquete(xRaqueteOponente,yRaqueteOponente);
movimentaRaqueteOponente();
verificaColisaoRaquete(xRaqueteOponente,yRaqueteOponente);
incluiPlacar();
marcaPonto();
calculaChanceDeErrar();
bolinhaNaoFicaPresa();
gameOver()
}
function mostraBolinha() {
circle(xBolinha, yBolinha, diametro);
}
function movimentaBolinha() {
xBolinha += velocidadexBolinha;
yBolinha += velocidadeyBolinha;
}
function verificaColisaoBorda() {
if(xBolinha + raio > width ||
xBolinha - raio < 0) {
velocidadexBolinha *= -1;
}
//width é a borda do deixo x
if(yBolinha + raio > height ||
yBolinha - raio < 0) {
velocidadeyBolinha *= -1;
}
//heigth é a borda do eixo y
//+= é o valor de umavariavel somado ao valor da próxima varivel de uma forma abreviada !
}
function mostraRaquete(x,y) {
rect(x,y,raqueteComprimento,raqueteLargura);
}
function mostraRaqueteOponente() {
rect(xRaqueteOponente,yRaqueteOponente,raqueteComprimento,raqueteLargura);
}
function movimentaMinhaRaquete(){
if(keyIsDown(UP_ARROW)) {
yRaquete -= 10;
}
if (keyIsDown(DOWN_ARROW)) {
yRaquete += 10;
}
}
function verificaColisaoRaquete() {
if(xBolinha - raio < xRaquete + raqueteComprimento
&& yBolinha - raio < yRaquete + raqueteLargura
&& yBolinha + raio > yRaquete) {
velocidadexBolinha *= -1;
raquetada.play();
}
}
function verificaColisaoRaquete(x,y) {
colidiu =
collideRectCircle(x, y, raqueteComprimento, raqueteLargura, xBolinha, yBolinha, raio);
if(colidiu) {
velocidadexBolinha *= -1;
raquetada.play();
}
}
function movimentaRaqueteOponente(){
velocidadeyOponente = yBolinha - yRaqueteOponente - raqueteComprimento / 2 - 30;
yRaqueteOponente += velocidadeyOponente + chanceDeErrar;
calculaChanceDeErrar();
}
function incluiPlacar() {
textAlign(CENTER,CENTER);
textSize(16);
fill(color(255,140,0));
rect(130, 14, 40 ,20);
fill(255);
stroke("White");
text(meusPontos, 150, 26);
fill(color(255,140,0));
rect(430, 14, 40 ,20);
fill(255);
stroke("White");
text(pontosOponente, 450, 26);
}
function marcaPonto() {
if(xBolinha > 590) {
meusPontos += 1;
ponto.play();
}
if(xBolinha < 10) {
pontosOponente += 1;
ponto.play();
}
}
function calculaChanceDeErrar() {
if (pontosOponente >= meusPontos) {
chanceDeErrar += 1
if (chanceDeErrar >= 39){
chanceDeErrar = 40
}
} else {
chanceDeErrar -= 1
if (chanceDeErrar <= 35){
chanceDeErrar = 35
}
}
}
function bolinhaNaoFicaPresa(){
if (xBolinha - raio < 0){
xBolinha = 23;
}
}
function gameOver(){
if(pontosOponente > 4){
velocidadexBolinha = 0;
velocidadeyBolinha = 0;
xBolinha = 300;
yBolinha = 200;
clear();
background(0);
textSize(90);
fill(("lightgreen"));
stroke("green");
text("Você Perdeu !!!",300,200);
textAlign(CENTER,CENTER);
}
if(meusPontos > 4){
velocidadexBolinha = 0;
velocidadeyBolinha = 0;
xBolinha = 300;
yBolinha = 200;
clear();
background(0);
textSize(90);
fill(("lightgreen"));
stroke("green");
text("Você Ganhou !!!",300,200);
textAlign(CENTER,CENTER);
}
}