Skip to content

Instantly share code, notes, and snippets.

@mono0x
Created January 25, 2012 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mono0x/1676589 to your computer and use it in GitHub Desktop.
Save mono0x/1676589 to your computer and use it in GitHub Desktop.
ActionGame
int cellSize = 16;
int defaultJumpLimit = 10;
int readyState = 0;
int gameState = 1;
int clearedState = 2;
int emptyCell = 0;
int wallCell = 1;
int goalCell = 2;
int needleCell = 3;
int[][] stage = {
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,1,0,1,},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,1,0,1,},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,1,3,3,0,0,0,0,0,0,0,0,0,0,1,0,1,},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,3,0,0,0,0,3,3,3,3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0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,1,},
{1,0,0,1,1,3,3,3,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,3,3,3,3,3,0,0,0,0,3,3,3,3,3,3,3,3,3,3,3,3,3,1,},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,},
};
float gravity = 0.9;
abstract class Actor {
float x, y, px, py, ax, ay;
boolean terminated;
public Actor(float x, float y) {
this.x = x;
this.y = y;
this.px = x;
this.py = y;
this.ax = 0;
this.ay = 0;
this.terminated = false;
}
public void move() {
float tx = x;
float ty = y;
x += (x - px) + ax;
y += (y - py) + ay;
px = tx;
py = ty;
}
public abstract void display();
}
class Ship extends Actor {
int jumpLimit;
int jumpCount;
public Ship(float x, float y) {
super(x, y);
jumpLimit = 0;
jumpCount = 0;
}
public void move() {
float ax = 0;
float ay = gravity;
if(left) {
ax -= 0.9;
}
if(right) {
ax += 0.9;
}
if(up) {
if(jumpLimit > 0) {
--jumpLimit;
ay -= 3.0;
}
}
else {
if(jumpCount < 2) {
++jumpCount;
jumpLimit = defaultJumpLimit;
}
}
ship.ax = ax - 0.2 * (ship.x - ship.px);
ship.ay = ay - 0.2 * (ship.y - ship.py);
if(jumpCount == 0) {
jumpCount = 1;
}
super.move();
float vx = x - px;
float vy = y - py;
if(vx < 0) {
if(test(x, y + cellSize / 2, wallCell)) {
x = ((int)(x / cellSize) + 1) * cellSize;
}
else if(test(x, y + cellSize / 2, needleCell)) {
terminated = true;
}
}
else if(vx > 0) {
if(test(x + cellSize, y + cellSize / 2, wallCell)) {
x = ((int)(x / cellSize)) * cellSize;
}
else if(test(x + cellSize, y + cellSize / 2, needleCell)) {
terminated = true;
}
}
if(vy < 0) {
if(test(x, y, wallCell) || test(x + cellSize - 1, y, wallCell)) {
y = ((int)(y / cellSize) + 1) * cellSize;
jumpLimit = 0;
}
if(test(x, y, needleCell) || test(x + cellSize - 1, y, needleCell)) {
terminated = true;
}
}
else if(vy > 0) {
if(test(x, y + cellSize, wallCell) || test(x + cellSize - 1, y + cellSize, wallCell)) {
y = ((int)(y / cellSize)) * cellSize;
if(!up) {
jumpLimit = defaultJumpLimit + (int)(vx / 3);
jumpCount = 0;
}
}
else if(test(x, y + cellSize, needleCell) || test(x + cellSize - 1, y + cellSize, needleCell)) {
terminated = true;
}
}
}
public void display() {
fill(#00bb00);
rect(x, y, 16, 16);
}
public boolean test(float x, float y, int type) {
return stage[(int)(y / cellSize)][(int)(x / cellSize)] == type;
}
}
Ship ship;
boolean left = false;
boolean right = false;
boolean up = false;
int state = 0;
int time = 0;
PFont font;
void setup() {
size(864, 480);
font = loadFont("ArialMT-12.vlw");
textFont(font);
smooth();
setupGame();
}
void draw() {
move();
display();
}
void keyPressed() {
switch(keyCode) {
case LEFT:
left = true;
break;
case RIGHT:
right = true;
break;
case UP:
up = true;
}
if(state == readyState) {
state = gameState;
}
}
void keyReleased() {
switch(keyCode) {
case LEFT:
left = false;
break;
case RIGHT:
right = false;
break;
case UP:
up = false;
}
}
void setupGame() {
up = left = right = false;
state = readyState;
time = 0;
ship = new Ship(320, 240);
}
void move() {
if(state == gameState) {
++time;
}
ship.move();
if(ship.terminated) {
setupGame();
}
if(ship.test(ship.x + cellSize / 2, ship.y + cellSize / 2, goalCell)) {
state = clearedState;
}
}
void display() {
fill(#ffffff);
rect(0, 0, 864, 480);
for(int y = 0; y < 30; ++y) {
int top = y * cellSize;
for(int x = 0; x < 54; ++x) {
int left = x * cellSize;
int type = stage[y][x];
if(type >= 1 && type <= 2) {
switch(type) {
case 1:
fill(#000000);
break;
case 2:
fill(#0000ff);
break;
}
rect(left, top, cellSize, cellSize);
}
else if(type == 3) {
fill(#000000);
triangle(left + cellSize / 2, top, left, top + cellSize - 1, left + cellSize - 1, top + cellSize - 1);
}
}
}
ship.display();
fill(#ffffff);
text("" + time, 0, 12);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment