Skip to content

Instantly share code, notes, and snippets.

@ericoporto
Last active August 29, 2015 14:28
Show Gist options
  • Save ericoporto/ed7973ef787f358c04a8 to your computer and use it in GitHub Desktop.
Save ericoporto/ed7973ef787f358c04a8 to your computer and use it in GitHub Desktop.
This is my first pseudo game for Processing.
// A simple 'game' for Processing (tested with Processing 2.2.1)
// use ARROW KEYS to move
// this will be the pointers I'm going to use to sweep my map,
// and it's limit.
int xi, yi;
int i = 0;
int sceneHeight = 10;
int sceneWidth = 20;
// This is the "hero" coordinate
int x, y, xt, yt;
// This is the "enemy" coordinate, and his timer
int xe, ye, xet, yet;
int initialTime;
int interval = 300;
// This is my scenery, top-view
int[] scene = {
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2,
2, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2,
2, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 2,
2, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 2,
2, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 2,
2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2,
2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2,
2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
};
//This is the scale or zoom
int squareSize = 80;
// I will set the "hero" start position, the "enemy" start position and everything needed
void setup() {
size(squareSize*sceneWidth,squareSize*sceneHeight);
x = 1;
y = 1;
xe = 15;
ye = 7;
initialTime = millis();
}
// I'm listening to key press to move the "hero"
void keyPressed() {
if (key == CODED) {
if (keyCode == UP) {
yt = yt-1;
} else if (keyCode == DOWN) {
yt = yt+1;
} else if (keyCode == LEFT) {
xt = xt-1;
} else if (keyCode == RIGHT) {
xt = xt+1;
}
} else {
}
}
//
// I need to do four basic stuff here:
// 1 - Move the "enemy". It will do a move per <interval>.
// 2 - Draw the map.
// 3 - Check for colisions
// 4 - Draw the "hero" and "enemy"
//
void draw() {
background(21);
if (millis() - initialTime > interval) {
// My first 'if' guarantees no diagonal moves!
if( round(random(0,1)) == 0) {
xet = xet + round(random(-1,1));
} else {
yet = yet + round(random(-1,1));
}
initialTime = millis();
}
noStroke();
// I'm going to look through my scene array.
// Anything different from zero is a wall and must be treated as such
for ( yi = 0; yi < sceneHeight ; yi++) {
for ( xi = 0; xi < sceneWidth ; xi++) {
i = xi+yi*sceneWidth;
fill(scene[i]*40+20);
rect(xi*squareSize, yi*squareSize, squareSize,squareSize) ;
if(scene[i] !=0) {
// this is my lazy collision detection algorithm. It only works because the computer is much faster than the user.
if(xt == xi && yt == yi) {
xt=x;
yt=y;
}
if(xet == xi && yet == yi) {
xet=xe;
yet=ye;
}
}
}
}
x=xt;
y=yt;
xe=xet;
ye=yet;
fill(65,104,109); // the "hero" color
rect(xe*squareSize, ye*squareSize, squareSize,squareSize) ;
fill(238,226,214); // the "enemy" color
rect(x*squareSize, y*squareSize, squareSize,squareSize) ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment