Loading...
Logo
Processing Forum


Is a defender game searching for information against twitter. It search active  for tweets containing keywords like 2012 and search in those results after END n such. It converts tweets into comets that spawns randomly outside the sketch, aimin at the center..
 Your job as starfleet captain commander Buck is to save the earthlings against confusion AND mass destruction.
Achieve this by crashing your spaceship into every comet possible...


It's working but need som few tweaks and a mutch cleaner code.

1: 
How do I make the comets "lag" their way towards earth? 
(as In  old 8 bit "grid"-games and as in;" low frame rate", like the movement of the spaceship in this sketch)
Can i fake it in some kind of way?

2:
 Please look it thru and se if you find any superbig mistakes (exept the "press H for help"- loop, that's under construction).



Copy code


    MAIN SKETCH
    Copy code

    1. //import liberaries
    2. import ddf.minim.*;
    3. import ddf.minim.signals.*;
    4. import ddf.minim.analysis.*;
    5. import ddf.minim.effects.*;

    6. PImage gameover;
    7. PImage skull;
    8. PImage jorden;
    9. PImage heart;
    10. import simpleML.*;
    11. int e = 0;
    12. //boolean showComet;


    13. ArrayList kometer = new ArrayList();

    14. int y = 0;
    15. int x = 0;
    16. int douche = 0;
    17. int i1 = 503;
    18. int i2 = 245;
    19. int i3 = 62;
    20. int i = 0;
    21. int rx = 100;
    22. int ry = 50;
    23. int hp = 10;
    24. int kills = 0;
    25. int sqx = 500;
    26. int sqy = 250;
    27. int sqr = 20;

    28. komet k;

    29. PFont Font;


    30. XMLRequest xmlRequest;
    31. int startTime;     // for the timer to make request ever N seconds
    32. String html = "";  // String to hold data from request

    33. import ddf.minim.*;
    34. Minim minim;
    35. AudioPlayer groove;


    36. void setup() {
    37.   // Music
    38.   minim = new Minim(this);
    39.   groove = minim.loadFile("test_rymd2.mp3", 512);
    40.   groove.loop();
    41.   // Creating and starting the request
    42.   xmlRequest = new XMLRequest(this, "http://search.twitter.com/search.rss?q=2012");
    43.   xmlRequest.makeRequest();
    44.   size(height = 1000, width = 500);
    45.   smooth();

    46.   //showComet = true;
    47. }

    48. void draw() {

    49.   background(0);  
    50.   ellipse(i1, i2, i3*2, i3*2);
    51.   fill(255, 255, 255);

    52.   // Every 5 seconds, make a new request
    53.   int now = millis();
    54.   if (now - startTime > 5000) {
    55.     xmlRequest.makeRequest();
    56.     println("Making request!");
    57.     startTime = now;
    58.   }

    59.   jorden = loadImage("earth.png"); //Bilden
    60.   imageMode(CENTER);
    61.   image(jorden, 500, 250);



    62.   for (int i = 0; i < kometer.size(); i++) {
    63.     komet k = (komet)kometer.get(i);
    64.     k.smart();
    65.   }

    66.   Font = loadFont("PressStartK-25.vlw"); //text corners
    67.   textFont(Font, 25);
    68.   text(hp, 15, 40);
    69.   text("H=help", 835, 485);
    70.   text(kills, 150, 40);
    71.   //  text(kills, 15, 485);

    72.   heart = loadImage("heart2.png");     //hp heart
    73.   imageMode(CENTER);
    74.   image(heart, 90, 29);

    75.   skull = loadImage("skull.png");     //hp heart
    76.   imageMode(CENTER);
    77.   image(skull, 220, 28);
    78.   // image(skull, 80, 475);


    79.   fill(255, 255, 255);
    80.   if (hp < 5) {
    81.     fill(255, 255, 0);
    82.   }
    83.   if (hp < 2) {
    84.     fill(255, 0, 0);
    85.   }

    86.   if (hp < 1) {
    87.     gameover = loadImage("gameover.png");
    88.     imageMode(CENTER);
    89.     image(gameover, 500, 250);
    90. //    groove = minim.loadFile("gameover2.mp3", 512);
    91. //    groove.loop(0);
    92.   }     
    93.   jorden = loadImage("ship10.png"); //Bilden
    94.   imageMode(CENTER);
    95.   image(jorden, sqx, sqy);

    96.   //  ellipse(sqx,sqy,sqr*2,sqr*2);

    97.   if (sqx > 980) {
    98.     sqx = 980;
    99.   } 
    100.   if (sqy < 20) {
    101.     sqy = 20;
    102.   }
    103.   if (sqx < 20) {
    104.     sqx = 20;
    105.   }
    106.   if (sqy > 480) {
    107.     sqy = 480;
    108.   }
    109. }


    110. void stop() {
    111.   groove.close();
    112.   minim.stop();
    113.   super.stop();
    114. }


    115. // When the request is complete
    116. void netEvent(XMLRequest ml) {
    117.   // Retrieving an array of all XML elements inside "<title*>" tags
    118.   String[] headlines = ml.getElementArray("title");
    119.   for (int i = 0; i < headlines.length; i++) {
    120.     // println(headlines[i]);
    121.   }

    122.   for (String html: headlines) {  
    123.     if (html.toLowerCase().contains("end")) {    
    124.       douche++;      
    125.       println(douche);     


    126.       switch ((int) random(4)) {
    127.       case 0:
    128.         // top edge
    129.         k = new komet( random(width), -10 );
    130.         break;
    131.       case 1:
    132.         // right edge
    133.         k = new komet( width+10, random(height) );
    134.         break;
    135.       case 2:
    136.         // left edge
    137.         k = new komet( -10, random(height) );
    138.         break;
    139.       case 3:
    140.         // bot edge
    141.         k = new komet( random(width), height+10 );
    142.         break;
    143.       }
    144.       kometer.add(k);
    145.     }
    146.   }
    147. }
    148. void keyPressed() {
    149.   if (keyPressed);
    150.   if (key == ('r') && (hp <= 10)) {
    151.     hp = 10;
    152.     kills = 0;  

    153.     println("RESTART");
    154.   }
    155.   if (key == CODED) {              //Gamepad
    156.     if (keyCode == RIGHT) {
    157.       sqx = sqx + 10;
    158.     }
    159.     if (keyCode == LEFT) {
    160.       sqx = sqx - 10;
    161.     }
    162.     if (keyCode == UP) {
    163.       sqy = sqy - 10;
    164.     }
    165.     if (keyCode == DOWN) {
    166.       sqy = sqy + 10;
    167.     }
    168.   }  
    169.   if (key == 'h') {                                                                                          //////////////HELP ME HERE
    170.     Font = loadFont("PressStartK-12.vlw");
    171.     textFont(Font, 12);
    172.     text(" A info-defender game, searching active \n for tweets containing keywords like 2012 / END  \n & spawn thoose as comets. Your job as  \n starfleet  captain BUCK is  to defend earth \n against confusion. \n \n R = restart \n arrows = move ", 410, 30);
    173.     println("HELP");
    174.   }
    175. }



    KOMET CLASS


    Copy code
    1. PImage kometen;
    2. PImage jorden2;
    3. PFont Font2;


    4. class komet {

    5.   int a = 15;
    6.   int b = 15;
    7.   float x = 0;
    8.   float y = 0;
    9.   float komet1, komet2;
    10.   boolean show = true;

    11.   komet(float _x, float _y) {
    12.     x = _x;
    13.     y = _y;
    14.     float speed = 2;
    15.     float ang = atan2(height/2-y, width/2-x);
    16.     komet1 = cos(ang)*speed;
    17.     komet2 = sin(ang)*speed;
    18.   }
    19.   void movement() {
    20.     x +=komet1;
    21.     y +=komet2;
    22.   }
    23.   void kometer() {
    24.     if (show) {
    25.       kometen = loadImage("komet5.png");
    26.       imageMode(CENTER);
    27.       image(kometen, x, y);
    28.     }
    29.   }        
    30.   void collision() {  
    31.     if (show) {
    32.       if ( dist(x, y, i1, i2) < i3) {            
    33.       
    34.         groove = minim.loadFile("hithit_earth.mp3", 512);
    35.         groove.loop(0);
    36.         
    37.         show = false;

    38.         

    39.         println("HIT");  //Räknar träffar
    40.         hp--;
    41.         rx--;

    42.         kometen = loadImage("testar3.png");
    43.         imageMode(CENTER);
    44.         image(kometen, x, y);
    45.       }
    46.     
    47.         if ( dist(x, y, sqx, sqy) < sqr) {  
    48.         
    49.         groove = minim.loadFile("hit_comet.mp3", 512);
    50.         groove.loop(0);
    51.         
    52.         show = false;
    53.         
    54.         kills++;
    55.     
    56.         kometen = loadImage("testar3.png");
    57.         imageMode(CENTER);
    58.         image(kometen, x, y);
    59.     }
    60.   }
    61. }  
    62.   void smart() {
    63.     movement();
    64.     kometer();
    65.     collision(); 
    66.   }  
    67. }



    >>>> <<<<>>>>  <<<<
    erase all that belong so minim lib and all mp3's and change font, if you want to test it and the speedyshare timed out... all images found above

    Replies(1)

    The loadFile and loadImage calls in collision() are going to be relatively slow. You better load them in setup() as well. A class can access global static data.
    size(height = 1000, width = 500); is superfluous: Processing sets these variables itself, user shouldn't mess with them. And size() should be the first instruction of setup(). I don't know if that's still the case, but at some time, instructions before size() were executed twice...
    A Tweeter request every 5s is a bit too much by Tweeter standards, I think...