7120179

how to repaint a point in snake game :(

Recommended Posts

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Forum Statistics

    • Total Topics
      628294
    • Total Posts
      2981876
  • Similar Content

    • By Madolite
      (Note to Mods: Could "Input" or "Peripherals" be a good Tag to add to the tag list? Just a thought.)

      Hey, I'm currently working on which keys on a keyboard that a user can rebind their actions to. The trick is that I use a Norwegian keyboard, so it's not obvious which keys correspond to the actual C#/XNA Keys enum values.

      Are there any keys from the XNA Keys enum that, in your opinion, I've neglected to add? I don't need all Keys to be bindable, only the "most commonly used keys on a keyboard". Thanks.
      https://pastebin.com/n1cz8Y0u
    • By Ender Blue
      Hello,
      Me and a friend have been thinking of making a game, but the thing is that, we only have someone who only knows how to write and very VERY basic coding (Me) and another person who can make the music and know a bit more coding and know how to use Game Maker.
      We are thinking of making a top down Action RPG with some crafting elements, is it possible to make it with our limited abilities? obviously we are open to learn and improve, but, can we start and do something with what we already have?
    • By Brian Paek
      Football Dash now on iOS! Over 1 million downloads on Android
      iOS:
      https://itunes.apple.com/us/app/football-dash-endless-runner/id1312590451?ls=1&mt=8
      Android:
      https://play.google.com/store/apps/details?id=com.beastattack.c1434846484727

    • By adapelin
      I am a computer engineering student and i have the assignment below. İ only can write the 2D maze array and have no idea about creating car and time as well. Could anyone write and explain hot to do???
      Minimum Criteria: You are expected to design the game by using C ++ . Below are the minimal criteria: • You must create game board with 2 - Dimensional Matrix • Bonuses create with randomly in the game board • All bonuses have got the same value but different effect for car and score . These effects may be positive or negative . • You must use pointer for creating and using car . Some bonuses may be change car type. • When the game finish, you must show high - score. • For moving car , you need to create coordinate s randomly and you need to write proper control statements. • You must use functions for drawing game board and changing car type . If you need extra functions, you can use it. • If you cannot get out the maze when the time is up , the game is over and you need to show high score. In this project, you must do all minimum criteria. In the end, your program must be work without any errors. Bonus: • Save and load high score information to/from disk • Each bonus has got different random values. • You can create cheat codes for the game. • You can create alternative control for car . • Car can jump over the wall but may lose the score . When car exit the maze , game is over and you need to show high score.    
    • By G-Dot
      Hello, everybody. I've made a model and create a skeleton, but there is a problem. I don't know how to rig this piston parts. Should I detach them to separate objects and use "Look At Constrain"? Can i rig this parts using only bones? So I need an advise how to solve this problem. Game Engine witch I'm using is Unreal Engine 4.

  • Popular Now