Pong
Pong
if(ball.yVelocity>0)
ball.yVelocity++;
//optional for more difficulty
else
ball.yVelocity--;
ball.setXDirection(-ball.xVelocity);
ball.setYDirection(ball.yVelocity);
}
//stops paddles at window edges
if(paddle1.y<=0)
paddle1.y=0;
if(paddle1.y >= (GAME_HEIGHT-PADDLE_HEIGHT))
paddle1.y = GAME_HEIGHT-
PADDLE_HEIGHT;
if(paddle2.y<=0)
paddle2.y=0;
if(paddle2.y >= (GAME_HEIGHT-PADDLE_HEIGHT))
paddle2.y = GAME_HEIGHT-PADDLE_HEIGHT;
newBall();
System.out.println("Player 1: "+score.player1);
}
}
public void run()
{
//game loop
long lastTime = System.nanoTime();
double amountOfTicks =60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
while(true)
{
long now = System.nanoTime();
delta += (now -lastTime)/ns;
lastTime = now;
if(delta >=1)
{
move();
checkCollision();
repaint();
delta--;
}
}
}
public class AL extends KeyAdapter
{
public void keyPressed(KeyEvent e)
{
paddle1.keyPressed(e);
paddle2.keyPressed(e); }
public void keyReleased(KeyEvent e)
{
paddle1.keyReleased(e);
paddle2.keyReleased(e);
}
}
}
//********************************* *************************
import java.awt.*;
import java.awt.event.*;
public class Paddle extends Rectangle
{
int id;
int yVelocity;
int speed = 10;
Paddle(int x, int y, int PADDLE_WIDTH, int PADDLE_HEIGHT, int id)
{
super(x,y,PADDLE_WIDTH,PADDLE_HEIGHT);
this.id=id;
}
public void keyPressed(KeyEvent e)
{
switch(id)
{
case 1:
if(e.getKeyCode()==KeyEvent.VK_W)
{
setYDirection(-speed);
}
if(e.getKeyCode()==KeyEvent.VK_S)
{
setYDirection(speed);
}
break;
case 2:
if(e.getKeyCode()==KeyEvent.VK_UP)
{
setYDirection(-speed);
}
if(e.getKeyCode()==KeyEvent.VK_DOWN)
{
setYDirection(speed);
}
break;
}
}
public void keyReleased(KeyEvent e)
{
switch(id)
{
case 1:
if(e.getKeyCode()==KeyEvent.VK_W)
{
setYDirection(0);
}
if(e.getKeyCode()==KeyEvent.VK_S)
{
setYDirection(0);
}
break;
case 2:
(e.getKeyCode()==KeyEvent.VK_UP)
{
setYDirection(0);
}
if(e.getKeyCode()==KeyEvent.VK_DOWN)
{
setYDirection(0);
}
break;
}
}
public void setYDirection(int yDirection)
{
yVelocity = yDirection;
}
public void move()
{
y= y + yVelocity;
}
public void draw(Graphics g)
{
if(id==1)
g.setColor(Color.blue);
else
g.setColor(Color.red);
g.fillRect(x, y, width, height);
}
}
//********************************* *************************
import java.awt.*;
import java.util.*;
public class Ball extends Rectangle
{
Random random;
int xVelocity;
int yVelocity;
int initialSpeed = 2;
Ball(int x, int y, int width, int height)
{
super(x,y,width,height);
random = new Random();
randomXDirection = random.nextInt(2);
if(randomXDirection == 0)
randomXDirection--;
setXDirection(randomXDirection*initialSpeed);
Int randomYDirection = random.nextInt(2);
if(randomYDirection == 0)
randomYDirection--;
setYDirection(randomYDirection*initialSpeed);
}
public void setXDirection(int randomXDirection)
{
xVelocity = randomXDirection;
}
public void setYDirection(int randomYDirection)
{
yVelocity = randomYDirection;
}
public void move()
{
x += xVelocity;
y += yVelocity;
}
public void draw(Graphics g)
{
g.setColor(Color.white);
g.fillOval(x, y, height, width);
}
}
//********************************* *************************
import java.awt.*;
public class Score extends Rectangle
{
static int GAME_WIDTH;
static int GAME_HEIGHT;
int player1;
int player2;
Score(int GAME_WIDTH, int GAME_HEIGHT)
{
Score.GAME_WIDTH = GAME_WIDTH;
Score.GAME_HEIGHT = GAME_HEIGHT;
}
public void draw(Graphics g)
{
g.setColor(Color.white);
g.setFont(new Font("Consolas",Font.PLAIN,60));
g.drawString(String.valueOf(player1/10)+String.valueOf(player1%10),
(GAME_WIDTH/2)-85, 50);
g.drawString(String.valueOf(player2/10)+String.valueOf(player2%10),
(GAME_WIDTH/2)+20, 50);
}
}
//********************************* *************************