Juego esquivar obstaculos v1.0
This commit is contained in:
commit
b32b8d71e7
|
@ -0,0 +1,14 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.mycompany</groupId>
|
||||
<artifactId>JuegoAscii</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>20</maven.compiler.source>
|
||||
<maven.compiler.target>20</maven.compiler.target>
|
||||
<exec.mainClass>com.mycompany.juegoascii.JuegoAscii</exec.mainClass>
|
||||
</properties>
|
||||
</project>
|
|
@ -0,0 +1,111 @@
|
|||
package com.mycompany.juegoascii;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.KeyAdapter;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.util.Random;
|
||||
|
||||
public class JuegoAscii extends JPanel {
|
||||
private static final int ROAD_WIDTH = 10;
|
||||
private static final int ROAD_HEIGHT = 15;
|
||||
private static final char PLAYER_CAR = '^';
|
||||
private static final char OBSTACLE = '#';
|
||||
private static final char EMPTY = ' ';
|
||||
private int playerPosition = ROAD_WIDTH / 2;
|
||||
private char[][] road = new char[ROAD_HEIGHT][ROAD_WIDTH];
|
||||
private boolean gameRunning = true;
|
||||
private Random random = new Random();
|
||||
private int score = 0;
|
||||
|
||||
public JuegoAscii() {
|
||||
setPreferredSize(new Dimension(300, 450));
|
||||
setBackground(Color.BLACK);
|
||||
initializeRoad();
|
||||
|
||||
Timer timer = new Timer(300, e -> {
|
||||
updateRoad();
|
||||
checkCollision();
|
||||
score++;
|
||||
repaint();
|
||||
});
|
||||
timer.start();
|
||||
|
||||
setFocusable(true);
|
||||
addKeyListener(new KeyAdapter() {
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
|
||||
movePlayer(-1);
|
||||
} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
|
||||
movePlayer(1);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void initializeRoad() {
|
||||
for (int i = 0; i < ROAD_HEIGHT; i++) {
|
||||
for (int j = 0; j < ROAD_WIDTH; j++) {
|
||||
road[i][j] = EMPTY;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void updateRoad() {
|
||||
for (int i = ROAD_HEIGHT - 1; i > 0; i--) {
|
||||
System.arraycopy(road[i - 1], 0, road[i], 0, ROAD_WIDTH);
|
||||
}
|
||||
for (int j = 0; j < ROAD_WIDTH; j++) {
|
||||
road[0][j] = (random.nextInt(10) < 2) ? OBSTACLE : EMPTY;
|
||||
}
|
||||
}
|
||||
|
||||
private void movePlayer(int direction) {
|
||||
int newPosition = playerPosition + direction;
|
||||
if (newPosition >= 0 && newPosition < ROAD_WIDTH) {
|
||||
playerPosition = newPosition;
|
||||
}
|
||||
}
|
||||
|
||||
private void checkCollision() {
|
||||
if (road[ROAD_HEIGHT - 1][playerPosition] == OBSTACLE) {
|
||||
gameRunning = false;
|
||||
JOptionPane.showMessageDialog(this, "¡Juego terminado!\nPuntaje: " + score, "Game Over", JOptionPane.ERROR_MESSAGE);
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
g.setColor(Color.WHITE);
|
||||
g.setFont(new Font("Monospaced", Font.PLAIN, 14));
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
int y = 20;
|
||||
|
||||
for (int i = 0; i < ROAD_HEIGHT; i++) {
|
||||
StringBuilder line = new StringBuilder("|");
|
||||
for (int j = 0; j < ROAD_WIDTH; j++) {
|
||||
if (i == ROAD_HEIGHT - 1 && j == playerPosition) {
|
||||
line.append(PLAYER_CAR);
|
||||
} else {
|
||||
line.append(road[i][j]);
|
||||
}
|
||||
}
|
||||
line.append("|");
|
||||
g2d.drawString(line.toString(), 20, y);
|
||||
y += 16;
|
||||
}
|
||||
g2d.drawString("Puntaje: " + score, 20, y + 20);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
JFrame frame = new JFrame("ASCII Car Game");
|
||||
JuegoAscii game = new JuegoAscii();
|
||||
frame.add(game);
|
||||
frame.pack();
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
frame.setVisible(true);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue