-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsketch.js
More file actions
133 lines (111 loc) · 2.92 KB
/
sketch.js
File metadata and controls
133 lines (111 loc) · 2.92 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
let player;
let ball;
let computer;
var pongballs = [];
var courtSound;
var racketSound;
var scoreSound;
function preload() {
courtSound = loadSound("sounds/court.mp3");
racketSound = loadSound("sounds/racket.mp3");
scoreSound = loadSound("sounds/points.mp3");
battleSound = loadSound("sounds/battle.mp3");
noiceSound = loadSound("sounds/noice.mp3");
awwSound = loadSound("sounds/aww.mp3");
}
function setup() {
// put setup code here
mode = 0;
createCanvas(700, 650);
player = new Player();
ball = new Ball;
computer = new Computer;
textAlign(CENTER);
textSize(50);
scoreSound.setVolume(0.5);
battleSound.setVolume(0.2);
battleSound.play();
/* generate the pong balls */
for (let i = 0; i < 5; i++) {
pongballs.push(new Ball());
}
}
//this function keeps updating
function draw() {
// put drawing code here
clear();
if (mode == 0) {
//battleSound.stop();
textSize(21);
text("press ENTER to start ", width/2, height/2);
}
if (mode == 1) {
/*draw background and player*/
fill(1000, 1000, 1000, 1000);
background(0);
textSize(48);
text(player.points, 30, 40);
text(computer.points, width - 80, 40);
player.move();
player.show();
ball.move();
ball.show();
/*draw computer*/
fill(255, 0, 0)
computer.show();
computer.move(ball);
/*draw set line*/
fill(1000, 1000, 1000, 1000);
rect(width / 2, 600, 10, 1200);
/*draw score board*/
fill(1000, 1000, 1000, 1000);
text(player.score + " " + computer.score, width / 2, 80);
/* handle collisions */
if (ball.collision(computer)) // if ball and computer collide
{
racketSound.play();
ball.vx = -4; //set speed to negative so that it goes in the opposite direction
}
if (ball.collision(player)) // if ball collides w/ player
{
racketSound.play();
ball.vx = 4; //set speed to positive so that it goes in the opposite direction
}
/* handle score */
if (ball.x > width) {
player.score++;
scoreSound.play();
newGame();
}
if (ball.x < 0) {
computer.score++;
scoreSound.play();
newGame();
}
}
}
/* new game set */
function newGame() {
if (pongballs.length > 1) {
ball = pongballs.pop(); //throw balls
} else {
background(0);
fill(0, 0, 0);
battleSound.stop();
window.location.reload();
if (computer.score > player.score) {
awwSound.play();
window.alert("You lose. Pathetic." + "\n" + "\n" + "Your final score: " + player.score + "\n" + "Computer's final score: " + computer.score + "\n" + "\n" );
}
else if (computer.score < player.score) {
noiceSound.play();
window.alert("You win! " + "\n" + "\n" + "Your final score: " + player.score + "\n" + "Computer's final score: " + computer.score + "\n" + "\n" );
}
}
}
/* start game */
function keyPressed() {
if (keyCode === ENTER) {
mode = 1;
}
}