I'm having trouble getting the program to randomly display the food. Right now I'm just trying to get a rectangle box to randomly display in some x,y coordinates and the program is doing this and switching the coordinates each time but the object(rectangle) isn't appearing on the screen, any suggestions? Thanks again you guys always help out.
Here is the code there are four parts to it now.
The frame
import java.awt.event.*; import javax.swing.*; import java.awt.*; public class snakeFrame extends JFrame { private static final int FRAME_WIDTH = 400; private static final int FRAME_HEIGHT = 400; private boolean right = false; private boolean left = false; private boolean up = false; private boolean down = false; private boolean wallCollision = true; private boolean onBoard = true; private boolean food = true; // calls the other classes private snakeBody snake; private snakeFood newFood; private Timer t; class KeyStrokeListener implements KeyListener { public void keyPressed(KeyEvent event) { String key = KeyStroke.getKeyStrokeForEvent(event).toString().replace("pressed ", ""); if (key.equals("DOWN")) { down = true; right = false; left = false; up = false; } else if ( key.equals("UP")) { up = true; right = false; down = false; left = false; } else if ( key.equals("LEFT")) { left = true; right = false; down = false; up = false; } else if (key.equals("RIGHT")) { right = true; left = false; down = false; up = false; } } public void keyTyped(KeyEvent event) {} public void keyReleased(KeyEvent event) { } } public snakeFrame() { snake = new snakeBody(); newFood = new snakeFood(); add(newFood); add(snake); snake.addKeyListener(new KeyStrokeListener()); snake.setFocusable(true); class TimerListener implements ActionListener { public void actionPerformed(ActionEvent event) { if (left == true) { snake.moveRectangleBy(-5, 0); snake.getBox_X(); snake.getBox_Y(); System.out.println(snake.getBox_Y()); System.out.println(snake.getBox_X()); } if (right == true) { snake.moveRectangleBy(5, 0); snake.getBox_X(); System.out.println(snake.getBox_X()); snake.getBox_Y(); System.out.println(snake.getBox_Y()); } if (up == true) { snake.moveRectangleBy(0, -5); snake.getBox_Y(); System.out.println(snake.getBox_Y()); snake.getBox_X(); System.out.println(snake.getBox_X()); } if (down == true) { snake.moveRectangleBy(0, 5); snake.getBox_Y(); System.out.println(snake.getBox_Y()); snake.getBox_X(); System.out.println(snake.getBox_X()); } // checks collision for snake and frame height if( snake.getBox_X()> FRAME_HEIGHT) onBoard = false; if (snake.getBox_X() < 0) onBoard = false; if(snake.getBox_Y() > FRAME_WIDTH) onBoard = false; if(snake.getBox_Y() < 0) onBoard = false; if(onBoard == false) System.out.println("Collision"); if(onBoard == true) System.out.println("On Board"); /* // checks collision for snake and food if( food.getFood_X() == snake.getBox_X()) food = false; if (food.getFood_X() == snake.getBox_X()) newFood = false; if(food.getFood_Y() == snake.getBox_Y()) newFood = false; if(food.getFood_Y() == snake.getBox_Y()) newFood = false; if(newFood == false) System.out.println("Food in Game"); if(onBoard == true) System.out.println("Need new Food"); */ } } TimerListener listener2 = new TimerListener(); final int DELAY = 100; t = new Timer(DELAY, listener2); t.start(); setSize(FRAME_WIDTH, FRAME_HEIGHT); } }
The Food code for the random food to generate
import java.awt.event.*; import javax.swing.*; import java.awt.*; public class snakeFood extends JComponent { // Food_x and y will be declared in find food private int Food_X; private int Food_Y; // size of the food private int food_WIDTH = 10; private int food_HEIGHT = 10; // used to create a random coordinate for food x and y private int xyCoordinate; private final int RandomPosition = 124; private Graphics g2; private Rectangle food; //Used to place food at random Coordinates public snakeFood() { food = new Rectangle(Food_X, Food_Y, food_WIDTH, food_HEIGHT); findFood(); getRectangle(); repaint(); System.out.println(food); } //Creates the random Coordinates // may need to change the xyCoordinate again. Doesn't seem to move out of the uppper left hand corner public void findFood() { xyCoordinate = (int) (Math.random() * RandomPosition); Food_X = xyCoordinate; xyCoordinate = (int) (Math.random() * RandomPosition); Food_Y = xyCoordinate; } public int getFood_X() { return Food_X; } public int getFood_Y() { return Food_Y; } public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.draw(food); g2.setColor(Color.BLUE); g2.fill(food); repaint(); } public Rectangle getRectangle() { return food; } }
then the viewer
import java.awt.event.*; import javax.swing.*; import java.awt.*; public class snakeViewer { public static void main(String[] args) { final JFrame frame = new snakeFrame(); JMenuBar menuBar = new JMenuBar(); frame.setJMenuBar(menuBar); //Make the menu JMenu menu = new JMenu("File"); //create and add menuItems to menu final JMenuItem newItem = new JMenuItem("New Game"); final JMenuItem exitItem = new JMenuItem("Exit"); menu.add(newItem); menu.add(exitItem); menuBar.add(menu); //add menu to menuBar class MenuItemListener implements ActionListener { public void actionPerformed(ActionEvent event) { if(event.getSource() == newItem ) System.exit(0); else if(event.getSource() == exitItem ) System.exit(99); } } //Make listener and register sources to listener ActionListener listener = new MenuItemListener(); newItem.addActionListener(listener); exitItem.addActionListener(listener); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
here is the body class as well and this works and will show up
import java.awt.event.*; import javax.swing.*; import java.awt.*; public class snakeBody extends JComponent { private int BOX_X = 100; private int BOX_Y = 100; private int BOX_WIDTH = 10; private int BOX_HEIGHT = 10; private Rectangle box; public snakeBody() { box = new Rectangle(BOX_X, BOX_Y, BOX_WIDTH, BOX_HEIGHT); } public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.draw(box); g2.setColor(Color.GREEN); g2.fill(box); } // used to move the rectangle //Box x + dx allows us to recognize where the coordinates of the box have moved to after the translation public void moveRectangleBy(int dx, int dy) { BOX_X = BOX_X + dx; BOX_Y = BOX_Y + dy; box.translate(dx, dy); repaint(); } public int getBox_Y() { return BOX_Y; } public int getBox_X() { return BOX_X; } }
I just need to figure out why the food(blue rectangle) isn't appearing when I run the program and that's all for now. Thanks