Kód: Vybrať všetko
package breakout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JOptionPane;
public class Breakout extends JPanel {
public class Ball {
int x = 30;
int y = 0;
int dirx = 1;
int diry = 1;
int speed = 2;
private Breakout game;
private Paddle paddle;
private Brick brick;
public Ball(Breakout game) {
this.game = game;
}
int getX() {
return x;
}
int getY() {
return y;
}
boolean checkCollision(int brickX, int brickY) {
if (x + 5 >= brickX && x <= brickX + 60 && y + 5 >= brickY && y <= brickY + 35)
return true;
else return false;
}
void bounce() {
if (checkCollision(game.brick.getX(),game.brick.getY()) && game.brick.isInAreaX())
dirx *= -1;
if (checkCollision(game.brick.getX(),game.brick.getY()) && game.brick.isInAreaY())
diry *= -1;
if (checkCollision(game.brick.getX(),game.brick.getY())) {
dirx *= -1;
diry *= -1;
}
}
void move() {
x += speed*dirx;
y += speed*diry;
if ((x <= 0 && dirx == -1) || (x >= game.getWidth() - 10))
dirx *= -1;
if ((y <= 0 && diry == -1) || (y >= 550 - 10 && diry == 1
&& x >= game.paddle.getX() && x <= game.paddle.getX() + 60 && y <= 555 - 10))
diry *= -1;
}
public void paint(Graphics2D g) {
g.fillOval(x,y,10,10);
}
}
public class Paddle {
private Breakout game;
int x = 0;
int y = 550;
int speed = 5;
int dir = 0;
int getX() {
return x;
}
public Paddle(Breakout game) {
this.game = game;
}
void move() {
x += speed*dir;
if (x <= 0)
x = 0;
if (x >= game.getWidth() - 60)
x = game.getWidth() - 60;
}
public void paint(Graphics2D g) {
g.fillRect(x,y,60,5);
}
public void keyReleased(KeyEvent e) {
dir = 0;
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT)
dir = -1;
else if (e.getKeyCode() == KeyEvent.VK_RIGHT)
dir = 1;
else
dir = 0;
}
}
public class Brick {
int x;
int y;
boolean inAreaX;
boolean inAreaY;
Brick (int x, int y) {
this.x = x;
this.y = y;
}
int getX() {
return x;
}
int getY() {
return y;
}
void CheckAreaX(int ballx) {
if (ballx + 10 >= x && ballx <= x + 60 && !inAreaY)
inAreaX = true;
else inAreaX = false;
}
void CheckAreaY(int bally) {
if (bally + 10 >= y && bally <= y + 35 && !inAreaX)
inAreaY = true;
else inAreaY = false;
}
boolean isInAreaX () {
if (inAreaX)
return true;
else return false;
}
boolean isInAreaY () {
if (inAreaY)
return true;
else return false;
}
void checkAreas() {
CheckAreaX(ball.getX());
CheckAreaY(ball.getY());
}
public void paint(Graphics2D g) {
g.drawRect(x, y, 60, 35);
}
}
Ball ball = new Ball(this);
Paddle paddle = new Paddle(this);
Brick brick[][] = new Brick[13][7];
for (int i = 0; i < 13; i++)
for (int j = 0; j < 7; j++)
brick[i][j] = new Brick(i*J*(60 + 2), i*j*(15 + 2));
public Breakout() {
addKeyListener(new KeyListener() {
public void keyTyped(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
paddle.keyReleased(e);
}
public void keyPressed(KeyEvent e) {
paddle.keyPressed(e);
}
});
setFocusable(true);
}
void move () {
ball.move();
paddle.move();
ball.bounce();
brick.checkAreas();
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
ball.paint(g2d);
paddle.paint(g2d);
brick.paint(g2d);
/*g2d.setColor(Color.RED);
g2d.setFont(new Font("Courier_new",Font.BOLD,10));
if (brick.isInAreaX())
g2d.drawString("IN AREA X", 600, 20);
else g2d.drawString("NOT IN AREA X", 600, 20);
if (brick.isInAreaY())
g2d.drawString("IN AREA Y", 600, 50);
else g2d.drawString("NOT IN AREA Y", 600, 50);*/
}
public static void main(String[] args) throws InterruptedException {
JFrame window = new JFrame("Tennis");
Breakout game = new Breakout();
window.add(game);
window.setSize(800,600);
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
while (true) {
game.move();
game.repaint();
Thread.sleep(10);
}
}
}