Reputation: 595
i need some help with WPF
I'm getting started with WPF and wanted to make a simple snake game with the following classes:
GameBoard : Window
{
private Game game;
public GameBoard(){ this.game = new Game(this); }
public Rectangle rectangleFactory(int x, int y)
{
Rectangle rect = new Rectangle();
rect.Width = 10;
rect.Height = 10;
GameArea.Children.Add(rect); /// My Canvas is called GameArea
Canvas.SetLeft(rect, 10 * x);
Canvas.SetTop(rect, 10 * y);
return rect;
}
public void rectangleBin(Rectangle)
{
GameArea.Children.Remove(rect);
}
}
Thing{
private GameBoard Board;
private System.Windows.Shapes.Rectangle rect;
private int[] coordinaters;
public Thing(GameBoard board, int[] coordinates)
{
this.Board = board;
Draw();
}
public Draw(){ rect = Board.rectangleFactory(coordinates);}
public Dispose(){ Board.rectangleBin(rect)}
}
SnakeSegment : Thing
{
SnakeSegment next;
public SnakeSegment(GameBoard board, int[] coordinates) : base(board, coordinates) { }
}
Apple : Thing()
{
public Apple(GameBoard board, int[] coordinates) : base(board, coordinates) { }
public void newApple()
{
Dispose();
getNewCoordinate();
Draw();
}
}
Game
{
private GameBoard Board;
private Apple apple;
Private Snake snake;
private static System.timers.timer timer
public Game(GameBoard board)
{
this.Board = board;
this.apple = new Apple(board, int start_coordinates);
this.snake = new snake(this);
this.timer = new Timer(100);
timer.Elapsed += new ElapsedEventHandler(update_game);
}
public void update_game(object o, ElapsedEventArgs s){ snake.move_forward(); }
}
Snake
{
private SnakeSegmet Head;
Private SnakeSegment Tail
private GameBoard Board;
public Snake(Game game)
{
this.Board = game.Board;
this.Head = new SnakeSegment(Board, start_cooridantse)
this.Tail = Head;
}
private void AddSegment()
{
Head.Next = new SnakeSegment(Board, newCoordinates);
Head = Head.Next;
}
}
There is of course more but i left it out in the interest of readability.
When i start the game it draws the game area and adds an apple and an initial rectangle for the snake, so far so good
But then i update the game and things go south
my next call to SnakeSegment constructor in Head.Next = new SnakeSegment()
ends in rectangleFactory when calling new Rectangle()
the program doesn't crash, but it topples the current stack and nothing more is executed untinl the timer raises an other event
ive tried to troubleshoot and reroute the flow a bit and if i instead call Apple::newApple() the same thing happens when i get to GameArea.Children.Remove(rect);
Im completely new to WPF and this really gives me a headache, if anybody could explain why it happens id be very grateful.
Regards, Tobias
Upvotes: 1
Views: 211
Reputation: 69979
If you want to write your program like that, use WinForms. If you want to use WPF, you'll need to pretty much start again from scratch. WPF is a data-centric language... we manipulate data objects, not UI objects.
Using WPF, you'd need to think about your game objects as a collection of data objects. The board would then be the UI container control that you data bind your collection to. You would then define what each object should look like in a DataTemplate
in the XAML. After binding the location properties of the data objects to the location properties of the UI objects you could then move the objects in the UI by changing the location properties of the data objects from code.
Having said all that, I think that you'll find that WPF is not a good Framework for making games. Its main strengths are its data binding capabilities and rich UI possibilities, but it's not very efficient. You'll need a good graphics card to get the most out of it.
Upvotes: 3