Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I am doing a mouse race game with winforms and C#. The elements are supposed to move accordingly to their Move function but when they move the last elements stills appear instead of moving to another place or in other words invalidating the last element appeared.

This is my Form:
C#
<pre>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;
using System.Threading;


namespace MyMouseGame
{
    public partial class Form1 : Form
    {
        Graphics g;
        Pen p = new Pen(Color.Black);

        ElementClass[] factory;
        const int NUM = 5;
        private static Random R = new Random();


        public Form1()
        {
            InitializeComponent();
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

            g = panel1.CreateGraphics();
          

        }


        public void threadCounter()
        {
            int counter = 0;
            while (true)
            {
                counter++;
                this.Invoke(new Action(() =>
                {
                    lcountRes.Text = counter.ToString();
                }));
                Thread.Sleep(1000);
            }
        }
        public int generateRandomNum(int start, int end)
        {
            int randomNum = R.Next(start, end);
            return randomNum;
        }
        public ElementClass[] ElementsFactory()
        {
            int i = NUM;
            ElementClass[] shapesArr = new ElementClass[NUM];
            while (i > 0)
            {
                int numOfShape = generateRandomNum(1, 4);
                switch (numOfShape)
                {
                    case 1:
                        shapesArr[i-1] = new E_Random();
                        break;
                    case 2:
                        shapesArr[i-1] = new E_Chase();
                        break;
                    case 3:
                        shapesArr[i-1] = new E_Escape();
                        break;
                    default:
                        break;
                }
                i--;
            }
            return shapesArr;

        }
        private void Form1_Load(object sender, EventArgs e)
        {
            factory = ElementsFactory();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Thread counterThread = new Thread(threadCounter);
            counterThread.Start();
            t.Interval = 100;
            t.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            for (int i = 0; i < NUM; i++)
            {
                factory[i].Draw(g, p);

                factory[i].MoveElement();
            }

            //Refresh();
            this.Invalidate();
        }

    }
}



And another question: I need to move one of the elements far from the Mouse Position.
I attach my code. By now it is going on the mouse direction and not the opposite.
C#
public override void MoveElement() {
           rect.X += move_x;
           rect.Y += move_y;
           double dif_x = MousePosition.X - rect.X;
           double dif_y = MousePosition.Y - rect.Y;

           move_x = (int)(dif_x / 20);
           move_y = (int)(dif_y / 20);
       }

Thanks a lot.

What I have tried:

I've tried Invalidate() and this.Invalidate() but doensnt work. Also tried Refresh() but it is not what I need. Maybe I should put them in a different place?
Posted
Updated 27-Apr-21 9:31am

1 solution

You don't do anything useful in your Paint handler, so there is no way to say "it doesn't work" because you can't see anything happen.

Instead, what you do do, is cause your app to crash, and fairly rapidly.

If you create a Graphics context, it is your responsibility to Dispose it when you have finished with it. If you don't, your app will get an "out of memory" problem not because the heap is exhausted, but because graphics handles are a very scarce commodity in Windows which you are using up at a dramatic rate.

In fact, you shouldn't be creating a Graphics context at all in a Paint event, since one is provided for you in the PaintEventArgs.Graphics property ...
 
Share this answer
 
Comments
Michal 2021 27-Apr-21 16:10pm    
Don't be angry, it is my first time with winforms and C# and I didnt really get where should i do each thing..
Instead I would appreciate your help on how to correct it.
You can see what it is drawn in the timer_Tick.
Thank you.
OriginalGriff 27-Apr-21 16:15pm    
I'm not angry, and I didn't mean to give you the impression I was.

It's obvious that you are a beginner, trust me! :laugh:

And no, I can't "see what happens" - all I can see is you call a random method called Draw that takes two parameters; I have no idea what it is doing, or how it does it.

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