Skip to content

Instantly share code, notes, and snippets.

@alecchen
Created July 29, 2013 09:41
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 alecchen/6103260 to your computer and use it in GitHub Desktop.
Save alecchen/6103260 to your computer and use it in GitHub Desktop.
/* @pjs font="led.ttf"; */
/* @pjs preload="bg_img.jpg"; */
/* @pjs preload="title.png"; */
/* @pjs preload="blood.png"; */
/* @pjs preload="twitter.png"; */
/* @pjs preload="facebook.png"; */
Minim maxim;
AudioPlayer attack_titan;
AudioPlayer attack_user;
PFont font;
PImage title_img, bg_img, blood_img, twitter_img, facebook_img;
int position = 0;
int timeout = 30;
int start_time = -1;
int orig_image_width = 1920;
int orig_image_height = 1080;
int image_ratio = 0.5;
int image_width = orig_image_width*image_ratio;
int image_height = orig_image_height*image_ratio;
int image_shift = 50;
int movement = 2;
int canvas_width = image_width;
int canvas_height = image_height+image_shift;
int titan_hp = 100;
int user_hp = 100;
int STARTING = 0;
int PLAYING = 1;
int GAME_OVER = 2;
int drawing_page = STARTING;
int init_mousex = 0;
boolean is_mouse_pressed = false;
boolean is_game_over = false;
boolean overTwitterButton = false;
boolean overFacebookButton = false;
void setup()
{
// image
//size(canvas_width, canvas_heigh);
size(960, 590);
background(0);
bg_img = loadImage("bg_img.jpg");
title_img = loadImage("title.png");
blood_img = loadImage("blood.png");
twitter_img = loadImage("twitter.png");
facebook_img = loadImage("facebook.png");
// font
font = loadFont("led.ttf");
textFont(font, 32);
// audio
maxim = new Maxim(this);
attack_titan = maxim.loadFile("attack_titan.wav");
attack_user = maxim.loadFile("attack_user.wav");
attack_user.setLooping(true);
}
void draw()
{
if (drawing_page == STARTING) {
starting();
}
else if (drawing_page == PLAYING) {
if (start_time == -1) {
start_time = millis();
}
playing();
}
else {
}
}
// -------------------------------------
// drawing
// -------------------------------------
void starting()
{
if (is_mouse_pressed == false && millis() % 5 == 0) {
background(0);
String t = "CLICK TO START";
float tw = textWidth(t);
fill(255, 255, 0);
text(t, (width-tw)/2, (height/2)*1.5);
position++;
//println("position = " + position);
if (position == 0) {
image(title_img, width/2-640/2, height/2-(300/2)*1.5);
}
else if (position == 1) {
image(title_img, width/2-640/2+movement, height/2-(300/2)*1.5);
}
else if (position == 2) {
image(title_img, width/2-640/2, height/2-(300/2)*1.5+movement);
position = 0;
}
}
}
void playing()
{
// LED text
int remained = timeout - (int) ((millis()-start_time) / 1000);
//println(remained);
stroke(0);
strokeWeight(1);
fill(0);
rect(0, 0, width, 50);
String t = remained;
float tw = textWidth(t);
fill(255, 255, 0);
text(t, (width-tw)/2, 32);
// titan's HP
if (titan_hp > 0) {
stroke(255, 0, 0);
strokeWeight(1);
fill(255, 0, 0);
rect(width/2+tw/2+5, 5, ((width/2-tw/2-5)*titan_hp/100), 30);
}
else {
game_over();
return;
}
// user's HP
if (user_hp > 0) {
stroke(0, 0, 255);
strokeWeight(1);
fill(0, 0, 255);
rect(((width/2-tw/2-5)*(100-user_hp)/100), 5, ((width/2-tw/2-5)*user_hp/100), 30);
}
else {
game_over();
return;
}
// game over
if (remained == 0) {
game_over();
return;
}
// titan's movement
if (is_mouse_pressed == false && millis() % 5 == 0) {
position++;
//println("position = " + position);
if (position == 0) {
image(bg_img, 0, image_shift, image_width, image_height);
}
else if (position == 1) {
image(bg_img, movement, image_shift, image_width, image_height);
}
else if (position == 2) {
image(bg_img, 0, image_shift+movement, image_width, image_height);
position = 0;
}
}
if (random(1000) < 5) {
attack_titan.stop();
attack_titan.cue(0);
attack_titan.play();
user_hp -= random(3, 10);
// user's bleeding
image(blood_img, 0, 300, 400, 400);
}
}
void game_over()
{
is_game_over = true;
attack_user.stop();
attack_titan.stop();
drawing_page = GAME_OVER;
background(0);
// result
textFont(font, 64);
String status_text = titan_hp >= user_hp ? "YOU LOSE" : "YOU WIN";
float status_text_width = textWidth(status_text);
fill(255, 255, 0);
text(status_text, (width-status_text_width)/2, (height/2));
// twitter
textFont(font, 32);
image(twitter_img, 300, 400, 64, 64);
String twitter_text = "Share on Twitter";
fill(255);
text(twitter_text, 370, 440);
// facebook
image(facebook_img, 300, 500, 64, 64);
String facebook_text = "Share on Facebook";
fill(255);
text(facebook_text, 370, 540);
//noLoop();
}
// -------------------------------------
// callbacks
// -------------------------------------
void mouseDragged()
{
if (drawing_page != PLAYING) return;
// smoke
float speed = dist(pmouseX, pmouseY, mouseX, mouseY);
float alpha = map(speed, 0, 20, 0, 10);
float lineWidth = map(speed, 0, 10, 40, 20);
//lineWidth = constrain(lineWidth, 0, 30);
noStroke();
fill(0, alpha);
// smoke color
stroke(random(105, 220));
strokeWeight(lineWidth);
ellipse(mouseX, mouseY, lineWidth, lineWidth);
//line(pmouseX, pmouseY, mouseX, mouseY);
is_mouse_pressed = true;
checkButtons();
}
void mouseReleased()
{
if (drawing_page != PLAYING) return;
if ((mouseY > image_shift && mouseX > width/2) || (mouseX < width/2 && mouseY > image_shift && mouseY < height / 3)) {
if (titan_hp > 0) {
titan_hp -= abs(mouseX-init_mousex)*5/590;
}
println("titan_hp = " + titan_hp + ", user_hp = " + user_hp);
}
image(bg_img, 0, image_shift, image_width, image_height);
// titan's bleeding
image(blood_img, mouseX-200, mouseY-200, 400, 400);
attack_user.stop();
is_mouse_pressed = false;
}
void mousePressed() {
//println("drawing_page = " + drawing_page);
if (drawing_page == STARTING) {
drawing_page = PLAYING;
}
else if (drawing_page == PLAYING) {
init_mousex = mouseX;
attack_user.cue(0);
attack_user.play();
}
if (is_game_over && overTwitterButton) {
link("https://twitter.com/intent/tweet?text=I%20just%20played%20attack%20on%20titan%20at%20http%3A%2F%2Fattack%2Don%2Dtitan%2Eheroku%2Ecom");
}
else if (is_game_over && overFacebookButton) {
link("https://www.facebook.com/sharer/sharer.php?u=http%3A%2F%2Fattack%2Don%2Dtitan%2Eheroku%2Ecom");
}
}
void mouseMoved() {
checkButtons();
}
void checkButtons() {
if (mouseX > 300 && mouseX < 364 && mouseY > 400 && mouseY < 464) {
overTwitterButton = true;
}
else if (mouseX > 300 && mouseX < 364 && mouseY > 500 && mouseY < 564) {
overFacebookButton = true;
}
else {
overTwitterButton = overFacebookButton = false;
}
//println("overTwitterButton = " + overTwitterButton);
//println("overFacebookButton = " + overFacebookButton);
}
// -------------------------------------
// brushes
// -------------------------------------
void brush3(float x, float y, float px, float py, float lineWidth) {
stroke(255, 0, 0);
strokeWeight(lineWidth);
pushMatrix();
translate(x, y);
rotate(random(px));
line(0+random(100), 0+random(100), 0, 0);
rotate(random(px));
line(0+random(100), 0+random(100), 0, 0);
rotate(random(px));
line(0+random(100), 0+random(100), 0, 0);
popMatrix();
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment