Hello Everybody
first of all id' like to mention that I am not very into programming. I am studying social science and I am
interested in Processing because
i would like to do data visualizations.This is my first little project.
The idea is to create a data-game. This means it should be a game which lets the user interact with data in a funny way but also lets him/her get know the data. In a second step the user schould be free to upload his own data and play with it.
In the game
the user has to manipulate a moving graph by choosing data. For example he plays with country data about Child Mortality; if he chooses Sweden his graph probably will move downwards whereas choosing the Kongo will make his graph heading to the top. Manipulating the graph the user is creating a path on which a little circle is moving. The aim of the game is not to hit the obstacles moving around (obstacles
not yet included).
I suppose this is hard to understand :) but see it yourself…
The game was initially thought to be an android app or a web-browser game. But the first time i launched it on my cell phone i noticed that it is super slow. On my laptop it's consuming about 20-30% of my CPU (2.4Ghz Macbook) and up too 300MB of memory. As .js in Firefox it is about 40% of CPU and up to 400MB!
http://www.unet.univie.ac.at/~a0406222/prozent/game.html As Javascript it is not slow (as on the cellphone) but its flickering and hardly reacts (RIGHT, LEFT to switch between options and DOWN to choose one option
)
As I said before
i don't know much about programming and have know clue what I could do/change to speed it up?
I suppose it is unusual to post a whole project-code and let people search for problems but I just ran out of ideas. So sorry for that!
The data has to be stored in a .txt archive
in the following format.
Algeria;50.0
Austria;30.0
...
So here the code. Its based an a example from the O'reillys Processing book (Erik Bartman, german version
). On page 216 there is a so called
Simulierter Herzmonitor. (Just for the case someone posseses this book
)
float [] yPos;//array for yPositons of selected Data. twice as long as the screensize boolean choice=false; //true if player has coosen an option boolean newselection=true; //true if new data has to be presented float value;//choosen value int lastPointx;//calculates next data Point on the left side of the Ball xPosition float lastPointy;//calculates next data Point on the left side of the Ball yPosition int nextPointx; //calculates next data Point on the right side of the Ball xPosition float nextPointy;//calculates next data Point on the right side of the Ball yPosition int endPointx;//rightest Data Point int zaehler; //counts the total number of data points = Number of yPosition array elements which are not -1 float ballVel=0.09; // stes the velocity the ball moves forward on the x-axis float xBall=250;//xPosition of the ball float yBall=height/2; //sets starting point for yBall position PFont font; int selecta=1; //number of currently selected option =1,2,3 int optionAsize=12; //fontsize for otpion int optionBsize=12; int optionCsize=12; int optionAindex; // int optionBindex; int optionCindex; String dateiName;
void setup() { size(500, 300); // smooth(); frameRate(60); // orientation(LANDSCAPE); // for ANDROID: fix Lndscape mode yPos = new float[2*width]; //array for selected value. //if nothing is selected its filled width -1. it is twice as long as the screensize yPos[width]=height/2;//sets start value yPos[2*width-1]=height/2;//sets a second start value //dateiName = selectInput("Please choose your data (.txt)."); //option for selecting own data }
//Data is selectes with arrows. DOWN chooses a option (for android DPAD)
void keyPressed() { if (key == CODED) { if (keyCode == LEFT) { selecta--; } if (keyCode == RIGHT) { selecta++; } if (keyCode ==DOWN) { //for ANDROID: DPAD choice=true; //boolean true if an option is choosen newselection=true; //new data has to be presented } } }
void draw() { background (255); stroke(0); strokeWeight(2); int hohenabstand=height/30; //distance from top and bottom to percentage axis int seitenabstand=width/60; //distance from right side to percentage axis ////////////////////////////////////////////////////////////////////////////// //Load Data and save it as Array /////////////////////////////////////////////////////////////////////////////
String []landerDaten; //country Data landerDaten = loadStrings("daten.txt");//or dateiName String []landerNamen= new String[landerDaten.length]; //array length float []landerWerte= new float[landerDaten.length]; for (int i=0; i<landerDaten.length; i++) { String [] list = split(landerDaten[i], ";"); //split data into country-Names (landerNamen) and country-values(landerWerte) landerNamen[i]=list[0];//fill float f = float(list[1]);//convert float data into integer f=height-(f*(height-2*hohenabstand)/100)-hohenabstand; //make data fit into percentage axis landerWerte[i]=f; //fill DatenArray }
/////////////////////////////////////////////////////////////////////////////// //SELECTOR ////////////////////////////////////////////////////////////////////////////// if (selecta>3) { //makes selestion go back to first option after last option selecta =1; } if (selecta<1) { selecta=3; } //////////Change fontsize for selection if (selecta==1) { optionAsize=25; } else { optionAsize=12; }
if (selecta==2) { optionBsize=25; } else { optionBsize=12; }
if (selecta==3) { optionCsize=25; } else { optionCsize=12; }
//Move Array to the left = move graph for (int x=1;x<2*width;x++) yPos[x-1] = yPos[x];
if (choice) { if (selecta==1) { value=landerWerte[optionAindex];//set last array position to country value for selection if Option A is choosen } if (selecta==2) { value=landerWerte[optionBindex];//set last array position to country value for selection if Option B is choosen } if (selecta==3) { value=landerWerte[optionCindex];//set last array position to country value for selection if Option C is choosen } } choice=false; yPos[2*width-1]=value;//set yPos array to -1 if no choice is made value=0;//
if (newselection) {//if a selection is made > new random Options optionAindex=int(random(landerDaten.length)); optionBindex=int(random(landerDaten.length)); optionCindex=int(random(landerDaten.length)); } newselection=false;
Could it be the array which consumes
so much memory? Is there an other way to do this without an array of this length
? i tried to store NULL if no choice is made but it seems this isnt posible?
if someone read all this thank you very much for your patience!! :)