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 9260509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:54:36+00:00 2026-06-18T12:54:36+00:00

https://i.stack.imgur.com/P7Ano.png <- Image ~..~ Hopefully the title is somewhat informative. I am currently making

  • 0

https://i.stack.imgur.com/P7Ano.png <- Image ~..~

Hopefully the title is somewhat informative.
I am currently making my first game in Java and i need some help with a certain piece of code.

Here we go:

As the snake moves over a red square (hereafter “blob”), the square is repainted with new, randomly generated coordinates. As these coordinates are randomly generated there is a chance of the blob being painted on top of my snake.

I want to avoid that and i’ve made the method generating the random coordinates check them against the current coordinates of the snake. The snake consists of 5x5px segments, each with their own coordinates stored in a 2D-array.

My logic goes as follows: 1) Generate X and Y coordinates. 2) Get the differential of the coordinates (as in, how far they are away from eachother) 3) if they are within COLLISIONRANGE (5px) of eachother -> do not use those coords.

What im really asking:

Is this an alright solution? Is there any glaring faults with the way i check for this?
As i play the game, i get more triggers of the System.out.println(“Log: No-go spawn #” + i); i have there for logging than i would assume is natural.

It seems that the randomly generated coordinates are within COLLISIONRANGE of the snakesegments too often for everything to be correct.

My method:

private void generateBlobCoordinates() {
    Random randomGenerator = new Random();
    boolean isClearSpawn = true;

    int x = randomGenerator.nextInt(BORDER_X_MAX);
    int y = randomGenerator.nextInt(BORDER_Y_MAX);

    int xDiff;
    int yDiff;

    // If generated coords are within COLLISIONRANGE, set isClearSpawn to false
    for(int i = 0; i < snakeSegments.length; i++) {
        if(snakeSegments[i][0] != -1) {
            xDiff = Math.abs(x - snakeSegments[i][0]);
            yDiff = Math.abs(y - snakeSegments[i][1]);

            if(xDiff <= COLLISIONRANGE || yDiff <= COLLISIONRANGE) {
                isClearSpawn = false;
                System.out.println("Log: No-go spawn #" + i);
                break;
            }
        }
        else
            break;
    }

    if(isClearSpawn == true) {
        blobX = x;
        blobY = y;
    }
    else
        generateBlobCoordinates();
}
  • 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-06-18T12:54:37+00:00Added an answer on June 18, 2026 at 12:54 pm

    You are right to be concerned. You are testing for the OR condition, but need AND. Otherwise you will get a collision anywhere on a row or column where there is a snake!

    Change

            if(xDiff <= COLLISIONRANGE || yDiff <= COLLISIONRANGE) {
    

    To

            if(xDiff <= COLLISIONRANGE && yDiff <= COLLISIONRANGE) {
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

https://i.stack.imgur.com/bRXcy.png This image is a 78*24 image of the alphabet, with each letter in
Image: https://i.stack.imgur.com/53Ms1.png I have been working with XNA some time now and this is
See the following image: https://i.stack.imgur.com/F3znK.png See those transparent circles in the background? What i
Image: https://i.stack.imgur.com/pbzar.png I want to get text on 90-270 degrees (text Aroma 7 to
please see the image for understand what I want https://i.stack.imgur.com/eC4ZR.png Activity 1 FULLSCREEN +
First of here is the Exception that I'm getting: https://i.stack.imgur.com/6V7ts.png Just to give little
https://i.stack.imgur.com/YZXZN.png (I'm currently not allowed to embed images) I could really use some help
https://i.stack.imgur.com/vK4OB.png Currently my display is like this. I have a master page which I
I have to go from a) to b) : https://i.stack.imgur.com/9hWxJ.png (the green stock-image is
https://i.stack.imgur.com/C9m5g.jpg The first image is the way my app looked before I made the

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.