uh

 avatar
unknown
java
13 days ago
3.1 kB
1
Indexable
package RaceCar;

import bridges.base.NamedColor;
import bridges.base.NamedSymbol;
import bridges.games.NonBlockingGame;
import credentials.User;
import java.util.Random;

class Racecar extends NonBlockingGame {
    static int gridCol = 30; // Grid width
    static int gridRow = 30; // Grid height

    private int carX, carY; // Car position
    private int leftWall, rightWall; // Road boundaries
    private int obstacleX, obstacleY; // Obstacle position
    private int score = 0; // Score tracking
    private int frameCounter = 0; // Frame counter for road movement
    private Random rand = new Random();

    public Racecar(int assid, String login, String apiKey, int c, int r) {
        super(assid, login, apiKey, c, r);
        setTitle("Race Car");
        setDescription("Drive the car and avoid obstacles!");
        start();
    }

    public void initialize() {
        leftWall = 8; 
        rightWall = 22;
        carX = 15; 
        carY = gridRow - 3; 

        obstacleX = rand.nextInt(rightWall - leftWall - 1) + leftWall + 1;
        obstacleY = rand.nextInt(gridRow / 2) + 5;
    }

    public void handleInput() {
        if (keyLeft() && carX > leftWall + 1) {
            carX--; // Move left
        } else if (keyRight() && carX < rightWall - 1) {
            carX++; // Move right
        }
    }

    public void checkCollision() {
        if (carX <= leftWall || carX >= rightWall || (carX == obstacleX && carY == obstacleY)) {
            die(); // End the game
        }
    }

    private void moveRoad() {
        frameCounter++;

        if (frameCounter % 150 == 0) {
            if (leftWall < rightWall - 5) { 
                leftWall++;
                rightWall--;
            }
        }

        if (frameCounter % 90 == 0) {
            int shift = rand.nextBoolean() ? 1 : -1;
            if (leftWall + shift >= 3 && rightWall + shift <= gridCol - 3) { // Keep within bounds
                leftWall += shift;
                rightWall += shift;
            }
        }
    }

    public void paintScreen() {
        // Clear screen
    	
    	gamegrid.setBGColor(0, 2, NamedColor.purple);
        //fillGrid(NamedSymbol.none, NamedColor.black);

        for (int i = 0; i < gridRow; i++) {
            setBGColor(leftWall, i, NamedColor.gray);
            setBGColor(rightWall, i, NamedColor.gray);
        }

        drawSymbol(carX, carY, NamedSymbol.heart, NamedColor.yellow);
        setBGColor(carX, carY, NamedColor.red);

        drawSymbol(obstacleX, obstacleY, NamedSymbol.diamond, NamedColor.pink);
        setBGColor(obstacleX, obstacleY, NamedColor.orange);
    }

   

	public void die() {
        System.out.println("Game Over! Score: " + score);
        System.exit(0);
    }

    public void gameLoop() {
        handleInput();
        checkCollision();
        moveRoad();
        checkCollision();
        paintScreen();
        score++;
    }

    public static void main(String[] args) throws Exception {
        Racecar game = new Racecar(21, User.USERNAME, User.APIKEY, gridCol, gridRow);
    }
}
Leave a Comment