C
C#4mo ago
vma

automatic restart game

i want my game to auto restart when im game over
No description
7 Replies
CrumpetMan
CrumpetMan4mo ago
$code
MODiX
MODiX4mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
CrumpetMan
CrumpetMan4mo ago
$codegif
vma
vma4mo ago
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 flappy_bird_windows_form
{
public partial class Form1 : Form
{

int pipeSpeed = 8;
int gravity = 10;
int score = 0;



public Form1()
{
InitializeComponent();
}

private void pictureBox4_Click(object sender, EventArgs e)
{

}



private void gameTimerEvent(object sender, EventArgs e)
{
flappyBird.Top += gravity;
pipeBottom.Left -= pipeSpeed;
pipeTop.Left -= pipeSpeed;
scoreText.Text = "score: " + score;

if(pipeBottom.Left < -100)
{
pipeBottom.Left = 500;
score++;
}

if(pipeTop.Left < -130 )
{
pipeTop.Left = 650;
score++;
}
if (flappyBird.Bounds.IntersectsWith(pipeBottom.Bounds) ||
flappyBird.Bounds.IntersectsWith(pipeTop.Bounds) ||
flappyBird.Bounds.IntersectsWith(ground.Bounds) || flappyBird.Top < -25

)
{
endGame();
}

}

private void gamekeyisdown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Space)
{
gravity = -10; }
}

private void gamekeyisup(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Space)
{
gravity = 10;
}

}
private void endGame()
{
gameTimer.Stop();
scoreText.Text += "game over!!!";


}



}
}
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 flappy_bird_windows_form
{
public partial class Form1 : Form
{

int pipeSpeed = 8;
int gravity = 10;
int score = 0;



public Form1()
{
InitializeComponent();
}

private void pictureBox4_Click(object sender, EventArgs e)
{

}



private void gameTimerEvent(object sender, EventArgs e)
{
flappyBird.Top += gravity;
pipeBottom.Left -= pipeSpeed;
pipeTop.Left -= pipeSpeed;
scoreText.Text = "score: " + score;

if(pipeBottom.Left < -100)
{
pipeBottom.Left = 500;
score++;
}

if(pipeTop.Left < -130 )
{
pipeTop.Left = 650;
score++;
}
if (flappyBird.Bounds.IntersectsWith(pipeBottom.Bounds) ||
flappyBird.Bounds.IntersectsWith(pipeTop.Bounds) ||
flappyBird.Bounds.IntersectsWith(ground.Bounds) || flappyBird.Top < -25

)
{
endGame();
}

}

private void gamekeyisdown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Space)
{
gravity = -10; }
}

private void gamekeyisup(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Space)
{
gravity = 10;
}

}
private void endGame()
{
gameTimer.Stop();
scoreText.Text += "game over!!!";


}



}
}
CrumpetMan
CrumpetMan4mo ago
First, know which method handles the game over event:
private void EndGame()
{
gameTimer.Stop();
scoreText.Text += "game over!!!";

Thread.Sleep(2000); // wait for 2 seconds before resetting the game
// reset all game object values to default
}
private void EndGame()
{
gameTimer.Stop();
scoreText.Text += "game over!!!";

Thread.Sleep(2000); // wait for 2 seconds before resetting the game
// reset all game object values to default
}
Then, you need to set components like flappyBird, scoreText, etc... back to their defaults individually
daniel2
daniel24mo ago
U can try close and re-open the form