dimanche 28 décembre 2014

How to make RapidRoll game with Java


Vote count:

0




I'm writing RapidRoll game and I think all people know the rules of this game. I have to send my project to my teacher about next 10 days and I need to help! Now, I'm trying to simulate this game with just two racquet for once lap. I have problem on class ball and "move" method. I don't know how simulate it that it runs... If you run it you'll know that the game will be gameOver without the ball arrive the end of height. Please help me... Thank you


Class Racquet:



package RapidBall;


import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;

public class Racquet {
private static int X = 330;
private static int Y=150, D=300;
private static final int WIDTH = 60;
private static final int HEIGHT = 10;
static int x = 20, a=35, b=25;
private Game game;
Thread t;

public Racquet(Game game) {
this.game = game;
}

public void move() {
Y = Y-1;
D = D-1;
}


public void paint(Graphics2D g) {
g.fillRect(x, Y, WIDTH, HEIGHT);
g.fillRect(b, D, WIDTH, HEIGHT);
}

public Rectangle getBoundY() {
return new Rectangle(x, Y, WIDTH, HEIGHT);
}
public Rectangle getBoundD() {
return new Rectangle(x, D, WIDTH, HEIGHT);
}

public int getTopY() {
return Y;
}

public int getTopD() {
return D;
}
}


And it is Ball Class:



package RapidBall;

import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;

public class Ball {
private static final int DIAMETER = 10;
int x = 20;
static int y2 = 0,y=0;
static int d2 = 0,d=0;
int xa = 1;
int ya = 1;
int da = 1;
private Game game;
// *************
boolean keyPressed = false;
boolean bCollisionY = true;
boolean bCollisionD = false;

public Ball(Game game) {
this.game = game;
}

public void keyReleased(KeyEvent e) {
xa = 0;
}

public void keyPressed(KeyEvent e) {
keyPressed = true;
if (e.getKeyCode() == KeyEvent.VK_LEFT)
xa = -2;
if (e.getKeyCode() == KeyEvent.VK_RIGHT)
xa = 2;
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
y += 2;
d += 2;
}
}

void move() {
if (keyPressed && x + xa < game.getWidth() - DIAMETER && x + xa > 0) {
x = x + xa;
}
if(collisionY()){
bCollisionY=true;
bCollisionD=false;
y=game.racquet.getTopY() - DIAMETER;
if(y+ya<0)
game.gameOver("Y: From up"); //for know about where it have collision on the bounds
}
d++;
if(collisionD()){
bCollisionD=true;
bCollisionY=false;
d=game.racquet.getTopD() - DIAMETER;
if(d+da<0)
game.gameOver("D: From up"); //for know about where it have collision on the bounds

}
y++;
if(y+ya> game.getHeight() - DIAMETER)
game.gameOver("Y: From down"); //for know about where it have collision on the bounds
if(d+da> game.getHeight() - DIAMETER)
game.gameOver("D: From down"); //for know about where it have collision on the bounds
}
public void paint(Graphics2D g) {
if (bCollisionY) {
g.fillOval(x, y, DIAMETER, DIAMETER);

}
if(bCollisionD){
g.fillOval(x, d, DIAMETER, DIAMETER);
}
}

private boolean collisionY() {
return game.racquet.getBoundY().intersects(getBoundY());
}

private boolean collisionD() {
return game.racquet.getBoundD().intersects(getBoundD());
}

public Rectangle getBoundY() {
return new Rectangle(x, y, DIAMETER, DIAMETER);
}

public Rectangle getBoundD() {
return new Rectangle(x, d, DIAMETER, DIAMETER);
}
}


Finally it is Game Class:



package RapidBall;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class Game extends JPanel {

Ball ball = new Ball(this);
Racquet racquet = new Racquet(this);

public Game() {

addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
}

@Override
public void keyReleased(KeyEvent e) {
ball.keyReleased(e);
}

@Override
public void keyPressed(KeyEvent e) {
ball.keyPressed(e);
}
});
setFocusable(true);
}

private void move() {
ball.move();
racquet.move();
}

@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
ball.paint(g2d);
racquet.paint(g2d);
}

public void gameOver(String s) {
JOptionPane.showMessageDialog(this, "Game Over", s, JOptionPane.YES_NO_OPTION);
System.exit(ABORT);
}

public static void main(String[] args) throws InterruptedException {
JFrame frame = new JFrame("Mini Tennis");
Game game = new Game();
frame.add(game);
frame.setSize(300, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

while (true) {
game.move();
game.repaint();
Thread.sleep(10);
}
}
}


asked 44 secs ago







How to make RapidRoll game with Java

Aucun commentaire:

Enregistrer un commentaire