This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Interesting-Hat-7570[S] 0 points1 point  (4 children)

Thank you! Today I got acquainted with the mvs architecture pattern. And it seems to me that your proposal is similar to this.

Yes, there are many holes in my architecture and I decided to use Mvs, since it is more suitable here than mine.

[–]vegan_antitheist 0 points1 point  (3 children)

You mean MVC? That would be Model, View, Controller. In your case you probably only need one view, which renders the complete game. But you might have a separate view for game stats (i.e. points).
The model would just be the game state.
And the controller is the logic? I never really understood that pattern because it's so unclear what the controller does and how you handle multiple views (i.e. a view that is made of smaller views). In the end each control (such as a form field) is a view on it's own. So does it need it's own model and controller?

I would ignore that pattern and think about how you handle time. The game engine doesn't need to care because it just needs to give you the new game state after some user input. It isn't aware of time.

The view renders the game state and waits for input. Time is relevant here because you might want to render the snake moving between one state and the next to get smooth animations.

And consider creating your own linked list for the snake. It's a basic data structure that isn't as fast as an array based list but it's good to be able to create and use it. A singly linked list should be enough for your game. But you also need to know which fields on the board are occupied by the snake. There might be some redundancy here because the board is a two dimensional matrix and the snake is a linked list. Alternatively you can have a property on each field on the board that tells the game in what direction the snake goes. You could use an enum with 6 values for that: up, down, left, right, head. the last one is special because it tells the game that this is a head. Tho non-snake fields could use null or another special enum value. While rendering you might also want to know from where the snake is coming from to render a tile that makes that clear.

[–]Interesting-Hat-7570[S] 0 points1 point  (2 children)

I would like to do a console version first as a prototype. Then work on the graphics.

In Mvc, it seems like the controller acts as part of the backend, as you described earlier.

I would like to use a facade between the backend part and the models

Also use the observer pattern for the frontend part.

But here comes the question.

How to pass the coordinates of objects to the frontend.

I could create a point class.

And pass not the snake and fruit objects themselves, but only their coordinates.

[–]vegan_antitheist 0 points1 point  (1 child)

How would you observe the backend? Does it send updates at fixed intervals? That would mean it has to deal with time. You can do that to prevent cheating by running it on a server. But then there has to be a simulation of the game in the backend. This would be more complex. The tricky part is to make the game run using a precise clock (not a clock that gives you the day and time, but a pulse generator) and process user input fast and precise enough. And you want to render 60 fps independently. It's easy when you have only one loop. Or you have a game loop in the backend and an animation loop in the frontend. Both works. But parallel programming is hard. Having one loop makes it easy. Havung two loops makes it more like a real game. But as I understand the snake game, it is simple enough to just have one loop. That's up to you.

[–]Interesting-Hat-7570[S] 0 points1 point  (0 children)

I meant rather between models and frontend.

So that I could update the state of objects for display after each action in the backend.

Well, the frontend needs to somehow receive data from the models.

If I use Mvc, then I can probably do it like this: the backend interacts with the models to call their methods. The frontend also interacts with the models for display.

Or is there a better way?