You are on page 1of 8

package examen2;

import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.FloatBuffer;

import javax.imageio.ImageIO;
import javax.swing.JFrame;

import com.jogamp.common.nio.Buffers;
import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLCapabilities;
import com.jogamp.opengl.GLEventListener;
import com.jogamp.opengl.GLProfile;
import com.jogamp.opengl.awt.GLJPanel;
import com.jogamp.opengl.util.FPSAnimator;
import com.jogamp.opengl.util.awt.ImageUtil;
import com.jogamp.opengl.util.texture.Texture;
import com.jogamp.opengl.util.texture.awt.AWTTextureIO;

public class Principal extends GLJPanel implements GLEventListener, KeyListener {


private static final long serialVersionUID = 1L;

float light = 1f;

// Nombres de las imagenes.extension


private String[] textureFileRonaldo = { "ambiente3.png",

};

private Texture[] textures = new Texture[textureFileRonaldo.length];

// forma
private float[] polygonCoords = {
// cuadrado
-1f, 0.2f, 0f, -1f, 1f, 0f, -0.6f, 1f, 0f, -0.6f, 0.2f, 0f,
// fantastma anaranjado
0.0f, 0.0f, 0, 0.2f, 0.0f, 0, 0.2f, 0.2f, 0, 0.0f, 0.2f, 0,
// fantasma rojo
0.0f, 0.0f, 0, 0.2f, 0.0f, 0, 0.2f, 0.2f, 0, 0.0f, 0.2f, 0,

};
// Textura de los cuadrados
private float[] polygonTexture = {
// cuadrado
0.017f, 0.386f, 0f, 0.017f, 0.984f, 0f, 0.409f, 0.9846f, 0f,
0.409f, 0.386f, 0f,
// fantastma anaranjado
0.55f, 0.39f, 0, 0.82f, 0.39f, 0, 0.82f, 0.68f, 0, 0.55f, 0.68f,
0,
// fantasma rojo
0.56f, 0.7f, 0, 0.83f, 0.7f, 0, 0.83f, 0.98f, 0, 0.56f, 0.98f, 0,

};
// Color de los poligonos
private float[] polygonFaceColors = {
// cuadsrado
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
// fantasma
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1,

};

private FloatBuffer polygonCoordBuffer =


Buffers.newDirectFloatBuffer(polygonCoords);
private FloatBuffer polygonTextureBuffer =
Buffers.newDirectFloatBuffer(polygonTexture);
private FloatBuffer polygonFaceColorBuffer =
Buffers.newDirectFloatBuffer(polygonFaceColors);

private float[] luzAmbiente = { 0.2f, 0.2f, 0.2f, 1.0f };


private float[] luzDifusa = { light, light, light, 1.0f };
private float[] luzPos = { -1.0f, 0.5f, 5f, 0f };
// rojo 1
ghost g1 = new ghost(0.4f, 0.0f);
// rojo 2
ghost g2 = new ghost(0.4f, 0.6f);

// naranja 1
ghost g3 = new ghost(-0.4f, 1.5f);
// naranja 2
ghost g4 = new ghost(-0.4f, 1.2f);

public static void main(String[] args) {


JFrame window = new JFrame("Programación Grafica-RRY");
Principal panel = new Principal();
window.setContentPane(panel);
window.pack();
window.setLocationRelativeTo(null);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
panel.requestFocusInWindow();
FPSAnimator animator = new FPSAnimator(panel, 400, true);
animator.start();
}

public Principal() {
super(new GLCapabilities(null)); // Makes a panel with default OpenGL
"capabilities".
setDoubleBuffered(true);
setPreferredSize(new Dimension(720, 480));
addGLEventListener(this); // A listener is essential! The listener is
where the OpenGL programming lives.
addKeyListener(this);

@Override
public void display(GLAutoDrawable drawable) {
// TODO Auto-generated method stub
GL2 gl = drawable.getGL().getGL2();
// gl.glClearColor(0.2f, 0.2f, 0.2f, 0.0f);
gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT);

gl.glVertexPointer(3, GL2.GL_FLOAT, 0, polygonCoordBuffer);


gl.glColorPointer(3, GL2.GL_FLOAT, 0, polygonFaceColorBuffer);
gl.glTexCoordPointer(3, GL2.GL_FLOAT, 0, polygonTextureBuffer);

// Definir luz (tipo de luz, componente de la luz, array, )


gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_AMBIENT, luzAmbiente, 0);
gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_DIFFUSE, luzDifusa, 0);
gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, luzPos, 0);

// gl.glEnable(GL2.GL_LIGHT0);
// gl.glEnable(GL2.GL_LIGHTING);
gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL2.GL_COLOR_ARRAY);
gl.glEnableClientState(GL2.GL_TEXTURE_COORD_ARRAY);

gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();

// cuadrados
textures[0].bind(gl);
gl.glPushMatrix();
gl.glDrawArrays(GL2.GL_QUADS, 0, 4);
gl.glPopMatrix();

textures[0].bind(gl);
gl.glPushMatrix();
gl.glTranslatef(0.8f, 0, 0);
gl.glDrawArrays(GL2.GL_QUADS, 0, 4);
gl.glPopMatrix();

textures[0].bind(gl);
gl.glPushMatrix();
gl.glTranslatef(0f, -1.2f, 0);
gl.glDrawArrays(GL2.GL_QUADS, 0, 4);
gl.glPopMatrix();

textures[0].bind(gl);
gl.glPushMatrix();
gl.glTranslatef(0.8f, -1.2f, 0);
gl.glDrawArrays(GL2.GL_QUADS, 0, 4);
gl.glPopMatrix();

textures[0].bind(gl);
gl.glPushMatrix();
gl.glTranslatef(1.6f, 0f, 0);
gl.glDrawArrays(GL2.GL_QUADS, 0, 4);
gl.glPopMatrix();

textures[0].bind(gl);
gl.glPushMatrix();
gl.glTranslatef(1.6f, -1.2f, 0);
gl.glDrawArrays(GL2.GL_QUADS, 0, 4);
gl.glPopMatrix();

gl.glDisableClientState(GL2.GL_TEXTURE_COORD_ARRAY);
gl.glDisableClientState(GL2.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL2.GL_COLOR_ARRAY);

gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL2.GL_COLOR_ARRAY);
gl.glEnableClientState(GL2.GL_TEXTURE_COORD_ARRAY);
textures[0].bind(gl);

gl.glPushMatrix();
gl.glTranslatef(-(0.2f) / 2 + g3.getX(), -(0.2f) / 2 + g3.getY(), 0);
gl.glDrawArrays(GL2.GL_POLYGON, 4, 4);
gl.glPopMatrix();

gl.glPushMatrix();
gl.glTranslatef(-(0.2f) / 2 + g1.getX(), -(0.2f) / 2 + g1.getY(), 0);
gl.glDrawArrays(GL2.GL_POLYGON, 8, 4);
gl.glPopMatrix();

gl.glPushMatrix();
gl.glTranslatef(-(0.2f) / 2 + g2.getX(), -(0.2f) / 2 + g2.getY(), 0);
gl.glDrawArrays(GL2.GL_POLYGON, 8, 4);
gl.glPopMatrix();

// gl.glPushMatrix();
// gl.glTranslatef(-(0.2f) / 2 + g4.getX(), -(0.2f) / 2 + g4.getY(), 0);
// gl.glDrawArrays(GL2.GL_POLYGON, 8, 4);
// gl.glPopMatrix();
if (g1.getY() >= -1.2f) {
if (g1.y <= 0f) {
if (g1.x > -0.4f) {
g1.x -= g1.movx;
} else {
g1.y -= g1.movy;
}

} else {
g1.y -= g1.movy;
}

} else {
g1.x = 0.4f;
g1.y = 1.2f;

if (g2.getY() >= -1.2f) {


if (g2.y <= 0f) {
if (g2.x > -0.4f) {
g2.x -= g2.movx;
} else {
g2.y -= g2.movy;
}

} else {
g2.y -= g2.movy;
}
} else {
g2.x = 0.4f;
g2.y = 1.2f;

if (g3.getY() >= -1.2f) {


if (g3.y <= 0f) {
if (g3.x < 0.4f) {
g3.x += g3.movx;
} else {
g3.y -= g3.movy;
}

} else {
g3.y -= g3.movy;
}

} else {
g3.x = -0.4f;
g3.y = 1.2f;

}
if (g4.getY() >= -1.2f) {
if (g4.y <= 0f) {
if (g4.x < 0.4f) {
g4.x += g4.movx;
} else {
g4.y -= g4.movy;
}

} else {
g4.y -= g4.movy;
}

} else {
g4.x = -0.4f;
g4.y = 1.2f;

}
colision();

gl.glDisable(GL2.GL_LIGHTING);
gl.glDisableClientState(GL2.GL_TEXTURE_COORD_ARRAY);
gl.glDisableClientState(GL2.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL2.GL_COLOR_ARRAY);

gl.glFlush();

void colision() {

colisionfantastamas(g1, g2);
colisionfantastamas(g1, g3);
colisionfantastamas(g1, g4);

colisionfantastamas(g2, g3);
colisionfantastamas(g2, g4);
colisionfantastamas(g3, g4);

}
void colisionfantastamas(ghost g1, ghost g2) {
if (g1.getY() >= -1.2f) {
if (g1.getY() <= 0f) {
if (g1.getX() > -0.4f) {
g1.setX(g1.getX() - g1.movx);
} else {
g1.setY(g1.getY() + g1.movy);
}
} else {
g1.setY(g1.getY() + g1.movy);
}
} else {
g1.setX(0.4f);
g1.setY(1.2f);
}

// Reducir la velocidad de movimiento de g2 ajustando el valor de movy


float reducedMovy = g2.movy * 0.15f; // Puedes ajustar el factor de
reducción según tu preferencia
g2.movy = reducedMovy;

if (g2.getY() >= -1.2f) {


if (g2.getY() <= 0f) {
if (g2.getX() > -0.4f) {
g2.setX(g2.getX() - g2.movx);
} else {
g2.setY(g2.getY() + g2.movy);
}
} else {
g2.setY(g2.getY() + g2.movy);
}
} else {
g2.setX(0.4f);
g2.setY(1.2f);
}
}

double boca = 0;
int rotar = 0;
boolean abierto = true;
float xpacma = 0.5f;

@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
int key = e.getKeyCode();
// arriba
if (key == KeyEvent.VK_UP) {

}
// abajo
if (key == KeyEvent.VK_DOWN) {

}
// Derecha
if (key == 39) {

// Izquierda
if (key == 37) {
rotar = 180;
if (xpacma >= -1.1f) {
xpacma -= 0.01f;
} else {
xpacma = 1.1f;
}

}
// espacio
if (key == 32) {

@Override
public void dispose(GLAutoDrawable drawable) {
// TODO Auto-generated method stub

@Override
public void init(GLAutoDrawable drawable) {
// TODO Auto-generated method stub
GL2 gl2 = drawable.getGL().getGL2();
gl2.glClearColor(1f, 1f, 1f, 1.0f);

for (int i = 0; i < textureFileRonaldo.length; i += 1) {


BufferedImage img = null;
try {
img = ImageIO.read(new File("images/" +
textureFileRonaldo[i]));
ImageUtil.flipImageVertically(img);
textures[i] =
AWTTextureIO.newTexture(GLProfile.getDefault(), img, true);
textures[i].setTexParameteri(gl2, GL2.GL_TEXTURE_WRAP_S,
GL2.GL_REPEAT);
textures[i].setTexParameteri(gl2, GL2.GL_TEXTURE_WRAP_T,
GL2.GL_REPEAT);
textures[i].enable(gl2);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

@Override
public void reshape(GLAutoDrawable drawable, int arg1, int arg2, int arg3,
int arg4) {
// TODO Auto-generated method stub
GL2 gl = drawable.getGL().getGL2();
}

@Override
public void keyTyped(java.awt.event.KeyEvent e) {
// TODO Auto-generated method stub

@Override
public void keyReleased(java.awt.event.KeyEvent e) {
// TODO Auto-generated method stub

public class ghost {


private float x = 0;
private float y = 0;
private float movx = 0.005f;
private float movy = 0.005f;

public ghost() {

public ghost(float x, float y) {


super();
this.x = x;
this.y = y;
}

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;
}

}
}

You might also like