Snake Array Assignment

When working in a business environment, I really can’t get the code or use the old console. My work is repeated and therefore not very complicated.

I decided to challenge myself by writing a snake game on the C # console; and the boy did it so that my brain would work. I never had to think about it so much day by day, but I felt that my programming skills were not improving.

I have a problem. The main approach I took is to create a snake class and a food class. The snake class uses an array to store all coordinates, and then the drawing class decides which coordinates to draw on the screen.

The problem is that when moving the snake, the array is filled (maxsize is 250 for performance), so when I get to the end of the array, I want to copy the last few coordinates to the temp array, reset the original array and copy the time coordinates back to the main array.

The problem I have is copying the x coordinates back to the original array. I decided to do it manually to check, but this decision always makes my poor snake leave behind one of its segments on the screen when it should not be.

How will I do this programmatically?

spoints[4, 0] = stemp[249, 0]; spoints[4, 1] = stemp[249, 1]; spoints[4, 2] = stemp[249, 2]; spoints[3, 0] = stemp[248, 0]; spoints[3, 1] = stemp[248, 1]; spoints[3, 2] = stemp[248, 2]; spoints[2, 0] = stemp[247, 0]; spoints[2, 1] = stemp[247, 1]; spoints[2, 2] = stemp[247, 2]; spoints[1, 0] = stemp[246, 0]; spoints[1, 1] = stemp[246, 1]; spoints[1, 2] = stemp[246, 2]; spoints[0, 0] = stemp[245, 0]; spoints[0, 1] = stemp[245, 1]; spoints[0, 2] = stemp[245, 2]; 

I really don't mind posting the whole game here if someone really wants to delve into the code.

+6
source share
3 answers

Consider using an array for "common points" rather than drawing points, create another class called SnakePart , so your Snake class has manageable parts.

You can also create your own array class, so in each part of the grid you should only have a hasSnakePart value and manage it.

+1
source

The array seems to be the wrong data type for your purposes. Use List instead, as it provides more flexibility when moving records around.

+3
source

What were you thinking about, adding a new coordinate to the "front" of the array, deleting the one that is on the "back" when you go. So basically a kind of collection of FIFO types ?! You can use Queue in C #. Take a look at this article and see if it helps anyone ?! http://www.dotnetperls.com/queue .

Cheers
Chris.

+1
source

Source: https://habr.com/ru/post/906922/


All Articles