Click here to Skip to main content
16,019,764 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello.Before 7 months, I made the famous snake game with graphics using python3 and tkinter.I also put some bombs and moving rockets as obstacles & other stuff.

Now, I want to make the snake move itself.
What I've done so far is to make it move itself trying to find and catch the apple, successfully.
But, I also want not to eat itself and avoid circles, because if for instance it moves like: right -> down -> left -> up and then it moves on the right side again,it will be trapped and there will be no way out (if the length of snake is big enough).
I didn't think of anything good that would work so far, so I ask you to tell me some good ideas (no code required) in order to solve these 2 problems.

What I have tried:

Problem #1: avoid eat itself
Problem #2: avoid circles
Posted
Updated 28-Dec-21 14:24pm
Comments
Richard MacCutchan 28-Dec-21 4:04am    
What do you mean by "avoid eat itself"? As for point 2, you need to keep track of each cell that the snake's body occupies. So, if it hits an occupied cell you code needs to figure out which is the best direction to turn to.
Nick_is_asking 28-Dec-21 4:14am    
By avoid eat itself I mean I mean I don't want the head of snake hit the body.
Richard MacCutchan 28-Dec-21 4:30am    
That is the same issue as #2.
Nick_is_asking 28-Dec-21 4:18am    
As about the 2nd problem, I have a lot of cells(1400 columns and 740 rows).How can I keep track of each cell and what is the algorithm to make it work?
Richard MacCutchan 28-Dec-21 4:35am    
The snake consists of a set of segments. Each segment should identify the current cell location and the direction of travel. The snake itself is a collection of segments. You use that information to identify when the snake turns on itself or hits the edge of the screen. I don't know if there is a simple algorithm that will help to simplify it.

1 solution

Quote:
What I've done so far is to make it move itself trying to find and catch the apple, successfully.

This claim says that you already have the solution because after first level, there are walls in middle of play ground. This is unless you forgot to say something.
If your routing algorithm can already avoid hitting walls, including snake body and other bombs is only a little tweak.
Quote:
Problem #1: avoid eat itself
Problem #2: avoid circles

Those are only a different kind of walls.
Quote:
As about the 2nd problem, I have a lot of cells(1400 columns and 740 rows).How can I keep track of each cell and what is the algorithm to make it work?

Just to play manually the game, you already need to keep track of everything on play ground. Without your code, it is complicated for us to guess the reason why you can't, in automated play, use information that you use in another place in your code.
A play ground of 1400*740 is a bad idea, in snake, cells are scaled to screen as little rectangles containing different pictures for each kind of cells.

Automating is done by using a routing algorithm.
If your automated code is just doing the bee line to apple as the program plays, you are wrong.
A routing algorithm is what is used in GPS devices. When you are in A and want to go in B, the GPS prepare the route, if it knows a road is closed, it get around that road.
You code must do the same, every time there is a new goal (apple) or an obstacle is set (new bomb), you must set a new path or check that actual planned path is not disturbed by new event.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900