Helllllo there! I've some problems with the sound. This is a Snake game code and I added a sound for the eaten food and one for the Game Over. While the first one works, the sound of the Game Over plays firstly correctly and afterwards horribly distorted :( What do I do wrong?
ArrayList x = new ArrayList(), y = new ArrayList();
int w = 27, h = 27, bs = 28, dir = 2, foodx = 12, foody = 10;
int[] dx = {
0, 0, 1, -1
}
, dy = {
1, -1, 0, 0
};
boolean gameover = false;
PFont font;
import processing.sound.*;
SoundFile file;
SoundFile file2;
void setup() {
size (756, 756);
smooth();
x.add(5);
y.add(5);
}
void draw() {
background(38);
for (int i = 0; i < w; i++) line(i*bs, 0, i*bs, height);
for (int i = 0; i < h; i++) line(0, i*bs, width, i*bs);
for (int i = 0; i < x.size (); i++) {
fill(0, 255, 255);
noStroke();
rect(x.get(i)*bs, y.get(i)*bs, bs, bs);
}
if (!gameover) {
fill(255, 0, 0);
rect(foodx*bs, foody*bs, bs, bs);
if (frameCount%5==0) {
x.add(0, x.get(0) + dx[dir]);
y.add(0, y.get(0) + dy[dir]);
if (x.get(0) < 0) {
x.set(0, w);
}
if (y.get(0) < 0) {
y.set(0, h);
}
if (x.get(0) > w) {
x.set(0, 0);
}
if (y.get(0) > h) {
y.set(0, 0);
}
for (int i = 1; i < x.size (); i++) if (x.get(0)==x.get(i) && y.get(0) == y.get(i)) gameover = true;
if (x.get(0)==foodx && y.get(0)==foody) {
foodx = (int)random(0, w);
foody = (int)random(0, h);
file = new SoundFile(this, "Eat food.mp3");
file.play();
} else {
x.remove(x.size()-1);
y.remove(y.size()-1);
}
}
} else {
x.clear();
y.clear();
x.add(5);
y.add(5);
gameover = false;
}
int curTimeMs = millis();
if (curTimeMs > 20000) {
gameover = true;
file2 = new SoundFile(this, "game over arcade.mp3");
file2.play();
background(0);
font = createFont("Joystix", 50);
textFont(font);
fill(0, 255, 255);
textAlign(CENTER);
text("Game" + "\nOver", width/2, height/2);
}
}
void keyPressed() {
int newdir = keyCode==DOWN ? 0 : (keyCode==UP ? 1 : (keyCode==RIGHT ? 2 : (keyCode==LEFT ? 3 : -1)));
if (newdir != -1 && (x.size()