This site requires JavaScript, please enable it in your browser!
Greenfoot back
SASCHA
SASCHA wrote ...

2013/4/16

Tor Programieren HILFEEEEEEE

SASCHA SASCHA

2013/4/16

#
Der Ball geht überall ins Tor, wir wollen es aber natürlich begrenzen! HELP!! public void act(){ //player.setLocation(player.getX(),getY()); ball.setLocation(ball.getX() + dx, ball.getY() + dy); if(ball.getY() >= computer.getY() - 25 && ball.getY() <= computer.getY() + 25 && ball.getX() + dx >= computer.getX()){ dx = -dx; dy = (ball.getY() - computer.getY())/2; Greenfoot.playSound("beep.wav"); } if(ball.getY() >= player.getY() - 25 && ball.getY() <= player.getY() + 25 && ball.getX() + dx <= player.getX()){ dx = -dx; dy = (ball.getY() - player.getY())/2; Greenfoot.playSound("beep.wav"); } if(ball.getY() <= 5){ ball.setLocation(ball.getX(),6); dy = - dy; } if(ball.getY() >= this.getHeight() - 5 ){ ball.setLocation(ball.getX(),this.getHeight() -6); dy = -dy; } else if(ball.getX() <= 5 || ball.getX() >= this.getWidth() -5){ if(ball.getX() <= 5){ c++; cScore = String.valueOf(c); read2.clear(); read2.drawString(cScore,2,20); s2.setImage(read2); } else if( ball.getX() >= this.getWidth() -5){ p++; pScore = String.valueOf(p); read1.clear(); read1.drawString(pScore,2,20); s1.setImage(read1); } if(p == 4 || c == 4){ Greenfoot.stop(); } ball.setLocation(getWidth()/2, getHeight()/2); player.setLocation(50, getHeight()/2); computer.setLocation(getWidth()-50 , getHeight()/2); dy = 0; // Codes von anderm Spiel übernommen! } DANKE IM VORAUS!!!!
Gevater_Tod4711 Gevater_Tod4711

2013/4/16

#
This is an english webside so you should try writing in english. But however: If I have understood your code right the problem is in this part:
if(ball.getX() <= 5 || ball.getX() >= this.getWidth() -5){ 
    if(ball.getX() <= 5){

    }
}
You just check whether the balls X location is correct. You also have to check the Y coordinates to make the goal smaler. Otherwhile the goal will be the whole left or right side. Another suggestion: Underneath the text editor you can find a link 'code'. If you want to add parts of your code you should do this using this link because that will make your code look more clear.
Game/maniac Game/maniac

2013/4/16

#
Kannst du mir zeigen die ganze Klasse bitte
SASCHA SASCHA

2013/4/18

#
Thank you first!! How i can change the code? and here the complete Code.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Table here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Table  extends World
{
    int dx = 10;
    int dy = 0;
    Player player = new Player();
    Computer computer = new Computer();
    Ball ball = new Ball();
    Score1 s1 = new Score1();
    Score2 s2 = new Score2();
    GreenfootImage read1 = new GreenfootImage(100,30);
    GreenfootImage read2 = new GreenfootImage(100,30);
    int p = 0;
    int c = 0;
    String pScore = String.valueOf(p);
    String cScore = String.valueOf(c);
    
    
    public Table()
    {    
        super(500,365, 1);
        addObject(player, 50, getHeight()/2);
        addObject(computer, getWidth()-50 , getHeight()/2);
        addObject(ball, getWidth()/2, getHeight()/2);
        read1.drawString(pScore,2,20);
        s1.setImage(read1);
        addObject(s1,getWidth()/4,50);
        read2.drawString(cScore, 2, 20);
        s2.setImage(read2);
        addObject(s2,(getWidth()/4)*3,50);
        Greenfoot.setSpeed(45);
    }
    public void act(){
        //player.setLocation(player.getX(),getY());
        
        ball.setLocation(ball.getX() + dx, ball.getY() + dy);
        
        if(ball.getY() >= computer.getY() - 25 && ball.getY() <= computer.getY() + 25 && ball.getX() + dx >= computer.getX()){
            dx = -dx;
            dy = (ball.getY() - computer.getY())/2;
            Greenfoot.playSound("beep.wav");
        }
        if(ball.getY() >= player.getY() - 25 && ball.getY() <= player.getY() + 25 && ball.getX() + dx <= player.getX()){
            dx = -dx;
            dy = (ball.getY() - player.getY())/2;
            Greenfoot.playSound("beep.wav");
        }
        if(ball.getY() <= 5){
            ball.setLocation(ball.getX(),6);
            dy = - dy;
        }
        if(ball.getY() >= this.getHeight() - 5 ){
            ball.setLocation(ball.getX(),this.getHeight() -6);
            dy = -dy;
        }
        else if(ball.getX() <= 5 || ball.getX() >= this.getWidth() -5){
                if(ball.getX() <= 5){
                c++;
                cScore = String.valueOf(c);
                read2.clear();
                read2.drawString(cScore,2,20);
                s2.setImage(read2);
            }
            else if( ball.getX() >= this.getWidth() -5){
                p++;
                pScore = String.valueOf(p);
                read1.clear();
                read1.drawString(pScore,2,20);
                s1.setImage(read1);
            }
            if(p == 4 || c == 4){
                Greenfoot.stop();
            }
            ball.setLocation(getWidth()/2, getHeight()/2);
            player.setLocation(50, getHeight()/2);
            computer.setLocation(getWidth()-50 , getHeight()/2);
            dy = 0;
            
                
        }
    }
}
Gevater_Tod4711 Gevater_Tod4711

2013/4/18

#
In line 63, 64 you are just checking whether the X component of the actors location is in a certain area. You also have to check the Y component. So after line 64 you should add some code like this:
if (ball.getY() > getWorld().getHeight()/2 - 100 && ball.getY() < getWorld().getHeight()/2 + 100) {
    //now your goal has a size of 200 pixels;
}
else {
   //if the ball hasn't reached the goal it should bounce off;
}
You will have to do the same after line 71. If you want your goal to get bigger you have to use a greater value than 100. For a smaler goal a smaler value.
You need to login to post a reply.