В настоящее время я делаю свою первую игру, простую итерацию понга.Я хочу, чтобы кнопка была нажата, и это запускает таймер, однако, когда я добавляю кнопку в игру, я обнаруживаю, что игра теряет способность к управлению.
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;
}
}
}
Сама игра работает, когда нет кнопки, но я хотел бы, чтобы игра запускалась нажатием кнопки, поскольку я расширяю горизонты.Я был бы признателен за любую помощь или руководство с этим!