Created
August 22, 2012 22:23
-
-
Save jromeem/3430042 to your computer and use it in GitHub Desktop.
Cave game similar to this: http://www.canvasdemos.com/2010/08/09/helicopter/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.Random; | |
import org.lwjgl.LWJGLException; | |
import org.lwjgl.input.Keyboard; | |
import org.lwjgl.input.Mouse; | |
import org.lwjgl.opengl.Display; | |
import org.lwjgl.opengl.DisplayMode; | |
import org.lwjgl.opengl.GL11; | |
import org.newdawn.slick.Color; | |
import org.newdawn.slick.opengl.Texture; | |
import org.newdawn.slick.opengl.TextureLoader; | |
import org.newdawn.slick.util.ResourceLoader; | |
public class CaveRunner { | |
// textures | |
private Texture filir; | |
// texNo | |
private int texNo; | |
private int holder_texNo; | |
// cylce though animation number | |
private int count; | |
// control speed of gravity | |
private int gravity_count; | |
// texture controls | |
public int left_right; | |
public int up_down; | |
// control texture with mouth | |
private int mouse_x; | |
private int mouse_y; | |
// pseudo random seed for the game | |
private int gameSeed; | |
// generate ceiling and floor belts | |
private Stalactite ceil; | |
private Stalagmite floor; | |
private final static int gameSpeed = 10; | |
/** | |
* Start the example | |
*/ | |
public void start() { | |
initGL(800,600); | |
init(); | |
while (true) { | |
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT); | |
try { | |
Thread.sleep(5); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
render(); | |
update(); | |
if (Display.isCloseRequested()) { | |
Display.destroy(); | |
System.exit(0); | |
} | |
} | |
} | |
/** | |
* Initialise the GL display | |
* | |
* @param width The width of the display | |
* @param height The height of the display | |
*/ | |
private void initGL(int width, int height) { | |
try { | |
Display.setDisplayMode(new DisplayMode(width,height)); | |
Display.create(); | |
// makes it suuuuuuuuuuuper slow | |
//Display.setVSyncEnabled(true); | |
} catch (LWJGLException e) { | |
e.printStackTrace(); | |
System.exit(0); | |
} | |
GL11.glEnable(GL11.GL_TEXTURE_2D); | |
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); | |
// // enable alpha blending | |
GL11.glEnable(GL11.GL_BLEND); | |
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); | |
// GL11.glViewport(0,0,width,height); | |
// GL11.glMatrixMode(GL11.GL_MODELVIEW); | |
// GL11.glMatrixMode(GL11.GL_PROJECTION); | |
// GL11.glLoadIdentity(); | |
GL11.glOrtho(0, width, height, 0, 1, -1); | |
// GL11.glMatrixMode(GL11.GL_MODELVIEW); | |
} | |
/** | |
* Initialise resources | |
*/ | |
public void init() { | |
try { | |
// // load texture from PNG file | |
// //texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("C:/Users/Jerome Martinez/Desktop/Artsy/pokemon/302MS.png")); | |
// //texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("C:/Users/Jerome Martinez/Desktop/Font/PoringCard.JPG")); | |
// //texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("C:/Users/Jerome Martinez/Desktop/Font/font-a.png")); | |
// //texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("C:/Users/Jerome Martinez/Desktop/Font/happy.png")); | |
filir = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("filir-final-right.png")); | |
// System.out.println("Texture loaded: "+texture); | |
// System.out.println(">> Image width: "+texture.getImageWidth()); | |
// System.out.println(">> Image height: "+texture.getImageHeight()); | |
// System.out.println(">> Texture width: "+texture.getTextureWidth()); | |
// System.out.println(">> Texture height: "+texture.getTextureHeight()); | |
// System.out.println(">> Texture ID: "+texture.getTextureID()); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
// generate game seed | |
Random r = new Random(); | |
gameSeed = r.nextInt(); | |
// generate structures | |
ceil = new Stalactite(); | |
floor = new Stalagmite(); | |
holder_texNo = 0; | |
texNo = 0; | |
count = 0; | |
} | |
/** | |
* draw a quad with the image on it | |
*/ | |
public void render() { | |
// draw structures | |
for (Quad g : floor.quad_ray) { | |
if (g.isFloatToRight()) | |
g.generateNewStructure(); | |
g.drawQuad(); | |
g.moveLeft(); | |
} | |
for (Quad c : ceil.quad_ray) { | |
if (c.isFloatToRight()) | |
c.generateNewStructure(); | |
c.drawQuad(); | |
c.moveLeft(); | |
} | |
// // red quad | |
// GL11.glColor3f(1.0f,0.3f,0.3f); | |
// GL11.glBegin(GL11.GL_QUADS); | |
// GL11.glVertex2f(100 +left_right, 290 + up_down); | |
// GL11.glVertex2f(100+20 +left_right, 290 + up_down); | |
// GL11.glVertex2f(100+20 +left_right, 290+20 + up_down); | |
// GL11.glVertex2f(100 +left_right, 290+20 + up_down); | |
// GL11.glEnd(); | |
int[] sprite_order = {3, 2, 1, 0, 6, 7}; | |
// cycle through animation | |
count++; | |
if (count == 22) { | |
texNo = sprite_order[holder_texNo++]; | |
if (texNo > 5) | |
holder_texNo = 0; | |
count = 0; | |
} | |
// how many columns && how many rows | |
float texCol = 4; | |
float texRow = 4; | |
// square length of one sprite | |
int singleTex = 64; | |
// square length of the whole sheet | |
int wholeTex = 256; | |
float x0 = singleTex * ((texNo) % (texCol)); | |
float x1 = x0 + singleTex; | |
float y0 = (float) (singleTex * (Math.floor((texNo) / texRow))); | |
float y1 = y0 + singleTex; | |
x0 = x0 / wholeTex; | |
x1 = x1 / wholeTex; | |
y0 = y0 / wholeTex; | |
y1 = y1 / wholeTex; | |
Color.white.bind(); | |
filir.bind(); // or GL11.glBind(texture.getTextureID()); | |
GL11.glBegin(GL11.GL_QUADS); | |
GL11.glTexCoord2f(x0,y0); | |
GL11.glVertex2f(230 + left_right,230 + up_down); | |
GL11.glTexCoord2f(x1,y0); | |
GL11.glVertex2f(230+64+ left_right,230+ up_down); | |
GL11.glTexCoord2f(x1,y1); | |
GL11.glVertex2f(230+64+ left_right,230+64+ up_down); | |
GL11.glTexCoord2f(x0,y1); | |
GL11.glVertex2f(230+ left_right,230+64+ up_down); | |
GL11.glEnd(); | |
// // loaded texture2 | |
// Color.white.bind(); | |
// texture2.bind(); // or GL11.glBind(texture.getTextureID()); | |
// | |
// int tex_width = texture2.getTextureWidth(); | |
// int tex_height = texture2.getTextureHeight(); | |
// center image on the mouse | |
// GL11.glBegin(GL11.GL_QUADS); | |
// GL11.glTexCoord2f(0,0); | |
// GL11.glVertex2f(mouse_x - 70, -mouse_y + tex_height + 10); | |
// GL11.glTexCoord2f(1,0); | |
// GL11.glVertex2f(mouse_x + 50+30, -mouse_y + tex_height + 10); | |
// GL11.glTexCoord2f(1,1); | |
// GL11.glVertex2f(mouse_x + 50+30, -mouse_y + 50 + tex_height + 110); | |
// GL11.glTexCoord2f(0,1); | |
// GL11.glVertex2f(mouse_x-70, -mouse_y + 50 + tex_height + 110); | |
// GL11.glEnd(); | |
// // loaded texture | |
// Color.white.bind(); | |
// texture.bind(); // or GL11.glBind(texture.getTextureID()); | |
// GL11.glBegin(GL11.GL_QUADS); | |
// GL11.glTexCoord2f(0,0); | |
// GL11.glVertex2f(10 + left_right ,10 + up_down); | |
// GL11.glTexCoord2f(1,0); | |
// GL11.glVertex2f(10 + left_right +texture.getTextureWidth(),10 + up_down); | |
// GL11.glTexCoord2f(1,1); | |
// GL11.glVertex2f(10 + left_right + texture.getTextureWidth(),10 + up_down + texture.getTextureHeight()); | |
// GL11.glTexCoord2f(0,1); | |
// GL11.glVertex2f(10 + left_right ,10+texture.getTextureHeight() + up_down); | |
// GL11.glEnd(); | |
} | |
public void update() { | |
mouse_x = Mouse.getX(); | |
mouse_y = Mouse.getY(); | |
if (Mouse.isButtonDown(0)) { | |
System.out.println(); | |
System.out.println("x:" +mouse_x); | |
System.out.println("y:" +mouse_y); | |
} | |
if (Keyboard.isKeyDown(Keyboard.KEY_Q)) { | |
Display.destroy(); | |
System.exit(0); | |
} | |
if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) { | |
// border | |
if (left_right > -230 + -16) | |
left_right-=1; | |
} | |
if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) { | |
// border | |
if (up_down <= 320) | |
up_down+=1; | |
} | |
if (Keyboard.isKeyDown(Keyboard.KEY_UP)) { | |
// border | |
if (up_down > -230 + -8) | |
up_down-=1; | |
} | |
if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) { | |
// border | |
if(left_right <= 520) | |
left_right+=1; | |
} | |
// int up_gravity = 1; | |
// | |
// gravity_count++; | |
// | |
// | |
// if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)) { | |
// | |
// if (gravity_count == 25 || gravity_count == 50) | |
// up_gravity++; | |
// | |
// if (up_down > -230 + -8) { | |
// up_down -= 2 * up_gravity; | |
// System.out.println("up_gravity: " + up_gravity); | |
// } | |
// } | |
// | |
// | |
// if (gravity_count == 75) | |
// up_gravity--; | |
// | |
// if (up_gravity < 2) | |
// up_gravity = 1; | |
// | |
// up_down += 1 * up_gravity; | |
// | |
// | |
// if (gravity_count == 100) | |
// gravity_count = 0; | |
Display.update(); | |
} | |
/** | |
* Main Class | |
*/ | |
public static void main(String[] argv) { | |
CaveRunner textureExample = new CaveRunner(); | |
textureExample.start(); | |
} | |
} | |
class Stalagmite { | |
public ArrayList<Quad> quad_ray; | |
private Random r; | |
private int groundSeed; | |
private int depth; | |
public Stalagmite() { | |
r = new Random(); | |
quad_ray = new ArrayList<Quad>(); | |
groundSeed = r.nextInt(); | |
depth = r.nextInt(280) + 300; | |
startBuilding(); | |
} | |
public void startBuilding() { | |
int gradient = 10; | |
for (int i = 800 +10+1; i>0; i-=gradient) { | |
int change = r.nextInt(13)+1; | |
quad_ray.add(new Quad(i-gradient, depth, i, 600)); | |
depth = depth + (change - 7); | |
} | |
} | |
} | |
class Stalactite { | |
public ArrayList<Quad> quad_ray; | |
private Random r; | |
private int ceilingSeed; | |
private int height; | |
public Stalactite() { | |
r = new Random(); | |
quad_ray = new ArrayList<Quad>(); | |
ceilingSeed = r.nextInt(); | |
height = r.nextInt(280); | |
startDripping(); | |
} | |
public void startDripping() { | |
int gradient = 10; | |
for (int i=800 +10+1; i>0; i-=gradient) { | |
int change = r.nextInt(13) + 1; | |
quad_ray.add(new Quad(i-gradient, 0, i, height)); | |
height = height + (change - 7); | |
} | |
} | |
} | |
class Quad{ | |
private int vertex1_x; | |
private int vertex1_y; | |
private int vertex2_x; | |
private int vertex2_y; | |
private float red; | |
private float green; | |
private float blue; | |
private boolean floatToRight; | |
private Random r; | |
private Texture stone; | |
public Quad(int vertex1x, int vertex1y, int vertex2x, int vertex2y) { | |
setVertex1_x(vertex1x); | |
setVertex1_y(vertex1y); | |
setVertex2_x(vertex2x); | |
setVertex2_y(vertex2y); | |
r = new Random(); | |
try { | |
stone = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("stone.png")); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
public void generateNewStructure() { | |
floatToRight = false; | |
int change = r.nextInt(13) + 1; | |
if (vertex1_y == 0) | |
vertex2_y = vertex2_y + (change - 7); | |
else | |
vertex1_y = vertex1_y + (change - 7); | |
} | |
public void setColor() { | |
Random r = new Random(); | |
red = (float) (r.nextInt(255) / 255.0); | |
green = (float) (r.nextInt(255) / 255.0); | |
blue = (float) (r.nextInt(255) / 255.0); | |
} | |
public void moveLeft() { | |
vertex1_x--; | |
vertex2_x--; | |
if (vertex2_x < 0) { | |
setFloatToRight(true); | |
vertex1_x = 800; | |
vertex2_x = 820; | |
} | |
} | |
public void drawQuad() { | |
// GL11.glColor3f(0.8f,0.7f,0.6f); | |
// //GL11.glColor3f(red,green,blue); | |
Color.white.bind(); | |
stone.bind(); // or GL11.glBind(texture.getTextureID()); | |
GL11.glBegin(GL11.GL_QUADS); | |
GL11.glTexCoord2f(0,0); | |
GL11.glVertex2f(vertex1_x, vertex1_y); | |
GL11.glTexCoord2f(1,0); | |
GL11.glVertex2f(vertex2_x, vertex1_y); | |
GL11.glTexCoord2f(1,1); | |
GL11.glVertex2f(vertex2_x, vertex2_y); | |
GL11.glTexCoord2f(0,1); | |
GL11.glVertex2f(vertex1_x, vertex2_y); | |
GL11.glEnd(); | |
} | |
public int getVertex2_y() { | |
return vertex2_y; | |
} | |
public void setVertex2_y(int vertex2_y) { | |
this.vertex2_y = vertex2_y; | |
} | |
public int getVertex2_x() { | |
return vertex2_x; | |
} | |
public void setVertex2_x(int vertex2_x) { | |
this.vertex2_x = vertex2_x; | |
} | |
public int getVertex1_y() { | |
return vertex1_y; | |
} | |
public void setVertex1_y(int vertex1_y) { | |
this.vertex1_y = vertex1_y; | |
} | |
public int getVertex1_x() { | |
return vertex1_x; | |
} | |
public void setVertex1_x(int vertex1_x) { | |
this.vertex1_x = vertex1_x; | |
} | |
public boolean isFloatToRight() { | |
return floatToRight; | |
} | |
public void setFloatToRight(boolean floatToRight) { | |
this.floatToRight = floatToRight; | |
} | |
} |
Hehe... startDripping()
Yeah collision detection is fun, I wrote a bunch of games in Java and C# that had to do it.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
the code is kinda messy...but overall it works.
collision detection is a bitch to code though...