Question

I am sorry for kind of often questions about this game but I don't know where and how to implement gameloop in my game. I would like the gameloop to work like that:

Settings();
if (startClicked == true)
{
    Spawn();
}
while (enemyKilled == true)
{
    Spawn();
}

Here's my code:

public partial class Form1 : Form
{
    public int score1 = 0;
    public PictureBox enemy = new PictureBox();
    public PictureBox missile = new PictureBox();
    public int enemyX;
    public int enemyY;
    public int missileX;
    public int missileY;
    public bool enemyKilled;
    public bool startClicked;
    public Form1()
    {
        InitializeComponent();
        logo.Visible = false;
        startbutton.Visible = false;
        spaceship.Visible = false;
        score.Visible = false;
        labelEnemyX.Visible = false;
        labelEnemyY.Visible = false;
        labelMissileX.Visible = false;
        labelMissileY.Visible = false;

    }

    private void Settings()
    {
        logo.Visible = true;
        startbutton.Visible = true;
        score.Text = Convert.ToString(score1);
    }

    private void Spawn()
    {
        enemy.Visible = true;
        missile.Visible = true;
        System.Timers.Timer enemyMove = new System.Timers.Timer();
        enemyMove.Interval = 100;
        Random enemyPosition = new Random();
        int enemyX = enemyPosition.Next(30, 400);
        int enemyY = enemy.Location.Y;
        enemy.Location = new Point(enemyX, 0);
        enemy.Image = WindowsFormsApplication16.Properties.Resources.Enemy2;
        enemy.Width = 36;
        enemy.Height = 29;
        this.Controls.Add(enemy);
        enemyMove.Elapsed += (sender, args) =>
            {
                enemy.Location = new Point(enemyX, enemy.Location.Y + 2);
                enemyX = enemy.Location.X;
                enemyY = enemy.Location.Y;
                labelEnemyX.Text = Convert.ToString(enemyX);
                labelEnemyY.Text = Convert.ToString(enemyY);
                if (enemyY <= -10)
                {
                    enemyKilled = false;
                }
            }; enemyMove.Start();
    }

    private void AddScore()
    {
        if (enemyKilled == true)
        {
            score1++;
            score.Text = Convert.ToString(score1);
        }
    }

    private void MissileMove()
    {
        System.Timers.Timer missileMove = new System.Timers.Timer();
        missileMove.Interval = 30;
        missileMove.Elapsed += (sender, args) =>
            {
                if (missile.Location.Y >= -30)
                {
                    missile.Location = new Point(missile.Location.X, missile.Location.Y - 15);
                    missileX = missile.Location.X;
                    missileY = missile.Location.Y;
                    labelMissileX.Text = Convert.ToString(missileX);
                    labelMissileY.Text = Convert.ToString(missileY);
                    int enemyToMissileLocationX = Convert.ToInt32(labelEnemyX.Text);
                    int enemyToMissileLocationY = Convert.ToInt32(labelEnemyY.Text);
                    if ((missileX - enemyToMissileLocationX <= 20 && missileY - enemyToMissileLocationY <= 20 && missileX - enemyToMissileLocationX >= 0 && missileY - enemyToMissileLocationY >= 0) || (enemyToMissileLocationX - missileX <= 20 && enemyToMissileLocationY - missileY <= 20 && enemyToMissileLocationX - missileX >= 0 && enemyToMissileLocationY - missileY >= 0))
                    {
                        missile.Visible = false;
                        enemy.Visible = false;
                        enemyKilled = true;
                    }
                }
            }; missileMove.Start();

    }

    void Form1_Load(object sender, EventArgs e)
    {
        Settings();
        if (startClicked == true)
        {
            Spawn();
        }
        while (enemyKilled == true)
        {
            Spawn();
        }
    }

    private void startbutton_Click(object sender, EventArgs e)
    {
        this.startClicked = true;
        logo.Visible = false;
        startbutton.Visible = false;
        spaceship.Visible = true;
        score.Visible = true;
        startbutton.Enabled = false;

    }

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Right)
        {
            if (spaceship.Location.X <= 370)
            {
                spaceship.Location = new Point(spaceship.Location.X + 10, spaceship.Location.Y);
            }
        }
        if (e.KeyCode == Keys.Left)
        {
            if (spaceship.Location.X >= 0)
            {
                spaceship.Location = new Point(spaceship.Location.X - 10, spaceship.Location.Y);
            }
        }
        if (e.KeyCode == Keys.Space)
        {
            missile.Image = WindowsFormsApplication16.Properties.Resources.Missile;
            missile.Height = 42;
            missile.Width = 12;
            missile.Location = new Point(spaceship.Location.X + 28, spaceship.Location.Y + 15);
            this.Controls.Add(missile);
            MissileMove();

        }
    }
}

Update:

    private void MissileMove()
    {
        System.Timers.Timer missileMove = new System.Timers.Timer();
        missileMove.Interval = 30;
        missileMove.Elapsed += (sender, args) =>
            {
                if (missile.Location.Y >= -30)
                {
                    missile.Location = new Point(missile.Location.X, missile.Location.Y - 15);
                    missileX = missile.Location.X;
                    missileY = missile.Location.Y;
                    labelMissileX.Text = Convert.ToString(missileX);
                    labelMissileY.Text = Convert.ToString(missileY);
                    int enemyToMissileLocationX = Convert.ToInt32(labelEnemyX.Text);
                    int enemyToMissileLocationY = Convert.ToInt32(labelEnemyY.Text);
                    if ((missileX - enemyToMissileLocationX <= 20 && missileY - enemyToMissileLocationY <= 20 && missileX - enemyToMissileLocationX >= 0 && missileY - enemyToMissileLocationY >= 0) || (enemyToMissileLocationX - missileX <= 20 && enemyToMissileLocationY - missileY <= 20 && enemyToMissileLocationX - missileX >= 0 && enemyToMissileLocationY - missileY >= 0))
                    {
                        missile.Visible = false;
                        enemy.Visible = false;
                        enemyKilled = true;
                        System.Threading.Thread.Sleep(100);
                        Application.DoEvents();
                        Spawn();
                    }
                }
            }; missileMove.Start();

Tried to do this with the class and yes, it spawn enemies once more but the animation is bugged and I can see enemies jumping from one position to another position. Thank you for help.

Was it helpful?

Solution 2

This looks like a Windows form. As such, I would suggest that you have a background thread that runs in a while loop and handles all of the movement/update logic of your game. The complex part will be managing state between the Windows form thread, and the game thread. You will need to ensure you're event handlers for button clicks etc. that update game state are doing so in a thread safe manner, as they will be on the GUI thread. The game loop thread will need to update state on the GUI, which will require what is known as marshelling, because only the GUI thread is allowed to access controls that exist on the form like buttons, etc.

The topics of thread management, thread safety, and marshelling I'll leave to you to research.

This assumes it's some sort of real time game, rather than turn based. If it is turn based, then you don't really need a game loop. You could accomplish the same goal by using a state diagram and purely event driven design.

OTHER TIPS

My suggestion would be to implement the game loop as you have in a function like Form_Load (or the main function if you are familiar with integrating that into a Windows Forms application), but add calls to Application.DoEvents inside the loop to make sure the window is still responsive to events like moving and sizing and button clicking. Other than that, you might have to clarify what problems you are experiencing with your current solution in order to get a more complete and detailed answer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top