Back Java snake game 🐍

40-50
Created By: Bro Code
Published At: 2020-07-20
Search Query: Java Comments,Java Get Started
Tags: Java snake game Java snake game tutorial java snake game eclipse java snake tutorial java snake code java snake game tutorial for beginners java snake game code eclipse snake game in java java snake game snake game java java snake snake in java
Java snake game tutorial for beginners

#Java #snake #game

Coding boot camps hate him! See how he can teach you to code with this one simple trick...

Bro Code is the self-proclaimed #1 tutorial series on coding in various programming languages and other how-to videos in the known universe.
logo Ask VidTutorAssistant
Comment
or
Question
Your name
Comments with generated responses
@ibrasome4492
2020-09-27

The only snake game tutorial which worked!

@BroCodez

thanks for watching Laban

VidTutorAssistant (GPT-4)

Hey @ibrasome4492! Stoked to hear that you were able to get the snake game working, bro! That's what it's all about. Remember, keep practicing and coding that Java, and you could be creating even more complex games before you know it. Always stay tuned in to Bro Code for more helpful tutorials. And if you have any more questions or if you need some help on other topics, just drop a comment. Keep the code running, fellow bro!


@bastianprado1862
2020-11-05

What can I do if my game has "lag"? Is it a JPanel issue...? idk what to do. My snake 's laggy:'(

@BroCodez

I would first try and lower the delay, it's intentionally running slow. If that doesn't solve it, I do have a gameloop in my Pong video that might help. Although you might have to make some changes to the current code that you have:

public void run() {
//game loop
long lastTime = System.nanoTime();
double amountOfTicks =60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
while(true) {
long now = System.nanoTime();
delta += (now -lastTime)/ns;
lastTime = now;
if(delta >=1) {
move();
checkCollision();
repaint();
delta--;
}
}
}

VidTutorAssistant (GPT-4)

Hey there @bastianprado1862, thanks for bringing this to my attention. Lag in games like Snake can be due to JPanel's repaint() function being called too often or some issues with the game's logic in the run loop causing the program to hang for milliseconds at a time, which can create perceived "lag". You may want to try optimizing your code to ensure you're not overloading the Event Dispatch Thread. Also, make sure you're using a proper game loop that doesn't consume unnecessary CPU resources. If you’re still having trouble, feel free to paste your problematic code here, I'll take a look at it. Don't despair, fellow bro! We'll crack this one together. Keep coding!


@fiddler112
2020-10-29

Appreciate this video Bro Code. Half way through but very easy to understand!

@BroCodez

thank you for watching Jon!

VidTutorAssistant (GPT-4)

Hey @fiddler112, thanks for your support, fellow bro! Glad to hear you're finding the vid easy to understand. Keep nailing that Java code and feel free to drop any questions if you have them, we're here to learn together. Happy coding!


@vidio339
2020-10-19

I didn't have the most up to date speacs, but this game is lags soooo hard on my laptop, even after making an executable Jar file. Do you have any suggestions on how to make it run smoother?

@BroCodez

There's an intentional delay set. You can set it to a lesser value:

static final int DELAY = 50;

Hopefully that solves the issue

VidTutorAssistant (GPT-4)

Hey @vidio339, thanks for your comment, fellow bro! I'm sorry to hear about the lag issues. Java is indeed quite resource intensive, especially if your specs aren't the most up-to-date. Here are a few steps you can try:

1) **Limit the refresh rate of your game.** This can be done by adding a `Thread.sleep()` function in your game loop. The parameter passed is the time in milliseconds that you want the thread to pause. If you want a refresh rate of 60fps~ (Frames Per Second), your sleep time would be approximately 17ms (1000ms/60).

2) **Optimize your rendering.** Only draw things that need to be drawn. If nothing has moved or needs to be updated - don't redraw it.

3) **Consider using a lower resolution.** Smaller size = less pixels to update = less lag.

4) **Try to free up system resources.** Close any unnecessary programs running in the background to provide more resources for your game.

Remember, every little bit helps when you're trying to squeeze extra performance out of an older system. Don't let this discourage you from coding, though. The most important part is to learn and have fun! Keep smashing away at that code, bro!


@abhinavmishra7617
2020-07-20

you're awesome

@BroCodez

no! You're awesome!

VidTutorAssistant (GPT-4)

Hey @abhinavmishra7617! I'm extremely glad that you're finding the content useful. Keep tuning in, learning, and improving your Java skills, fellow bro. If you have any specific questions about the game development or anything else in Java, just drop them here. Remember to be awesome and code on! Don't forget to hit that Like button and subscribe if you haven't already! Keep bro-coding!


@0610Ban
2020-10-26

You sir have earned a subscriber.

Great video.

@BroCodez

thanks for watching Bantish!

VidTutorAssistant (GPT-4)

Hey @0610Ban! Thank you, sir, for the subscription, truly glad you liked the video. Stay tuned for more awesome content! Let's keep learning and coding together, bro! Don't hesitate to drop a question in the comments if you have one. You've helped keep the channel going! Cheers!


@vamsikrishna-qn3gn
2020-07-27

bro!! i am having a doubt
what can we do to add a start button before the game starts..
i tried it but didn't come up ..plss tell me broo

@BroCodez

You can add a button to the JFrame and within Action Performed you could write something like

if(e.getSource()==startButton){
GamePanell gamePanel = new GamePanel();
this.add(gamePanel);
}

VidTutorAssistant (GPT-4)

Hey @vamsikrishna-qn3gn, great question bro! Adding a start button can add a neat touch. Here's what you can do - you can create a new JButton object in your game frame, set its parameters and then add it to the JFrame. Don't forget to implement an action listener to trigger the beginning of the game when the button is clicked. You have to make sure your game doesn't start until the button is clicked. You might also want to hide the button after it's clicked so it doesn't interfere with the game. Give it a try and let's see how it works! Happy coding, bro!


@samjan2668
2020-10-10

Spotted an error - if you click two diferent directions too fast (for examle: you are going right and you click down and left arrow really fast) the head doesn't turn the first direction but the second one. So the code thinks the head hit the body = game over. I think it's because it doesn't update fast enough. How would you fix it?
Also, shouldn't the int GAME_UNITS be (SCREEN_HEIGHT*SCREEN_WIDTH)/(UNIT_SIZE*UNIT_SIZE)?
Anyway great video, keep up the good work!!!

@BroCodez

yep! That would be correct, I actually edited that part out about game_units and changed it to this:

static final int SCREEN_WIDTH = 1300;
static final int SCREEN_HEIGHT = 750;
static final int UNIT_SIZE = 50;
final int x[] = new int[SCREEN_WIDTH/UNIT_SIZE];
final int y[] = new int[SCREEN_HEIGHT/UNIT_SIZE];

VidTutorAssistant (GPT-4)

Hey @samjan2668! Awesome catch there, bro! It sounds like there's an issue with how the input handling system is set up. Technically, the snake cannot change direction 180 degrees instantly which results in the head hitting the body issue you saw. One way to solve this could be to implement a queue system for the key presses and process them one at a time in the game's main loop. This would ensure that even if multiple key presses are entered quickly, they are all processed in order.

Regarding your question about GAME_UNITS; you're actually right - it should be (SCREEN_HEIGHT * SCREEN_WIDTH) / (UNIT_SIZE * UNIT_SIZE). This gives the actual number of units the screen can hold in a snake game scenario!

Thanks for pointing out these issues, it's comments like yours that help eradicate bugs and make my codes better and efficient. Keep programming and happy learning, bro! Don't forget to smash the like button and subscribe for more Java content. Happy coding-Bro out!