leereilly / games

:video_game: A list of popular/awesome video games, add-ons, maps, etc. hosted on GitHub. Any genre. Any platform. Any engine.
22.45k stars 3.07k forks source link

Island game #665

Open jasons123fortheuse opened 1 year ago

jasons123fortheuse commented 1 year ago

import java.util.Scanner;

public class Game {
    private Player player;
    private Level currentLevel;
    private boolean running;

    public Game() {
        player = new Player();
        currentLevel = new Level(1);
        running = true;
    }

    public void start() {
        while (running) {
            // Handle player input
            handleInput();

            // Update game state
            update();

            // Draw game elements
            draw();
        }
    }

    private void handleInput() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter a command: ");
        String input = scanner.nextLine();

        switch(input) {
            case "up":
                player.move(0, -1);
                break;
            case "down":
                player.move(0, 1);
                break;
            case "left":
                player.move(-1, 0);
                break;
            case "right":
                player.move(1, 0);
                break;
            default:
                System.out.println("Invalid command!");
                break;
        }
    }

    private void update() {
        // Update game state, such as player position and level progress
        player.update();
        currentLevel.update(player);
    }

    private void draw() {
        // Draw game elements, such as player sprite and level environment
        player.draw();
        currentLevel.draw();
    }

    public static void main(String[] args) {
        Game game = new Game();
        game.start();
    }
}

public class Player {
    private int x, y;
    private int health;
    private int score;

    public Player() {
        x = 0;
        y = 0;
        health = 100;
        score = 0;
    }

    public void update() {
        // Update player state, such as position and health
        health -= 1;

        if (health <= 0) {
            System.out.println("You have died!");
            System.exit(0);
        }
    }

    public void draw() {
        // Draw player sprite
        System.out.println("Player position: (" + x + ", " + y + ")");
    }

    public void move(int dx, int dy) {
        // Move player position by dx, dy
        x += dx;
        y += dy;
    }

    public void takeDamage(int damage) {
        // Reduce player health by damage
PS I love you. And i asked the Ask AI app to write this for me. Get it for free --> https://get-askai.app