Jump to content
  • Advertisement
phil67rpg

c# game

Recommended Posts

I am trying to get my snake to follow the mouse pointer. here is my updated code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = this.CreateGraphics();
            SolidBrush greenBrush = new SolidBrush(Color.Green);
            SolidBrush blackBrush = new SolidBrush(Color.Black);
            Rectangle rect_green = new Rectangle(340, 280, 100, 10);
            Rectangle rect_black = new Rectangle(240, 280, 100, 10);
            g.FillRectangle(greenBrush, rect_green);
            g.FillRectangle(blackBrush, rect_black);
            greenBrush.Dispose();
            g.Dispose();
        }

        int x = 0, y = 0;

        private void timer1_Tick(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            SolidBrush greenBrush = new SolidBrush(Color.Green);
            SolidBrush blackBrush = new SolidBrush(Color.Black);
            Rectangle rect_green = new Rectangle(340 + x, 280 + y, 100, 10);
            Rectangle rect_black = new Rectangle(240 + x, 280 + y, 100, 10);
            g.FillRectangle(greenBrush, rect_green);
            g.FillRectangle(blackBrush, rect_black);
            if (x >= 340)
            {
                x = 340;
            }
            x += 5;
            greenBrush.Dispose();
            g.Dispose();

            if (MouseButtons == MouseButtons.Left)
            {
                Graphics g_two = this.CreateGraphics();
                SolidBrush greenBrush_two = new SolidBrush(Color.Green);
                SolidBrush blackBrush_two = new SolidBrush(Color.Black);
                Rectangle rect_green_two = new Rectangle(340 + x, 280 + y, 10, 100);
                Rectangle rect_black_two = new Rectangle(340 + x, 180 + y, 10, 100);
                g_two.FillRectangle(greenBrush_two, rect_green_two);
                g_two.FillRectangle(blackBrush_two, rect_black_two);
                y += 5;
                greenBrush_two.Dispose();
                g_two.Dispose();
            }
        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {

        }
    }
}

 

Share this post


Link to post
Share on other sites
Advertisement
1 hour ago, phil67rpg said:

I am trying to get my snake to follow the mouse pointer. here is my updated code

Ask a question or start a dev blog.

ED: for the umpteenth time...

Edited by fleabay

Share this post


Link to post
Share on other sites

I do not like your code. Let's make some changes.

We can set a game field size like this:

// Set a game field size
ClientSize = new Size(200, 200);

Let's create a method for drawing of a rectangle:

private void DrawRect(int x, int y, Color color, int size = 10)
{
    Graphics g = CreateGraphics();
    SolidBrush brush = new SolidBrush(color);
    g.FillRectangle(brush, new Rectangle(x, y, size, size));
    brush.Dispose();
    g.Dispose();
}

 

Share this post


Link to post
Share on other sites
if (MouseButtons == MouseButtons.Left)

Why do you want to move a snake with a mouse click?! I think it will be a little complicated. Did you hear about KISS principle? "Keep it simple, stupid! Especially when you make your first snake game.

Let's make a very simple classic snake game. For example, if we have a snake head with 10x10 pixels then we will move our snake by a step with 10 pixels using timer.

Each game must have a game loop that will be called by timer. I created the GameLoop method that just prints "Hello, World!" to the debug console every 500 ms:

public Form1()
{
    InitializeComponent();

    // Set a game field size
    ClientSize = new Size(200, 200);

    // Create a timer for the GameLoop method
    var timer = new Timer();
    timer.Tick += GameLoop;
    timer.Interval = 500;
    timer.Start();
}

private void GameLoop(object sender, System.EventArgs e)
{
    System.Console.WriteLine("Hello, World!");
}

 

Edited by 8Observer8

Share this post


Link to post
Share on other sites

For a while our GameLoop will have only two called methods Update() and Draw(). The Update() method will have updates for snake cell coordinates and etc. The Draw() method will have only draw methods for game entities.

For example:

private void GameLoop(object sender, System.EventArgs e)
{
    Update();
    Draw();
}

private void Update()
{
    Console.WriteLine("Game entities coords was updated");
}

private void Draw()
{
    DrawFood();
    DrawSnake();
}

private void DrawSnake()
{
    Console.WriteLine("Snake was drawn");
}

private void DrawFood()
{
    Console.WriteLine("Food was drawn");
}

 

Share this post


Link to post
Share on other sites

List data structure is ideal for keeping snake cells coordinates:

// Snake list of (x, y) positions
private List<Point> _snake = new List<Point>()
{
    new Point(10, 10)
};

Point (10, 10) - it is position of the head.

It is better to move a general Draw() method call to Form1_Paint() and add Invalidate() call to GameLoop:

private void Form1_Paint(object sender, PaintEventArgs e)
{
    Draw();
}

private void GameLoop(object sender, System.EventArgs e)
{
    Update();
    Invalidate();
}

Method for drawing the snake:

private void DrawSnake()
{
    foreach (var cell in _snake)
    {
        DrawRect(cell.X, cell.Y, Color.Green);
    }
}

 

Edited by 8Observer8

Share this post


Link to post
Share on other sites

For moving the snake we need to create the "snakeDir" variable:

// Snake movement direction
private Point _snakeDir = new Point(10, 0);

The snake moving is very simple. Please, read comments:

private void Update()
{
    // Calc a new position of the head
    Point newHeadPosition = new Point(
        _snake[0].X + _snakeDir.X,
        _snake[0].Y + _snakeDir.Y
    );

    // Insert new position in the beginning of the snake list
    _snake.Insert(0, newHeadPosition);

    // Remove the last element
    _snake.RemoveAt(_snake.Count - 1);
}

SnakeWinforms.gif.c648e86021d90f82ac812b89d61d81a7.gif

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!