Kyaria

gg, pong, gg

Jan 9th, 2018
393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.17 KB | None | 0 0
  1. using System;
  2. using System.Windows.Forms;
  3.  
  4. namespace Pong
  5. {
  6.     public partial class Form1 : Form
  7.     {
  8.         public int bewegung_x = 4;      //Links / Rechts
  9.         public int bewegung_y = 4;      //Hoch / Runter
  10.         public int punkte = 0;
  11.  
  12.         public Form1()
  13.         {
  14.             InitializeComponent();
  15.  
  16.             timer1.Enabled = true;
  17.             Cursor.Hide();
  18.  
  19.             label2.Text = "Punkte: " + punkte;
  20.  
  21.             label1.Visible = false;
  22.         }
  23.  
  24.         private void timer1_Tick(object sender, EventArgs e)
  25.         {
  26.             pictureBox1.Left = Cursor.Position.X - (pictureBox1.Width / 2);   //Mitte Schläger = Cursor
  27.  
  28.             pictureBox2.Left += bewegung_x;
  29.             pictureBox2.Top += bewegung_y;
  30.  
  31.             if (pictureBox2.Bottom >= pictureBox1.Top && pictureBox2.Bottom <= pictureBox1.Bottom && pictureBox2.Left >= pictureBox1.Left && pictureBox2.Right <= pictureBox1.Right) //Ball trifft Schläger
  32.             {
  33.                 bewegung_x += 2;
  34.                 bewegung_y += 2;
  35.                 bewegung_y = -bewegung_y;       //Richtungswechsel
  36.                 punkte += 1;
  37.  
  38.                 label2.Text = "Punkte: " + punkte;
  39.             }
  40.  
  41.             if (pictureBox2.Left <= panel1.Left) bewegung_x = -bewegung_x;
  42.             if (pictureBox2.Right >= panel1.Right) bewegung_x = -bewegung_x;
  43.             if (pictureBox2.Top <= panel1.Top) bewegung_y = -bewegung_y;
  44.             if (pictureBox2.Bottom >= panel1.Bottom)
  45.             {
  46.                 timer1.Enabled = false;         //Game over
  47.                 label1.Visible = true;
  48.             }
  49.  
  50.         }
  51.  
  52.         private void Form1_KeyDown(object sender, KeyEventArgs e)
  53.         {
  54.             if (e.KeyCode == Keys.Escape) this.Close();         //Spiel mit Escape beenden
  55.             if (e.KeyCode == Keys.Enter)
  56.             {
  57.                 pictureBox2.Left = 50;
  58.                 pictureBox2.Top = 50;
  59.                 bewegung_x = 4;
  60.                 bewegung_y = 4;
  61.                 punkte = 0;
  62.                 label2.Text = "Punkte: " + punkte;
  63.                 timer1.Enabled = true;
  64.                 label1.Visible = false;
  65.             }
  66.         }
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment