We are about to switch to a new forum software. Until then we have removed the registration on this forum.
import ddf.minim.*; //sound
Minim minim;
AudioPlayer player1, player2, player3, player4;
AudioInput input;
PImage bg, sball;
float goalKeeper;
float xspeed = 4;
float ballX= 170;
float ballY= 170;
float ballYspeed=0;
int score=0;
int miss=3;
float goalieL=20;
void setup()
{
size (600, 600);
bg = loadImage("SoccerField.png");
sball = loadImage("ball.jpg");
minim = new Minim(this);
player1 = minim.loadFile("kick.wav");
goalKeeper=170;
}
void draw()
{
if (mousePressed) {
player1.play(0);
}
background(bg);
fill(255, 255, 255);
textAlign(CENTER);
textSize(10);
textSize(40);
text("SCORE: "+score, 90, 520);
text("LIVES: " + miss, 80, 580);
stroke(255, 0, 0);
strokeWeight(4);
//goalie moving back and forth
line(goalKeeper, 100, goalKeeper+goalieL, 100);
if (goalKeeper >= 350-goalieL)
{
xspeed *= -1;
goalKeeper= 200-goalieL;
}
if ( goalKeeper <= 80)
{
xspeed *= -1;
}
goalKeeper=goalKeeper+xspeed;
//soccer ball
imageMode(CENTER);
image( loadImage("ball.png"), 300, ballY, 50, 30);
ballY=ballY+ballYspeed;
// println(ballY + ", " + ballYspeed);
if (ballY <=90)
{
ballYspeed = 0;
ballY = 170;
if (goalKeeper <= 200 && goalKeeper >= 200-goalieL)
{
miss--;
} else
{
score=score+1;
if ( abs(xspeed) < 20)
{
xspeed=xspeed*1.1;
goalieL= goalieL+1;
}
}
}
if (miss==0)
{
fill(35, 193, 34);
rect(0, 0, width, height);
fill(255, 255, 255);
text("End Game", 300, 200);
text ("Reset: Spacebar", 300, 250);
text("Score: " + score, 300, 300);
noLoop();
}
}
void mousePressed()
{
ballYspeed = -7;
}
void keyPressed()
{
if (key == ' ')
{
loop();
ballY = 170;
goalieL=20;
score=0;
miss=3;
xspeed=4;
goalKeeper=width/2;
}
}
Answers
You'll get a better response if you format your code. Here's how:
http://forum.processing.org/two/discussion/8045/how-to-format-code-and-text
Thanks i just fixed it
Have a marker, a bool var thereWasAGoal
If so reset the pos
Use sball instead of loadImage() in line 62 - you're loading the image 60 times a second despite having already loaded it in setup()