-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpingpong.js
More file actions
79 lines (70 loc) · 2.16 KB
/
pingpong.js
File metadata and controls
79 lines (70 loc) · 2.16 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
//aqui eu vou criar a bolinha
let xBolinha = 300;
let yBolinha = 200;
let tamBolinha = 25;
let raioBolinha = tamanhoBolinha/2;
// aqui estou configurando a raquete
let xRaquete = 5;
let yRaquete = 150;
let larguraRaquete = 10;
let alturaRaquete = 90
//config velocidade bolinha
let xvelocidadeBolinha = 6;
let yvelocidadeBolinha = 6;
function setup(){
//aqui vou criar minha "mesa"
createCanvas(600,400);
}
function draw(){
//função responsável pelo "desenho e animação da mesa"
//aqui vou por a cor da "mesa"
//A cor da mesa é em rgb
background(147,112,219);
//chamando a função cria bolinha para criar a bolinha
criaBolinha(xBolinha, yBolinha, tamBolinha);
//chamando a função mov bolinha
moveBolinha();
bolinhaBorda();
//chamando a borda
Borda();
criaRaquete(xRaquete, yRaquete, larguraRaquete, alturaRaquete);
movimentaRaquete();
colideRaquete();
}
//função bolinha
function criaBolinha(xBolinha, yBolinha,tamBolinha){
circle (xBolinha,yBolinha,tamBolinha);
}
//função move bolinha
function moveBolinha(){
xBolinha = xvelocidadeBolinha + xBolinha;
yBolinha = yvelocidadeBolinha + yBolinha;
}
function Borda(){
if (xBolinha > width || xBolinha < 0){
xvelocidadeBolinha *= -1;
}
if (yBolinha > height || yBolinha < 0){
yvelocidadeBolinha *= -1;
}
}
//função responsável por criar o retângulo que representa a raquete
function criaRaquete(xBolinha, yRaquete, larguraRaquete, alturaRaquete){
fill("blue");
rect(xRaquete, yRaquete, larguraRaquete, alturaRaquete);
}
//função responsável por movimentar a raquete
function movimentaRaquete(){
if(keyIsDown(UP_ARROW)){
yRaquete -= 10;
}
if(keyIsDown(DOWN_ARROW)) {
yRaquete += 10;
}
}
//função responsável por quando a bolinha bater na raquete, retornar em direção contrária.
function colideRaquete(){
if(xBolinha - raioBolinha < xRaquete + larguraRaquete && yBolinha - raioBolinha < yRaquete + alturaRaquete && yBolinha + raioBolinha > yRaquete){
xvelocidadeBolinha *=-1;
}
}