error ... Written program doesn't do that what I want. (Ping Pong)

Fist of all, I hope you can understand me, because of my English. I've written a small Program, similar like "Ping Pong". If you start the Program, you have to press "e" and "o", so that a timer is counting. If the game start, the ball takes a route which crosses the bottom of the screen. After that there is a Problem. I suppose it's in line 177 to 188. The paragraph is divided with "-------------- Ballbewegungen" and it's written bold. The ball in this game have to recoil from the bottom, but the ball isn't doing that. I tried to multiply the Y-Speed by -1 but it didn't works. I hope you can help me. It's a School project. Thank you! for the future. Stephan Rosin Here the program code:

int ypos1=270;//Position des Schlägers 1 auf dem Spielfeld in Y-Richtung
int ypos2=270;//Position des Schlägers 2 auf dem Spielfeld in Y-Richtung
int ballgr=30;//Ballgröße
float bxpos=480;//Position des Balles in X-Richtung
float bypos=200;//Position des Balles in y-Richtung
float bxges;//Geschwindigkeit des Balles in X-Richtung
float byges;//Geschwindigkeit des Balles in Y-Richtung
int gess1=10;//die Geschwindigkeit mit der sich die Schläger bewegen
int gess2=10;//Geschwindikeit der Schläger auf dem Spielfeld
int gess=40;//wirkliche geschwindigkeit der Schläger
PImage background;//deklariert die Variable background
PFont font;//erstellt ein Objekt namens font
int color1=180;//deklariert Variable color1 die für die Durchsichtigkeit der Startfelder zuständig ist
int color2s1=180;//deklariert Variable color1 die für die Durchsichtigkeit des Textes der auf den Startfeldern zuständig ist
int color2s2=180;
int color3s1=180;
int color3s2=180;
int colort=180;
int colortr=255;
int colortg=0;
boolean bereit1;
boolean bereit2;
int timer=5;//die Zeit die vor dem Spielanfang ableuft
int currenttime=-1;
int seconds=-1;
boolean stopped = true;
float timerxpos=423;
char timerypos=140;
int timergr=100;
int timerrect=0;
int timerrectx=443;
int timerrectxb=73;




void setup(){
  size (960,540);
  background= loadImage ("background.png");//weist der Variablen background die Datei background.png zu
  font = loadFont("LucidaConsole-100.vlw");//weist der Variablen foto die Datei LucidaConsole-100.vlw zu
}

void draw(){
  image(background,1,1,960,540);//gibt die Variable background auf dem Bildschirm aus  
  textSize(11);
  fill(0);
  text ("X",240,50);
  text (mouseX,250,50);
  text ("Y",290,50);
  text (mouseY,300,50);

  fill(255);
  stroke(0,120);
  line(0,270,960,270);
  line(480,0,480,540);
  stroke(0);
  fill(255,200);
  ellipse(50,ypos1,10,120);
  ellipse(910,ypos2,10,120);
  fill(255,230);
  ellipse(bxpos,bypos,ballgr,ballgr);




  noStroke();
  fill(255,color1);
  rect(width/4-80,155,160,90);
  rect(width/4*3-80,155,160,90);

  fill(255,0,0,color2s1);
  textSize(18);
  text("Wenn du bereit",172,177);
  text("bist, drücke",172,205);
  text("E",172,233);
  if (keyPressed){
    if (key =='e'){
      bereit1=true;
    }
  }
  if (bereit1==true){
    color2s1=0;
    textSize(45);
    fill(255,0,0,color3s1);
    text("Bereit",175,220);

  }


  fill(255,0,0,color2s2);
  textSize(18);
  text("Wenn du bereit",172+width/2,177);
  text("bist, drücke",172+width/2,205);
  text("O",172+width/2,233);
  if (keyPressed){
    if (key =='o'){
      bereit2=true;
    }
  }
  if (bereit2==true){
    color2s2=0;
    textSize(45);
    fill(255,0,0,color3s2);
    text("Bereit",175+width/2,220);
  }

  if (bereit1==true && bereit2==true){
    color1=0;
    color2s1=0;
    color2s2=0;
    color3s1=0;
    color3s2=0;
    stroke(100,timerrect);
    fill(255,timerrect);
    rect(timerrectx,30,timerrectxb,25);

    fill(colortr,colortg,0,colort);
    textSize(timergr);
    text(seconds,timerxpos,timerypos);
    if(!stopped) {
    seconds=(millis()/1000-currenttime-timer);
    }  
  }

  if (seconds>=0){
    timerrect=220;
    colort=255;
    colortr=0;
    colortg=200;
    timerxpos=960/2-5;
    timerypos=50;
    timergr=20;
    if (keyPressed){
      if (key=='w'){
        ypos1-=gess1;
      }
      if (key=='s'){
        ypos1+=gess1;
      }
    }
    if (keyPressed){
      if (key=='p'){
        ypos2-=gess2;
      }
      if (key=='l'){
        ypos2+=gess2;
      }
    }
    if (ypos1<270){
      gess1=ypos1/gess;
    }
    if (ypos1>270){
      gess1=ypos2/gess;
    }
    if (ypos1>520){
      gess1=0;
    }
    textSize(11);
    fill(0);
    text(gess1,100,50);//gibt die Variable gess1 auf dem Bildschirm aus
    text(ypos1,150,50);//gibt die Variable gess1 auf dem Bildschirm aus

    if (ypos2<270){
      gess2=ypos2/20;
    }
    if (ypos2>270){
      gess2=ypos1/20;
    }
    if (ypos2>520){ 
      gess2=0;
    }
    fill(0);
    text(gess2,860,50);//gibt die Variable gess2 auf dem Bildschirm aus
    text(ypos2,810,50);//gibt die Variable ypos1 auf dem Bildschirm aus


   ** //--------------------------------------------Ballbewegungen
    ballgr;//saves the weight of ball in pixels
    bxpos;//Position of the ball in the war of the x-Axis
    bypos;//Position of the ball in the war of the Y-Axis
    bxges=1;//Speed of the ball in the way of the X-Axis
    byges=3;//Speed of the ball in the way of the Y-Axis
    bxpos=bxpos+bxges;
    bypos=bypos+byges;    
    if (bypos>height-ballgr/2){
      byges=byges*-1;
    }
    //---------------------------------------------Ballbewegungen**
    if (seconds>=10){
      timerxpos-=6.666;
    }
    if (seconds>=100){
      timerxpos-=6.666;
    }
    if (seconds>=1000){
      timerxpos-=6.666;
    }

    fill(0);
    textSize(11);
    text(bypos,400,50);
    text(byges,350,50);
  }
}

void keyPressed(){
  if (currenttime==-1){
    if(key=='o'){
      stopped = false;
      currenttime = millis()/1000;
    }
    if(key=='o'){
      stopped = false;
      currenttime = millis()/1000;
    }
  }
}

Answers

  • Please don't use topic titles like this, they make the board a mess. I don't know the solution for your problem now cause i have little time.

    However,

    -please use English comments, i'm sure your teacher doesn't mind and it's good practice. -strip your sketch from using images if they are not needed to illustrate the problem, this saves us time.

    Sorry i'm not really helpful, to help you a little:

    One of your next problems will be multiple keypresses, this can help you: http://wiki.processing.org/w/Multiple_key_presses

    You have to use! import java.awt.event.KeyEvent;

    Also i suggest to read both those: http://processing.org/reference/class.html http://processing.org/tutorials/objects/

    And create a class for the ball. That way it's easier for us (and you) to read your code. And for you to maintain you code or make additional changes (let's say you want to make phong with more then 1 ball).

  • in line 182 you set the value so the multiply -1 never gets to line 184

    pls move line 181 and 182 into setup()

  • also later you might encounter a stutter effect : byges can become negative and positive abwechselnd very fast

    better than multiply -1

    is therefore (line 186)

    byges=abs(byges)*-1;

    this always gives a negative result (abs always gives positive result in the hight byges has (the distance to zero if you like))

  • Firstly thank you Chrisir for solving my problem. You were a great help!

  • you're welcome!

Sign In or Register to comment.