-1

I'm currently making my first game, a simple iteration of Pong. I want to have it so that a button can be pressed and this starts the timer, however when I add the button to the game, I find that the game loses the ability to be controlled.

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;
        }
    }
}

The game itself works when there is no button, but I would like for the game to be started with a button press as I am broadening the horizons. I would appreciate any help or guidance with this!

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • `pongTimer = true;` can't work, pongTimer seems to be a Timer, not a bool. – Rabban Jun 04 '18 at 08:45
  • @ConnerTurner What's the problem with this question? I would say it isn't obvious. At least be kind enough to point this new user in the correct direction. – FishySwede Jun 04 '18 at 08:45
  • `pongTimer.Start();` ? Does this solve the issue ? Ps: timerTick is hard to read please cut it in little function. And switch on keypress `switch (e.KeyData)` will make it easier for you to read and maintain. The variable `posx` and `posy` ring the bell that there is an object ball with x/y position but comment said it's for speed .. – Drag and Drop Jun 04 '18 at 09:28
  • At every tick you update `playerScore.Text` even if no on scored. there is also a lot of comment , pehaps better variable name will help understanding. Perhaps some are just overkilllikie : `playerPoints++;//Give the player one point.` – Drag and Drop Jun 04 '18 at 09:38
  • I have give you a few tips on your code let me do the same for your question. Following the guideline: [ask], [mcve]. If your issue is "Clicking on a button prevent KeyCapture", your question should be limited to exactly this ! Is all this code about pong related to the issue ? Will a code with only a label displaying a text based on key press and a button enought to desmonstrate the issue ? I can look a big rude but as you are strating with C# and stackoverflow i try to give you some advice to get the best out of you. – Drag and Drop Jun 04 '18 at 09:44
  • And you would have found the duplicate easly, because your question would have been focus on your issue instead of beeing focus on pong. – Drag and Drop Jun 04 '18 at 09:47
  • Related question : https://stackoverflow.com/questions/18280976/button-prevents-keydown-event-from-triggering-even-with-keypreview-true, https://stackoverflow.com/questions/34006951/prevent-button-from-being-focused-by-arrow-key-click – Drag and Drop Jun 04 '18 at 09:47
  • Possible duplicate of [Button prevents KeyDown event from triggering even with keypreview = true](https://stackoverflow.com/questions/18280976/button-prevents-keydown-event-from-triggering-even-with-keypreview-true) – Drag and Drop Jun 04 '18 at 09:48

1 Answers1

0

just a tought: When working with games, I like to think/work with scenes. e.g. Splash Screens --> Menu --> Game --> Pause --> Credits....

Translated to your problem, maybe you could have a form containing your startbutton, and load the next form containing your game on click.

Gaander
  • 81
  • 1
  • 8
  • Not related, no matter the number of form you add any button will prevent the key. up/down/l/r as they will trigger the navigation between form element. – Drag and Drop Jun 04 '18 at 09:51