Advertisement

how to repaint a point in snake game :(

Started by September 26, 2017 02:07 AM
2 comments, last by 7120179 7 years, 5 months ago

i have a prolem with my first game that i can't re paint a point after the snake ate. Can you resolve my problem :(

i use an array 2D to draw map, 2 arrays to draw snake and an array 2D to draw food. But i can't make the food disapear when the snake meet food and draw a new food :( Please help me or give me an idea. i also want add another map in the future or some of food like poison for the snake.

Controller.txt

Food.txt

GameScreen.txt

Main.txt

MapNoWall.txt

Snake.txt

I don't use the same language you use, so I might be mistaken, but it looks like you have two separate Foods.

You have one inside Snake:


public class Snake {
	
	
	static int GO_UP = 1;
	static int GO_DOWN = -1;
	static int GO_LEFT = 2;
	static int GO_RIGHT = -2;
	
	int vector = Snake.GO_DOWN;
	Food food; //<<------

And you have one inside GameScreen:


public class GameScreen extends JPanel implements Runnable{
private static final long serialVersionUID = 1L;
	static int weightpanel = 650;
	static int heightpanel = 650;
	Controller controller;
	MapNoWall map;
	Snake snake;
	Thread thread;
	Food food; //<<-----

 

The one inside GameScreen is the one you are drawing. The one inside Snake is the one you are erasing. These are two separate copies (called "instances") of the Food class, and if you change one, it's not going to change the other, they are two different copies / instances.

Snake should not own the food - what you need to do is somehow give Snake temporary access to GameScreen's 'food' variable only during the 'Snake.update()' function, by passing GameScreen's food into the Snake.update() as a parameter. However! It has to be passed by reference (temporarily sharing a single instance), not passed by value (copying the instance is not what you want), and since I don't know your language, I can't tell you exactly how to do that - perhaps someone else knows, or you can google "<your language name> pass by reference" and learn about it. It's possible your language passes function parameters by reference by default.

Advertisement
1 hour ago, Servant of the Lord said:

I don't use the same language you use, so I might be mistaken, but it looks like you have two separate Foods.

You have one inside Snake:



public class Snake {
	
	
	static int GO_UP = 1;
	static int GO_DOWN = -1;
	static int GO_LEFT = 2;
	static int GO_RIGHT = -2;
	
	int vector = Snake.GO_DOWN;
	Food food; //<<------

And you have one inside GameScreen:



public class GameScreen extends JPanel implements Runnable{
private static final long serialVersionUID = 1L;
	static int weightpanel = 650;
	static int heightpanel = 650;
	Controller controller;
	MapNoWall map;
	Snake snake;
	Thread thread;
	Food food; //<<-----

 

The one inside GameScreen is the one you are drawing. The one inside Snake is the one you are erasing. These are two separate copies (called "instances") of the Food class, and if you change one, it's not going to change the other, they are two different copies / instances.

Snake should not own the food - what you need to do is somehow give Snake temporary access to GameScreen's 'food' variable only during the 'Snake.update()' function, by passing GameScreen's food into the Snake.update() as a parameter. However! It has to be passed by reference (temporarily sharing a single instance), not passed by value (copying the instance is not what you want), and since I don't know your language, I can't tell you exactly how to do that - perhaps someone else knows, or you can google "<your language name> pass by reference" and learn about it. It's possible your language passes function parameters by reference by default.

thanks for your help, i will apply this. If i success i will post in this post

This topic is closed to new replies.

Advertisement