如何让按钮开始游戏?

时间:2018-06-04 08:41:45

标签: c# winforms

我目前正在制作我的第一个游戏,一个简单的Pong迭代。我想拥有它以便可以按下按钮并启动计时器,但是当我将按钮添加到游戏中时,我发现游戏失去了被控制的能力。

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 pong_200
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        bool moveup;//This is a boolean to detect when player's up.
        bool movedown;//This is a boolean to detect when player's down.
        int speed = 5;//Integer that holds a generic value of 5.
        int posx = 5;//Speed of ball horizontally.
        int posy = 5;//Speed of ball vertically.
        int playerPoints = 0;//Player's score
        int cpuPoints = 0;//Computer's score




        private void keypressdown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Up)
            {
                moveup = true;//If the player presses up, the boolean changes to true.
            }
            if (e.KeyCode == Keys.Down)
            {
                movedown = true;//If the player presses down, the boolean changes to true.
            }
        }

        private void keypressup(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Up)
            {
                moveup = false;//If the player lets go of up, the boolean changes to false.
            }
            if (e.KeyCode == Keys.Down)
            {
                movedown = false;//If the player lets go of down, the boolean changes to false.
            }
        }

        private void timerTick(object sender, EventArgs e)//This event occurs every 20 m/s.
        {
            playerScore.Text = "" + playerPoints;//Displays the player's score on the playerScore label.
            aiScore.Text = "" + cpuPoints;//Displays the computer's score on the cpuScore label.

            ball.Top -= posy;//Sets the top of the ball to Y.
            ball.Left -= posx;//Sets the left side of the ball to X.

            aiPaddle.Top += speed;//Sets the CPU as the maximum speed integer.

            if (playerPoints < 5)//If the score is lower than 5...
            {
                if (aiPaddle.Top < 0 || aiPaddle.Top > 455)
                {
                    speed = -speed;
                }   //If the computer has reached the top or bottom of the frame, change the direction back into the frame.
            }
            else
            {
                aiPaddle.Top = ball.Top + 30;//Else if the score is more than 5, let the paddle follow the ball for difficulty.
            }

            if (ball.Left < 0)//If the ball has gone past the player...
            {
                ball.Left = 434;//Set the ball back to the middle.
                posx = -posx;//Change the direction of the ball.
                posx -= 2;//Make the ball's speed faster.
                cpuPoints++;//Give the CPU one point.
            }

            if (ball.Left + ball.Width > ClientSize.Width)//If the ball has gone past the computer...
            {
                ball.Left = 434;//Set the ball back to the middle.
                posx = -posx;//Change the ball direction.
                posx += 2;//Speed up the ball.
                playerPoints++;//Give the player one point.
            }

            if (ball.Top < 0 || ball.Top + ball.Height > ClientSize.Height)
            {
                posy = -posy;//If the ball hits either the top or bottom of the frame, reverse the speed of the ball, to keep it in the screen.
            }

            if (ball.Bounds.IntersectsWith(playerPaddle.Bounds) || ball.Bounds.IntersectsWith(aiPaddle.Bounds))
            {
                posx = -posx;//Then bounce the ball in the opposite direction.
            }

            if (moveup == true && playerPaddle.Top > 0)
            {
                playerPaddle.Top -= 8;//If the boolean is up, and  within the upper bounds, move the player towards the top by 8.
            }

            if (movedown == true && playerPaddle.Top < 455)
            {
                playerPaddle.Top += 8;//If the boolean is down, and within the lower bounds, move the player towards the bottom by 8.
            }

            if (playerPoints > 10)
            {
                pongTimer.Stop();
                MessageBox.Show("You Win!");//If the player has more than 10 points, stop the game timer, and show the victory box.
            }

            if (cpuPoints > 10)
            {
                pongTimer.Stop();
                MessageBox.Show("You Lose!");//If the computer has more than 10 points, stop the game timer and display the loss box.
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            pongTimer = true;
        }
    }
}

当没有按钮时游戏本身可以正常工作,但是我希望通过按钮开始游戏,因为我正在拓宽视野。我将不胜感激任何帮助或指导!

1 个答案:

答案 0 :(得分:0)

只是一个: 在处理游戏时,我喜欢思考/使用场景。 例如 启动画面 - &gt;菜单 - &gt;游戏 - &gt;暂停 - &gt;积分....

翻译成您的问题,也许您可​​以拥有一个包含您的开始按钮的表单,并在点击时加载包含您游戏的下一个表单。