user3394968
user3394968

Reputation: 21

Issues with MouseListener

I would really like some help with this game I am making. I have tried for the longest time and the solution eludes me. It doesn't do very much at the moment. Basically, I need the mouse listener to look for a left click and change the direction of the snake one way, and the other for a right click. But I can't seem to get the listener set up correctly. Excuse me if my coding is poor or if there is a better solution to something I am doing, I haven't been coding for that long.

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.*;
import java.awt.Color;
import java.util.*;
import java.awt.event.*;

public class SnakeGame implements MouseListener{
static int delta = 10;

public static void main(String[] args) {

    JFrame f = new JFrame("Snake Game"){
        @Override
        public void paint(Graphics g){
            ArrayList<Integer> snakeX = new ArrayList<Integer>();
            ArrayList<Integer> snakeY = new ArrayList<Integer>();
            ArrayList<Integer> food = new ArrayList<Integer>();
            char direction = 'e';
            int tail = 2;
            int foodX;
            int foodY;
            boolean game = true;
            boolean check = false;
            boolean recheck = true;

            snakeX.add(0,20);
            snakeX.add(1,10);
            snakeX.add(2,0);

            snakeY.add(0,0);
            snakeY.add(1,0);
            snakeY.add(2,0);

            Random rnd = new Random();
            do{
                foodX = (rnd.nextInt(750/delta)+1) * delta;
                foodY = (rnd.nextInt(750/delta)+1) * delta;
                //System.out.println(foodX + " : " + foodY);
                for(int i = 0; i < snakeX.size(); i++){
                    if(recheck){
                        if(foodX != snakeX.get(i).intValue() && foodY != snakeY.get(i).intValue()){
                            check = true;
                            recheck = false;
                        }

                        else{
                            check = false;
                            recheck = true;
                        }
                    }

                }
            }while(!check);

            food.add(0, foodX);
            food.add(1, foodY);

            while (game){
            g.fillRect(0,0,759,781);
            drawSnake(snakeX, snakeY, food, g);
            moveSnake(snakeX, snakeY, direction, game, food, g);
            drawFood(food, g);
            for(long delay = 0; delay < 75000000; delay++);
            }
        }
    };
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(768,790);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
    }

    public void mousePressed(MouseEvent e){
    }

    public void mouseEntered(MouseEvent e){
    }

    public void mouseReleased(MouseEvent e){
    }

    public void mouseClicked(MouseEvent e){
    }

    public void mouseExited(MouseEvent e){
    }

    public static void drawFood(ArrayList<Integer> food, Graphics g){
        g.setColor(Color.red);
        g.fillRect(food.get(0).intValue()+9, food.get(1).intValue()+31, delta, delta);
    }

    public static void drawSnake(ArrayList<Integer> snakeX, ArrayList<Integer> snakeY, ArrayList<Integer> food, Graphics g){
        g.setColor(Color.black);
        g.fillRect(0,0,760,782);
        drawFood(food, g);
        g.setColor(Color.white);
        for(int i = 0; i < snakeX.size(); i++){
            g.fillRect(snakeX.get(i)+9, (int) snakeY.get(i)+31, delta, delta);
        }
    }

    public static void checkContact(ArrayList<Integer> snakeX, ArrayList<Integer> snakeY, Boolean game){
        for(int i = 1; i < snakeX.size(); i++){
            if(snakeX.get(i) == snakeX.get(0) && snakeY.get(i) == snakeY.get(0))
                game = false;
        }
    }

    public static void checkFood(ArrayList<Integer> snakeX, ArrayList<Integer> snakeY, ArrayList<Integer> food, Graphics g){
        if(snakeX.get(0).intValue() == food.get(0).intValue() && snakeY.get(0).intValue() == food.get(1).intValue()){
            setFood(food, snakeX, snakeY);
            snakeX.add(snakeX.get(snakeX.size()-1).intValue()-delta);
            snakeY.add(snakeY.get(snakeY.size()-1).intValue()-delta);
        }
    }
    public static void setFood(ArrayList<Integer> food, ArrayList<Integer> snakeX, ArrayList<Integer> snakeY){
        boolean check = false;
        boolean recheck = true;
        Random rnd = new Random();
        int foodX;
        int foodY;
        do{
            foodX = (rnd.nextInt(750/delta)+1) * delta;
            foodY = (rnd.nextInt(750/delta)+1) * delta;
            //System.out.println(foodX + " : " + foodY);
            for(int i = 0; i < snakeX.size(); i++){
                if(recheck){
                    if(foodX != snakeX.get(i).intValue() && foodY != snakeY.get(i).intValue()){
                        check = true;
                        recheck = false;
                    }

                    else{
                        check = false;
                        recheck = true;
                    }
                }

            }
        }while(!check);

        food.set(0, foodX);
        food.set(1, foodY);
    }

    public static void moveSnake(ArrayList<Integer> snakeX, ArrayList<Integer> snakeY, char direction, Boolean game, ArrayList<Integer> food, Graphics g){
        if(snakeX.get(0).intValue() < 740 && snakeX.get(0).intValue() >= 0 && snakeY.get(0).intValue() < 740 && snakeY.get(0).intValue() >= 0){
        if(direction == 'e'){
            int temp = snakeX.get(0).intValue();
            int temp2 = 0;
            snakeX.set(0, temp+delta);
            for(int i = 1; i < snakeX.size(); i++){
                temp2 = snakeX.get(i);
                snakeX.set(i,temp);
                temp = temp2;
            }

            int tempY = snakeY.get(0).intValue();
            int tempY2 = 0;
            snakeY.set(0, tempY);
            for(int i = 1; i < snakeY.size(); i++){
                tempY2 = snakeY.get(i);
                snakeY.set(i,tempY);
                tempY = tempY2;
            }

        }
        else if(direction == 'n'){
            int temp = snakeX.get(0).intValue();
            int temp2 = 0;
            snakeX.set(0, temp);
            for(int i = 1; i < snakeX.size(); i++){
                temp2 = snakeX.get(i);
                snakeX.set(i,temp);
                temp = temp2;
            }

            int tempY = snakeY.get(0).intValue();
            int tempY2 = 0;
            snakeY.set(0, tempY+delta);
            for(int i = 1; i < snakeY.size(); i++){
                tempY2 = snakeY.get(i);
                snakeY.set(i,tempY);
                tempY = tempY2;
            }
        }
        else if(direction == 'w'){
            int temp = snakeX.get(0).intValue();
            int temp2 = 0;
            snakeX.set(0, temp-delta);
            for(int i = 1; i < snakeX.size(); i++){
                temp2 = snakeX.get(i);
                snakeX.set(i,temp);
                temp = temp2;
            }

            int tempY = snakeY.get(0).intValue();
            int tempY2 = 0;
            snakeY.set(0, tempY);
            for(int i = 1; i < snakeY.size(); i++){
                tempY2 = snakeY.get(i);
                snakeY.set(i,tempY);
                tempY = tempY2;
            }

        }
        else if(direction == 's'){
            int temp = snakeX.get(0).intValue();
            int temp2 = 0;
            snakeX.set(0, temp);
            for(int i = 1; i < snakeX.size(); i++){
                temp2 = snakeX.get(i);
                snakeX.set(i,temp);
                temp = temp2;
            }

            int tempY = snakeY.get(0).intValue();
            int tempY2 = 0;
            snakeY.set(0, tempY-delta);
            for(int i = 1; i < snakeY.size(); i++){
                tempY2 = snakeY.get(i);
                snakeY.set(i,tempY);
                tempY = tempY2;
            }
        }
        checkFood(snakeX, snakeY, food, g);
        checkContact(snakeX, snakeY, game);
        }
        drawSnake(snakeX, snakeY, food, g);
    }
}

Upvotes: 2

Views: 220

Answers (2)

BitNinja
BitNinja

Reputation: 1487

Your game loop (the part of your code that says while(game)) which is supposed to run until the game is over is a little misplaced. Because that loop is infinite on purpose it will never reach the lines after it that set up values for the frame. These statements:

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(768,790);
f.setLocationRelativeTo(null);
f.setVisible(true);

Need to be placed before the loop. Then to properly register the frame's mouse listener you have to add it to your frame like this:

f.addMouseListener(this);

This works because SnakeGame can be considered a MouseListener because it implements that interface.

Bonus Tip: The correct way to pause execution is to use a swing timer, not have a for loop run a bunch of times.

Read this for more information about using timers. Hope that Helps!

Upvotes: 2

xtr&#233;
xtr&#233;

Reputation: 132

You must add the mouse listener f.addMouseListener(this); Your code is really hard to read as it's all done inside main. You should first start a new instance of your class and use its constructor

public SnakeGame(){
//code here 
}

It's a lot to explain look up how to structure your classes.

Upvotes: 0

Related Questions