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

2019/4/30

Need Help with falling object and making it rise smoothly when I click on it.

1
2
rycode rycode

2019/4/30

#
I am trying to create a game where an object falls but you have to save it from falling by clicking it. (Kind of like "Keep it up.") Here is my code. {World}
import greenfoot.*;

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{
    private int timer =  1800;
    Text timerText = new Text();
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        addObject(timerText, 100, 15);
        timerText.setText("Time Left" + (timer/60));

        prepare();
    }


    public void act()
    {
        if (timer>0)
        {
            timer--;
            if (timer%60==0) timerText.setText("Time Left" + (timer/60));
            if(timer == 0) Greenfoot.stop();
        }
    }

    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        Ball ball = new Ball();
        addObject(ball, 301, 217);
        Ground ground = new Ground();
        addObject(ground, 10, 398);
        Ground ground2 = new Ground();
        addObject(ground2, 50, 395);
        Ground ground3 = new Ground();
        addObject(ground3, 76, 396);
        Ground ground4 = new Ground();
        addObject(ground4, 104, 399);
        Ground ground5 = new Ground();
        addObject(ground5, 141, 397);
        Ground ground6 = new Ground();
        addObject(ground6, 128, 399);
        Ground ground7 = new Ground();
        addObject(ground7, 178, 399);
        Ground ground8 = new Ground();
        Ground ground9 = new Ground();
        Ground ground10 = new Ground();
        addObject(ground10, 207, 398);
        Ground ground11 = new Ground();
        addObject(ground11, 178, 397);
        Ground ground12 = new Ground();
        addObject(ground12, 238, 398);
        Ground ground13 = new Ground();
        addObject(ground13, 263, 398);
        Ground ground14 = new Ground();
        addObject(ground14, 283, 398);
        Ground ground15 = new Ground();
        addObject(ground15, 310, 399);
        Ground ground16 = new Ground();
        addObject(ground16, 333, 398);
        Ground ground17 = new Ground();
        addObject(ground17, 363, 398);
        Ground ground18 = new Ground();
        addObject(ground18, 392, 395);
        Ground ground19 = new Ground();
        addObject(ground19, 421, 397);
        Ground ground20 = new Ground();
        addObject(ground20, 435, 399);
        Ground ground21 = new Ground();
        addObject(ground21, 462, 392);
        Ground ground22 = new Ground();
        addObject(ground22, 478, 399);
        Ground ground23 = new Ground();
        addObject(ground23, 503, 398);
        removeObject(ground22);
        removeObject(ground21);
        Ground ground24 = new Ground();
        addObject(ground24, 457, 399);
        Ground ground25 = new Ground();
        addObject(ground25, 485, 399);
        Ground ground26 = new Ground();
        addObject(ground26, 522, 399);
        Ground ground27 = new Ground();
        Ground ground28 = new Ground();
        addObject(ground28, 552, 396);
        Ground ground29 = new Ground();
        addObject(ground29, 580, 398);
        Ground ground30 = new Ground();
        addObject(ground30, 594, 398);
        ball.setLocation(287, 18);
        Intro intro = new Intro();
        addObject(intro, 298, 181);
        intro.setLocation(288, 190);
    }
}
{Ball}
import greenfoot.*;

/**
 * Write a description of class Ball here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Ball extends Actor
{
  private int score = 0;
  private final int GRAV = 1;
  private int gravity = 0;
  private int velocity;
  private double speed = -9.0;
  public Ball(){
      velocity = 0;
      getImage().scale(50,50);
    }
    
    /**
     * Act - do whatever the Ball wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
        //moveAround();
        fall();
        if (Greenfoot.mouseClicked(this))
        {
            score++;
             //int amo = -30 + Greenfoot.getRandomNumber(70);
            //turn(amo);
            //jump();
            setLocation (getX(), getY()-120);
         
        }
      setLocation(getX()+5, getY());
        int x = getX();
int y = getY();
int ww = getWorld().getWidth();
int wh = getWorld().getHeight();
if (x == 0) setLocation(ww-2, y);
if (x == ww-1) setLocation(1, y);
if (y == 0) setLocation(x, wh-2);
if (y == wh-1) setLocation(x, 1);
    }    
    public void moveAround()
    {
        move(6);
        if (Greenfoot.getRandomNumber(100)<10)
        {
            turn(Greenfoot.getRandomNumber(90) - 45);
        }
        if (getX() <= 5  | getX() >= getWorld().getWidth() - 5)
        {
            turn(180);
        }
        if (getY() <= 5 | getY() >= getWorld().getHeight() - 5)
        {
            turn(180);
        }
}
 public void tryToCatch()
    {
        
        if (Greenfoot.mouseClicked(this))
        {
            ((Counter)getWorld().getObjects(Counter.class).get(0)).add(1);
            //getWorld().removeObject(actor);
            //Greenfoot.stop();
          
        }
    }
    public void jump(){
    
        gravity -= 20;
    }
     public void fall(){
        setLocation(getX(), getY() + velocity);
        if (getY() > getWorld().getHeight() - getImage().getHeight()/2){
            velocity = 0;
        } else {
            velocity += GRAV;
        }
}
}
{Intro}
import greenfoot.*;

/**
 * Write a description of class Intro here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Intro extends Actor
{
    public Intro()
    {
        getImage().scale(650,450);
    }
    /**
     * Act - do whatever the Intro wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       removeOnClick();
    }    
    public void removeOnClick()
{
 if (Greenfoot.isKeyDown("enter")) {
        World world;
        world = getWorld();
        world.removeObject(this);
        
        Greenfoot.stop();
        
    }    
}
}
{Ground}
import greenfoot.*;

/**
 * Write a description of class Ground here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Ground extends Actor
{
    public Ground()
    {
        getImage().scale(30,30);
    }
    /**
     * Act - do whatever the Ground wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
        if(isTouching(Ball.class))
        {
            removeTouching(Ball.class);
        }
    }    
}
{Text}
import greenfoot.*;
import java.awt.Color;
 
public class Text extends Actor
{
    public Text()
    {
        this("");
    }
 
    public Text(String text)
    {
        setText(text);
    }
 
    public void setText(String text)
    {
        setImage(new GreenfootImage(text, 24, Color.black, new Color(0, 0, 0, 0)));
    } 
}
Nate2002 Nate2002

2019/4/30

#
Try this in your Ball class
public int ySpeed = 0;
public boolean onGround = true;
private int Timer;

public void act
{
time++;
clicked();
}

public void clicked()
{
if(Greenfoot.mouseClicked(null))
       {
           ySpeed ++;
        setLocation(getX(), getY()+ySpeed);
        onGround = false;
        while (getOneObjectAtOffset (14, getImage().getHeight()/2 - 2, Ground.class)!= null)
        {
            setLocation(getX(), getY()-1);
            onGround = true;
            ySpeed=0;
        }
        if (onGround == true)
        {
            if ("space".equals(Greenfoot.getKey())) 
            {

                ySpeed = -15; 
                setLocation(getX(), getY()+ySpeed); 
                Timer = 3;  

            }

        }  
    
           
           
       }
}
danpost danpost

2019/4/30

#
Nate2002 wrote...
Try this in your Ball class << Code Omitted >>
The behavior wanted is not jumping from ground, but jumping when clicked. Also, I believe in this case, hitting the ground would not be a good thing if the idea was to "keep it up". I believe what is wanted is more like this:
import greenfoot.*;

public class Ball extends Actor
{
    private int ySpeed;
    
    public void act()
    {
        if (Greenfoot.mousePressed(this))
        {
            ySpeed = -20;
            ((Counter)getWorld().getObjects(Counter.class).get(0)).add(1);
        }
        setLocation(getX(), getY()+ySpeed);
        ySpeed++;
        if (isTouching(Ground.class)) Greenfoot.stop();
    }
}
Nate2002 Nate2002

2019/4/30

#
@danpost. Sorry about that, I only had a few minutes before my next class, so i couldn't really read it to closely.
danpost danpost

2019/4/30

#
@Nate2002, No need to apologize -- and your effort is encouraged. Hey, think of it as another opportunity for growth.
Nate2002 Nate2002

2019/4/30

#
@danpost. Thanks I will
rycode rycode

2019/5/1

#
Alright, I will try this out tomorrow and let you know what I think. Thanks
rycode rycode

2019/5/1

#
Works great, thanks.
rycode rycode

2019/5/1

#
The only problem I'd say is that when line
((Counter)getWorld().getObjects(Counter.class).get(0)).add(1);
is in it brings up the terminal screen and stops the game. Also, is there any way I could make the ball drop a little slower, because in order to make it playable I have to set the speed of the game lower. And that creates a difference in my timer, and makes each second longer. (sorry if this is an easy fix, but I will need help.)
danpost danpost

2019/5/1

#
rycode wrote...
The only problem I'd say is that when line << Code Omitted >> is in it brings up the terminal screen and stops the game.
Please show what the terminal output displays.
is there any way I could make the ball drop a little slower, because in order to make it playable I have to set the speed of the game lower. And that creates a difference in my timer, and makes each second longer.
For a slight change in speed (and I believe you are referring to vertical speed), you will have to incorporate a precision movement system (aka - smooth mover). This involves the following changes:
// add instance field
private double evactY; // to track exact vertical position

// change instance field (to allow fine tuning of speed)
private double ySpeed;

// override method (to set initial value of vertical position)
protected void addedToWorld(World world)
{
    exactY = getY();
}

// change in line 11 (to compensate for less acceleration)
ySpeed = -12;

// insert before line 14 (to keep track of exact vertical position)
exactY += ySpeed;

// change in line 14 ("exact" positioning)
setLocation(getX(), (int)exactY);

// change in line 15 (to lessen the acceleration)
ySpeed += 0.8;
You may need to adjust the values on lines 14 and 23 to suit your needs (I think I got it pretty close, however).
rycode rycode

2019/5/1

#
Ok thanks again, helped me out a lot.
rycode rycode

2019/5/2

#
What I noticed is that when I start the game, the ball moves to the middle of the screen, and is very low to the ground (which you do not want to touch the ground.)Although this code I think makes the game better, I still have the problem that the ball falls downwards super fast when the game speed is 1.0x (regular speed). When the ball is falling this fast the game is not playable, and it in order to play I must slow down the game speed. When I slow the game speed, it causes the timer to not work correctly. Problem - Need the ball to fall down slower and have it sync with the timer so the timer can work correctly and the game is more playable. Thanks again.
danpost danpost

2019/5/2

#
rycode wrote...
Need the ball to fall down slower
Just decrease the value on the last line.
rycode rycode

2019/5/2

#
Danpost you are a legend
danpost danpost

2019/5/2

#
rycode wrote...
Danpost you are a legend
Apparently, it does not profit much being one.
There are more replies on the next page.
1
2