I am making a PackMan game for my school project. After collecting all coins or running into a ghost or hitting a wall, it is game over, and a message box appears for retry or no. But the thing is, when I crash, the character keeps moving. How can I make so it just disappears or stands in one place? This is the code, which counts if all coins are collected or wall hit.
public partial class Form1 : Form
{
bool goup;
bool godown;
bool goleft;
bool goright;
int speed = 5;
//1 and 2 ghost variables
int ghost1 = 8;
int ghost2 = 8;
//ghost 3 variables
int ghost3x = 8;
int ghost3y = 8;
//score
int score = 0;
public Form1()
{
Thread t = new Thread(new ThreadStart(SplashStart));
t.Start();
Thread.Sleep(5000);
InitializeComponent();
t.Abort();
}
public void SplashStart()
{
Application.Run(new splashscreen());
}
private void keyisdown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left)
{
goleft = true;
packman.Image = Properties.Resources.left;
}
if (e.KeyCode == Keys.Right)
{
goright = true;
packman.Image = Properties.Resources.right;
}
if (e.KeyCode == Keys.Up)
{
goup = true;
packman.Image = Properties.Resources.Up;
}
if (e.KeyCode == Keys.Down)
{
godown = true;
packman.Image = Properties.Resources.down;
}
}
private void keyisup(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left)
{
goleft = false;
}
if (e.KeyCode == Keys.Right)
{
goright = false;
}
if (e.KeyCode == Keys.Up)
{
goup = false;
}
if (e.KeyCode == Keys.Down)
{
godown = false;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
label1.Text = "Score: " + score;
//player movement
if (goleft)
{
packman.Left -= speed;
//moving player to the left
}
if (goright)
{
packman.Left += speed;
//moving player to the right
}
if (goup)
{
packman.Top -= speed;
//moving top
}
if (godown)
{
packman.Top += speed;
//moving down
}
//end
//moving ghost and bumping with the walls
redGhost.Left += ghost1;
yellowGhost.Left += ghost2;
if (redGhost.Bounds.IntersectsWith(pictureBox1.Bounds))
{
ghost1 = -ghost1;
}
else if (redGhost.Bounds.IntersectsWith(pictureBox2.Bounds))
{
ghost1 = -ghost1;
}
if (yellowGhost.Bounds.IntersectsWith(pictureBox4.Bounds))
{
ghost2 = -ghost2;
}
else if (yellowGhost.Bounds.IntersectsWith(pictureBox3.Bounds))
{
ghost2 = -ghost2;
}
//end
//check walls, ghosts and points
foreach (Control x in this.Controls)
{
if (x is PictureBox && x.Tag == "coin")
{
//score adds
if (((PictureBox)x).Bounds.IntersectsWith(packman.Bounds))
{
this.Controls.Remove(x); //removes that point
score++; // adds score
}
}
if (x is PictureBox && x.Tag == "wall" || x.Tag == "ghost")
{
if (((PictureBox)x).Bounds.IntersectsWith(packman.Bounds) || score == 30)
{
//checks of ghost or player hits wall
packman.Left = 0;
packman.Top = 25;
DialogResult res = MessageBox.Show("Retry", "GAME OVER", MessageBoxButtons.YesNoCancel);
if (res == DialogResult.Yes)
{
Application.Restart();
}
else if (res == DialogResult.No)
{
Application.Exit();
}
else
{
}
timer1.Stop();
}
}
}
//end
//ghost 3
pinkGhost.Left += ghost3x;
pinkGhost.Top += ghost3y;
//ghost 3 bumping
if (pinkGhost.Left < 1 || pinkGhost.Left+ pinkGhost.Width > ClientSize.Width -2 ||
(pinkGhost.Bounds.IntersectsWith(pictureBox1.Bounds)) ||
(pinkGhost.Bounds.IntersectsWith(pictureBox2.Bounds)) ||
(pinkGhost.Bounds.IntersectsWith(pictureBox3.Bounds)) ||
(pinkGhost.Bounds.IntersectsWith(pictureBox4.Bounds)))
{
ghost3x = -ghost3x;
}
if (pinkGhost.Top < 1 || pinkGhost.Top + pinkGhost.Height > ClientSize.Height - 2)
{
ghost3y = -ghost3y;
}
//end
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Mainform_FormClosed(object sender, FormClosedEventArgs e)
{
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
//exit application when form is closed
Application.Exit();
}
}
Aucun commentaire:
Enregistrer un commentaire