Hello all, I am in the process of making my very own Snake game. It is going quite well. I have taken Computer Math in my junior year of high school and now during my senior year I am taking AP Computer Science. I was doing fine with the game until I came across this problem. I am, for some reason, having trouble getting a "snake" onto a panel that I have. The "food" works fine and appears randomly within the frame, but I created the snake in the panel using the paint component. The snake moves, but I do not know how to work on a collide method with the "snake" and the "food" if it is created in the paint. Thus I tried creating a SnakeBody class where the snake then could be moved around, but it would not show up on the panel, just the food showed up. Can someone help me get a SnakeBody class up and running or a way to work on a collide method between and object and a paint? That is all I need, I can do the rest. Here is my code, it is a tiny bit sloppy from all the trial and errors:
Food Class:
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; public class Food { double x = 0, y = 0; private static final int FRAME = 700; private double myX; // x and y coordinates of center private double myY; private double myDiameter; private Color myColor; private double myRadius; public Food() { myX = 200; myY = 200; myDiameter = 25; myColor = Color.RED; myRadius = myDiameter/2; } public Food(double x, double y, double d, Color c) { myX = x; myY = y; myDiameter = d; myColor = c; myRadius = d/2; } public double getX() { return myX; } public double getY() { return myY; } public double getDiameter() { return myDiameter; } public Color getColor() { return myColor; } public double getRadius() { return myRadius; } // modifier methods public void setX(double x) { myX = x; } public void setY(double y) { myY = y; } public void setColor(Color c) { myColor = c; } public void setDiameter(double d) { myDiameter = d; myRadius = d/2; } public void setRadius(double r) { myRadius = r; myDiameter = 2*r; } // instance methods public void jump(int rightEdge, int bottomEdge) { // moves location to random (x, y) within the edges myX = (Math.random()* (rightEdge-myDiameter) + myRadius); myY = (Math.random()* (bottomEdge-myDiameter) + myRadius); } public void draw(Graphics myBuffer) { myBuffer.setColor(myColor); myBuffer.fillOval((int)(myX - myRadius), (int)(myY-myRadius), (int)myDiameter, (int)myDiameter); } }
Snake Class:
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.awt.image.BufferedImage; public class Snake extends JPanel implements ActionListener, KeyListener { Timer t = new Timer(5, this); double x =350, y = 350, velx = 0, vely = 0, x1 = 0, y1 = 0; private static final int FRAME = 700; private Food food; private Snake snake; private BufferedImage myImage; private Graphics myBuffer; private static final Color BACKGROUND = new Color(204, 204, 204); public Snake() { myImage = new BufferedImage(FRAME, FRAME, BufferedImage.TYPE_INT_RGB); myBuffer = myImage.getGraphics(); myBuffer.setColor(BACKGROUND); myBuffer.fillRect(0, 0, FRAME,FRAME); x = 350; y = 350; x1 = Math.random()*FRAME; y1 = Math.random() *FRAME; food = new Food(x1,y1,15,Color.BLUE); t.start(); addKeyListener(this); setFocusable(true); setFocusTraversalKeysEnabled(false); } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; g2.setColor(Color.RED); g2.fill(new Rectangle2D.Double(x,y,15,15)); food.draw(g); // Graphics2D g3 = (Graphics2D) g; // g3.setColor(Color.BLUE); // g3.fill(new Rectangle2D.Double(x1,y1,15,15)); } public void actionPerformed(ActionEvent e) { // myBuffer.setColor(BACKGROUND); // myBuffer.fillRect(0,0,FRAME,FRAME); //food.draw(myBuffer); repaint(); x += velx; y += vely; } private void collide(Snake snake, Food food) { double d = distance(snake.getX(),snake.getY(),food.getX(),food.getY()); if(d <= snake.getWidth()/2 + food.getRadius()) { food.jump(FRAME,FRAME); } } //directions public void up() { vely = -1.5; velx = 0; } public void down() { vely = 1.5; velx = 0; } public void left() { velx = -1.5; vely = 0; } public void right() { velx = 1.5; vely = 0; } //movement keys public void keyPressed(KeyEvent e) { int code = e.getKeyCode(); if(code == KeyEvent.VK_LEFT) { if(velx == 1.5) { up(); } else if(velx == -1.5) { down(); } else if(vely == 1.5) { right(); } else if(vely == -1.5) { left(); } else { left(); } } if(code == KeyEvent.VK_RIGHT) { if(velx == 1.5) { down(); } else if(velx == -1.5) { up(); } else if(vely == 1.5) { left(); } else if(vely == -1.5) { right(); } else { right(); } } } public void keyTyped(KeyEvent e){} public void keyReleased(KeyEvent e){} private double distance(double x1, double y1, double x2, double y2) { return Math.sqrt(Math.pow(x2-x1,2)+Math.pow(y2-y1,2)); } }
Main Class:
import javax.swing.JFrame; public class SnakeMain { public static void main(String[] args) { JFrame f = new JFrame("Snake Game"); Snake s = new Snake(); f.add(s); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setSize(700,700); } }