I want to make a group of array fade out until the last array object in this group was appended. And I use millis() to make every three object fadeout at a slower speed! So I create a function called boolean timelag(int time, int number)and each time I pass the time and sequence number into it and expect it will fadeout after 2 seconds after every third object was created, but seems that nothing happened
void draw() {
background(255, 255, 255);
for (int i=0; i<zoog.length; i++) {
zoog[i].jiggle();
zoog[i].display();
if(i%3 ==0 && i>=3){
time = millis();
timelag(time,i);
}
}
if(fadeout){
zoog[thatnumber].disappear();
zoog[thatnumber-1].disappear();
zoog[thatnumber-2].disappear();
}
}
My timelag function:
boolean timelag(int time, int number){
int thattime = time;
if(millis()-thattime>2000){
thatnumber = number;
fadeout = true;
}
else
fadeout = false;
return fadeout;
}
The whole code is here
Zoog[]zoog = new Zoog[1];
float count=0;
int xpos =0;
int ypos =0;
String message="haha";
int ntextsize = 20;
int nopacity =200;
int thistime = 0;
int thiscount = 0;
int time =0;
int number =0;
boolean fadeout = false;
int thatnumber=0;
//Zoog zoog;
void setup() {
size(400, 400);
xpos = int(random(width/2-200, width/2+40));
ypos = int(random(height/2, height/2-40));
zoog[0] = new Zoog(xpos, ypos, message, nopacity);
}
void draw() {
background(255, 255, 255);
for (int i=0; i<zoog.length; i++) {
zoog[i].jiggle();
zoog[i].display();
if(i%3 ==0 && i>=3){
time = millis();
timelag(time,i);
}
}
if(fadeout){
zoog[thatnumber].disappear();
zoog[thatnumber-1].disappear();
zoog[thatnumber-2].disappear();
}
}
void mousePressed() {
count = count + 1;
// int thiscount = 0;
if (count%3 ==0) {
xpos=int(random(30, width-30));
ypos=int(random(10, height-10));
}
else {
ypos = ypos+50;
}
nopacity = int(random(100, 255));
// text(message, xpos, ypos);
Zoog b = new Zoog(xpos, ypos, message, nopacity);
zoog =(Zoog[]) append(zoog, b);
}
boolean timelag(int time, int number){
int thattime = time;
if(millis()-thattime>2000){
thatnumber = number;
fadeout = true;
}
else
fadeout = false;
return fadeout;
}
class Zoog {
int x;
int y;
String thatmessage;
int opaci =0;
Zoog(int xpo, int ypo, String thismessage, int opa) {
x = xpo;
y = ypo;
thatmessage = thismessage;
opaci = opa;
}
void jiggle() {
x = x+int(random(-2, 2));
y = y+int(random(-2, 2));
}
void display() {
fill(0, opaci);
text(thatmessage, x, y);
print("x position is "+ x);
print("y position is "+y);
}
void disappear() {
for (int j=0; j<255; j++) {
opaci = opaci -j;
}
}
}
I assume that when you wrote...
if(fadeout) { ... }
you meant...
if(timelag()) { ... }
in your timelag function it's much more readable and faster(if even minutely) to just return true or false from the function rather than returning a variable, unless that variable is needed throughout your project over and over, which it doesn't seem like it is and if it is the function that changes it usually isn't needed to return a value unless you're checking the boolean of whether or not the change happened.
boolean timelag(int time, int number){
//int thattime = time; //You also don't need to create this you
//can simply use the time you're getting in the boolean statement
if(millis()-time>2000){
thatnumber = number;
return true;
}
else {
return false;
}
}
also, if you're trying to fix how long it takes for each zoog to fade out you need to give them all a number and then decrease that number each time the disappear function is called. Take the for loop out of the disappear and just have it subtract a unit from it each call in the draw loop.
void disappear() {
opacity -= somenumber //somenumber is usually something small and you can tweak it.
if (opacity == 0) {
dead = true;
}
}
You can think of the draw loop as your for loop. If you embed too many singular for loops it'll slow down the flow of your program. Right now, you probably don't even see them fading out and you probably don't get rid of them with the way the code is written right now.
And when you're testing it you can tweak that number until you find the sweet spot. If you want an amazing overview of all these concepts you can take a look here. Shiffman really goes deep into each aspect we're talking about here and it's short and fun to read.
First time i read the other post I misunderstood your goal. Anyway i've made a little tweak in your code that might help you understanding the way to go. BUT i did not moved it to an ArrayList, so this code below, kind of sucks... It can only, maybe, help you get things clear...
Zoog[]zoog = new Zoog[1];
float count=0;
int xpos =0;
int ypos =0;
String message="haha";
int ntextsize = 20;
int nopacity =200;
int thistime = 0;
int thiscount = 0;
//Zoog zoog;
void setup() {
size(400, 400);
xpos = int(random(width/2-200, width/2+40));
ypos = int(random(height/2, height/2-40));
zoog[0] = new Zoog(xpos, ypos, message, nopacity);
}
void draw() {
background(255);
for (int i=0; i<zoog.length; i++) {
zoog[i].jiggle();
zoog[i].display(); }
}
void mousePressed() {
count = count + 1;
// int thiscount = 0;
if (count%3 ==0) {
xpos=int(random(30, width-30));
ypos=int(random(10, height-10));
}
else {
ypos = ypos+50;
// thiscount = thiscount +1;
// thistime = millis();
// }
}
nopacity = int(random(100, 255));
text(message, xpos, ypos);
Zoog b = new Zoog(mouseX, mouseY, message, nopacity);
zoog = (Zoog[]) append(zoog, b);
zoog[zoog.length -2].disappear = true;
}
class Zoog {
int x;
int y;
String thatmessage;
boolean disappear;
int opaci =0;
Zoog(int xpo, int ypo, String thismessage, int opa) {
x = xpo;
y = ypo;
thatmessage = thismessage;
opaci = opa;
}
void jiggle() {
x = x+int(random(-2, 2));
y = y+int(random(-2, 2));
}
void display() {
if(disappear)
disappear();
fill(0, opaci);
text(thatmessage, x, y);
}
void disappear() {
opaci-=0.5;
}
}
Related
i am trying to make all the circles i draw flicker in my code. i am currently able to make the latest circle that was drawn to flicker but the goal is to make all the circles on the canvas flicker.
here is my code;
float colour = random(256);
final int DIAM = 20;
final int MAX_NUM = 1000;
int numPointsX = 0;
int numPointsY = 0;
int [] xPos = new int[MAX_NUM];
int [] yPos = new int [MAX_NUM];
boolean start = false;
void setup() {
size (500, 500);
}
void draw() {
if (start) {
if (xPos[numPointsX-1] > 0 || (yPos[numPointsY-1]>0)) {
fill(random(256), random(256), random(256));
circle(xPos[numPointsX-1], yPos[numPointsY-1], DIAM);
}
}
println(xPos[0]);
}
void mouseClicked() {
insertXandY();
}
void insertXandY() {
int x = mouseX;
int y = mouseY;
xPos[numPointsX] = x;
yPos[numPointsY] = y;
numPointsX += 1;
numPointsY += 1;
start = true;
}
You need to draw all the circles in a loop with a different color in each frame:
void draw() {
background(255);
fill(random(256), random(256), random(256));
for (int i = 0; i< numPointsX; ++i) {
circle(xPos[i], yPos[i], DIAM);
}
}
Complete example:
float colour = random(256);
final int DIAM = 20;
final int MAX_NUM = 1000;
int numPointsX = 0;
int numPointsY = 0;
int [] xPos = new int[MAX_NUM];
int [] yPos = new int [MAX_NUM];
boolean start = false;
void setup() {
size (500, 500);
}
void draw() {
background(255);
fill(random(256), random(256), random(256));
for (int i = 0; i< numPointsX; ++i) {
circle(xPos[i], yPos[i], DIAM);
}
}
void mouseClicked() {
insertXandY();
}
void insertXandY() {
int x = mouseX;
int y = mouseY;
xPos[numPointsX] = x;
yPos[numPointsY] = y;
numPointsX += 1;
numPointsY += 1;
start = true;
}
So I am making this game in processing JAVA and the code is below so the gist is I have a ball and it avoids the falling rectangles so, for now, the ball can move sideways and the rectangles fall from above but how do I decrease my life when a rectangle hits the ball.
I made a function named Lives which was supposed to decrease lives if the circle gets hit with a rectangle but I can't come up with the code for that. Could anyone help with that?
int score;
int score1;
int miss;
int lives=10;
int ballx, bally;
class Rect
{
float x;
float y;
float speed;
float leng;
color c;
boolean valid;
final int MAX_COLOR = 255;
final int MIN_X = 50, MAX_X = 750;
final int MIN_Y = -800, MAX_Y = -100;
int MIN_SPEED = 1, MAX_SPEED = 2;
final int MIN_Leng = 50, MAX_Leng =100 ;
Rect()
{
initAll();
}
void initAll() {
valid = true;
c = color(random(255), random(255), random(255));
x = random(MIN_X, MAX_X);
y = random(MIN_Y, MAX_Y);
speed = random(MIN_SPEED, MAX_SPEED);
leng = random(MIN_Leng, MAX_Leng);
}
void update() {
if (!valid) {
initAll();
return;
}
move();
draw_rect();
}
void draw_rect()
{
fill(c);
rect (x, y, leng, leng);
}
void move()
{
if (y-leng <= height)
{
y += speed;
} else if (y-leng > height )
{
valid = false;
miss++;
}
}
void Lives()
{
}
void GameOver()
{
if (lives==0)
{
for (int i = 0; i < Obj.length; i++)
{
Obj[i] = new Rect();
}
background(0);
textSize(50 );
fill(255);
text( "You Lose ", 15, 150);
text( "Score: " + score, 15, 100);
}
}
boolean isOver(int mx, int my) {
float disX = x - mx;
float disY = y - my;
if (sqrt(sq(disX) + sq(disY)) < leng/2 ) {
return true;
} else {
return false;
}
}
}
Rect [] Obj = new Rect [10];
void setup() {
size (400, 600);
ballx=200;
bally=300;
for (int i = 0; i < Obj.length; i++)
{
Obj[i] = new Rect();
}
}
void draw() {
background(0);
textSize(50);
//fill(0);
text( "Score: " + score, 0, 100);
text("Lives: " + lives, 0, 50);
ellipse(ballx,bally,20,20);
for (int i = 0; i < Obj.length; i++) {
Obj[i].update();
//Obj[i].Lives();
Obj[i].GameOver();
}
surface.setTitle(nf(frameRate, 3, 2));
}
void keyPressed(){
for(Rect s : Obj){
if ( key =='q' ){
ballx=ballx-2;
score++;
score1++;
s.valid = false;
break;
}
if ( key =='e'){
ballx=ballx+2;
score++;
score1++;
s.valid = false;
break;
}
}
}
Easiest i think think of is -
case 1 - if you want health of ball to reduce gradually :
Define maximum_Health variable and assign it to 100.
Every time a block hits your ball, reduce the health by some weight you want to define.
case 2 - if ball life is lost for every block hit
Define remaining_Lives variable and give it some value you want the number of lives to start with.
Every time a block hits your ball, reduce the remaining_Lives by one.
I think that should give you some hint.
this code is supposed to have an ellipse with a "trail" of ellipses behind it that decrease in size and become more white. It is called "Smoke Trail" and the ellipses in this code work properly for the most part. There is one problem in the code where one of the ellipses draws over the trail and doesn't go behind like all the others. I think it has something to do with the first or last ellipse of the for loop
int count = 75;
int made = 0;
Smokes[] arrSmokes = new Smokes[count];
void setup()
{
size(800, 800);
}
void draw()
{
background(255);
if(made < count)
{
arrSmokes[made] = new Smokes();
made += 1;
}
for(int i = 0; i < made; i += 1)
{
arrSmokes[i].render();
}
}
public class Smokes{
int xCoord, yCoord;
float size;
Smokes()
{
xCoord = mouseX;
yCoord = mouseY;
size = 100;
}
void render()
{
noStroke();
ellipse(xCoord, yCoord, size, size);
size -= 4;
if(size <= 0)
{
xCoord = mouseX;
yCoord = mouseY;
size = 100;
}
}
}
Since you are keeping your old Instances of smoke and just reset them you have to store the top of your circles. Just use a Integer for that.
int top = 0;
Now you have to give every circle its number in the array...
if (made < count) {
arrSmokes[made] = new Smokes(made);
made ++;
}
/*
...
*/
int number;
Smokes(int number)
{
this.number = number;
//Initialyze
}
Now you have to change the top of your list every time you reset the Smoke particle.
void reset()
{
xCoord = mouseX;
yCoord = mouseY;
c = 0;
size = 100;
top = number;
}
Your for-loop should now start at the entry one above the top and go the way up to the end of the array and then jump to the first entry and run until it reaches the top entry.
for (int i = (top+1)%count; i != top; i = (i+1)%count) {
if (i >= made) continue;
arrSmokes[i].render();
}
Since you are not allowed to change the top while executing "render();" you have to add a function "evaluate();" which you run after having done every "render();".
void evaluate()
{
c += 10.2;
size -= 4;
if (size <= 0) {
reset();
}
}
void render()
{
noStroke();
fill(c);
ellipse(xCoord, yCoord, size, size);
}
Now you should be good to go. Here is the whole source again if I messed you up a bit: https://pastebin.com/urzbzmEb
But I recommend to use a ArrayList instead since Java is object-based and has its own garbage-collector. The good thing at the ArrayList is you know at every time that the last object in the list is the top circle.
Here is how it would look like with an ArrayList.
ArrayList<Smokes> smokes = new ArrayList<Smokes>();
void setup() {
size(800, 800);
}
void draw() {
background(255);
smokes.add(new Smokes(mouseX, mouseY));
for (int i = 0; i < smokes.size(); i ++) {
smokes.get(i).render();
}
}
public class Smokes {
int xCoord, yCoord;
float size = 100, c = 0;
Smokes(int x, int y) {
xCoord = x;
yCoord = y;
}
void render() {
noStroke();
fill(c);
ellipse(xCoord, yCoord, size, size);
c += 10.2;
size -= 4;
if (size <= 0) {
smokes.remove(this);
}
}
}
Hi all so I a tank war game thats ran over a network and is multiplayer, however whenever I fire a bullet it shows on my screen but not the other player's screen. Can you help, here's a snippet of code form where i think the problem is coming from:
class Tank implements Ball {
double locX, locY, radius, angle;
int self; // index of this tank in WarPanel.tanks
public boolean turnL, turnR, forth, back, fire;
boolean prevtL, prevtR, prevfo;
Color color;
Image image;
public static final double twoPi = Math.PI * 2.0;
public static final double turnRate = Math.PI / 8;
public static final double speed = 4.0;
public static final int RELOAD = 8; // delay between bullets
int count; // timer for reloading
public static final int MAXBULLETS = 7; // max simultaneous shots
Bullet bullets[] = new Bullet[MAXBULLETS];
AffineTransform saveAT; // place to hold current affine transform
public Tank(double x, double y, double a, int index, Image im){
locX = x;
locY = y;
angle = a;
self = index;
image = im;
radius = 22;
// create bullets for this tank
for (int i = 0; i < bullets.length; i++)
bullets[i] = new Bullet(self);
}
public double getX() { return locX; }
public double getY() { return locY; }
public double getRadius() { return radius; }
public boolean isAlive() { return true; }
void update(Boolean local) {
if (turnL)
turnLeft(turnRate);
if (turnR)
turnRight(turnRate);
if (forth){
moveForward();
// Check for rocks
if (WarPanel.hitAnItem(this, WarPanel.rocks) >= 0)
backUp();
}
if (local){
if (turnL != prevtL){
WarPanel.send("turnL "+turnL+" "+locX+" "+locY+" "+angle);
prevtL = turnL;
}
if (turnR != prevtR){
WarPanel.send("turnR "+turnR+" "+locX+" "+locY+" "+angle);
prevtR = turnR;
}
if (forth != prevfo){
WarPanel.send("forth "+forth+" "+locX+" "+locY+" "+angle);
prevfo = forth;
}
}
if (fire){
fireBullet();
}
// Update all of our bullets
for (Bullet b: bullets)
b.update();
}
public void processMove(String s){
// Update movement parameters based on s
Scanner sc = new Scanner(s);
// Get the flag change
String command = sc.next();
boolean value = sc.nextBoolean();
if (command.equals("turnL"))
turnL = value;
else if (command.equals("turnR"))
turnR = value;
else if (command.equals("forth"))
forth = value;
else
System.out.println("Unexpected move: "+command);
// then unpack position update
locX = sc.nextDouble();
locY = sc.nextDouble();
angle = sc.nextDouble();
}
void paint(Graphics g){
// Use the affine transform feature in Graphics2D
// to easily rotate the tank's image.
Graphics2D g2 = (Graphics2D)g;
saveAT = g2.getTransform();
g2.translate(locX, locY);
g2.rotate(angle);
g2.drawImage(image, (int)(-radius), (int)(-radius), null);
// Reset the transform (this is important)
g2.setTransform(saveAT);
// Then draw bullets
for (Bullet b: bullets)
b.paint(g2);
}
void fireBullet(){
// If it has been long enough since the last shot...
count--;
if (count > 0) return;
// ...and if all the bullets aren't currently in use...
int slot = getAvailableBullet();
if (slot < 0)
return;
// ...then launch a new bullet
bullets[slot].setLocation(locX, locY);
bullets[slot].setDirection(angle);
bullets[slot].reset();
// Reset the timer
count = RELOAD;
}
int getAvailableBullet(){
for (int i = 0; i < bullets.length; i++)
if (!bullets[i].isAlive())
return i;
return -1;
}
void turnRight(double a){
angle += a;
if (angle > twoPi)
angle -= twoPi;
}
void turnLeft(double a){
angle -= a;
if (angle < 0.0)
angle += twoPi;
}
void moveForward(){
locX += speed*Math.cos(angle);
locY += speed*Math.sin(angle);
}
void backUp(){
locX -= speed*Math.cos(angle);
locY -= speed*Math.sin(angle);
}
}
I've made this code that successfully creates a 16x12 grid by 50x50 squares on a 800x600px board.
As you can see, the player moves to the coordinates of the players mouseclick.
Each square of the grid has an object of Felt (field) on it, which can be laast (locked). If a fields lock attribute is set to 1, the player should not be moved to that position.
How do i detect the field a player tries to move on to achieve this?
public class SimpleGame extends BasicGame{
private Image plane;
private float planeX;
private float planeY;
public SimpleGame()
{
super("SpilTest");
}
#Override
public void init(GameContainer gc) throws SlickException {
plane = new Image("figur.png");
}
#Override
public void update(GameContainer gc, int delta) throws SlickException {
Input input = gc.getInput();
if (input.isMousePressed(input.MOUSE_LEFT_BUTTON)) {
this.planeX = input.getMouseX() - 30;
this.planeY = input.getMouseY() - 50;
}
}
public void render(GameContainer gc, Graphics g) throws SlickException {
Felt board[][] = nytGrid();
int distancex = 0;
int distancey = 0;
int counter = 0;
for (int i=0; i < board.length ; i++) {
for (int j=0; j < board[i].length ; j++) {
if (board[i][j].getLaast() == 1) {
g.setColor(Color.red);
g.fillRect(distancex, distancey, 50, 50);
}
distancex += 50;
counter++;
if (counter == 16) {
distancey += 50;
distancex = 0;
counter = 0;
}
}
}
g.drawImage(plane, planeX, planeY);
}
public static void main(String[] args) throws SlickException {
AppGameContainer app = new AppGameContainer(new SimpleGame());
app.setDisplayMode(800, 600, false);
app.setTargetFrameRate(60);
app.start();
}
public Felt[][] nytGrid() {
Felt [][] board = new Felt[16][12];
for (int i=0; i < board.length ; i++) {
for (int j=0; j < board[i].length ; j++) {
int x = i;
int y = j;
board[i][j] = new Felt(x, y);
if (i == 5 && j == 5) {
board[i][j].setLaast(1);
}
}
}
return board;
}
}
First off, you should probably initialize the board in the init() method instead of render, so it doesn't have to do it every frame, and move the declaration for the grid next to the plane, planeX and planeY declarations in the class.
Now to disable movement into a locked square, first add a method to check if a square at certain coordinates is locked, so something along the lines of:
private boolean isLocked(int x, int y) {
int square = board[x/50][y/50];
if (square == 1) return true;
else return false;
}
Next modify the part of your update() method where you update the plane coordinates, so vaguely something like:
if (input.isMousePressed(input.MOUSE_LEFT_BUTTON)) {
int destX = input.getMouseX() - 30;
int destY = input.getMouseY() - 50;
if (!isLocked(destX, destY)) {
this.planeX = destX;
this.planeY = destY;
}
}
It's easy!
int mx = Mouse.getX();
int my = Mouse.getY();
But, it gives you the world cordinates, and you have to translate it to pixels:
int mx = Mouse.getX();
int my = Mouse.getY() * -1 + (Window.WIDTH / 2) + 71;