Load all project

This commit is contained in:
Dev Core
2026-10-07 17:27:49 +04:00
commit ba8128c882
10 changed files with 540 additions and 0 deletions
+3
View File
@@ -0,0 +1,3 @@
.idea/
out/
*.iml
+4
View File
@@ -0,0 +1,4 @@
Ping-Pong — классическая аркадная десктопная игра для двух игроков на Java Swing с авторским визуальным оформлением.
Игрок 1 управляет левой ракеткой перемещением курсора мыши по вертикали, а Игрок 2 управляет правой ракеткой стрелками "Вверх" и "Вниз".
Мяч динамически отскакивает от ракеток и ускоряется при каждом ударе; игра ведётся до 5 очков с фиксацией победы одного из игроков.
Управление горячими клавишами: клавиша "Пробел" ставит игру на паузу и снимает с неё, клавиша "R" перезапускает матч заново, а клавиша "Esc" закрывает приложение.
+86
View File
@@ -0,0 +1,86 @@
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.Random;
public class Ball {
private int x;
private int y;
private final int size;
private int speedX;
private int speedY;
public Ball(int x, int y) {
this.x = x;
this.y = y;
this.size = GameConfig.BALL_SIZE;
resetDirection();
}
public void resetDirection() {
Random random = new Random();
this.speedX = random.nextBoolean() ? GameConfig.BALL_SPEED : -GameConfig.BALL_SPEED;
this.speedY = random.nextBoolean() ? GameConfig.BALL_SPEED : -GameConfig.BALL_SPEED;
}
public void move() {
x += speedX;
y += speedY;
}
public void reverseX() {
speedX = -speedX;
increaseSpeed();
}
public void reverseY() {
speedY = -speedY;
}
private void increaseSpeed() {
if (Math.abs(speedX) < GameConfig.BALL_MAX_SPEED) {
if (speedX > 0) {
speedX++;
} else {
speedX--;
}
}
}
public void draw(Graphics g) {
g.setColor(GameConfig.BALL_COLOR);
g.fillOval(x, y, size, size);
}
public Rectangle getBounds() {
return new Rectangle(x, y, size, size);
}
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 getSize() {
return size;
}
public int getSpeedY() {
return speedY;
}
public void setSpeedY(int speedY) {
this.speedY = speedY;
}
}
+48
View File
@@ -0,0 +1,48 @@
import java.awt.Color;
import java.awt.Font;
import java.awt.event.KeyEvent;
public interface GameConfig {
// размеры окна
int WIDTH = 800;
int HEIGHT = 500;
// ракетки
int PADDLE_WIDTH = 14;
int PADDLE_HEIGHT = 90;
int PADDLE_SPEED = 6;
int PADDLE_OFFSET_X = 25;
// мячик
int BALL_SIZE = 14;
int BALL_SPEED = 5;
int BALL_MAX_SPEED = 9;
// ФПС
int FPS = 60;
// правила
int WIN_SCORE = 5;
// клавиши
int KEY_PAUSE = KeyEvent.VK_SPACE;
int KEY_RESTART = KeyEvent.VK_R;
int KEY_EXIT = KeyEvent.VK_ESCAPE;
int KEY_P2_UP = KeyEvent.VK_UP;
int KEY_P2_DOWN = KeyEvent.VK_DOWN;
// цвета
Color BACKGROUND = new Color(20, 24, 38);
Color CENTER_LINE_COLOR = new Color(70, 85, 120);
Color PADDLE1_COLOR = new Color(0, 220, 255);
Color PADDLE2_COLOR = new Color(255, 95, 105);
Color BALL_COLOR = new Color(255, 230, 80);
Color SCORE_COLOR = new Color(240, 240, 250);
Color HINT_COLOR = new Color(150, 160, 185);
// шрифт
Font SCORE_FONT = new Font("Arial", Font.BOLD, 44);
Font STATUS_FONT = new Font("Arial", Font.BOLD, 28);
Font HINT_FONT = new Font("Arial", Font.PLAIN, 14);
}
+18
View File
@@ -0,0 +1,18 @@
import javax.swing.JFrame;
public class GameFrame extends JFrame {
public GameFrame() {
GamePanel panel = new GamePanel();
this.add(panel);
this.setTitle("Ping Pong");
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setLocationRelativeTo(null);
this.setVisible(true);
panel.startGame();
}
}
+145
View File
@@ -0,0 +1,145 @@
public class GameLogic {
private final GamePanel panel;
private final Paddle player1;
private final Paddle player2;
private final Ball ball;
private int score1;
private int score2;
private boolean paused;
private boolean gameOver;
private int winner;
public GameLogic(GamePanel panel) {
this.panel = panel;
this.player1 = new Paddle(GameConfig.PADDLE_OFFSET_X, GameConfig.HEIGHT / 2 - GameConfig.PADDLE_HEIGHT / 2);
this.player2 = new Paddle(GameConfig.WIDTH - GameConfig.PADDLE_OFFSET_X - GameConfig.PADDLE_WIDTH, GameConfig.HEIGHT / 2 - GameConfig.PADDLE_HEIGHT / 2);
this.ball = new Ball(GameConfig.WIDTH / 2 - GameConfig.BALL_SIZE / 2, GameConfig.HEIGHT / 2 - GameConfig.BALL_SIZE / 2);
this.score1 = 0;
this.score2 = 0;
this.paused = false;
this.gameOver = false;
this.winner = 0;
}
public void update() {
if (paused || gameOver) {
return;
}
player1.move();
player2.move();
ball.move();
checkCollisions();
checkBorders();
checkGoal();
}
private void checkCollisions() {
if (ball.getBounds().intersects(player1.getBounds())) {
ball.setX(player1.getX() + player1.getWidth());
ball.reverseX();
applyPaddleBounce(player1);
} else if (ball.getBounds().intersects(player2.getBounds())) {
ball.setX(player2.getX() - ball.getSize());
ball.reverseX();
applyPaddleBounce(player2);
}
}
private void applyPaddleBounce(Paddle paddle) {
int paddleCenter = paddle.getY() + paddle.getHeight() / 2;
int ballCenter = ball.getY() + ball.getSize() / 2;
int diff = ballCenter - paddleCenter;
ball.setSpeedY(diff / 10);
if (ball.getSpeedY() == 0) {
ball.setSpeedY(1);
}
}
private void checkBorders() {
if (ball.getY() <= 0) {
ball.setY(0);
ball.reverseY();
} else if (ball.getY() + ball.getSize() >= GameConfig.HEIGHT) {
ball.setY(GameConfig.HEIGHT - ball.getSize());
ball.reverseY();
}
}
private void checkGoal() {
if (ball.getX() + ball.getSize() < 0) {
score2++;
checkWinCondition();
resetBall();
} else if (ball.getX() > GameConfig.WIDTH) {
score1++;
checkWinCondition();
resetBall();
}
}
private void checkWinCondition() {
if (score1 >= GameConfig.WIN_SCORE) {
gameOver = true;
winner = 1;
} else if (score2 >= GameConfig.WIN_SCORE) {
gameOver = true;
winner = 2;
}
}
private void resetBall() {
ball.setX(GameConfig.WIDTH / 2 - GameConfig.BALL_SIZE / 2);
ball.setY(GameConfig.HEIGHT / 2 - GameConfig.BALL_SIZE / 2);
ball.resetDirection();
}
public void restartGame() {
score1 = 0;
score2 = 0;
gameOver = false;
winner = 0;
paused = false;
resetBall();
}
public void togglePause() {
if (!gameOver) {
paused = !paused;
}
}
public Paddle getPlayer1() {
return player1;
}
public Paddle getPlayer2() {
return player2;
}
public Ball getBall() {
return ball;
}
public int getScore1() {
return score1;
}
public int getScore2() {
return score2;
}
public boolean isPaused() {
return paused;
}
public boolean isGameOver() {
return gameOver;
}
public int getWinner() {
return winner;
}
}
+33
View File
@@ -0,0 +1,33 @@
public class GameLoop implements Runnable {
private final GamePanel panel;
private volatile boolean running;
public GameLoop(GamePanel panel) {
this.panel = panel;
this.running = false;
}
public void start() {
running = true;
Thread thread = new Thread(this);
thread.start();
}
@Override
public void run() {
long sleepTime = 1000 / GameConfig.FPS;
while (running) {
panel.updateGame();
panel.repaint();
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;
}
}
}
}
+124
View File
@@ -0,0 +1,124 @@
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
public class GamePanel extends JPanel implements KeyListener, MouseMotionListener {
private final GameLogic logic;
public GamePanel() {
this.setPreferredSize(new Dimension(GameConfig.WIDTH, GameConfig.HEIGHT));
this.setBackground(GameConfig.BACKGROUND);
this.setFocusable(true);
this.addKeyListener(this);
this.addMouseMotionListener(this);
this.logic = new GameLogic(this);
}
public void startGame() {
GameLoop loop = new GameLoop(this);
loop.start();
}
public void updateGame() {
logic.update();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
drawCenterLine(g);
logic.getPlayer1().draw(g, GameConfig.PADDLE1_COLOR);
logic.getPlayer2().draw(g, GameConfig.PADDLE2_COLOR);
logic.getBall().draw(g);
drawScore(g);
drawStatus(g);
drawHints(g);
}
private void drawCenterLine(Graphics g) {
g.setColor(GameConfig.CENTER_LINE_COLOR);
for (int y = 0; y < GameConfig.HEIGHT; y += 30) {
g.fillRect(GameConfig.WIDTH / 2 - 2, y, 4, 18);
}
}
private void drawScore(Graphics g) {
String scoreText = logic.getScore1() + " : " + logic.getScore2();
drawCenteredText(g, scoreText, 60, GameConfig.SCORE_FONT, GameConfig.SCORE_COLOR);
}
private void drawStatus(Graphics g) {
if (logic.isGameOver()) {
String winText = "ПОБЕДА ИГРОКА " + logic.getWinner() + "!";
drawCenteredText(g, winText, GameConfig.HEIGHT / 2 - 20, GameConfig.STATUS_FONT, GameConfig.SCORE_COLOR);
drawCenteredText(g, "Нажмите R для новой игры", GameConfig.HEIGHT / 2 + 25, GameConfig.HINT_FONT, GameConfig.HINT_COLOR);
} else if (logic.isPaused()) {
drawCenteredText(g, "ПАУЗА", GameConfig.HEIGHT / 2, GameConfig.STATUS_FONT, GameConfig.SCORE_COLOR);
}
}
private void drawHints(Graphics g) {
String hints = "Мышь: Игрок 1 | Стрелки: Игрок 2 | Пробел: Пауза | R: Заново | Esc: Выход";
drawCenteredText(g, hints, GameConfig.HEIGHT - 18, GameConfig.HINT_FONT, GameConfig.HINT_COLOR);
}
private void drawCenteredText(Graphics g, String text, int y, Font font, Color color) {
g.setFont(font);
g.setColor(color);
FontMetrics fm = g.getFontMetrics(font);
int x = (GameConfig.WIDTH - fm.stringWidth(text)) / 2;
g.drawString(text, x, y);
}
@Override
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
if (code == GameConfig.KEY_P2_UP) {
logic.getPlayer2().setUp(true);
} else if (code == GameConfig.KEY_P2_DOWN) {
logic.getPlayer2().setDown(true);
} else if (code == GameConfig.KEY_PAUSE) {
logic.togglePause();
} else if (code == GameConfig.KEY_RESTART) {
logic.restartGame();
} else if (code == GameConfig.KEY_EXIT) {
System.exit(0);
}
}
@Override
public void keyReleased(KeyEvent e) {
int code = e.getKeyCode();
if (code == GameConfig.KEY_P2_UP) {
logic.getPlayer2().setUp(false);
} else if (code == GameConfig.KEY_P2_DOWN) {
logic.getPlayer2().setDown(false);
}
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void mouseMoved(MouseEvent e) {
logic.getPlayer1().setYByCenter(e.getY());
}
@Override
public void mouseDragged(MouseEvent e) {
logic.getPlayer1().setYByCenter(e.getY());
}
}
+5
View File
@@ -0,0 +1,5 @@
public class Main {
public static void main(String[] args) {
new GameFrame();
}
}
+74
View File
@@ -0,0 +1,74 @@
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
public class Paddle {
private final int x;
private int y;
private final int width;
private final int height;
private final int speed;
private boolean up;
private boolean down;
public Paddle(int x, int y) {
this.x = x;
this.y = y;
this.width = GameConfig.PADDLE_WIDTH;
this.height = GameConfig.PADDLE_HEIGHT;
this.speed = GameConfig.PADDLE_SPEED;
}
public void move() {
if (up && y > 0) {
y -= speed;
}
if (down && y + height < GameConfig.HEIGHT) {
y += speed;
}
}
public void setYByCenter(int centerY) {
int newY = centerY - height / 2;
if (newY < 0) {
newY = 0;
} else if (newY + height > GameConfig.HEIGHT) {
newY = GameConfig.HEIGHT - height;
}
this.y = newY;
}
public void draw(Graphics g, Color color) {
g.setColor(color);
g.fillRoundRect(x, y, width, height, 8, 8);
}
public Rectangle getBounds() {
return new Rectangle(x, y, width, height);
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public void setUp(boolean up) {
this.up = up;
}
public void setDown(boolean down) {
this.down = down;
}
}