Advertisement
Guest User

Untitled

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