Check out my new Kotlin tutorial series: How to make snake in java!
We create movement and apples!
Thumbnails by:
Best Coding Tricks Online
Check out my new Kotlin tutorial series: How to make snake in java!
We create movement and apples!
Thumbnails by:
If anyone is still looking at this, i need some help.
I did everything the same but my snake does not react to the keys that i press
I can't find what's wrong
if the snake touches the border game will be over. right?
Nothing happens when I click a key. I tested this by making it print something when I clicked a key, nothing.
its working but it the snake going so fast is there a way to control the speed of the snake?
i cant move my snake plz help
Finished your turorial. Thanks for teaching us 😀
For those of you that are wondering on how to change the color enter this code under
public void paint(Graphics g){
g.clearRect(0, 0, WIDTH, HEIGHT);
—BACKGROUND CODE-g.setColor(new Color(18, 40, 11)); //replace the 3 numbers with the color you like
g.fillRect(0, 0, WIDTH, HEIGHT);
Awesome Videos dude!
Incase this helps anyone still going through this, I think its better to replace the tick int in the tick method in the screen class which counts up to 250,000 and replacing it with sleeping the thread (i.e. Thread.sleep(60);).
So instead of running through each of those for loops and if statements in the tick method 250,000 times the thread waits 60 milliseconds and only runs that code once it needs to.
This should remove any speed issues and allows you to set how long the user gets to make a move and makes the game less processor dependent I would think.
Guys help I cannot change the color of the background. I make the Screen s setted to Background color black but it doesnt work HELP ?!!?
How did you change the background color? In the last video it was white, now it's a dark grey kinda
what was the point in creating the tick methods inside the bodypart class and the apples class.. when you are not going to use them
… love the tutorials by the way…
+pj6444 Could you please send me the code to make the snake loop, I'm new to JAVA and don't know how to make it loop myself
My apple will sometimes spawn out of the range I ca get to what did I do wrong?
How would you use that messy for loop instead of the ArrayList iterator?
I'm at about 3:15 and I have some errors I think they are pretty simple but I can't seem to figure them out Bymyself (there are stars where the errors are)package com.lol.game.graphics;import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;import javax.swing.JPanel;import com.lol.game.entities.BodyPart;public class Screen extends JPanel implements Runnable { private static final long serialVersionUID = 1L;
public static final int WIDTH = 800, HEIGHT = 800;
private Thread thread;
private boolean running = false;
private BodyPart b;
private ArrayList<BodyPart> snake;
private int xCoor = 10, yCoor = 10;
private int size = 5;
private boolean right = false, left = false, up = false, down = true;
private int ticks = 0;
*private Key key;
public Screen() {
setFocusable(true);
* key = new Key();
* addKeyListener(key);
setPreferredSize(new Dimension(WIDTH, HEIGHT));
snake = new ArrayList<BodyPart>();
start();
}
public void tick() {
if(snake.size() == 0 ) {
b = new BodyPart(xCoor, yCoor, 10);
snake.add(b);
if(snake.size() > size) {
snake.remove(0);
}
}
ticks++;
if(ticks > 250000) {
if(right) xCoor++;
if(left) xCoor–;
if(up) yCoor–;
if(down) yCoor++;
ticks = 0;
b = new BodyPart(xCoor, yCoor, 10);
snake.add(b);
if(snake.size() > size) {
snake.remove(0);
}
}
}
public void paint(Graphics g) {
g.clearRect(0, 0, WIDTH, HEIGHT);
g.setColor(Color.BLACK);
for(int i = 0; i < WIDTH / 10; i++) {
g.drawLine(i * 10, 0, i * 10, HEIGHT);
}
for(int i = 0; i < HEIGHT / 10; i++) {
g.drawLine(0, i * 10, WIDTH, i * 10);
}
for(int i = 0; i < snake.size(); i++) {
snake.get(i).draw(g);
}
}
public void start () {
running = true;
thread = new Thread(this, "Game Loop");
thread.start();
}
public void stop () {
}
public void run() {
while(running) {
tick();
repaint();
}
}
private class key implements KeyListener { public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if(key == KeyEvent.VK_RIGHT && !left) {
up = false;
down = false;
right = true;
}
if(key == KeyEvent.VK_LEFT && !right) {
up = false;
down = false;
left = true;
}
if(key == KeyEvent.VK_UP && !down) {
left = false;
right = false;
up = true;
}
if(key == KeyEvent.VK_DOWN && !up) {
left = false;
right = false;
down = true;
}
} public void keyReleased(KeyEvent arg0) {
} public void keyTyped(KeyEvent arg0) {
}
}}
how can i make my snake slim so i can see where i turned?? example: when snake is going right and i turn up-right-down-right-up-right…(i will get big rectangle)
Hey ive got a problem saying screen.key is not an abstract something on the key implements keylistener.
WOW this is too awesome !!!
thank you very much <3
Cool tutorial, man. Only question, sometimes snake slightly slow, maybe you should somehow sync frames and drawing an output?
Is there any kind of potential memory problem with constantly creating and removing BodyParts? Will garbage collect always do its job? I was thinking about trying something like this but having each BodyPart hold a reference to the body part in front of it and moving toward it when it's time to tick.
Please help! My Snake is going super slow and the keys are sometimes not working.
Thanks for the epic ad clear tut, Thanks for everything. 😀
how can i set the speed of the snake ???
Instead of stopping the game when it goes out of bounds replace that piece of code with:
if(xCoor < 0) xCoor = 79;
if(xCoor > 79) xCoor = 0;
if(yCoor < 0) yCoor = 79;
if(yCoor > 79) yCoor = 0;
Thanks for the video, I've been working for quite a few hours but can't seem to figure out how to restart the program when prompted (i.e with an if statement) i just keep getting errors, or the snake starts off dead