We are about to switch to a new forum software. Until then we have removed the registration on this forum.
PImage startscreen; int startscreen; int stage; int base=20; int x,y; int hit = 0; int miss = 0; int changeX=-5; int changeY=-5; int gameOver=0;
void setup(){
stage = 1;
startscreen = LoadImage("2teniscourt.jpg");
img(startscreen,0,0,600,600);
size(600, 600);
x=(int)random(width);
y=height-base;
}
void draw()
{
if(stage==1){
image(startscreen,600,600);
textAlign(CENTER);
textSize(45);
fill(66, 104, 244);
text("PONG GAME",100,170);
text("Press any key to start",100,170);
if (keyPressed == true){
stage = 2;
}
}
if (stage == 2){
background(100,200,100);
}
if(gameOver==0)
{
background(0);
fill(244, 66, 66);
text("hit: " + hit,50,40);
fill(66, 104, 244);
rect(mouseX,height-base,150,base);
ellipse(x,y,40,40);
x=x+changeX;
y=y+changeY;
if(x<0 | x>width)
{
changeX=-changeX;
}
if(y<0)
{
changeY=-changeY;
}
if(y>height-base)
{
//check whether it is falling inside the rectangle or not
if(x>mouseX && x<mouseX+200)
{
changeY=-changeY; //bounce back
hit +=1;
}
else
{
gameOverSplash();
}
}
}
else
{
background(244, 78, 66);
fill(66, 104, 244);
text("Game Over!",width/2,height/2);
text("CLICK MOUSE TO RESTART",width/2,height/2+20);
}
}
void gameOverSplash()
{
gameOver=1;
}
void mouseClicked()
{
changeY=-changeY;
hit +=1;
gameOver=0;
}
Answers
You aren't formatting your code properly for the forums. Edit your post, select your code, and press Ctrl + o.
You have two variables called startscreen. One is an image. The other is a int. You probably only want the image one.
Hello,
I formatted the code a little and made some corrections :
e.g. it's loadImage with a small start letter
and image, not img
I inserted
20
in a few points because that is the radius of the ball (it looks better that way since it is not leaving the screen or overlap / reaching over the border/ paddle).Text align:
text("hit: " + hit, 7, 40);
is now withtextAlign(LEFT);
which looks betterAlso you had one closing } here:
which isolated the whole part after it inside
draw()
from this.Instead we want the
}
at the end of draw().Best, Chrisir ;-)
you have stage 1 and 2
now you can name your stages by saying
and use those like
or
Also, since gameover is also stage you can alter the structure and think of gameover as a 3rd stage:
now you don't need a confusing
if (gameOver==0) {
inside theif (stage == 2) {
anymore...now you can say in draw():
or even use
switch
(you'll need constants for this, see below):BTW
since those names never change (only stage does, not its names) we can mark them as constants (with
final
):you're unbelieveble, thank you so much:)
continued here
https://forum.processing.org/two/discussion/20049/hi-i-want-to-add-the-pong-game-below-a-speed-function-which-makes-game-speed-increase-with-10-hits