Kyaria icon

gg, pong, gg

Kyaria | PRO | 01/09/18 07:33:30 PM UTC | 0 ⭐ | 363 👁️ | Never ⏰ | []
C# |

2.17 KB

|

None

|

0 👍

/

0 👎

using System;
using System.Windows.Forms;
 
namespace Pong
{
    public partial class Form1 : Form
    {
        public int bewegung_x = 4;      //Links / Rechts
        public int bewegung_y = 4;      //Hoch / Runter
        public int punkte = 0;
 
        public Form1()
        {
            InitializeComponent();
 
            timer1.Enabled = true;
            Cursor.Hide();
 
            label2.Text = "Punkte: " + punkte;
 
            label1.Visible = false;
        }
 
        private void timer1_Tick(object sender, EventArgs e)
        {
            pictureBox1.Left = Cursor.Position.X - (pictureBox1.Width / 2);   //Mitte Schläger = Cursor
 
            pictureBox2.Left += bewegung_x;
            pictureBox2.Top += bewegung_y;
 
            if (pictureBox2.Bottom >= pictureBox1.Top && pictureBox2.Bottom <= pictureBox1.Bottom && pictureBox2.Left >= pictureBox1.Left && pictureBox2.Right <= pictureBox1.Right) //Ball trifft Schläger
            {
                bewegung_x += 2;
                bewegung_y += 2;
                bewegung_y = -bewegung_y;       //Richtungswechsel
                punkte += 1;
 
                label2.Text = "Punkte: " + punkte;
            }
 
            if (pictureBox2.Left <= panel1.Left) bewegung_x = -bewegung_x;
            if (pictureBox2.Right >= panel1.Right) bewegung_x = -bewegung_x;
            if (pictureBox2.Top <= panel1.Top) bewegung_y = -bewegung_y;
            if (pictureBox2.Bottom >= panel1.Bottom)
            {
                timer1.Enabled = false;         //Game over
                label1.Visible = true;
            }
 
        }
 
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape) this.Close();         //Spiel mit Escape beenden
            if (e.KeyCode == Keys.Enter)
            {
                pictureBox2.Left = 50;
                pictureBox2.Top = 50;
                bewegung_x = 4;
                bewegung_y = 4;
                punkte = 0;
                label2.Text = "Punkte: " + punkte;
                timer1.Enabled = true;
                label1.Visible = false;
            }
        }
    }
}

Comments