drawing body(snake game)

Started by
2 comments, last by ChristianFrantz 11 years, 8 months ago
this is the code for my snake game. i know a lot of it may not be right but right now im working on just getting the body of the snake drawn. in my init() method, i have code to add two new balls to snake size because i want to start the game off with the snake as just 3 balls. For some reason, nothing is being drawn and i suspect the problem is in my paint method

[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;

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;

Rectangle randomAppleRectangle;
Rectangle BallRectangle;

Ball Ball = new Ball(100, 100, 10, 10, 0, 0);

Apple randomApple = new Apple(randomNumber(), randomNumber(), 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 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>();

SnakeSize.add(new Ball(Ball.xpos - 13, Ball.ypos - 13, 10, 10, 0, 0));
SnakeSize.add(new Ball(Ball.xpos - 26, Ball.ypos - 26, 10, 10, 0, 0));

}

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

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

for (int i = 0; i < SnakeSize.size(); i++)
{
Ball.paintBall(g);
}
}

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

public Rectangle getAppleBounds()
{
return new Rectangle(randomApple.x, randomApple.y, 15, 15);
}

public void checkCollision()
{
if(BallRectangle.intersects(randomAppleRectangle))
{
randomApple = new Apple(randomNumber(), randomNumber(), 15, 15);

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

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

Advertisement
You are correct that the problem is in your paint method.

In your for loop, you're not putting the incrementing variable 'i' to use. And you're not drawing your snake 'SnakeSize'.

You also have a class named Ball as well as a variable named Ball. Java sorts that out pretty easily, but it can be a little tougher on us humans. You should start variables and methods with lowercase letters and classes with uppercase letters. :)
For clarity: you actually are drawing the correct number of balls, but you're drawing all of them on top of each other, because you have Ball draw itself repeatedly instead of using the SnakeSize list.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

damn i can never get the naming of methods and variables right lol -.-

alright i see that now. so how do i draw the arraylist?

SnakeSize.paintBall(g); doesnt work obviously lol

i think it should be something like SnakeSize.get(i).paintBall(g); but i dont remember

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

This topic is closed to new replies.

Advertisement