Created
August 1, 2012 22:09
-
-
Save ra4king/3231135 to your computer and use it in GitHub Desktop.
This file contains 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 static org.lwjgl.opengl.GL11.*; | |
import java.util.ArrayList; | |
import java.util.Random; | |
import org.lwjgl.opengl.*; | |
import org.lwjgl.*; | |
import org.lwjgl.input.Keyboard; | |
public class Map { | |
static int displayHeight = 480; | |
static int displayWidth = 640; | |
static int currHeight = 300; | |
ArrayList<Integer> map = new ArrayList<Integer>(); | |
Random randomHeight = new Random(); | |
static int b = 0; | |
static int offset = 0; | |
static int direction = 1; | |
public Map() { | |
try { | |
Display.setDisplayMode(new DisplayMode(displayWidth, displayHeight)); | |
Display.setTitle("Input"); | |
Display.create(); | |
} catch (LWJGLException e) { | |
e.printStackTrace(); | |
} | |
glMatrixMode(GL_PROJECTION); | |
glLoadIdentity(); | |
glOrtho(0, displayWidth, displayHeight, 0, 1, -1); | |
glMatrixMode(GL_MODELVIEW); | |
for(int i=1; i<=displayWidth; i+=10){ | |
int height = randomHeight.nextInt(60); | |
if (height > 0 && height < 20) { | |
currHeight += 10; | |
} | |
if (height > 40 && height < 60) { | |
currHeight -= 10; | |
} | |
if (currHeight < 200) { | |
currHeight = 300; | |
} | |
if (currHeight > displayHeight) { | |
currHeight = 300; | |
} | |
map.add(currHeight); | |
} | |
while (!Display.isCloseRequested() && !Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) { | |
glClear(GL_COLOR_BUFFER_BIT); | |
// Draw | |
for (int i=1; i<=map.size(); i+= 1){ | |
//System.out.println("Height: "+Integer.valueOf(map.get(i-1).toString())); | |
Box.draw(map.get(i-1)); | |
} | |
offset += direction * Box.boxWidth; | |
if(offset >= displayWidth) | |
offset = 0; | |
if(offset < 0) | |
offset = displayWidth-Box.boxWidth; | |
if (Keyboard.next()) { | |
if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) { | |
direction = 1; | |
} | |
if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) { | |
direction = 0; | |
} | |
if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) { | |
direction = -1; | |
} | |
} | |
Display.update(); | |
Display.sync(60); | |
} | |
Display.destroy(); | |
} | |
private static class Box { | |
public static int boxWidth = 10; | |
public static void draw(int h) { | |
h = displayHeight - h; | |
int x = (b+offset)%displayWidth; | |
glBegin(GL_QUADS); | |
glVertex2f(x, h); // Upper Left | |
glVertex2f(x, displayHeight); // Bottom Left | |
glVertex2f(x + boxWidth, displayHeight); // Bottom Right | |
glVertex2f(x + boxWidth, h); // Upper Right | |
glEnd(); | |
b += 10; | |
if(b == displayWidth) | |
b = 0; | |
} | |
} | |
public static void main(String[] args) { | |
new Map(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment