Hi, I'm new to this site, and I thought this place might help me with some of the problems I am hitting.
I am currently in grade 12 Computer Science, and I am coding a third-dimensional snake game (isometric view, so can all be drawn in 2D)
I have taken a simple JFrame code from the internet for a base start, and rehauled the program to work both in Applet and in 3D.
My current issue is that nothing repaints, and I am not exactly sure why. This is meant to be run in another class, but seems to work by itself. I am not sure if that is how it is supposed to work though.
My current progress:
package snake;
//The "Board" class.
import java.applet.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.ActionEvent;
public class Board extends Applet
{
// Place instance variables here
private final int total_dots = 1000;
private boolean left = false;
private boolean right = true;
private boolean up = false;
private boolean down = false;
private boolean in = false;
private boolean out = false;
private boolean inGame = false;
private int x[] = new int [total_dots];
private int y[] = new int [total_dots];
private int z[] = new int [total_dots];
private int dots;
private int delay = 2000;
private int applex;
private int appley;
private int applez;
private int score;
private Graphics g;
private Image offscreen;
public int highscore = 0;
public Board ()
{
addKeyListener (new TAdapter ());
setBackground (Color.cyan);
init ();
}
public void init ()
{
inGame = true;
dots = 3;
x [0] = 2;
x [1] = 1;
x [2] = 0;
y [0] = 0;
y [1] = 0;
y [2] = 0;
z [0] = 0;
z [1] = 0;
z [2] = 0;
score = 0;
locateApple ();
offscreen = createImage (800, 600);
try
{
Thread.sleep (delay); // do nothing for delay time
repaint ();
}
catch (InterruptedException e)
{
e.printStackTrace ();
}
} // init method
public void paint (Graphics g1)
{
g = offscreen.getGraphics ();
if (inGame)
{
int j[] = {40 + 40 * applex + 25 * applez, 65 + 40 * applex + 25 * applez, 105 + 40 * applex + 25 * applez, 105 + 40 * applex + 25 * applez, 80 + 40 * applex + 25 * applez, 40 + 40 * applex + 25 * applez};
int k[] = {190 + 40 * appley - 15 * applez, 175 + 40 * appley - 15 * applez, 175 + 40 * appley - 15 * applez, 215 + 40 * appley - 15 * applez, 230 + 40 * appley - 15 * applez, 230 + 40 * appley - 15 * applez};
g.setColor (Color.red);
g.fillPolygon (j, k, 6);
for (int i = 0 ; i < dots ; i++)
{
if (i == 0)
g.setColor (Color.red);
else
g.setColor (Color.orange);
int m[] = {40 + 40 * x [i] + 25 * z [i], 65 + 40 * x [i] + 25 * z [i], 105 + 40 * x [i] + 25 * z [i], 105 + 40 * x [i] + 25 * z [i], 80 + 40 * x [i] + 25 * z [i], 40 + 40 * x [i] + 25 * z [i] };
int n[] = {190 + 40 * y [i] - 15 * z [i], 175 + 40 * y [i] - 15 * z [i], 175 + 40 * y [i] - 15 * z [i], 215 + 40 * y [i] - 15 * z [i], 230 + 40 * y [i] - 15 * z [i], 230 + 40 * y [i] - 15 * z [i] };
g.fillPolygon (m, n, 6);
}
}
else
{
gameOver (g);
}
g1.drawImage (offscreen, 0, 0, this);
} // paint method
public void gameOver (Graphics g)
{
if (score > highscore)
{
highscore = score;
}
}
public void checkApple ()
{
if ((x [0] == applex) && (y [0] == appley) && (z [0] == applez))
{
dots++;
score += 1;
locateApple ();
}
}
public void move ()
{
for (int i = dots ; i > 0 ; i--)
{
x [i] = x [i - 1];
y [i] = z [i - 1];
z [i] = z [i - 1];
}
//left
if (left && x [0] == 0)
x [0] = 9;
else if (left && x [0] != 0)
x [0] -= 1;
//right
if (right && x [0] == 9)
x [0] = 0;
else if (right && x [0] != 9)
x [0] += 1;
//up
if (up && y [0] == 9)
y [0] = 0;
else if (up && y [0] != 9)
y [0] += 1;
//down
if (down && y [0] == 0)
y [0] = 9;
else if (down && x [0] != 0)
y [0] -= 1;
//forward
if (in && z [0] == 9)
z [0] = 0;
else if (in && z [0] != 9)
z [0] += 1;
//backward
if (out && z [0] == 0)
z [0] = 9;
else if (out && z [0] != 0)
z [0] -= 1;
}
public void checkCollision ()
{
for (int i = dots ; i > 0 ; i--)
{
if ((i > 0) && (x [0] == x [i]) && (y [0] == y [i]) && (z [0] == z [i]))
inGame = false;
}
}
public void locateApple ()
{
int r = (int) (Math.random () * 10);
applex = r;
r = (int) (Math.random () * 10);
appley = r;
r = (int) (Math.random () * 10);
applez = r;
}
public void actionPerformed (ActionEvent e)
{
if (inGame)
{
checkApple ();
checkCollision ();
move ();
}
paint (g);
}
private class TAdapter extends KeyAdapter
{
public void keyPressed (KeyEvent e)
{
int key = e.getKeyCode ();
if ((key == KeyEvent.VK_LEFT) && (!right))
{
left = true;
up = false;
down = false;
in = false;
out = false;
}
if ((key == KeyEvent.VK_RIGHT) && (!left))
{
right = true;
up = false;
down = false;
in = false;
out = false;
}
if ((key == KeyEvent.VK_UP) && (!down))
{
up = true;
right = false;
left = false;
in = false;
out = false;
}
if ((key == KeyEvent.VK_DOWN) && (!up))
{
down = true;
right = false;
left = false;
in = false;
out = false;
}
if ((key == KeyEvent.VK_A) && (!out))
{
in = true;
down = false;
left = false;
right = false;
up = false;
}
if ((key == KeyEvent.VK_Z) && (!in))
{
out = true;
down = false;
left = false;
right = false;
up = false;
}
}
}
} // Board class
Original code I used as frame of reference: http://zetcode.com/tutorials/javagamestutorial/snake/
If anyone can help, it would be largely appreciated.