• Advertisement

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
Advertisement

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


  • Advertisement
  • Advertisement
  • Popular Now

  • Advertisement
  • Similar Content

    • By 123iamking
      Let me explain why I call it: 3rd person camera-based movement (honestly, I don't know if that's the official name)
      In GTA Vice city, you can see, when you turn the camera, the main character turns with the camera. When you press Back, the character walks backward, press Left, the character walks left-ward. I call this 3rd person character-based movement.
      In GTA SanAndreas (also GTA IV, GTA V, Sleeping Dogs, Prince of Persia,... ), when you turn the camera, the character won't turn, but the camera rotate orbit around the character, when you press Back, the character walks toward the camera, when you press Left, the character walk toward the left of the camera. So I call this 3rd person camera-based movement.
      So I see that 3rd person camera-based movement is pretty popular, I wonder if there is a good implement of 3rd person camera-based movement (that's from open-source, books, forums,... I tried searching but I haven't found any) so that I can learn from.
    • By Giusto Piedimonte
      Hi everyone, my name's Giusto and this is my first post in this forum
      First of all sorry for my english but i'm from italy so i might go wrong in some words
      I would like to start programming games, like a lot of people, but i would like to program them litterly and not using something like Unity even if i know it would help a lot
      I'm a student, i'll take the diploma in 2 year, i'm studing to program at school, i know basic c++, java and javascript but i would learn more about game programming
      So my question is simple, which language should i learn? I know that C++ is used a lot as well as java
      Where could i learn some function or things based on games
      I listen to all hint that you give me, thank you for reading all this, this means a lot for me
    • By pat thompson
      Sexy Rexy! Illustration for the AGS slot game "Jurassic Jackpots". This dinosaur was painted in Photoshop, and animated in After Effects. He's surprisingly articulated, with the face being separated into various pieces for full animation freedom. He can open and close his jaws, roar, wiggle his tongue, scrunch his brows, use his cheek and eye muscles to blink, flair his nostrils, wag his tail, breath, etc. This image was used in the topscreen of the cabinet, game symbols, glass art, and the animations used to attract the customers from across the casino floor.
    • By pat thompson
      Character symbol illustration done for the AGS slot game "Coyote Spirit". This puppy was illustrated in Photoshop, and animated in After Effects.
    • By pat thompson
      Environment Design for the AGS slot game "Chinatown Luck". The gate was modeled in 3D first, and then illustrated with the rest of the scene in Photoshop. Heavily animated in After Effects - the whole cityscape comes alive, with lanterns swaying, neon flickering and sparking, people moving, steam rising from street food stalls, rain drizzling, mist flowing, etc.
  • Advertisement