7 Replies
$code
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/$codegif
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!!!";
}
}
}
First, know which method handles the game over event:
Then, you need to set components like
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
}
flappyBird
, scoreText
, etc... back to their defaults individuallyU can try close and re-open the form