thread error

Started by
138 comments, last by ChristianFrantz 11 years, 8 months ago
What's the error and what line?

Also, what do you intend for the 'i' in .get(i) to be/mean?
Advertisement
the error is at if(mainBallRectangle.intersects(BallRectangle.get(i)))

i is supposed to be the number of rectangles stored in the array BallRectangle. but now that i type it out it makes more sense to put that in a for loop

[source lang="java"]for(int i = 0; i < BallRectangle.size(); i++)
if(mainBallRectangle.intersects(BallRectangle.get(i)))
{
mainBall.xspeed = 0;
mainBall.yspeed = 0;
mainBall.xpos = 100;
mainBall.ypos = 100;
}[/source]

HOLY SHIT I WIN. Game complete :'D

If you see a post from me, you can safely assume its C# and XNA :)

Congrats! :-D

Woot woot
yay!!! now for snake lol

1. add a ball that deletes itself everytime mainBall intersects it. im just going to use fixedBallRectangle and mainBall for this.
2. somehow make the snake bigger... add another ball that follows the mainBall?

ok so i have one arraylist just to store the length of the snake. the rest should be relatively easy, drawing a new apple for the snake to eat and check collision and if the snake comes in contact with the apple i delete the apple and draw a new one at a random point

how do you remove an object drawn to the screen?

If you see a post from me, you can safely assume its C# and XNA :)

Sounds like a strategy that can work. Good job setting up an approach of getting a little bit working then getting a little more working and so on.
thank you for all your help :) i think i can do this one by myself!!

If you see a post from me, you can safely assume its C# and XNA :)

Awesome. Knock it out of the park! And feel free to make a thread to show off your work. Your fellow GameDevers enjoy trying out completed projects and throwing out (usually challenging) suggestions of other things to do/add.
aw man one last question. i have a mainBall and a randomApple and im testing collision for them. every time i hit the randomApple with the mainBall it adds another main ball to my arrayList MainBall. The only problem is that it adds 4 balls to my mainball arraylist instead of just one. how do i fix that?

[source lang="java"]import java.applet.Applet;
import java.awt.Color;
import java.awt.Component;
import java.awt.Event;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

public class snake extends Applet implements Runnable
{
static final int WIDTH = 450; //set screen height
static final int HEIGHT = 450; // set screen width
private Image dbImage;
private Graphics dbg;

public ArrayList<Ball> SnakeSize;

static final int NUM_OF_BALLS = 4;

int i;
int t;

Rectangle randomAppleRectangle;
Rectangle BallRectangle;

Ball Ball = new Ball(100, 100, 10, 10, 0, 0);
Apple randomApple = new Apple(250, 250, 15, 15);

public void start()
{
Thread th = new Thread(this);
th.start();//start main game
}

public void updateGame()
{
randomAppleRectangle = getAppleBounds();
BallRectangle = getBallBounds();

checkCollision();

Ball.ypos += Ball.yspeed;
Ball.xpos += Ball.xspeed;

if (Ball.xpos < 1)
{
Ball.xpos = 449;
}

if (Ball.xpos > 449)
{
Ball.xpos = 1;
}
if (Ball.ypos < 1)
{
Ball.ypos = 449;
}

if (Ball.ypos > 449)
{
Ball.ypos = 1;
}

try
{
Thread.sleep(20);
}
catch(InterruptedException ex){}
}

private void debugPrint(String value)
{
System.out.println(value);
}

public class updateTime extends Thread implements Runnable
{
public void run()
{
for(t = 0; ; t++)
{
try
{
Thread.sleep(1000);
}
catch(InterruptedException e){}
}
}
}

public int randomNumber()
{
return (int)(Math.random() * 400);
}

public class Ball
{
int width;
int height;
int xpos;
int ypos;
int xspeed;
int yspeed;

public Ball( int xpos, int ypos, int width, int height, int xspeed, int yspeed)
{
this.width = 10;
this.height = 10;
this.xpos = 100;
this.ypos = 100;
this.xspeed = 0;
this.yspeed = 0;
}

public void paintBall(Graphics g)
{
g.setColor(Color.black);
g.fillOval(Ball.xpos, Ball.ypos, width, height);
g.drawString(xpos + ", " + ypos, 20, 40);
}
}//mainBall

public class Apple
{
int x;
int y;
int width;
int height;

public Apple(int x, int y, int width, int height)
{
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}//end ball

public void paint(Graphics g)
{
g.setColor(Color.red);
g.fillOval(x, y, width, height);
g.drawString("Apple printed at: " + randomApple.x + ", " + randomApple.y, 20, 60);
g.drawString("Size of snake: " + SnakeSize.size(), 20, 80);
} //end paint

} //ball class

public void update(Graphics g) //double buffer don't touch!!
{
if(dbImage == null)
{
dbImage = createImage(this.getSize().width, this.getSize().height);
dbg = dbImage.getGraphics();
}

dbg.setColor(getBackground());
dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);

dbg.setColor(getForeground());
paint(dbg);

g.drawImage(dbImage, 0, 0, this);
}

public boolean keyDown (Event e, int key)
{
if(key == Event.LEFT)
{
Ball.xspeed = -5;
Ball.yspeed = 0;
}

if(key == Event.RIGHT)
{
Ball.xspeed = 5;
Ball.yspeed = 0;
}

if(key == Event.UP)
{
Ball.yspeed = -5;
Ball.xspeed = 0;
}

if(key == Event.DOWN)
{
Ball.yspeed = 5;
Ball.xspeed = 0;
}
return true;
}

public void run()
{
while(true)
{
repaint();
}
}

public void init()
{
this.setSize(WIDTH, HEIGHT);

SnakeSize = new ArrayList<Ball>();

}

public void paint(Graphics g)
{
updateGame();

g.drawString("time: " + t, 20, 20);

Ball.paintBall(g);
randomApple.paint(g);
}

public Rectangle getBallBounds()
{
return new Rectangle(Ball.xpos, Ball.ypos, 10, 10);
}

public Rectangle getAppleBounds()
{
return new Rectangle(250, 250, 15, 15);
}

public void checkCollision()
{
if(BallRectangle.intersects(randomAppleRectangle))
{
SnakeSize.add(new Ball(Ball.xpos - 10, Ball.ypos - 10, 10, 10, 0, 0));
}
}
}[/source]

If you see a post from me, you can safely assume its C# and XNA :)

I don't see where you're getting rid of the random apple after they collide. With that being the case, it seems very likely that the ball and apple are colliding so one gets added. Then next time through updateGame, they are still colliding so another gets added. And so on. If you get rid of that random apple after the collision(likely by making your one random apple be somewhere else), that should solve that.
[source lang="java"]public void checkCollision()
{
if(BallRectangle.intersects(randomAppleRectangle))
{
randomApple = new Apple(randomNumber(), randomNumber(), 15, 15);

SnakeSize.add(new Ball(Ball.xpos - 10, Ball.ypos - 12, 12, 10, 0, 0));
}
}[/source]

perfect. once again, thank you so much :D

If you see a post from me, you can safely assume its C# and XNA :)

This topic is closed to new replies.

Advertisement