proyecto subido
This commit is contained in:
parent
109c3c5e9e
commit
54428561a9
pom.xml
src/main/java/com/mycompany/ascii
target
classes/com/mycompany/ascii
maven-status/maven-compiler-plugin/compile/default-compile
|
@ -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>Ascii</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>19</maven.compiler.source>
|
||||
<maven.compiler.target>19</maven.compiler.target>
|
||||
<exec.mainClass>com.mycompany.ascii.Ascii</exec.mainClass>
|
||||
</properties>
|
||||
</project>
|
|
@ -0,0 +1,8 @@
|
|||
package com.mycompany.ascii;
|
||||
|
||||
public class Ascii {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Ventana v=new Ventana();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,160 @@
|
|||
package com.mycompany.ascii;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Random;
|
||||
|
||||
public class Ventana extends JFrame {
|
||||
private MiPanel panel;
|
||||
|
||||
public Ventana() {
|
||||
setTitle("Spacecraft Animation");
|
||||
setSize(800, 400);
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
setLocationRelativeTo(null);
|
||||
|
||||
panel = new MiPanel();
|
||||
add(panel);
|
||||
setVisible(true);
|
||||
|
||||
panel.inicializarEstrellas();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class MiPanel extends JPanel implements KeyListener {
|
||||
private Nave nave;
|
||||
private ArrayList<Point> estrellas;
|
||||
private Random rand = new Random();
|
||||
|
||||
public MiPanel() {
|
||||
nave = new Nave(this);
|
||||
setFocusable(true);
|
||||
addKeyListener(this);
|
||||
|
||||
estrellas = new ArrayList<>();
|
||||
}
|
||||
|
||||
public void inicializarEstrellas() {
|
||||
int ancho = Math.max(getWidth(), 800);
|
||||
int alto = Math.max(getHeight(), 400);
|
||||
|
||||
for (int i = 0; i < 100; i++) {
|
||||
estrellas.add(new Point(rand.nextInt(ancho), rand.nextInt(alto)));
|
||||
}
|
||||
|
||||
Thread hilo = new Thread(nave);
|
||||
hilo.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
|
||||
g.setColor(Color.BLACK);
|
||||
g.fillRect(0, 0, getWidth(), getHeight());
|
||||
|
||||
g.setColor(Color.WHITE);
|
||||
for (Point estrella : estrellas) {
|
||||
g.fillRect(estrella.x, estrella.y, 2, 2);
|
||||
}
|
||||
|
||||
nave.dibujar(g);
|
||||
}
|
||||
|
||||
public void moverEstrellas() {
|
||||
int ancho = getWidth();
|
||||
int alto = getHeight();
|
||||
|
||||
for (Point estrella : estrellas) {
|
||||
estrella.y += 2;
|
||||
if (estrella.y > alto) {
|
||||
estrella.y = 0;
|
||||
estrella.x = rand.nextInt(Math.max(ancho, 800));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void keyPressed(KeyEvent e) {
|
||||
nave.keyPressed(e);
|
||||
}
|
||||
|
||||
public void keyReleased(KeyEvent e) {
|
||||
nave.keyReleased(e);
|
||||
}
|
||||
|
||||
public void keyTyped(KeyEvent e) {}
|
||||
}
|
||||
|
||||
class Nave implements Runnable {
|
||||
private int x = 100;
|
||||
private int y = 150;
|
||||
private int xVel = 0;
|
||||
private int yVel = 0;
|
||||
private final int VELOCIDAD = 8;
|
||||
private MiPanel panel;
|
||||
|
||||
public Nave(MiPanel panel) {
|
||||
this.panel = panel;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (true) {
|
||||
try {
|
||||
mover();
|
||||
panel.moverEstrellas();
|
||||
panel.repaint();
|
||||
Thread.sleep(30);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void dibujar(Graphics g) {
|
||||
g.setColor(Color.CYAN);
|
||||
g.drawString(" oOOOo", x, y);
|
||||
g.drawString(" AAAAAAAAAAA", x, y + 10);
|
||||
g.drawString(" zzzzzzzzzzzzzzzz", x, y + 20);
|
||||
g.drawString(" <><><><><><><><><>", x, y + 30);
|
||||
g.drawString(" XXXXXXXXXXXXXXXXXXXXXX", x, y + 40);
|
||||
g.drawString("XXXXXXXXXXXXXXXXXXXXXXXXX", x, y + 50);
|
||||
g.drawString(" XXX[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]XXX", x, y + 60);
|
||||
g.drawString(" ~XXXXXXXXXXXXXXX~", x, y + 70);
|
||||
g.drawString(" ( <><><><> )", x, y + 80);
|
||||
g.drawString(" ( ) ( ) ( )", x, y + 90);
|
||||
g.drawString(" ( ) ( ) ( )", x, y + 100);
|
||||
g.drawString(" ( ) ( ) ( )", x, y + 110);
|
||||
g.drawString(" ( ) ( ) ( )", x, y + 120);
|
||||
g.drawString(" ( ) ( )", x, y + 130);
|
||||
}
|
||||
|
||||
private void mover() {
|
||||
x += xVel;
|
||||
y += yVel;
|
||||
|
||||
// Evitar que la nave salga de la pantalla
|
||||
if (x < 0) x = 0;
|
||||
if (x > panel.getWidth() - 100) x = panel.getWidth() - 100;
|
||||
if (y < 0) y = 0;
|
||||
if (y > panel.getHeight() - 70) y = panel.getHeight() - 70;
|
||||
}
|
||||
|
||||
public void keyPressed(KeyEvent e) {
|
||||
int keyCode = e.getKeyCode();
|
||||
if (keyCode == KeyEvent.VK_A) xVel = -VELOCIDAD;
|
||||
if (keyCode == KeyEvent.VK_D) xVel = VELOCIDAD;
|
||||
if (keyCode == KeyEvent.VK_W) yVel = -VELOCIDAD;
|
||||
if (keyCode == KeyEvent.VK_S) yVel = VELOCIDAD;
|
||||
}
|
||||
|
||||
public void keyReleased(KeyEvent e) {
|
||||
int keyCode = e.getKeyCode();
|
||||
if (keyCode == KeyEvent.VK_A || keyCode == KeyEvent.VK_D) xVel = 0;
|
||||
if (keyCode == KeyEvent.VK_W || keyCode == KeyEvent.VK_S) yVel = 0;
|
||||
}
|
||||
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,4 @@
|
|||
com\mycompany\ascii\Ascii.class
|
||||
com\mycompany\ascii\Nave.class
|
||||
com\mycompany\ascii\MiPanel.class
|
||||
com\mycompany\ascii\Ventana.class
|
|
@ -0,0 +1,2 @@
|
|||
C:\Users\alasn\Desktop\Tareas UV\desarrolloSoftware\asciiArt\Ascii\src\main\java\com\mycompany\ascii\Ventana.java
|
||||
C:\Users\alasn\Desktop\Tareas UV\desarrolloSoftware\asciiArt\Ascii\src\main\java\com\mycompany\ascii\Ascii.java
|
Loading…
Reference in New Issue