C# 如何使按钮开始游戏?

C# 如何使按钮开始游戏?,c#,winforms,C#,Winforms,我现在正在做我的第一个游戏,一个简单的乒乓球迭代。我想有它,这样一个按钮可以按下,这启动计时器,但当我添加到游戏的按钮,我发现游戏失去了控制的能力 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threadi

我现在正在做我的第一个游戏,一个简单的乒乓球迭代。我想有它,这样一个按钮可以按下,这启动计时器,但当我添加到游戏的按钮,我发现游戏失去了控制的能力

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;
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
名称空间pong_200
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
bool moveup;//这是一个布尔值,用于检测玩家何时起床。
bool movedown;//这是一个布尔值,用于检测玩家何时倒下。
int speed=5;//保存泛型值5的整数。
int posx=5;//球的水平速度。
int posy=5;//球的垂直速度。
int playerPoints=0;//球员得分
int cpuPoints=0;//计算机的分数
private void键按下(对象发送者,KeyEventArgs e)
{
if(e.KeyCode==Keys.Up)
{
moveup=true;//如果播放器向上按,布尔值将变为true。
}
if(e.KeyCode==Keys.Down)
{
movedown=true;//如果播放器按下,布尔值将变为true。
}
}
private void keypressup(对象发送方,KeyEventArgs e)
{
if(e.KeyCode==Keys.Up)
{
moveup=false;//如果玩家放弃up,布尔值将变为false。
}
if(e.KeyCode==Keys.Down)
{
movedown=false;//如果播放机放弃down,布尔值将变为false。
}
}
private void timerTick(对象发送方,EventArgs e)//此事件每20 m/s发生一次。
{
playerScore.Text=”“+playerPoints;//在playerScore标签上显示玩家的分数。
aiScore.Text=”“+cpuPoints;//在cpuScore标签上显示计算机的分数。
ball.Top-=posy;//将球的顶部设置为Y。
ball.Left-=posx;//将球的左侧设置为X。
aipable.Top+=速度;//将CPU设置为最大速度整数。
如果(玩家分数<5)//如果分数低于5。。。
{
如果(aipalle.Top<0 | | aipalle.Top>455)
{
速度=-速度;
}//如果计算机已到达框架的顶部或底部,请将方向更改回框架。
}
其他的
{
Aipabler.Top=ball.Top+30;//否则,如果得分超过5分,则让划桨跟随球,以增加难度。
}
if(ball.Left<0)//如果球已经越过了球员。。。
{
ball.Left=434;//将球放回中间。
posx=-posx;//更改球的方向。
posx-=2;//使球的速度更快。
cpuPoints++;//给CPU一分。
}
if(ball.Left+ball.Width>ClientSize.Width)//如果球经过了计算机。。。
{
ball.Left=434;//将球放回中间。
posx=-posx;//更改球的方向。
posx+=2;//加快球的速度。
playerPoints++;//给玩家一分。
}
if(ball.Top<0 | | ball.Top+ball.Height>ClientSize.Height)
{
posy=-posy;//如果球击中画面的顶部或底部,则反转球的速度,使其保持在屏幕上。
}
if(ball.Bounds.IntersectsWith(playerPaddle.Bounds)| | ball.Bounds.IntersectsWith(aiball.Bounds))
{
posx=-posx;//然后向相反方向弹起球。
}
if(moveup==true&&playerPaddle.Top>0)
{
playerPaddle.Top-=8;//如果布尔值是向上的,并且在上限内,则将玩家向顶部移动8。
}
if(movedown==true&&playerPaddle.Top<455)
{
playerPaddle.Top+=8;//如果布尔值为向下,且在下限内,则将玩家向底部移动8。
}
如果(播放点数>10)
{
pongTimer.Stop();
MessageBox.Show(“你赢了!”);//如果玩家得分超过10分,停止游戏计时器,并显示胜利框。
}
如果(cpuPoints>10)
{
pongTimer.Stop();
MessageBox.Show(“你输了!”);//如果电脑的积分超过10分,请停止游戏计时器并显示“输了”框。
}
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
pongTimer=真;
}
}
}
游戏本身的工作时,没有按钮,但我希望游戏是一个按钮按下开始,因为我正在拓宽视野。我将非常感谢您的帮助和指导

只是一个话题: 在处理游戏时,我喜欢思考/处理场景。 例如 启动屏幕-->菜单-->游戏-->暂停-->积分


转化为您的问题,也许您可以拥有一个包含startbutton的表单,并在单击时加载包含游戏的下一个表单。

pongTimer=true无法工作,pongTimer似乎是计时器,而不是bool。@ConnerTurner这个问题有什么问题?我想说这并不明显。至少要善意地为这个新用户指出正确的方向。
pongTimer.Start()?这能解决问题吗?注:timerTick很难阅读,请将其剪切成小函数。然后打开按键
开关(如KeyData)
即可