在线编辑迷你吃豆子游戏的教程。我仍然需要修复一些错误,但无法获取用户输入。我希望在游戏结束后用户按下“ r”键时重新启动。
我曾尝试接受用户输入,但是经常崩溃。因此,我在比赛结束或获胜后自行重新启动。随着游戏结束,它开始一个新窗口。如果我赢了,它会产生多个窗口。我知道这是因为我的foreach循环。
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 WindowsFormsApp1
{
public partial class Form1 : Form
{
char restart;
bool goup;
bool godown;
bool goleft;
bool goright;
int speed = 5;
int ghost1 = 8;
int ghost2 = 8;
int ghost3x = 6;
int ghost3y = 6;
int score = 0;
public Form1()
{
InitializeComponent();
label2.Visible = false;
}
private void keyisdown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left)
{
goleft = true;
pacman.Image = Properties.Resources.Left;
}
if (e.KeyCode == Keys.Right)
{
goright = true;
pacman.Image = Properties.Resources.Right;
}
if (e.KeyCode == Keys.Up)
{
goup = true;
pacman.Image = Properties.Resources.Up;
}
if (e.KeyCode == Keys.Down)
{
godown = true;
pacman.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;
if (goleft)
{
pacman.Left -= speed;
}
if (goright)
{
pacman.Left += speed;
}
if (goup)
{
pacman.Top -= speed;
}
if (godown)
{
pacman.Top += speed;
}
redGhost.Left += ghost1;
yellowGhost.Top += ghost2;
if (redGhost.Width + redGhost.Left < 20 || redGhost.Right > ClientSize.Width - 1)
ghost1 = -ghost1;
if (yellowGhost.Bounds.IntersectsWith(pictureBox1.Bounds))
ghost2 = -ghost2;
else if (yellowGhost.Bounds.IntersectsWith(pictureBox5.Bounds))
ghost2 = -ghost2;
foreach (Control x in this.Controls)
{
if(x is PictureBox && x.Tag == "wall" || x.Tag == "ghost")
{
if(((PictureBox)x).Bounds.IntersectsWith(pacman.Bounds) || score == 30)
{
pacman.Left = 0;
pacman.Top = 25;
label2.Text = "GAMEOVER";
label2.Visible = true;
timer1.Stop();
Application.Restart();
}
}
if (x is PictureBox && x.Tag == "coin")
{
if (((PictureBox)x).Bounds.IntersectsWith(pacman.Bounds))
{
this.Controls.Remove(x);
score++;
}
}
}
pinkGhost.Left += ghost3x;
pinkGhost.Top += ghost3y;
if(pinkGhost.Left < 1 ||
pinkGhost.Left + pinkGhost.Width > ClientSize.Width -2 ||
(pinkGhost.Bounds.IntersectsWith(pictureBox5.Bounds)) ||
(pinkGhost.Bounds.IntersectsWith(pictureBox4.Bounds)) ||
(pinkGhost.Bounds.IntersectsWith(pictureBox3.Bounds)) ||
(pinkGhost.Bounds.IntersectsWith(pictureBox2.Bounds)) ||
(pinkGhost.Bounds.IntersectsWith(pictureBox1.Bounds)))
{
ghost3x = -ghost3x;
}
else if(pinkGhost.Top < 1 || pinkGhost.Top + pinkGhost.Height > ClientSize.Height - 2 ||
pinkGhost.Left + pinkGhost.Width > ClientSize.Width - 2 ||
(pinkGhost.Bounds.IntersectsWith(pictureBox5.Bounds)) ||
(pinkGhost.Bounds.IntersectsWith(pictureBox4.Bounds)) ||
(pinkGhost.Bounds.IntersectsWith(pictureBox3.Bounds)) ||
(pinkGhost.Bounds.IntersectsWith(pictureBox2.Bounds)) ||
(pinkGhost.Bounds.IntersectsWith(pictureBox1.Bounds)))
ghost3y = -ghost3y;
}
}
}
Id希望在游戏结束后一直坐在那里,直到我按下r或关闭窗户 它会自动重新启动,从而产生一个新的屏幕。理想情况下,id只是重新启动游戏。同样,我希望能够根据用户输入而不是在游戏结束后使用它。 如果您赢了,它会产生许多新窗口,因为每个窗口都是理想的,但是再次理想地,id只是希望用户输入“ r”
答案 0 :(得分:0)
您可以生成一个新的消息框,然后等待它们单击确定。
DialogResult d = MessageBox.Show("Do you wanna Retry?", "delete", MessageBoxButtons.YesNo);
if (d == DialogResult.Yes)
{
//do something
}