Cześć. Zrobiłem grę pong, ale moja piłka porusza się tylko w górę i w dół. Po prostu spada i niezależnie od tego jaką częścią paletki ją odbije ona leci prosto do góry. Tutaj fragment mojego kodu, który pewnie trzeba poprawić. Dzięki z góry za pomoc !
public partial class Form1 : Form
{
public int speed_left = 4;
public int speed_top = 4;
public int points = 0;
public Form1()
{
InitializeComponent();
timer1.Enabled = true;
Cursor.Hide();
this.FormBorderStyle = FormBorderStyle.None;
this.TopMost = true;
this.Bounds = Screen.PrimaryScreen.Bounds;
racket.Top = polegry.Bottom - (polegry.Bottom / 10);
koniecgry.Left = (polegry.Width / 2) - (koniecgry.Width / 2);
koniecgry.Top = (polegry.Height / 2) - (koniecgry.Height / 2);
koniecgry.Visible = false;
}
private void polegry_Paint(object sender, PaintEventArgs e)
{
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape) { this.Close(); }
if (e.KeyCode == Keys.F1)
{
pilka.Top = 50;
pilka.Left = 50;
speed_left = 4;
speed_top = 4;
points = 0;
point.Text = "0";
timer1.Enabled = true;
koniecgry.Visible = false;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
racket.Left = Cursor.Position.X - (racket.Width / 2);
pilka.Left += speed_left;
pilka.Top += speed_top;
if (pilka.Bottom >= racket.Top && pilka.Bottom <= racket.Bottom && pilka.Left >= racket.Left && pilka.Right <= racket.Right)
{
speed_top += 2;
speed_left += 2;
speed_top = -speed_top;
points += 1;
point.Text = points.ToString();
Random r = new Random();
polegry.BackColor = Color.FromArgb(r.Next(150, 250), r.Next(150, 255), r.Next(150, 255));
}
if (pilka.Left <= polegry.Left)
{
speed_left = -speed_left;
}
if (pilka.Right <= polegry.Right)
{
speed_left = -speed_left;
}
if (pilka.Top <= polegry.Top)
{
speed_top = -speed_top;
}
if (pilka.Bottom >= polegry.Bottom)
{
timer1.Enabled = false;
koniecgry.Visible = true;
}