2016-05-04 10 views
0

Ich versuche, eine Schaltfläche zum erneuten Abspielen für mein Spiel in einer separaten Windows-Form zu machen, aber ich bin mir nicht sicher, wie ich das neue Formular aufrufen soll. Kann mir jemand in die richtige Richtung zeigen?Wie man einen Spielknopf wieder herstellt?

Hier ist mein Form1 Code:

namespace BrickO1 
{ 
public partial class Form1 : Form 
{ 
    private const int kNumberOfTries = 3; 

    public int rounds; 
    private Ball TheBall = new Ball(); 
    private Paddle ThePaddle = new Paddle(); 
    private Paddle TheOtherPaddle = new Paddle(); 
    private PlayAgain playagain = new PlayAgain(); 

    // private System.Windows.Forms.Timer timer1; 

    private Score TheScore = null; 
    private Score TheOtherScore = null; 
    private Thread oThread = null; //thread is used to run sounds independently 

    [DllImport("winmm.dll")] 
    public static extern long PlaySound(String lpszName, long hModule, long dwFlags); 
    //method PlaySound must be imported from the .dll file winmm 



    public Form1() 
    { 
     InitializeComponent(); 

     ThePaddle.Position.X = 5; 
     ThePaddle.Position.Y = this.ClientRectangle.Bottom - ThePaddle.Height; 

     TheOtherPaddle.Position.X = 5; 
     TheOtherPaddle.Position.Y = 0; 
     // this.ClientRectangle refers to the current container (the instance of Form1) 

     TheBall.Position.Y = this.ClientRectangle.Bottom - 200; 

     TheScore = new Score(ClientRectangle.Right - 30, ClientRectangle.Bottom - 40); 
     TheOtherScore = new Score(ClientRectangle.Right - 30, ClientRectangle.Bottom - 370); 
     //positions the score - 0 at this moment 

     // choose Level 
     SpeedDialog dlg = new SpeedDialog(); 
     /* makes sure that, if the DialogResult property of the button "OK" is on, 
     the SpeedDialog form appears and stays on the screen, and timer's Interval 
     gets an appropriate value */ 
     if (dlg.ShowDialog() == DialogResult.OK)            
     { 
      timer1.Interval = dlg.Speed; 
      rounds = dlg.SelectedLength; 
     } 
    } 
     private string m_strCurrentSoundFile = "BallOut.wav"; //sound file is initialized 

    public void PlayASound() //method to play a sound; to be called by a thread 
    { 
     if (m_strCurrentSoundFile.Length > 0) 
     { 
      PlaySound(Application.StartupPath + "\\" + m_strCurrentSoundFile, 0, 0); 
      /* the above gives full path to the location of the sound file from the startup path 
       of the executable file: Application.StartupPath */ 
     } 
     m_strCurrentSoundFile = ""; 
     oThread.Abort(); //aborts the tread playing sound 
    } 

    public void PlaySoundInThread(string wavefile) //creates and starts a new thread to play a sound 
    { 
     m_strCurrentSoundFile = wavefile; 
     oThread = new Thread(new ThreadStart(PlayASound)); //calls the method PlayASound 
     oThread.Start(); 
    } 


    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) //method to draw Form1 
    { 
     Graphics g = e.Graphics; 
     g.FillRectangle(Brushes.White, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height); 
     TheScore.Draw(g); 
     TheOtherScore.Draw(g); 
     ThePaddle.Draw(g); 
     TheOtherPaddle.Draw(g); 
     TheBall.Draw(g); 
    } 
    private void CheckForCollision() 
    { 
     if (TheBall.Position.X < 0) // hit the left side, switch polarity 
     { 
      TheBall.XStep *= -1; 
      TheBall.Position.X += TheBall.XStep; 
      PlaySoundInThread("WallHit.wav"); 
     } 

     if (TheBall.Position.Y < 0) // hit the top of the form 
     { 
      //Lost the ball for 2nd paddle 
      TheScore.Increment(); 
      if(TheScore.Count == rounds) 
      { 
       MessageBox.Show("Congrats! Player 1 wins!"); 
       playagain.Show(); 
       //Application.Exit(); 
      } 
      Reset(); 
      PlaySoundInThread("BallOut.wav"); 
     } 


     if (TheBall.Position.X > this.ClientRectangle.Right - TheBall.Width) // hit the right side, switch polarity 
     { 
      TheBall.XStep *= -1; 
      TheBall.Position.X += TheBall.XStep; 
      PlaySoundInThread("WallHit.wav"); 
     } 

     if (TheBall.Position.Y > this.ClientRectangle.Bottom - TheBall.YStep) // lost the ball! 
     { 
      TheOtherScore.Increment(); 
      if (TheOtherScore.Count == rounds) 
      { 
       MessageBox.Show("Congrats! Player 2 wins!"); 
       playagain.Show(); 
      } 
      Reset(); 
      PlaySoundInThread("BallOut.wav"); 
     } 

     int hp = HitsPaddle(TheBall.Position); //check if the ball hit the paddle 
     if (hp > -1)// checks if the ball is not lost 
     { 
      PlaySoundInThread("PaddleHit.wav"); 
      switch (hp) //new direction of the ball depends on which quarter of the paddle is hit 
      { 
       case 1: 
        TheBall.XStep = -7; 
        TheBall.YStep = -3; 
        break; 
       case 2: 
        TheBall.XStep = -5; 
        TheBall.YStep = -5; 
        break; 
       case 3: 
        TheBall.XStep = 5; 
        TheBall.YStep = -5; 
        break; 
       default: 
        TheBall.XStep = 7; 
        TheBall.YStep = -3; 
        break; 

      } 
     } 

     int hp2 = HitsPaddle2(TheBall.Position); //check if the ball hit the paddle 
     if (hp2 > -1)// checks if the ball is not lost 
     { 
      PlaySoundInThread("PaddleHit.wav"); 
      switch (hp2) //new direction of the ball depends on which quarter of the paddle is hit 
      { 
       case 1: 
        TheBall.XStep = -7; 
        TheBall.YStep = 3; 
        break; 
       case 2: 
        TheBall.XStep = -5; 
        TheBall.YStep = 5; 
        break; 
       case 3: 
        TheBall.XStep = 5; 
        TheBall.YStep = 5; 
        break; 
       default: 
        TheBall.XStep = 7; 
        TheBall.YStep = 3; 
        break; 

      } 
     } 
    } 


    private int HitsPaddle(Point p) 
    { 
     Rectangle PaddleRect = ThePaddle.GetBounds(); //current position of the paddle 

     if (p.Y >= this.ClientRectangle.Bottom - (PaddleRect.Height + TheBall.Height))//If the ball has hit the paddle according to its y value 
     { 
      if ((p.X > PaddleRect.Left) && (p.X < PaddleRect.Right)) //ball hits the paddle (horizontally)! 
      { 
       if ((p.X > PaddleRect.Left) && (p.X <= PaddleRect.Left + PaddleRect.Width/4)) 
        return 1; //hits leftmost quarter of the paddle 
       else if ((p.X > PaddleRect.Left + PaddleRect.Width/4) && (p.X <= PaddleRect.Left + PaddleRect.Width/2)) 
        return 2; //hits the second quarter of the paddle 
       else if ((p.X > PaddleRect.Left + PaddleRect.Width/2) && (p.X <= PaddleRect.Right - PaddleRect.Width/2)) 
        return 3; //hits the third quarter of the paddle 
       else 
        return 4; //hits the rightmost quarter of the paddle 
      } 
     } 
     return -1; 
    } 

    private int HitsPaddle2(Point q) 
    { 
     Rectangle Paddle2Rect = TheOtherPaddle.GetBounds(); //current position of the paddle 

     if (q.Y <= this.ClientRectangle.Top + Paddle2Rect.Height) 
     { 
      if ((q.X > Paddle2Rect.Left) && (q.X < Paddle2Rect.Right)) //ball hits the paddle! 
      { 
       if ((q.X > Paddle2Rect.Left) && (q.X <= Paddle2Rect.Left + Paddle2Rect.Width/4)) 
        return 1; //hits leftmost quarter of the paddle 
       else if ((q.X > Paddle2Rect.Left + Paddle2Rect.Width/4) && (q.X <= Paddle2Rect.Left + Paddle2Rect.Width/2)) 
        return 2; //hits the second quarter of the paddle 
       else if ((q.X > Paddle2Rect.Left + Paddle2Rect.Width/2) && (q.X <= Paddle2Rect.Right - Paddle2Rect.Width/2)) 
        return 3; //hits the third quarter of the paddle 
       else 
        return 4; //hits the rightmost quarter of the paddle 
      } 
     } 
     return -1; 
    } 

    private void Reset() //resets the ball, stops timer, and redraws the main form 
    { 
     TheBall.XStep = 5; 
     TheBall.YStep = 5; 
     TheBall.Position.Y = this.ClientRectangle.Bottom - 190; 
     TheBall.Position.X = 5; 
     timer1.Stop(); 
     TheBall.UpdateBounds(); 
     Invalidate(TheBall.GetBounds()); 
    } 

    private void timer1_Tick(object sender, System.EventArgs e) //runs one round of the game, when started 
    { 
     TheBall.UpdateBounds(); //gets the ball position 
     Invalidate(TheBall.GetBounds()); //redraws the ball 
     TheBall.Move(); //moves the ball 
     TheBall.UpdateBounds(); //updates position of the ball 
     Invalidate(TheBall.GetBounds()); //redraws the boll 
     CheckForCollision(); //checks for collision 
     Invalidate(TheScore.GetFrame()); //redraws the score 
     Invalidate(TheOtherScore.GetFrame()); 
    } 
    private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) 
    { 
     string result = e.KeyData.ToString(); 
     Invalidate(ThePaddle.GetBounds()); 
     switch (result) 
     { 
      case "Left": 
       ThePaddle.MoveLeft(); 
       Invalidate(ThePaddle.GetBounds()); 
       if (timer1.Enabled == false) //starts the game if it does not run yet 
        timer1.Start(); 
       break; 
      case "Right": 
       ThePaddle.MoveRight(ClientRectangle.Right); 
       Invalidate(ThePaddle.GetBounds()); 
       if (timer1.Enabled == false) //starts the game if it does not run yet 
        timer1.Start(); 
       break; 
      case "Z": 
       TheOtherPaddle.MoveLeft(); 
       Invalidate(TheOtherPaddle.GetBounds()); 
       if (timer1.Enabled == false) //starts the game if it does not run yet 
        timer1.Start(); 
       break; 
      case "C": 
       TheOtherPaddle.MoveRight(ClientRectangle.Right); 
       Invalidate(TheOtherPaddle.GetBounds()); 
       if (timer1.Enabled == false) //starts the game if it does not run yet 
        timer1.Start(); 
       break; 
      default: 
       break; 

     } 

    } 
    public void init() 
    { 
     TheBall.XStep = 5; 
     TheBall.YStep = 5; 
     TheBall.Position.Y = this.ClientRectangle.Bottom - 190; 
     TheBall.Position.X = 5; 
     TheBall.UpdateBounds(); 
     Invalidate(TheBall.GetBounds()); 
     TheScore.Reset(); 
     TheOtherScore.Reset(); 
    } 
    private void Form1_Load(object sender, EventArgs e) 
    { 

    } 
} 
} 

Und meine PlayAgain Klasse:

namespace BrickO1 
{ 
public partial class PlayAgain : Form1 
{ 
    public PlayAgain() 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     init(); 
    } 
} 
} 

Antwort

0

Wenn Sie eine neue Form machen wollen, dann für das Formular ein neues Objekt erstellen.

Form1 f1=new Form1(); 
f1.Show(); 
+0

Denken Sie daran, Ihren Code gemäß den Standards zu formatieren, bevor Sie beide Fragen oder Antworten posten. –

Verwandte Themen