7 Replies - 445 Views - Last Post: 14 January 2013 - 10:59 AM Rate Topic: -----

#1 jammasterz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-January 13

How to write a nicely structured game

Posted 12 January 2013 - 06:58 PM

Hi. I am new to game scripting, and even scripting itself (mostly object orientated programming, but i have a pretty good understanding of it). I have programmed a little bit in C++, some low level languages, and now i just learned java. To test my abillities and have some fun i decided to code a 3D snake game, with multiple levels, obstackles, and tons of other good stuff.

The 3d part(openGL) is pretty much covered, as pretty much everything else, however there is one thing that i am bad at and that is why i come to you :).

To ask my question i first have to tell you what i want this game to be like. When i start the game i want there to be a Title Screen, where i can choose whether to start playing or look at high scores or maybe do something else.
(don't mind the upside down, mirrored text :D)
photo
When i decide to play, i choose the New Game button and press enter and the menu from the title screen(the set of buttons to the right on the picture above) gets replaced with another one(a submenu), but this time with the names of available levels. And there, when i choose one of the levels the game starts. But when i choose High Scores, i should get the highscores right away, no submenus.

Now this is a part of my question. The way i planned it out is that every screen (like high score screen, credits screen, and so on) has its own class where i define the render functions, and some other helper functions. But not every screen. For example the menu that you can see on the picture is rendered when i render the Title screen. But what do i do when i want to render the subMenu? This all got too complicated, and i believe there is an easier and cleaner way to do all this.

I think i can manage to just continue adding on to my code as it is, but as i do that i feel its harder and harder to do that.

So my problem is basically that i don't know how to properly structure the code. Because i know that what i just wrote may be a little bit hard to understand i will give you the code i've got so far.

The main class:http://pastebin.com/cChrMSVb
The TitleScreen class:http://pastebin.com/9wEkcv7y
The Menu class:http://pastebin.com/122Q7ks0
The Button class:http://pastebin.com/nZA9CD2Y
The Grid class:http://pastebin.com/NiLFDkEA This one is pretty much done for now
The Snake class:http://pastebin.com/anDbxf5i Same here, pretty much done for now

Right now im struggling to somehow pass the information that a button has been chosen by pressing enter to the instance of menu os that i can display the submenu. But it isn't that easy.

I think that if you take a brief look at it you should get an idea what i am talking about. I am not asking you to rewrite it, i am asking how to design any game code so that it is easier for me to write it. I know this is a big question, and im not expecting to get big answers, but small tips would be nice, or maybe some design tutorials.

Is This A Good Question/Topic? 0
  • +

Replies To: How to write a nicely structured game

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10771
  • View blog
  • Posts: 40,103
  • Joined: 27-December 08

Re: How to write a nicely structured game

Posted 12 January 2013 - 07:09 PM

First, please post your code here, using code tags: :code:.

Are you familiar with the MVC Pattern? You could use that, as well as using a StateManager to help separate your GUI and state/data.
Was This Post Helpful? 1
  • +
  • -

#3 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 1
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: How to write a nicely structured game

Posted 12 January 2013 - 09:52 PM

My first game demo before I got into the games industry was actually a full 3D Snake game. I put in Quake power-ups, everything you described plus AI. It was very challenging at the time as I wrote the entire code in C.

I can see where you have started off on the wrong foot, because:

1. You have no clear detailed feature sets laid out for the game - everything is in the air.

2. You don't seem to have any idea how to write the individual components you have planned for your game.

If I were you I would prototype everything in isolation; the menu system as one project, the in-game action in another for example. This was my approach when I was learning. This overall picture is too big for you to manage right now.
Was This Post Helpful? 2
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10771
  • View blog
  • Posts: 40,103
  • Joined: 27-December 08

Re: How to write a nicely structured game

Posted 12 January 2013 - 10:57 PM

Quote

If I were you I would prototype everything in isolation; the menu system as one project, the in-game action in another for example. This was my approach when I was learning. This overall picture is too big for you to manage right now.

I like this approach a lot. Test-driven development might be a good methodology for you to look into. It helps you define both functionality and correctness, as well as assert the latter, with more clarity.
Was This Post Helpful? 1
  • +
  • -

#5 jammasterz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-January 13

Re: How to write a nicely structured game

Posted 13 January 2013 - 03:08 AM

Quote

First, please post your code here, using code tags: .

Sorry for uploading the code to pastebin instead of pasting it here, i thought it would take too much space.

Quote

Are you familiar with the MVC Pattern? You could use that, as well as using a StateManager to help separate your GUI and state/data.

Thank you very much! I think its exactly what i need. Im going read a little about the MVC and get some examples and return.

Quote

I can see where you have started off on the wrong foot, because:

1. You have no clear detailed feature sets laid out for the game - everything is in the air.

2. You don't seem to have any idea how to write the individual components you have planned for your game.

The game features actually aren't of any problem to me. I avoided implementing them until i had my level choosing system done. This way i could avoid creating temporary arrays of data in the code and some other reasons.

Quote

If I were you I would prototype everything in isolation; the menu system as one project, the in-game action in another for example. This was my approach when I was learning. This overall picture is too big for you to manage right now.

And i think this is exactly what i am going to do. Thank you for the tip!
Was This Post Helpful? 0
  • +
  • -

#6 jammasterz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-January 13

Re: How to write a nicely structured game

Posted 13 January 2013 - 09:48 AM

Okay, i have read a little bit about MVC and analysed some examples. Im going to create a new project where im going to implement my Title Screen. And i would be greatful if you could tell me if i divided it properly.

Model:
Im going to create a Button class(same as the one i have written before), that has a point, width, height and a label and some getters and setters.

Then im going to implement A menu class that consists of a point, number of buttons, current Button, a List of Buttons, and an optional subMenu.

Controller:
Im going to implement a Menu Controller class that is going to get input from the user and change the current Button accordingly, and (this is a little bit blurry) select current button so that an action like starting the game is taken.

View:
Render class for the menu, that renders each button that is passed to it.
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10771
  • View blog
  • Posts: 40,103
  • Joined: 27-December 08

Re: How to write a nicely structured game

Posted 13 January 2013 - 03:46 PM

The Button class strikes me more of a View component. The Model is the state and data of the game, and the View is the UI.
Was This Post Helpful? 0
  • +
  • -

#8 jammasterz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-January 13

Re: How to write a nicely structured game

Posted 14 January 2013 - 10:59 AM

View Postmacosxnerd101, on 13 January 2013 - 03:46 PM, said:

The Button class strikes me more of a View component. The Model is the state and data of the game, and the View is the UI.

Yes, but i dont see where its more of a view component. Do you mean that a button should only be a graphic representation of its function (like for example opening a submenu)? Is then the menu object also a view component? Then im left with no model components.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1