Repository navigation
Expand file tree
/
Copy pathsketch.js
More file actions
181 lines (157 loc) · 3.64 KB
/
Copy pathsketch.js
File metadata and controls
181 lines (157 loc) · 3.64 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
//------------------------------- Variaveis -------------------------------//
//Variaveis da Bolinha
let xBolinha = 300;
let yBolinha = 200;
let diametro = 22;
let raio = diametro / 2;
//Velocidade da Bolinha
let veloXBall = 6;
let veloYBall = 6;
//Variaveis da Minha Raquete
let xRaquete = 5;
let yRaquete = 150;
let larguraRaquete = 13;
let alturaRaquete = 90;
let bordaRaquete = 5;
let veloYRaquete = 5;
//Variaveis da Raquete Oponente
let xRaqueteOponente = 583;
let yRaqueteOponente = 150;
let larguraRaqueteOponente = 13;
let alturaRaqueteOponente = 90;
let veloYRaqueteOponente;
//Variaveis de Pontos
let meusPontos = 0;
let pontosOponente = 0;
let chanceErro = 0;
//Sons do jogo
let raquetada;
let ponto;
let trilha;
//--------------------------- Functions -------------------------------------//
function preload(){
trilha = loadSound("trilha.mp3");
raquetada = loadSound("raquetada.mp3");
ponto = loadSound("ponto.mp3");
}
function setup() {
createCanvas(600, 400);
trilha.loop();
}
function draw() {
background(0);
mostraBolinha();
movimentaBolinha();
verificaColisaoBorda();
mostraRaquete(xRaquete, yRaquete);
movimentaMinhaRaquete();
verificaColisaoRaquete();
mostraRaquete(xRaqueteOponente, yRaqueteOponente);
movimentaRaqueteOponente();
verificaColisaoRaqueteOponente();
incluiPlacar();
marcaPonto();
limiteRaquete();
bolinhaNaoFicaPresa();
}
function mostraBolinha(){
circle(xBolinha,yBolinha,diametro);
}
function movimentaBolinha(){
xBolinha += veloXBall;
yBolinha += veloYBall;
}
function verificaColisaoBorda(){
if (xBolinha + raio > width || xBolinha - raio < 0){
veloXBall *= -1;
}
if (yBolinha + raio > height || yBolinha - raio < 0){
veloYBall *= -1;
}
}
function mostraRaquete(x, y){
rect(x, y, larguraRaquete, alturaRaquete, bordaRaquete);
}
function movimentaMinhaRaquete(){
if (keyIsDown(UP_ARROW)){
yRaquete -= 10;
}
if (keyIsDown(DOWN_ARROW)){
yRaquete += 10;
}
}
function verificaColisaoRaquete(){
if (xBolinha - raio < xRaquete + larguraRaquete
&& yBolinha - raio < yRaquete + alturaRaquete
&& yBolinha + raio > yRaquete){
veloXBall *= -1;
raquetada.play();
}
}
function movimentaRaqueteOponente(){
veloYRaqueteOponente = yBolinha - yRaqueteOponente - alturaRaqueteOponente / 2 - 30;
yRaqueteOponente += veloYRaqueteOponente;
calculaChanceErro();
}
function verificaColisaoRaqueteOponente(){
if (xBolinha + raio > xRaqueteOponente
&& yBolinha - raio < yRaqueteOponente + alturaRaqueteOponente
&& yBolinha + raio > yRaqueteOponente){
veloXBall *= -1;
raquetada.play();
}
}
function incluiPlacar(){
textSize(25);
textAlign(CENTER);
fill(color(150, 0, 150));
rect(230, 8, 40, 25, 10);
fill(255);
text(meusPontos, 250, 30);
fill(color(150, 0, 150));
rect(330, 8, 40, 25, 10);
fill(255);
text(pontosOponente, 350, 30);
}
function marcaPonto(){
if(xBolinha > 590){
meusPontos += 1;
ponto.play();
}
if(xBolinha < 10){
pontosOponente += 1;
ponto.play();
}
}
function limiteRaquete(){
if (yRaquete < 0) {
yRaquete = 0;
}
else if (yRaquete > 400 - alturaRaquete) {
yRaquete = 400 - alturaRaquete;
}
else if (yRaqueteOponente < 0) {
yRaqueteOponente = 0;
}
else if (yRaqueteOponente > 400 - alturaRaquete) {
yRaqueteOponente = 400 - alturaRaquete;
}
}
function calculaChanceErro() {
if (pontosOponente >= meusPontos) {
chanceErro += 1
if (chanceErro >= 39){
chanceErro = 40
}
} else {
chanceErro -= 1
if (chanceErro <= 35){
chanceErro = 35
}
}
}
function bolinhaNaoFicaPresa(){
if (XBolinha - raio < 0){
XBolinha = 23
}
}