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

2014/6/10

Pong

adi110 adi110

2014/6/10

#
How can i make, after the ball hit the left/right edge, the new ball move in the that direction (if the ball hit the left edge, the new ball start moving in the left side) public void remove() { PongWorld world = (PongWorld) getWorld(); if(getX() > 795) { Score(1); getWorld().removeObject(this); Greenfoot.delay(5); world.addObject(new Ball(), 400, 300); } else { if(getX() < 5) { Scoree(1); getWorld().removeObject(this); Greenfoot.delay(5); world.addObject(new Ball(), 400, 300); } } } i tried this but every time the ball start moving in the right side; a little help here?
danpost danpost

2014/6/10

#
Instead of creating a new Ball object, recycle the old ball. Its rotation will give it the proper direction to move.
// change both lines with this
world.addObject(new Ball(), 400, 300);
// to this
world.addObject(this, 400, 300);
Actually, instead of removing and re-adding the actor, you can just reset its location to the center of the world.
adi110 adi110

2014/6/11

#
thx, it worked; and do you know how to make in the same game, a game with 2 players (this one i have) and another game playing against computer?
adi110 adi110

2014/6/11

#
this is the code for my Paddle import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Bat here. * * @author (Adrain Copoiu) * @version 1.0(June 2014) */ public class Bat extends Actor { private int bat; private int batt; private String up; private String down; public Bat() { } public void act() { PongWorld theWorld = (PongWorld) getWorld(); if(!theWorld.isEsc) { if(!theWorld.isPaused) { keyPress(); } } } public Bat(String up, String down) { this.up = up; this.down = down; } public void keyPress() { if(Greenfoot.isKeyDown(up)) { setLocation(getX(),getY()-5); bat=-45; batt=-135; } else { if(Greenfoot.isKeyDown(down)) { setLocation(getX(),getY()+5); bat=45; batt=135; } else { bat=0; batt=180; } } } public int bat() { return bat; } public int batt() { return batt; } }
danpost danpost

2014/6/11

#
I would have set things up a bit different. But, with what you have now, I would change 'keyPress();' in your act method to:
if (up != null) keyPress(); else ai();
and add the AI movement in a new method called 'ai'. Asking if 'up' is not null basically asked if keys are assigned to the bat. If no keys are assigned, then it must be a computer operated bat. You will probably need a way for the user to select whether they want a player v. player or a player v. computer gameplay. Depending of user selection, either create a 'new Bat("up", "down")' or a 'new Bat()', respectively.
adi110 adi110

2014/6/11

#
ok, i will try, thx
adi110 adi110

2014/6/14

#
i didn't manage to do it; can you write here the program?
danpost danpost

2014/6/14

#
Please show what code you now have in the Bat class.
You need to login to post a reply.