Quantcast
Channel: Visual C# forum
Viewing all articles
Browse latest Browse all 31927

Problem with the creation of a Snake-game

$
0
0

I'd like to please ask if it's possible to solve this without changing completely what i've done so far, so please try to stick with this idea of the code, and if possible, to solve it without having to change much

So here's SnakeClass Fields:

public class Snake { #region Fields public static Rectangle[] SnakeRec = new Rectangle[1000];

//those two int fields will change the snakeRec[].X and Y position

public static int moveLeftorRight = 10; public static int moveUporDown = 10; static Graphics Draw;

// i'll be using those bool to check if snake is moving

//left - right - up - down public static bool Lactive; public static bool Ractive; public static bool Uactive; public static bool Dactive; #endregion


then a method which initialize the first snake rectangle :

public static void initializeSnake()
            {
                SnakeRec[0] = new Rectangle(100, 100, 10, 10);
            }


Now the Move Method, that loops through a timer_tick, pratically I set few key_downs event that will enable/disable the bools every time w/a/s/d is pressed

   public static void Move()
            {
                for (int i = 0; i < SnakeRec.Length; i++)     
                {
                    if (SnakeRec[i].Width == 0)     // Prevents looping in all of the 1000 rectangles            
                        break;

                    if      (Lactive == true)
                    { SnakeRec[i].X -= moveLeftorRight; }   // moves left
                    else if (Ractive == true)
                    { SnakeRec[i].X += moveLeftorRight; }   // moves right
                    else if (Uactive == true)
                    { SnakeRec[i].Y -= moveUporDown; }      // up
                    else if (Dactive == true)
                    { SnakeRec[i].Y += moveUporDown; }      // down

                }

The Grow() Method, which is supposed to be handled by the key R.  in other words, every time I press "R" the snake will grow just as if it has eaten a rectangle

  public static void Grows()
            {
                for (int i = 1; i < SnakeRec.Length; i++)     
                {
                    if (SnakeRec[i].Width == 0)                
                    {
                        SnakeRec[i].Width = SnakeRec[i - 1].Width;    
                        SnakeRec[i].Height = SnakeRec[i - 1].Height;

                        SnakeRec[i].X = SnakeRec[i - 1].X;            
                        SnakeRec[i].Y = SnakeRec[i - 1].Y;            
                        break;
                    }

                }
            }

I'm not placing the methods that clear the draw.graphics to re-draw it again with the new changed r[].x & r[].y values because i'm pretty sure they work well

To sum it : I've created a button that once pressed initialize the snake and set the Lactive bool to true, it also start a timer1, once started, the timer1 tick event is here shown :

private void timer1_Tick(object sender, EventArgs e) { timer1.Interval = 300; Snake.Move(); Snake.DrawSnake(Picturebox1); timer2.Start(); //i'm using this timer2 to clear whatever is drawn on the timer2.Interval = 290; //picturebox so that 10 milliseconds later (300 - 290)

//It will be re-drawn by this tick_event Snake.Rebounce(); // Method that re-draw the snake on the other side of the picturebox if it reach the bounds }


The problem here is that the grow method sets the x & y components of snakeRec[1] (the "new" rectangle that the snake acquires) to the components of r[0]   And they get printed/drawn togheter in the same place !! (and obviously, "move" togheter)

I hope that I gave somehow the idea behind the problem in the code. Please try to stick with it without changing much and avoid being harsh/angry because my code is completely wrong or for any other reason, i'm here to learn and correct my errors



Viewing all articles
Browse latest Browse all 31927

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>