Score in processing - processing

Hello I want to create a catch game in processing.
I added a score counter but it does not work properly.
When I catch the object I want the score to go up with 1.
If it touches the ground -5. The problem is that when it hit the ground the score still goes up with 1. anyone an idea? this is the class of the object:
class Burger {
int breedte;
int hoogte;
float snelheid;
float x ;
float y;
Burger() {
x = random(0,1000);
}
void run() {
display();
testBurger();
}
void display() {
img1.resize(60,60);
image(img1,x,y);
}
void testBurger() {
y = y+richting;
if(y > 800){
x = random(0,1000);
println(score);
}
if(y > 600 && mouseX -90 < x && mouseX +90 > x){
score = score+1;
y = -400;
x = random(0,1000);
}
}
}

There are a lot of "magic numbers" in this code (see http://en.wikipedia.org/wiki/Magic_number_%28programming%29 for why this is poor practice) but if I'm following correctly, then I think you have your signs the wrong way around.
You currently test if(y > 800){ which would mean that that Burger object is near the bottom of the screen (remember, y-axis starts from 0 at the TOP of screen). I suggest you rather test if (y < 800) for the "catching" condition, and then you can simply use } else { at the end for the "hit the bottom of the screen" condition.

Related

Optimizing Conway's Game of Life for an Arduino

I am making Conway's Game of life on an Arduino with a 64x64 dot matrix grid. it is working but its a little slow while running the full size. This is the code that I think is taking the longest:
int same;
int c;
// My code can run in different sizes so these needed to be writable.
int width1=64;
int height=64;
int row0[WIDTH]; // WIDTH is just the constant 64.
void check(int y)
{
int alive=0;
for(int x=0;x < width1;++x)
{
alive=0;
if(x > 0)
{
if(getPixelColor(x-1, y) > 0)
{
alive+=1;
//Serial.println("(left)");
}
}
if(x < width1-1)
{
if(getPixelColor(x+1, y) > 0)
{
alive+=1;
//Serial.println("(right)");
}
}
if(y > 0)
{
if(getPixelColor(x, y-1) > 0)
{
alive+=1;
//Serial.println("(top)");
}
if(x > 0)
{
if(getPixelColor(x-1, y-1) > 0)
{
alive+=1;
//Serial.println("(top left)");
}
}
if(x < width1-1)
{
if(getPixelColor(x+1, y-1) > 0)
{
alive+=1;
//Serial.println("(top right)");
}
}
}
if(row < height-1)
{
if(getPixelColor(x, y+1) > 0)
{
alive+=1;
//Serial.println("(bottom)");
}
if(x > 0)
{
if(getPixelColor(x-1, y+1) > 0)
{
alive+=1;
//Serial.println("(bottom left)");
}
}
if(x < width1-1)
{
if(getPixelColor(x+1, y+1) > 0)
{
alive+=1;
//Serial.println("(bottom right)");
}
}
}
god_Conway(x, y, alive);
}
}
void god_Conway(int x, int y, int a)
{
int born[]={3};
int survive[]={2, 3};
int kill=0;
bool birth1=0;
int living=getPixelColor(x, y);
if(living > 0)
{
if (a == 2 || a == 3)
{
kill=1;
}
else
{
kill=-1;
}
}
else
{
if (a == 3)
{
birth1=1;
}
}
if (kill == -1 || birth1 == 1)
{
for(int c=0;c<width1;c++)
{
if(row0[c]==-1)
{
row0[c]=x;
if(c,width1)
{
row0[c+1]=-1;
}
break;
}
}
}
if(kill == 1 || birth1 == 0)
{
same++;
}
}
This code checks around each pixel in a row and discovers how many pixels are on around a certain pixel. getPixelColor(x, y) is code I found for the matrix the reads the pixel's color and return a number greater than 0 if on. The check function takes about 29-30ms per row. Every millisecond counts.
I've tried a big if for just the non-edge pixels. getPixelColor(x, y) does not always return same number so dividing it by expected return number is not always accurate. I made a function to return 1 and 0 automatically then do alive+=That_function(x, y); but it slowed it down.
It only writes down the y of the pixels that needs changing on row0. The code that prints this stops when there is a -1.
Don't use the time consuming function getPixelColor to read the pixels from the display. Instead maintain the board in memory, e. g. static char board[64][64];, and keep a copy of the previous generation also in memory. If there is no library function to display the whole image at once, you need at least to call the presumed setPixelColor function only for the changed pixels.
You might even use a static char board[66][66]; with the image indexes 1..64 and spare yourself the edge considerations.

Matrix Text rain effect in Processing 3.3

I'm working on making a matrix text rain effect in Processing 3.3 as a simple starter project for learning the processing library and Java. My code so far:
class Symbol {
int x, y;
int switchInterval = round(random(2, 50));
float speed;
char value;
Symbol(int x, int y, float speed) {
this.x = x;
this.y = y;
this.speed = speed;
}
//Sets to random symbol based on the Katakana Unicode block
void setToRandomSymbol() {
if(frameCount % switchInterval == 0) {
value = char((int) random(0x30A0, 0x3100));
}
}
//rains the characters down the screen and loops them to the top when they
// reach the bottom of the screen
void rain() {
if(y <= height) {
y += speed;
}else {
y = 0;
}
}
}
Symbol symbol;
class Stream {
int totalSymbols = round(random(5, 30));
Symbol[] symbols = new Symbol[500];
float speed = random(5, 20);
//generates the symbols and adds them to the array, each symbol one symbol
//height above the one previous
void generateSymbols() {
int y = 0;
int x = width / 2;
for (int i = 0; i <= totalSymbols; i++) {
symbols[i] = new Symbol(x, y, speed);
symbols[i].setToRandomSymbol();
y -= symbolSize;
}
}
void render() {
for(Symbol s : symbols) {
fill(0, 255, 70);
s.setToRandomSymbol();
text(s.value, s.x, s.y);
s.rain();
}
}
}
Ok, so that was a lot of code, Let me explain my dilemma. The issue I'm having is that when I run the code I get a NullpointerException at the s.setToRandomSymbol(); method call in the for each loop in the render function. The weird part about this NullPointerException error and the part I'm not understanding is that it's being thrown on a method that doesn't take in any arguments that could be coming back empty, and the method itself is void, so it shouldn't be returning anything, right? Why is this returning Null and what did I do wrong to have it return this way?
First you come up with a random number betwen 5 and 30:
int totalSymbols = round(random(5, 30));
Then you create an array that holds 500 instances of your Symbol class:
Symbol[] symbols = new Symbol[500];
Note that this array holds 500 null values at this point.
Then you add a maximum of 30 instances of Symbol to your array:
for (int i = 0; i <= totalSymbols; i++) {
symbols[i] = new Symbol(x, y, speed);
Note that this array now holds at least 470 null values at this point.
Then you iterate over all 500 indexes:
for(Symbol s : symbols) {
s.setToRandomSymbol();
But remember that at least 470 of these indexes are null, which is why you're getting a NullPointerException.
Some basic debugging would have told you all of this. I would have started by adding a basic println() statement just before you get the error:
for(Symbol s : symbols) {
println("s: " + s);
s.setToRandomSymbol();
This would have showed you that you're iterating over null values.
Anyway, to fix your problem you need to stop iterating over your entire array, or you need to stop making room for indexes you never use.
In the future, please try to narrow your problem down to a MCVE before posting. Note that this much smaller example program shows your error:
String[] array = new String[10];
array[0] = "test";
for(String s : array){
println(s.length());
}

How to set up if statements so that loop goes forward and then in reverse? Processing

int x = 31;
int y = 31;
int x_dir = 4;
int y_dir = 0;
void setup ()
{
size (800, 800);
}
void draw ()
{
background (150);
ellipse (x,y,60, 60);
if (x+30>=width)
{
x_dir =-4;
y_dir = 4;
}
if (y+30>=height)
{
x_dir=4;
y_dir = 0;
}
if (x+30>=width)
{
x_dir = -4;
}
x+=x_dir;
y+=y_dir;
println(x,y);
}
Hi,
I have to create this program in processing which produces an animation of a ball going in a Z pattern (top left to top right, diagonal top right to bottom left, and then straight from bottom left to bottom right) which then goes backwards along the same path it came.
While I have the code written out for the forward direction, I don't know what 2 if or else statements I need to write for the program so that based on one condition it goes forwards, and based on another condition it will go backwards, and it will continue doing so until it terminates.
If I am able to figure out which two if statements I need to write, all I need to do is copy and reverse the x_dir and y_dir signs on the forward loop.
There are a ton of different ways you can do this.
One approach is to keep track of which "mode" you're in. You could do this using an int variable that's 0 when you're on the first part of the path, 1 when you're on the second part of the path, etc. Then just use an if statement to decide what to do, how to move the ball, etc.
Here's an example:
int x = 31;
int y = 31;
int mode = 0;
void setup ()
{
size (800, 800);
}
void draw ()
{
background (150);
ellipse (x, y, 60, 60);
if (mode == 0) {
x = x + 4;
if (x+30>=width) {
mode = 1;
}
} else if (mode == 1) {
x = x - 4;
y = y + 4;
if (y+30>=height) {
mode = 2;
}
} else if (mode == 2) {
x = x + 4;
if (x+30>=width) {
mode = 3;
}
} else if (mode == 3) {
x = x - 4;
y = y - 4;
if (y-30 < 0) {
mode = 2;
}
}
}
Like I said, this is only one way to approach the problem, and there are some obvious improvements you could make. For example, you could store the movement speeds and the conditions that change the mode in an array (or better yet, in objects) and get rid of all of the if statements.

A name for this primitive operation?

I use this function a lot in game/graphics programming.
float slide(float from, float to, float by) {
float difference = to - from;
if(difference > by) {
return from + by;
} else if(difference < -by) {
return from - by;
} else {
return to;
}
}
The basic idea is "move towards so-and-so by this much".
I've called it slide because if you call it each frame on something's position, it appears as sliding with constant speed towards a target position.
Any other suggestions for naming?
I think slide is OK, maybe moveTo or floatTo would be also OK.
And one more idea. when you use this function like this:
Slide(0, 5, 10);
(menaing that "by" is bigger than possible step)
and you'll call it several times, the object will oscilate around "to" position.
You should add some handling for it. Like:
float slide(float from, float to, float by) {
float difference = to - from;
if(difference > by && from + by < to) {
return from + by;
} else if(difference < -by && from - by > to) {
return from - by;
} else {
return to;
}
}
hope this will help.

How can I improve Java2D performance of TextLayout.draw(..)

I'm using the Java2D TextLayout class together with a LineBreakMeasurer and an AttributedCharacterIterator to draw a piece of text into a box. The text is wrapped.
Profiling shows me that the code is very slow. Most of the time is lost in the method TextLayout.draw(..).
Does anyone have a suggestion for speed improvement?
// Get iterator for string
AttributedCharacterIterator iterator = attribText.getIterator();
// Create measurer
LineBreakMeasurer measurer = new LineBreakMeasurer(iterator, context);
// loop over the lines
int i = 1;
while (measurer.getPosition() < iterator.getEndIndex()) {
// Get line
TextLayout textLayout = measurer.nextLayout(w);
// get measurements
float ascent = textLayout.getAscent();
float descent = textLayout.getDescent();
float leading = textLayout.getLeading();
float size = ascent + descent;
// Move down to baseline
if( i == 1 ) {
if( coverType == CoverType.SPINE ) {
y = (box.height-size)/2;
y -= (size+leading)*(lines-1)/2;
} else if( vAlign == Alignment.Center ) {
y += (h-size)/2-(size+leading)*(lines-1)/2;
} else if( vAlign == Alignment.Bottom ) {
y += (h-size) - (size+leading)*(lines-1);
}
}
y += ascent;
// calculate starting point for alignment
float paintX = x;
switch( hAlign ) {
case Right: {
paintX = x + w - textLayout.getVisibleAdvance();
break;
}
case Center: {
paintX = x + (w - textLayout.getVisibleAdvance())/2;
break;
}
}
// Draw line
textLayout.draw(g2d, paintX, y);
// Move down to top of next line
y += descent + leading;
i++;
}
The relevant code snippet is shown above. attribText is an AttributtedString set before. context is the g2d.getFontRenderContext().
This post is rather old now so I hope you have found a solution that works for your needs. If you haven't here is something to think about. You only need to draw the text that is within the visible region. Since you know the y coordinate of each line it is easy to check to see if the y lies within the bounds of getVisibleRect(). Only painting the text that is necessary greatly improves performance (assuming of course that your text is longer than a single page).

Resources