Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public partial class MainWindow : Window
- {
- private List<Point> snakePoints = new List <Point>();
- private Point startingPoint = new Point(100, 100);
- private Point currentPosition = new Point();
- private int direction = 0; //Steuerung mit Pfeiltasten (8,4,6,2)
- private List<Food> foodList = new List<Food>();
- public MainWindow()
- {
- InitializeComponent();
- DispatcherTimer timer = new DispatcherTimer();
- timer.Tick += new EventHandler(timer_Tick);
- timer.Interval = new TimeSpan(10000);
- timer.Start();
- this.KeyDown += new KeyEventHandler(OnButtonKeyDown);
- PaintSnake(startingPoint);
- currentPosition = startingPoint;
- }
- private void PaintSnake(Point currentPosition)
- {
- //##### Check, ob die Schlange in sich selbst fährt#####
- if (snakePoints.Contains(currentPosition))
- {
- GameOver();
- }
- Ellipse newEllipse = new Ellipse();
- newEllipse.Fill= Brushes.Red;
- newEllipse.Width = 6; //Stärke des Schlangenkörpers
- newEllipse.Height = 6; //Stärke des Schlangenkörpers
- Canvas.SetBottom(newEllipse, currentPosition.Y);
- Canvas.SetLeft(newEllipse, currentPosition.X);
- int count = paintCanvas.Children.Count;
- paintCanvas.Children.Add(newEllipse); //Ellipse auf Zeichenfläche einhängen
- snakePoints.Add(currentPosition);
- //########## Länge der Schlange begrenzen ##########
- int length = 100;
- if (count >length)
- {
- paintCanvas.Children.RemoveAt(count - length - 1);
- snakePoints.RemoveAt(count - length);
- }
- Ellipse food = new Ellipse();
- food.Fill = Brushes.Blue;
- food.Width = 4;
- food.Height = 4;
- Canvas.SetBottom(food, currentPosition.Y);
- Canvas.SetLeft(food, currentPosition.X);
- double punkte = Convert.ToInt32(lblpunkte.Content);
- }
- public partial class Food :
- {
- static Random random = new Random();
- Ellipse foodshape = new Ellipse();
- public Food (Canvas paintcanvas)
- : base (random.NextDouble() * paintcanvas.ActualWidth,
- random.NextDouble() *paintcanvas.ActualHeight,
- (random.NextDouble()-0.5)*100,
- (random.NextDouble()-0.5)*100)
- {
- foodshape.Height = 5;
- foodshape.Width = 5;
- foodshape.Fill = Brushes.Blue;
- }
- }
- private void GameOver()
- {
- MessageBox.Show("You lose. Game over!");
- this.Close();
- }
- private void OnButtonKeyDown(object sender, KeyEventArgs e)
- {
- switch (e.Key)
- {
- case Key.Down:
- direction = 2;
- break;
- case Key.Up:
- direction = 8;
- break;
- case Key.Left:
- direction = 4;
- break;
- case Key.Right:
- direction = 6;
- break;
- }
- }
- private void timer_Tick(object sender, EventArgs e)
- {
- switch (direction)
- {
- case 8: //up
- currentPosition.Y += 1;
- PaintSnake(currentPosition);
- break;
- case 2: //down
- currentPosition.Y -= 1;
- PaintSnake(currentPosition);
- break;
- case 4: //left
- currentPosition.X -= 1;
- PaintSnake(currentPosition);
- break;
- case 6: //right
- currentPosition.X += 1;
- PaintSnake(currentPosition);
- break;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement