Projekt. Etap 5 – klejenie się piłki do palety po kolizji
Kody źródłowe z odcinka
package pl.am.swing.gra.etap5;
import javax.swing.*;
import java.awt.*;
public class MyFrame extends JFrame {
public MyFrame() {
setTitle("Gra");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(1000, 800);
MyComponent myComponent = new MyComponent();
add(myComponent);
addKeyListener(new MyKeyListener(myComponent));
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
MyFrame myFrame = new MyFrame();
myFrame.setVisible(true);
});
}
}
package pl.am.swing.gra.etap5;
import javax.swing.*;
import java.awt.*;
public class MyComponent extends JComponent {
Ball ball = Ball.getBall();
public MyComponent() {
Timer timer = new Timer(40, e -> {
ball.updatePosition();
repaint();
});
timer.start();
}
@Override
protected void paintComponent(Graphics g) {
Toolkit.getDefaultToolkit().sync();
Graphics2D graphics2D = (Graphics2D) g;
if (Utils.gameOver) {
graphics2D.setPaint(Color.BLACK);
graphics2D.setFont(graphics2D.getFont().deriveFont(50.0F));
graphics2D.drawString("Koniec gry - przegrałeś", 30, 100);
} else {
Rectangle limit = new Rectangle(0, 0, Utils.maxWidth, Utils.maxHeight);
graphics2D.setPaint(Color.CYAN);
graphics2D.fill(limit);
graphics2D.setPaint(Color.BLUE);
graphics2D.fill(ball.getShape());
graphics2D.setPaint(Color.GRAY);
graphics2D.fill(Palette.getShape());
graphics2D.setPaint(Color.BLACK);
graphics2D.drawString("Ilość żyć: ", Utils.maxWidth, 20);
graphics2D.setFont(graphics2D.getFont().deriveFont(50.0F));
graphics2D.drawString(String.valueOf(Utils.livesNumber), Utils.maxWidth, 100);
}
}
}
package pl.am.swing.gra.etap5;
import java.awt.*;
import java.awt.geom.Ellipse2D;
public class Ball {
private int x = 500;
private int y = 0;
private int width = 30;
private int height = 30;
private int speedX = 5;
private int speedY = 10;
private boolean glue = false;
private static final Ball ball = new Ball();
private Ball() {
}
public static Ball getBall() {
return ball;
}
public void updatePosition() {
if (!glue) {
x += speedX;
y += speedY;
if (x + width > Utils.maxWidth) {
x = Utils.maxWidth - width;
speedX = -speedX;
}
if (y + height > Utils.maxHeight) {
y = Palette.y- height;
x = Palette.x + Palette.width/2 - width/2;
Utils.livesNumber--;
if (Utils.livesNumber == 0) {
Utils.gameOver = true;
}
glue = true;
}
if (x < 0) {
x = 0;
speedX = -speedX;
}
if (y < 0) {
y = 0;
speedY = -speedY;
}
}
if (checkCollision()) {
glue = true;
y = Palette.y- height;
}
}
public boolean checkCollision() {
return x + width > Palette.x && x < Palette.x + Palette.width
&& y + height > Palette.y && y < Palette.y + Palette.height;
}
public Shape getShape() {
return new Ellipse2D.Float(x,y, width, height);
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getSpeedX() {
return speedX;
}
public void setSpeedX(int speedX) {
this.speedX = speedX;
}
public int getSpeedY() {
return speedY;
}
public void setSpeedY(int speedY) {
this.speedY = speedY;
}
public boolean isGlue() {
return glue;
}
public void setGlue(boolean glue) {
this.glue = glue;
}
}
package pl.am.swing.gra.etap5;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class MyKeyListener extends KeyAdapter {
private MyComponent myComponent;
private Ball ball = Ball.getBall();
public MyKeyListener(MyComponent myComponent) {
this.myComponent = myComponent;
}
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
switch (keyCode) {
case KeyEvent.VK_LEFT -> {
Palette.x -= 10;
if (Palette.x < 0) {
Palette.x = 0;
} else {
if (ball.isGlue()) {
ball.setX(ball.getX() - 10);
}
}
}
case KeyEvent.VK_RIGHT -> {
Palette.x += 10;
if (Palette.x + Palette.width > Utils.maxWidth) {
Palette.x = Utils.maxWidth - Palette.width;
} else {
if (ball.isGlue()) {
ball.setX(ball.getX() + 10);
}
}
}
case KeyEvent.VK_SPACE -> {
if (ball.getSpeedY() > 0) {
ball.setSpeedY(-ball.getSpeedY());
}
ball.setGlue(false);
}
}
myComponent.repaint();
}
}
package pl.am.swing.gra.etap5;
import java.awt.*;
public class Palette {
public static int paletteSpace = 50;
public static int width = 100;
public static int height = 20;
public static int x = 500;
public static int y = Utils.maxHeight - paletteSpace - height;
public static Shape getShape() {
return new Rectangle(x, y, width, height);
}
}
package pl.am.swing.gra.etap5;
public class Utils {
public static int maxWidth = 800;
public static int maxHeight = 600;
public static int livesNumber = 3;
public static boolean gameOver = false;
}