Created
December 18, 2013 15:09
-
-
Save digulla/8023870 to your computer and use it in GitHub Desktop.
Writing Games with Processing: Winning
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
// Version 6: Winning the game | |
int px = 320, py = 240; | |
int tileSize = 20; | |
int signum(float value) { | |
return value < 0 ? -1 : value > 0 ? 1 : 0; | |
} | |
class Enemy { | |
color c; | |
int x, y; | |
String name; | |
boolean fighting; | |
Enemy(String name) { | |
this.name = name; | |
c = color(222,40,107); | |
} | |
void draw() { | |
fill(c); | |
ellipse(x + tileSize/2, y + tileSize/2, tileSize, tileSize); | |
fill(0); | |
textAlign(CENTER, TOP); | |
textSize(12); | |
text(name, x + tileSize/2, y + tileSize + 2); | |
} | |
void hunt() { | |
int dx = signum(px - x) * tileSize; | |
int dy = signum(py - y) * tileSize; | |
x += dx; | |
y += dy; | |
} | |
boolean closeTo(int tx, int ty) { | |
int dx = abs(x - tx) / tileSize; | |
int dy = abs(y - ty) / tileSize; | |
println(name+" dx="+dx+" dy="+dy); | |
return dx <= 1 && dy <= 1; | |
} | |
} | |
ArrayList<Enemy> enemies = new ArrayList<Enemy>(); | |
void addEnemy(int x, int y, String name) { | |
Enemy enemy = new Enemy(name); | |
enemy.x = x; | |
enemy.y = y; | |
enemies.add(enemy); | |
} | |
void drawEnemies() { | |
for(Enemy e: enemies) { | |
e.draw(); | |
} | |
} | |
void mousePressed() { | |
saveFrame("frame-######.png"); | |
println("Saved frame."); | |
} | |
void setup() { | |
size(640, 480); //VGA for those old enough to remember | |
addEnemy(20, 20, "Kenny"); | |
addEnemy(600, 20, "Benny"); | |
} | |
void drawPlatty() { | |
fill(235,220,160); | |
rect(px, py, tileSize, tileSize); | |
fill(0); | |
textSize(tileSize-2); | |
textAlign(CENTER, CENTER); | |
text("P", px+tileSize/2, py+tileSize/2-2); | |
} | |
void drawBackground() { | |
fill(174, 204, 27); | |
rect(0, 0, width, height); | |
} | |
void draw() { | |
drawBackground(); | |
drawPlatty(); | |
drawEnemies(); | |
} | |
void moveEnemies() { | |
for(Enemy e: enemies) { | |
e.hunt(); | |
} | |
} | |
void checkCollisions() { | |
ArrayList<Enemy> enemiesToCheck = new ArrayList<Enemy>(); | |
for(Enemy e: enemies) { | |
if(e.closeTo(px, py)) { | |
gameOver(e); | |
return; | |
} | |
for(Enemy e2: enemiesToCheck) { | |
if(e.closeTo(e2.x, e2.y)) { | |
e.fighting = true; | |
e2.fighting = true; | |
} | |
} | |
enemiesToCheck.add(e); | |
} | |
int notFighting = 0; | |
for(Enemy e: enemies) { | |
if(!e.fighting) { | |
notFighting ++; | |
} | |
} | |
if(notFighting == 0) { | |
youWon(); | |
} | |
} | |
void youWon() { | |
draw(); | |
noLoop(); | |
textAlign(CENTER, TOP); | |
textSize(40); | |
color outline = color(255,255,255); | |
color fill = color(255,0,0); | |
textWithOutline("YOU WON!", width/2, 200, outline, fill); | |
textSize(20); | |
textWithOutline("The cute platypus outsmarted "+enemies.size()+" crocodiles!", width/2, 240, outline, fill); | |
} | |
void gameOver(Enemy e) { | |
draw(); | |
noLoop(); | |
textAlign(CENTER, TOP); | |
textSize(40); | |
color outline = color(255,255,255); | |
color fill = color(255,0,0); | |
textWithOutline("GAME OVER", width/2, 200, outline, fill); | |
textSize(20); | |
textWithOutline("The poor platypus was eaten by "+e.name+"!", width/2, 240, outline, fill); | |
} | |
void textWithOutline(String message, int x, int y, color outline, color fill) { | |
fill(outline); | |
text(message, x-1, y); | |
text(message, x+1, y); | |
text(message, x, y-1); | |
text(message, x, y+1); | |
fill(fill); | |
text(message, x, y); | |
} | |
void movePlayer(int dx, int dy) { | |
dx *= tileSize; | |
dy *= tileSize; | |
int newX = px + dx; | |
int newY = py + dy; | |
if(newX >= 0 | |
&& newX < width | |
&& newY >= 0 | |
&& newY < height | |
) { | |
px = newX; | |
py = newY; | |
} | |
moveEnemies(); | |
checkCollisions(); | |
} | |
void keyPressed() { | |
if(key == CODED) { | |
if(keyCode == UP) { | |
movePlayer(0, -1); | |
} else if(keyCode == DOWN) { | |
movePlayer(0, 1); | |
} else if(keyCode == LEFT) { | |
movePlayer(-1, 0); | |
} else if(keyCode == RIGHT) { | |
movePlayer(1, 0); | |
} | |
} else if(key == ' ') { | |
movePlayer(0, 0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment