Untitled

mail@pastecode.io avatar
unknown
java
2 years ago
8.6 kB
9
Indexable
Never
package com.company;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;

class GameObject {
    private float x;
    private float y;

    private float width;
    private float height;

    GameObject() {

    }

    public void setPosition(float x, float y) {
        setX(x);
        setY(y);
    }

    public void move(float x, float y) {
        this.x += x;
        this.y += y;
    }

    public void setSize(float width, float height) {
        setWidth(width);
        setHeight(height);
    }

    public float getX() {
        return x;
    }

    public void setX(float x) {
        this.x = x;
    }

    public float getY() {
        return y;
    }

    public void setY(float y) {
        this.y = y;
    }

    public float getWidth() {
        return width;
    }

    public void setWidth(float width) {
        this.width = width;
    }

    public float getHeight() {
        return height;
    }

    public void setHeight(float height) {
        this.height = height;
    }

    public void update(float delta) {

    }

    public void render(Graphics2D g) {

    }

    public void handleKeyPress(char keyChar) {

    }

    public void handleKeyRelease(char keyChar) {

    }
}

class Player extends GameObject {
    private final Image image = ImageIO.read(new File("C:\\Users\\nikolai\\IdeaProjects\\untitled\\assets\\png\\player.png"));
    private float direction = 0.0f;

    private float acceleration = 9.8f;
    private float velocity = 0.0f;

    private boolean isOnPlatform = false;

    private ArrayList<Platform> platforms;

    Player(ArrayList<Platform> platforms) throws IOException {
        this.platforms = platforms;
    }

    @Override
    public void update(float delta) {
        float speed = 10.0f;
        move(speed * direction, 0);

        if (!isOnPlatform) {
            velocity += acceleration * delta;
            move(0, velocity);
        }

        updateHeight();
    }

    private void updateHeight() {
        float playerLeft = getX();
        float playerRight = getX() + getWidth();

        float playerBottom = getY() + getHeight();

        isOnPlatform = false;

        for (Platform platform : platforms) {
            float platformLeft = platform.getX();
            float platformRight = platform.getX() + platform.getWidth();

            float platformTop = platform.getY();
            float platformBottom = platform.getY() + platform.getHeight();

            if (platformLeft <= playerLeft && playerLeft <= platformRight || platformLeft <= playerRight && playerRight <= platformRight ||
                    playerLeft <= platformLeft && platformLeft <= playerRight || playerLeft <= platformRight && platformRight <= playerRight) {
                if (platformTop <= playerBottom && playerBottom <= platformBottom) {
                    velocity = 0;
                    setY(platformTop - getHeight() + 5);
                    isOnPlatform = true;
                }
            }
        }
    }

    @Override
    public void render(Graphics2D g) {
        g.drawImage(image, (int) getX(), (int) getY(), (int) getWidth(), (int) getHeight(), null);
    }

    @Override
    public void handleKeyPress(char keyChar) {
        // w a s d

        if (keyChar == 'd') {
            direction = 1.0f;
        } else if (keyChar == 'a') {
            direction = -1.0f;
        }
    }

    @Override
    public void handleKeyRelease(char keyChar) {
        if (keyChar == 'd' || keyChar == 'a') {
            direction = 0.0f;
        }
    }
}

enum PlatformType {
    Grass,
    Wood
}

class Platform extends GameObject {
    private static Image grassImage;

    static {
        try {
            grassImage = ImageIO.read(new File("C:\\Users\\nikolai\\IdeaProjects\\untitled\\assets\\grass_flat.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static Image woodImage;

    static {
        try {
            woodImage = ImageIO.read(new File("C:\\Users\\nikolai\\IdeaProjects\\untitled\\assets\\wood.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private PlatformType platformType;

    Platform(PlatformType type) throws IOException {
        this.platformType = type;
    }

    @Override
    public void update(float delta) {

    }

    @Override
    public void render(Graphics2D g) {
        Image platformImage = null;

        switch (platformType) {
            case Wood -> platformImage = woodImage;
            case Grass -> platformImage = grassImage;
            default -> platformImage = null;
        }

        g.drawImage(platformImage, (int) getX(), (int) getY(), (int) getWidth(), (int) getHeight(), null);
    }
}

class Viewport extends JPanel {
    private Random random = new Random();
    private final ArrayList<GameObject> gameObjects = new ArrayList<>();
    private final ArrayList<Platform> platforms = new ArrayList<>();

    public Viewport() {

    }

    public void initializeGame() throws IOException {
        Player player = new Player(platforms);
        player.setPosition(0.0f, 0.0f);
        player.setSize(75.0f, 75.0f);

        gameObjects.add(player);

        addPlatform(PlatformType.Grass, 10, 200, 50, 50);
        addPlatform(PlatformType.Grass, 10 + 50 * 1, 200, 50, 50);
        addPlatform(PlatformType.Grass, 10 + 50 * 2, 200, 50, 50);
        addPlatform(PlatformType.Grass, 10 + 50 * 3, 200, 50, 50);

        addPlatform(PlatformType.Grass, 10 + 50 * 4, 200 + 70 * 1, 50, 50);
        addPlatform(PlatformType.Grass, 10 + 50 * 5, 200 + 70 * 1, 50, 50);
        addPlatform(PlatformType.Grass, 10 + 50 * 6, 200 + 70 * 1, 50, 50);

        addPlatform(PlatformType.Grass, 10 + 50 * 7, 200 + 70 * 2, 50, 50);
        addPlatform(PlatformType.Grass, 10 + 50 * 8, 200 + 70 * 2, 50, 50);
    }

    private void addPlatform(PlatformType platformType, float x, float y, float width, float height) throws IOException {
        Platform platform = new Platform(platformType);
        platform.setPosition(x, y);
        platform.setSize(width, height);
        gameObjects.add(platform);
        platforms.add(platform);
    }

    public void handleKeyPress(char keyChar) {
        for (GameObject object : gameObjects) {
            object.handleKeyPress(keyChar);
        }

        repaint();
    }

    public void handleKeyRelease(char keyChar) {
        for (GameObject object : gameObjects) {
            object.handleKeyRelease(keyChar);
        }

        repaint();
    }

    public void updateGame(float delta) {
        for (GameObject object : gameObjects) {
            object.update(delta);
        }
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        g.setColor(Color.BLACK);
        g.fillRect(0, 0, getWidth(), getHeight());

        for (GameObject object : gameObjects) {
            object.render((Graphics2D) g);
        }
    }
}

class GraphicsWindow extends JFrame {
    private final Viewport viewport = new Viewport();

    public GraphicsWindow(String title, int width, int height) throws IOException {
        super(title);
        add(viewport);
        pack();
        setSize(width, height);
        setResizable(false);
        setFocusable(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);

        viewport.initializeGame();

        addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent event) {
                event.consume();
                viewport.handleKeyPress(event.getKeyChar());
            }

            public void keyReleased(KeyEvent event) {
                event.consume();
                viewport.handleKeyRelease(event.getKeyChar());
            }
        });

        Timer timer = new Timer(33, new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                long time = System.currentTimeMillis();

                viewport.updateGame(33.0f / 1000.0f);
                viewport.repaint();
            }
        });

        timer.start();
    }

    public Viewport getViewport() {
        return viewport;
    }
}


public class Main {
    public static void main(String[] args) throws IOException {
        new GraphicsWindow("Asteroids game", 640, 480);
    }
}