I decided to re-create Snake using Java, but I'm sort of stuck. At the moment, I have a square that the user can move around the screen using the arrow keys. When you press LEFT once, the square begins to move left using a timer.. You don't need to hold down the key or keep pressing it; it changes direction when you press any of the other keys that are set(Right, Up, Down). My goal is to use an ArrayList to hold the squares that make up the snake. At the moment, I've created an ArrayList with just one Snake object inside, if I add a second Snake object to the list and add it to the frame(with the first), only one Snake object is visible and the keys to move it don't function. I'm looking for some ideas as to how I can progress with this project - please don't give me the full answer, because I'd like to figure out most of it on my own; I just need some direction. Thanks in advance - code is below.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Snake extends JPanel implements KeyListener, ActionListener
{
int x = 0, y = 0, velx = 0, vely = 0;
Timer t = new Timer(250, this);
public Snake(int num1, int num2)
{
t.start();
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(true);
x = num1;
y = num2;
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.blue);
g.fillRect(x, y, 20, 20);
}
public void actionPerformed(ActionEvent e)
{
repaint();
x += velx;
y += vely;
}
public void up()
{
vely = -20;
velx = 0;
}
public void down()
{
vely = 20;
velx = 0;
}
public void left()
{
vely = 0;
velx = -20;
}
public void right()
{
vely = 0;
velx = 20;
}
public void keyPressed(KeyEvent e)
{
int code = e.getKeyCode();
if(code == KeyEvent.VK_UP)
up();
else if(code == KeyEvent.VK_DOWN)
down();
else if(code == KeyEvent.VK_RIGHT)
right();
else if(code == KeyEvent.VK_LEFT)
left();
}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
}
}
//Driver Class
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
public class UserClass
{
private static JFrame frame = new JFrame("Snake");
private static ArrayList<Snake> mySnake = new ArrayList<Snake>();
public static void main(String[] args)
{
frame.setSize(500,500);
frame.setBackground(Color.black);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mySnake.add(new Snake(20,20));
frame.add(mySnake.get(0));
}
}
P.S When I put this same exact code in Eclipse on my Mac, the background of my frame is black, but on Windows it's light gray... Anyone know why? Thanks.
To answer your postscript, set a background color or make a JPanel behind everything else with a painted color.
To progress with the project, consider making it an exercise in the MVC framework. What is happening now is that your Model and View are linked in the same class - this makes it hard to follow the logic behind everything.
What you can do is first separate your Snake from anything to do with the view - make a snake with an ArrayList<Integer[]> segmentLocations
or something, to represent the (x,y) of each of the segment locations on a board that you define - you can use absolute coordinates or you can make an arbitrary grid and change to absolute coordinates in your View (this typifies the MVC relationship better). Snake should also have a field for the direction the snake is facing - I would use an enum Direction {N, S, E, W}
, but you have options on that, as you could also have an integer representing direction, or a variety of other ways.
Your Snake would then also have ways to update itself - move(), shifting the location of all of the segments based on the current direction for the initial segment and causing all of the other segments to follow the movement of the one before it (this is pretty easy if you consider it for a couple of minutes).
Your view could be a JFrame with a GridLayout consisting of JPanels which poll your Snake instance and see if there is a segment in the location and if so, draw it, or a multitude of other options.
Your controller would be the KeyAdapter that sends the updates in direction to your Snake when the arrow keys are pressed.
Small hint, to make your life easier: when you add a new segment, just have it at the location of the last segment of the Snake. The next time it moves, the new segment will be in the same location, and the rest of the Snake should have move accordingly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With