For this c# exercise, we have to do an object intercepting a target. The problem is that after a certain point I suffer from extreme lag, I am almost certain that I am refreshing (Refresh()) in all the wrong places. I tried putting more in and taking almost all of them away and so on and so forth and am at my wits end. Can someone point me in the right direction? (Note: the shoot command has not been properly coded, just rying to get the thing to run at a good speed first, apoligies if my code is messy)

using System.Linq;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;
using System.Drawing;
using System;


class GFrame3 : Form
{
    protected SolidBrush brush;
    protected SolidBrush brush2;
    protected SolidBrush brush3;
    protected int x = 120, y = 180;
    protected int x2 = 60, y2 = 180;

    public GFrame3()
    {
        brush = new SolidBrush(Color.Blue);
        brush2 = new SolidBrush(Color.Red);
        brush3 = new SolidBrush(Color.Green);
    }

    protected override void OnPaint(PaintEventArgs pe)
    {
        base.OnPaint(pe);
        Graphics g = pe.Graphics;
        g.FillRectangle(brush, x, y, 30, 30);
        g.FillRectangle(brush2, x2, 10, 30, 30);        
    }
}


class GFrame4 : GFrame3
{
    protected Button b1 = new Button();
    protected Button b2 = new Button();
    protected Button b3 = new Button();

    private int stepx = 15;
    private int stepy = 30;
    private int stepxr = 15;
    char sym = '_';
    private bool shot = false;


    public GFrame4()
        : base()
    {
        b1.Text = "Shoot";
        b1.SetBounds(90, 220, 90, 23);
        b1.Click += new EventHandler(this.button1_Click);
        Controls.Add(b1);

        b2.Text = "<--";
        b2.SetBounds(30, 220, 90, 23);
        b2.Click += new EventHandler(this.button2_Click);
        Controls.Add(b2);

        b3.Text = "-->";
        b3.SetBounds(150, 220, 90, 23);
        b3.Click += new EventHandler(this.button3_Click);
        Controls.Add(b3);
    }
    protected override void OnPaint(PaintEventArgs pe)
    {
        base.OnPaint(pe);
        Graphics g = pe.Graphics;
        DoubleBuffered = true;
      
        target();

        if (shot == true)
        {
            g.FillRectangle(brush3, x + 5, y2, 10, 10);
            while (y2 >= 0)
            {

                y2 -= stepy;
                shot = false;
            }
        }

        Thread.Sleep(1000);
        this.Refresh();
    }

    private void target()
    {
        x2 += stepxr;
        if (x2 >= 230)
        {
            stepxr *= -1;
        }
        if (x2 < 30)
        {
            stepxr *= -1;
        }
        this.Refresh();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        shot = true;
    }

    private void button2_Click(object sender, EventArgs e)
    {
        sym = '-';
        if (sym == '-')
        {
            x -= stepx;
            sym = '_';
        }
    }

    private void button3_Click(object sender, EventArgs e)
    {
        sym = '+';
        if (sym == '+')
        {
            x += stepx;
            sym = '_';
        }
    }
}

public class Test92
{
    public static void Main(string[] args)
    {
        Application.Run(new GFrame4());
    }
}

Recommended Answers

All 2 Replies

On line 84: Thread.Sleep(1000) will likely be causing the lag. Every time you paint the form you pause the thread for a whole second. What is the reason behind this? Also, you shouldn't need a refresh() in the target() method.

Edit: Nevermind your right, I kept the thread.sleep in because I used it previously in a similar assignment and am not confident in my c# skills. Thank you very much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.