-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsketch.js
More file actions
152 lines (124 loc) · 2.87 KB
/
sketch.js
File metadata and controls
152 lines (124 loc) · 2.87 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
//variáveis da bolinha
let xBolinha = 100
let yBolinha = 200
let diametro = 15
//velocidade da bolinha
let velocidadeXBolinha = 6;
let velocidadeYBolinha = 6;
let raio = diametro/2
// variaveis da raquete
let xRaquete = 5
let yRaquete = 150
let raqueteComprimento = 10
let raqueteAltura = 90
// variaveis do oponente
let xRaqueteOponente = 585;
let yRaqueteOponente = 150;
let raqueteComprimentoOponente = 10
let raqueteAlturaOponente = 90
//placar do jogo
let meusPontos = 0;
let pontosDoOponente = 0;
//sons do jogo
let raquetada;
let ponto;
let trilha;
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();
verificaColisãoBorda();
mostraRaquete();
movimentaMinhaRaquete();
verificaColisaoRaquete();
mostraRaqueteOponente();
movimentaRaqueteOponente();
colisaoRaqueteOponente();
incluiPlacar();
marcaPonto();
}
function mostraBolinha (){
circle(xBolinha, yBolinha, diametro);
}
function movimentaBolinha(){
xBolinha += velocidadeXBolinha;
yBolinha += velocidadeYBolinha;
}
function verificaColisãoBorda(){
if (xBolinha + raio > width || xBolinha - raio <0) {
velocidadeXBolinha *= -1
}
if (yBolinha + raio > height|| yBolinha - raio < 0) {
velocidadeYBolinha *= -1
}
}
function mostraRaquete(){
rect (xRaquete, yRaquete, raqueteComprimento, raqueteAltura);
}
function movimentaMinhaRaquete(){
if (keyIsDown(UP_ARROW)){
yRaquete -= 10;
}
if (keyIsDown(DOWN_ARROW)){
yRaquete += 10;
}
}
function verificaColisaoRaquete(){
if (xBolinha - raio < xRaquete + raqueteComprimento
&& yBolinha - raio < yRaquete + raqueteAltura
&& yBolinha + raio > yRaquete){
velocidadeXBolinha *= -1;
raquetada.play();
}
}
function mostraRaqueteOponente(){
rect (xRaqueteOponente, yRaqueteOponente, raqueteComprimentoOponente, raqueteAlturaOponente);
}
function movimentaRaqueteOponente(){
if (keyIsDown(87)){
yRaqueteOponente -= 10;
}
if (keyIsDown(83)){
yRaqueteOponente += 10;
}
}
function colisaoRaqueteOponente(){
if (xBolinha + raio > xRaqueteOponente
&& yBolinha - raio < yRaqueteOponente + raqueteAlturaOponente
&& yBolinha - raio > yRaqueteOponente){
velocidadeXBolinha *= -1;
raquetada.play();
}
}
function incluiPlacar(){
stroke (255);
textAlign(CENTER);
textSize (18);
fill(color(255, 140, 0));
rect (150,10, 40, 20);
fill (255);
text ( meusPontos, 170, 26);
fill(color(255, 140, 0));
rect (450,10, 40, 20);
fill (255);
text ( pontosDoOponente, 470, 26);
}
function marcaPonto(){
if (xBolinha > 594){
meusPontos += 1;
ponto.play();
}
if (xBolinha < 10){
pontosDoOponente +=1;
ponto.play();
}
}