Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
- import java.awt.*;
- public class PongWorld extends World
- {
- Ball b;
- Paddle p1,p2;
- int hits1,hits2;
- boolean started=true;
- ScoreBoard1 sb1;
- ScoreBoard2 sb2;
- GameOver go;
- public PongWorld()
- {
- // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
- super(600, 400, 1);
- GreenfootImage bg=new GreenfootImage(600,600);
- bg.setColor(Color.green);
- bg.drawRect(0,0,600,600);
- bg.fill();
- bg.setColor(Color.white);
- bg.fillRect(300,0,2,600);
- setBackground(bg);
- b=new Ball(-2,2);
- addObject(b,300,300);
- p1=new Paddle(1);
- addObject(p1,10,100);
- p2=new Paddle(2);
- addObject(p2,590,100);
- sb1=new ScoreBoard1();
- addObject(sb1,100,50);
- sb2=new ScoreBoard2();
- addObject(sb2,500,50);
- }
- public void act()
- {
- // if (Greenfoot.isKeyDown("enter"))
- // {
- // started=true;
- // addObject(b,getWidth()/2,getHeight()/2);
- // }
- // if (started==false)
- // return;
- if (p1.checkForHit()==true)
- b.reverseX();
- if (p2.checkForHit()==true)
- b.reverseX();
- if (b.missedOnRight==true)
- {
- hits1++;
- //System.out.println("Hits1="+hits1);
- b.setLocation(getWidth()/2,getHeight()/2);
- b.reverseX();
- sb1.drawScore(hits1);
- }
- if (b.missedOnLeft==true)
- {
- hits2++;
- System.out.println("Hits2="+hits2);
- b.setLocation(getWidth()/2,getHeight()/2);
- b.reverseX();
- sb2.drawScore(hits2);
- }
- if (hits1==10)
- {
- removeObject(b);
- go=new GameOver();
- addObject(go,300,200);
- Greenfoot.stop();
- }
- if (hits2==10)
- {
- removeObject(b);
- go=new GameOver();
- addObject(go,300,200);
- Greenfoot.stop();
- }
- }
- }
- Ball
- import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
- public class Ball extends Actor
- {
- int X,Y,xSpeed,ySpeed,Width,Height;
- GreenfootImage pic;
- public boolean missedOnLeft=false;
- public boolean missedOnRight=false;
- public Ball(int xs,int ys)
- {
- xSpeed=xs;
- ySpeed=ys;
- pic=getImage();
- Width=pic.getWidth()/4;
- Height=pic.getHeight()/4;
- pic.scale(Width,Height);
- }
- public void act()
- {
- X=getX()+xSpeed;
- Y=getY()+ySpeed;
- World w=getWorld();
- missedOnLeft=false;
- missedOnRight=false;
- if (X<=Width/2)
- {
- missedOnLeft=true;
- return;
- }
- if (Y>=w.getHeight()-Height/2)
- ySpeed=-ySpeed;
- if (X>=w.getWidth()-Width/2)
- {
- missedOnRight=true;
- return;
- }
- if (Y<=Height/2)
- ySpeed=-ySpeed;
- setLocation(X,Y);
- }
- public void reverseX()
- {
- xSpeed=-xSpeed;
- }
- }
- Game Over
- import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
- /**
- * Write a description of class GameOver here.
- *
- * @author (your name)
- * @version (a version number or a date)
- */
- public class GameOver extends Actor
- {
- GreenfootImage pic;
- public GameOver()
- {
- pic=getImage();
- }
- }
- ScoreBoard1
- import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
- import java.awt.*;
- public class ScoreBoard1 extends Actor
- {
- GreenfootImage pic;
- public ScoreBoard1()
- {
- drawScore(0);
- }
- public void drawScore(int hits)
- {
- pic=new GreenfootImage (150,50);
- pic.setColor(Color.white);
- pic.setTransparency(150);
- pic.fill();
- pic.setFont(new Font("Serif",Font.BOLD,25));
- pic.setColor(Color.red);
- pic.drawString("Misses1="+hits,0,20);
- setImage(pic);
- }
- }
- ScoreBoard 2
- import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
- import java.awt.*;
- public class ScoreBoard2 extends Actor
- {
- GreenfootImage pic;
- public ScoreBoard2()
- {
- drawScore(0);
- }
- public void drawScore(int hits)
- {
- pic=new GreenfootImage (150,50);
- pic.setColor(Color.white);
- pic.setTransparency(150);
- pic.fill();
- pic.setFont(new Font("Serif",Font.BOLD,25));
- pic.setColor(Color.red);
- pic.drawString("Misses2="+hits,0,20);
- setImage(pic);
- }
- }
- Paddle
- import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
- import java.awt.*;
- public class Paddle extends Actor
- {
- int X,Y,Width,Height;
- int side;
- GreenfootImage pic;
- final int left=1,right=2;
- public Paddle(int s)
- {
- side=s;
- pic=new GreenfootImage(20,80);
- Width=20;
- Height=80;
- if (side==left)
- {
- pic.setColor(Color.black);
- pic.fillRect(0,0,Width,Height);
- setImage(pic);
- }
- if (side==right)
- {
- pic.setColor(Color.black);
- pic.fillRect(0,0,Width,Height);
- setImage(pic);
- }
- }
- public boolean checkForHit()
- {
- Ball b=(Ball)getOneIntersectingObject(Ball.class);
- if (b !=null)
- return true;
- return false;
- }
- public void act()
- {
- World w=getWorld();
- X=getX();
- Y=getY();
- if (side==left)
- {
- if (Greenfoot.isKeyDown("up"))
- {
- if(Y>=Height/2)
- Y=Y-5;
- setLocation(X,Y);
- }
- if (Greenfoot.isKeyDown("down"))
- {
- if (Y<=w.getHeight()-Height/2)
- Y=Y+5;
- setLocation(X,Y);
- }
- }
- if (side==right)
- {
- if (Greenfoot.isKeyDown("W"))
- {
- if(Y>=Height/2)
- Y=Y-5;
- setLocation(X,Y);
- }
- if (Greenfoot.isKeyDown("S"))
- {
- if (Y<=w.getHeight()-Height/2)
- Y=Y+5;
- setLocation(X,Y);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment