using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Drawing;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using OpenTK.Graphics;

namespace Snake
    {
    public partial class GameWindow : Form
    {
    private int glcontrolFieldWidth = 20, glcontrolFieldHeight = 20; //Playing Field 
    public int speedincr = 0; //Speed, Increase by 5% Check out GenerateFood()
    public int GamePoints = -10; //Score 
    private int shaderProgram, color_attribute; //Shader Program
    private int view, model, projection;//Shader Program
    private Matrix4 ViewMatrix, ModelMatrix, ProjectionMatrix; // Matrix4
    private List<Point> snake = new List<Point>() { new Point(1, 1) }; //Where the Snake Start off
    private Point snakeDirection = new Point(1, 0);//Snake Direction
    private Point mouse = new Point(); //Food
    private Random random = new Random(); //Random Genereator 
    public bool bGameOver;
    private static readonly Timer timer = new Timer();

    public GameWindow()
    {
        //Form1.Designer
        InitializeComponent();

        // Centers the form on the current screen
        CenterToScreen();

        // Create a timer 
        //var timer = new Timer();
        timer.Tick += new EventHandler(GameLoop);
        timer.Interval = 150; // This is the snake inital speed
        timer.Start();

        // Generate an initial random position for the food
        GenerateFood();
    }

    public void GameOver(bool bGameOver)
    {
        //Snake Collision with its own body
        //for (int i = 0; i < snake.Count; i++)
        //{
        //    //if Snake equal itself then snake hasn't touch any other parts of its body
        //    if (snake[0].X == snake[i].X || snake[0].Y == snake[i].Y)
        //    {
        //        this.bGameOver = false;
        //        //Console.WriteLine("Snake " + snake2[i].X + "," + snake2[i].Y);
        //        //Console.WriteLine("Snake2 " + snake[i].X + "," + snake[i].Y);
        //    }
        //    else
        //    {
        //        this.bGameOver = true;
        //        //Console.WriteLine(snake[i].X+","+snake[i].Y);
        //    }
        //}



        ////---Display GameOver Message If Where Dead-- - //

        //if (bGameOver == true)
        //{
        //    Console.WriteLine("Game Over! Push Enter to Continue");
        //    if (bGameOver == false)
        //    {
        //        //Game Start Over

        //    }
        //}
        return;
    } //GameOver Reset Game

    public void GameLoop(object sender, System.EventArgs e)
    {
        // Update coordinates of game entities and check collisions
        Update();
        // UpdateWall check collisions with snake to bounce off wall
        UpdateWall();
        GameOver(bGameOver);
        glControl.Invalidate();
    } //Basic of the Game Loop

    private void UpdateWall()
    {
        // Snake collison with Walls
        if (snake[0].X < 1)
        {
            //snake[0].X = (glcontrolFieldWidth - 2) / 10; //Warp to right
            //snakeDirection.X = (snake[0].X + glcontrolFieldWidth-2)/10;
            //snakeDirection.X = (glcontrolFieldWidth-2)/10;
            snakeDirection.X = (int)(glcontrolFieldWidth-0.5) / 10;
            snakeDirection.Y = (int)(glcontrolFieldWidth-0.5) / 10;
            Console.WriteLine("Snake hit left wall");
        }
        else if (snake[0].X > glcontrolFieldWidth - 2)
        {
            //snakeDirection.X = 0; //Warp to left
            snakeDirection.X = (int)(glcontrolFieldWidth - 0.5) / -10;
            snakeDirection.Y = (int)(glcontrolFieldWidth - 0.5) / -10;
            Console.WriteLine("Snake hit right wall");
        }

        if (snake[0].Y < 1)
        {
            //snake.getFirst().y = windowHeight / 10; //Warp to bottom
            //snakeDirection.Y = (snake[0].Y + glcontrolFieldWidth - 2)/10;
            snakeDirection.X = (int)(glcontrolFieldHeight - 0.5)/10;
            snakeDirection.Y = (int)(glcontrolFieldHeight - 0.5)/10;
            Console.WriteLine("Snake hit Top wall");
        }
        else if (snake[0].Y > glcontrolFieldHeight - 2)
        {
            //snake.getFirst().y = 0; //Warp to top
            snakeDirection.X = (int)(glcontrolFieldHeight - 0.5) / -10;
            snakeDirection.Y = (int)(glcontrolFieldHeight - 0.5) / -10;
            Console.WriteLine("Snake hit Bottom wall");
        }

    } //Wall Collision

    private new void Update()
    {
        // Calculate a new position of the head of the snake
        Point newHeadPosition = new Point(snake[0].X + snakeDirection.X, snake[0].Y + snakeDirection.Y);
        // Insert new position in the beginning of the snake list
        snake.Insert(0, newHeadPosition);
        snake.RemoveAt(snake.Count - 1);

        // Check snake collision with the food
        if (snake[0].X != mouse.X || snake[0].Y != mouse.Y)
        {
            return;
        }
        else
        {
            //Snake Grow
            snake.Add(new Point(mouse.X, mouse.Y));
        }
        Console.WriteLine();

        //foreach (Point aPart in snake)
        //{
        //    Console.WriteLine(aPart);
        //}

        // Generate a new food(mouse) position
        GenerateFood();

    }//...more code.

Le reste du code dans GitHub @mrcoffeecode Snake Game C#. J'arrive à créer le mur, la nourriture et le serpent. Aussi la détection avec de la nourriture et des murs. Maintenant, je suis coincé à essayer de comprendre Game Over, essentiellement le serpent détecte qu'il se fait mordre. Le code est prêt à fonctionner sans aucune erreur, vous avez juste besoin d'opentk, opengl, openglcontrol.dll.

1
Mr. Coffee Code 19 févr. 2020 à 07:39

1 réponse

Meilleure réponse

Il suffit d'évaluer si la tête du serpent frappe son corps. Testez si snake[0] atteint un élément de 1 à snake.Count-1. Vous devez vérifier si la composante x et (&&) la composante y sont égales :

//Snake Collision with its own body
for (int i = 1; i < snake.Count; i++)
{
    //if Snake equal itself then snake hasn't touch any other parts of its body
    if (snake[0].X == snake[i].X && snake[0].Y == snake[i].Y)
    {
        this.bGameOver = true;
        break;
    }
}

Notez que dans votre code à chaque fois que le serpent grandit, une collision de la tête avec le corps sera détectée, car vous insérez un nouvel élément à la position had.
Vous pouvez vous débarrasser de set, si vous évitez simplement de supprimer l'élément tail du serpent à la place :

private new void Update()
{
    // Calculate a new position of the head of the snake
    Point newHeadPosition = new Point(snake[0].X + snakeDirection.X, snake[0].Y + snakeDirection.Y);
    // Insert new position in the beginning of the snake list
    snake.Insert(0, newHeadPosition);
    # snake.RemoveAt(snake.Count - 1); <--------- DELETE    

    // Check snake collision with the food
    if (snake[0].X != mouse.X || snake[0].Y != mouse.Y)
    {
        # remove tail if snake does not grow
        snake.RemoveAt(snake.Count - 1); # <---------- ADD
        return;
    }
    else
    {
        //Snake Grow
        // snake.Add(new Point(mouse.X, mouse.Y)); <------ DELETE
    }
    Console.WriteLine();

    // Generate a new food(mouse) position
    GenerateFood();

} //Events 
0
Rabbid76 19 févr. 2020 à 08:48