Advertisement
Guest User

Untitled

a guest
Jun 1st, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.30 KB | None | 0 0
  1.  public partial class MainWindow : Window
  2.     {
  3.         private List<Point> snakePoints = new List <Point>();
  4.         private Point startingPoint = new Point(100, 100);
  5.         private Point currentPosition = new Point();
  6.         private int direction = 0; //Steuerung mit Pfeiltasten (8,4,6,2)
  7.  
  8.         private List<Food> foodList = new List<Food>();
  9.  
  10.        
  11.  
  12.      
  13.         public MainWindow()
  14.         {
  15.             InitializeComponent();
  16.  
  17.  
  18.             DispatcherTimer timer = new DispatcherTimer();
  19.             timer.Tick += new EventHandler(timer_Tick);
  20.             timer.Interval = new TimeSpan(10000);
  21.             timer.Start();
  22.  
  23.             this.KeyDown += new KeyEventHandler(OnButtonKeyDown);
  24.             PaintSnake(startingPoint);
  25.             currentPosition = startingPoint;
  26.  
  27.          
  28.  
  29.         }
  30.  
  31.        
  32.  
  33.         private void PaintSnake(Point currentPosition)
  34.         {
  35.             //##### Check, ob die Schlange in sich selbst fährt#####
  36.  
  37.             if (snakePoints.Contains(currentPosition))
  38.             {
  39.                 GameOver();
  40.             }
  41.  
  42.             Ellipse newEllipse = new Ellipse();
  43.             newEllipse.Fill= Brushes.Red;
  44.             newEllipse.Width = 6; //Stärke des Schlangenkörpers
  45.             newEllipse.Height = 6; //Stärke des Schlangenkörpers
  46.             Canvas.SetBottom(newEllipse, currentPosition.Y);
  47.             Canvas.SetLeft(newEllipse, currentPosition.X);
  48.  
  49.             int count = paintCanvas.Children.Count;
  50.             paintCanvas.Children.Add(newEllipse); //Ellipse auf Zeichenfläche einhängen
  51.             snakePoints.Add(currentPosition);
  52.  
  53.             //########## Länge der Schlange begrenzen ##########
  54.             int length = 100;
  55.  
  56.             if (count >length)
  57.             {
  58.                 paintCanvas.Children.RemoveAt(count - length - 1);
  59.                 snakePoints.RemoveAt(count - length);
  60.             }
  61.  
  62.             Ellipse food = new Ellipse();
  63.             food.Fill = Brushes.Blue;
  64.             food.Width = 4;
  65.             food.Height = 4;
  66.             Canvas.SetBottom(food, currentPosition.Y);
  67.             Canvas.SetLeft(food, currentPosition.X);
  68.  
  69.  
  70.  
  71.  
  72.             double punkte = Convert.ToInt32(lblpunkte.Content);
  73.  
  74.            
  75.  
  76.  
  77.  
  78.  
  79.         }
  80.  
  81.        
  82.  
  83.         public partial class Food :
  84.         {
  85.             static Random random = new Random();
  86.             Ellipse foodshape = new Ellipse();
  87.  
  88.             public Food (Canvas paintcanvas)
  89.  
  90.                 : base (random.NextDouble() * paintcanvas.ActualWidth,
  91.                       random.NextDouble() *paintcanvas.ActualHeight,
  92.                       (random.NextDouble()-0.5)*100,
  93.                       (random.NextDouble()-0.5)*100)
  94.             {
  95.                 foodshape.Height = 5;
  96.                 foodshape.Width = 5;
  97.                 foodshape.Fill = Brushes.Blue;
  98.             }
  99.  
  100.  
  101.         }
  102.  
  103.  
  104.         private void GameOver()
  105.         {
  106.             MessageBox.Show("You lose. Game over!");
  107.             this.Close();
  108.  
  109.            
  110.         }
  111.  
  112.        
  113.  
  114.  
  115.         private void OnButtonKeyDown(object sender, KeyEventArgs e)
  116.         {
  117.             switch (e.Key)
  118.             {
  119.                 case Key.Down:
  120.                     direction = 2;
  121.                     break;
  122.                 case Key.Up:
  123.                     direction = 8;
  124.                     break;
  125.                 case Key.Left:
  126.                     direction = 4;
  127.                     break;
  128.                 case Key.Right:
  129.                     direction = 6;
  130.                     break;
  131.  
  132.  
  133.             }
  134.  
  135.  
  136.         }
  137.  
  138.         private void timer_Tick(object sender, EventArgs e)
  139.         {
  140.  
  141.             switch (direction)
  142.             {
  143.                 case 8: //up
  144.                     currentPosition.Y += 1;
  145.                     PaintSnake(currentPosition);
  146.                     break;
  147.                 case 2: //down
  148.                     currentPosition.Y -= 1;
  149.                     PaintSnake(currentPosition);
  150.                     break;
  151.                 case 4: //left
  152.                     currentPosition.X -= 1;
  153.                     PaintSnake(currentPosition);
  154.                     break;
  155.                 case 6: //right
  156.                     currentPosition.X += 1;
  157.                     PaintSnake(currentPosition);
  158.                     break;
  159.  
  160.             }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement