Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6631673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:34:47+00:00 2026-05-25T22:34:47+00:00

So I am making a snake game , but if I move from one

  • 0

So I am making a snake game, but if I move from one direction to the other really fast then it says I made a collision with the body and ends the game (for example, if I am going left and I hit down and then left again really fast or down and right really fast, etc.)

I’ve tried a few things already. I changed the way that it checked for a collision by making it a .intersects rather than checking if (x == body[i][0] && y = body[i][1]). I also did a few System.out.println()‘s to see if maybe something was going wrong there. I noticed that sometimes one of the values repeats (either x or y depending on the direction), but I can’t figure out why… I figure that the repeating is why it messes up the collision, but I can’t find the spot where it would be making it repeat.

x and y are being changed by a thread.

Here is the “Drawing” portion of my code. If you need any other snippets of code please let me know.

public void paintComponent(Graphics g) {
    super.paintComponent(g);

    if (numOfFood == 1) {//Checks wether there is food on the GUI
        g.setColor(Color.BLUE);
        g.fillRect(foodX,foodY,12,12);
    }
    else {
        foodX = random.nextInt(103)*12; //Both this and the below line get a random x or y value to put on GUI for food placement
        foodY = random.nextInt(57)*12;

        numOfFood = 1;
    }
    Rectangle headRect = new Rectangle( x, y, 12, 12 ); //Actual rectangle of the head

    Rectangle foodRect = new Rectangle(foodX, foodY, 12, 12); //Food rectangle

    g.setColor(Color.RED);
    g.fillRect(x,y,12,12); //Draws head of Snake
    g.setColor(Color.WHITE);
    g.fillRect(x+2,y+2,8,8); //Draws a white square in the head of the snake

    for (int i = 0; i < n; ++i) { //Collision Checker
        Rectangle bodyRect = new Rectangle(body[i][0],body[i][1],12,12);
        if ( headRect.intersects(bodyRect)) {
            for (int j = 0; j < n; ++j) {
                body[j][0] = -1;
                body[j][1] = -1;
            }
            numOfFood = 1;
            n = 0;
            x = 624;
            y = 348;
            endGame = true;
        }
    }

    g.setColor(Color.RED);
    if (n > 0) { //Puts the snakes body behind the head
        for (int i = 0;i < n; ++i) {
            g.fillRect(body[i][0],body[i][1],12,12);
        }
    }

    for (int i = n-1;i >= 0; --i) { //Inserts the head at the first part of the array so that the body moves
        if (body[i][0] != -1 && body[i][1] != -1) {
            body[i+1][0] = body[i][0];
            body[i+1][1] = body[i][1];
        }

        if (i == 0) {
            body[i][0] = x;
            body[i][1] = y;
        }
    }

    if (headRect.intersects(foodRect)) { //If the food rectangle and the head rectangle intersect then the snake got the food.
      numOfFood = 0;
      n++;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T22:34:48+00:00Added an answer on May 25, 2026 at 10:34 pm

    When do you call paintComponent? I suspect that you have one method that continuously moves the snake forward in regular intervals, but paintComponent is responsible for making the snake longer.

    You should move collision and moving the snake into the same method that is responsible for moving the head in the direction the snake is moving.

    Otherwise, paintComponent might be called many times on one move update, and this is responsible for duplicates of x and y in your array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a new website. It will be blog style, but made from scratch
I am currently in the process of making a snake game using VB.NET... I
I made a basic snake game in a DOS enviroment using turbo C++ 3.0,
I am making a snake game in python with pygame and for moving the
I'm new to C# so I'm making a Snake Game to learn. I created
I'm making a simple snake game in as3. the problem I'm having is that
I'm making a snake game and on every tick the snake moves. because the
So I'm making a snake game with teleports and the usual mice. I had
I'm experimenting by making a social network from scratch in PHP/MySQL, but I'm having
I am making a timetabling program which does one to one matches from SubjectTeacherPeriod

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.