2 Replies - 33284 Views - Last Post: 14 April 2012 - 08:26 AM

#1 Denis1  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 32
  • View blog
  • Posts: 103
  • Joined: 29-July 09

CodeIgniter Text Based Mafia Game || Intro / Set-up Part 1

Posted 11 April 2012 - 03:12 PM

Most of you are familiar with my ten-part "Make a Mafia Game" tutorials. Thank you for all the support I got from the last set of tutorials, I didn't think it would be much help to people but I was wrong and it was used greatly, people made suggestions and even improved upon it which made me happy to see it being use like that. Those tutorials are a really basic and still a good place to start. When you want to really have a strong structure game with following things:
  • Light weight
  • Easy to configure
  • Exceptional performance
  • Add-ons and Modification made easy
  • Theming with ease
  • Etc etc

The best place i can think of looking is building a framework. Frameworks are great for you when you have many lines of code all with functions and classes etc and you want to be able to reuse then as much as you can wherever possible. The main reason i would suggest a framework to power your game is because of the model–view–controller (MVC) architectural pattern. Which separate the model(database) from View(user interface) and the Controller(library of functions and classes). We touched on this a bit in the last set of tutorials but this time it will be a main focus of these tutorials.

These tutorials take a different approach from the last one, because of that i will not be refereeing to them as much. I am not going to build on them in anyway, this is a fresh start and you don't need to know them in-order to follow these tutorials. The Prerequisite are still the same:-
  • You must know the basic of php and mysql
  • You must have a good idea about hosting and working with database(if working online)

Once you know all this you are ready to begin and once again thank you and I hope you enjoy this series.

While I am working on these tutorials I will also be building my game and the process I take in these tutorial is the process i will be using in my game. So if you are following these tutorials you can see the product. Click here to check it out

Hello, Codeigniter
Like I mention above frameworks are great, with over hundreds of frameworks out there how do you know which one is the best to work with? The simple answer to that is you wouldn't, by that I mean you have to see what each framework has to offer and then choose the one that closely relate with what you want to archive. For example, about 3 years ago just before I finished my last game, around Christmas. I wanted to update the theme to a more festive theme, it took me about 2 to 3 days to update all the game with the new theme (about a day with a framework), this is when I choose to go in to frameworks. After a few experiments with a couple of framework I decided that for me the best frameworks that gave me the best results was codeigniter. So since then I have been remaking my games from ground up with new knowledge and techniques to give me a very powerful output. I am not telling you to choose codeigniter to make your game. In fact you can even make your own framework if you want, just keep in mind that doing that is very hard and if you are new to PHP that's a definitely no no to jump straight in to. So choose a framework and trust me, you will see much better results. This tutorials isn't about frameworks so I won't be going in to them in great details(except codeigniter of course :) )

Intro to Codeigniter
I wont be explaining the awesomeness of codeigniter, if you want to know how great and easy it is to use, do some Google searches and read some other tutorials, and always remember that the Codeigniter User Manual is going to be your best friend.

So enough of the talking and what not. Lets begin, and have some real fun. :)
Part 1 // Setting up codeigniter.
The way i like to set up my work space is like the pic below. This is just how I work, it speed up development so I don't have to switch program on the task bar (please if you have a quicker way of doing things please let me know. I would very much like to know.). I use aptana studios to program must languages( PHP, CSS, HTML, JS and sometimes Ruby).
Posted Image

Right, head over to the codeigniter homepage and download the latest version of codeigniter( At the time of this tutorial it is 2.1.0). Once downloaded extract it to where you will be working. You can now head over Application Flow Chart and see how codeigniter works. I could tell you how it works but then I will just be repeating what's on the user guide.

Once everything is downloaded and extracted in to your working area, just it by going to the index page of where you install it and you should be like the pic below. If you see that without any errors then congratulation. You are ready to start, and if you are new. Welcome to Codeigniter.
Posted Image

Ok, We are going to jump in knee deep. If the above step was successful please navigate in to the application folder and enter config. All the configuration we will be doing in the framework will be in here, but for now all we need is the database.php file so we can connect to the database. Once the file is open you can see that there are comments to explain how to work with the file(Enter your details where the need to go), we will test if the connection is successful later.
Now open the routes.php and read it so you can understand what it does. This is basically going to let us change the main/default controller class(which we will make soon). so scroll down pass the commentary and you will see
$route['default_controller'] = "welcome";


if you look in the Controllers folder you can see the name of the file Welcome.php and in that file there is a class called Welcome, this is the class we are directed to.

Change the name of the Welcome to what ever you want. I changed my to home. so its looks like this. Remember lower cases.
$route['default_controller'] = "home";

Now you can go to the controllers folder and rename the file called welcome.php to what ever you called your default controller(remember lower-cases), then open it up for editing and you should see the following.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {

	 * Index Page for this controller.
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	public function index()

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */


Notice the name of the class is called Welcome (classes are upper-case and file names are lower-case.)I mentions above how MVC play a stonge part in this framework, you can see by the name of the model, views and controller folders, which is nicely organise for us to just put our files

i updated mine to be like this. Removing the comments and renaming the class. Don't worry about the welcome_message for now, we are moving on to that next.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// Changed the name of this calss to be my main class when you first go on my site.
class Home extends CI_Controller {

	public function index()

Part 2 // Theming/ template.
Well done so far so good. The above part is just the set up to get us ready for our templating and theming. Make sure that you are still seeing the welcome page when you test it out on your site.
Head back in to the application folder and enter views folder, this folder is used for design on our site and we will be creating our template here. Rename the welcome_message.php to something like home_view.php.
-Then create a folder called includes.
-Enter the newly created includes folder
-Create 2 files one called index_top.php and the other index_bottom.php (if you followed my last set of tutorials this is my bottom and top files which is for navigation etc).
make sure these files are empty. then head back to the views folder and create a new file called template_view.php and add the following code.





The $main_content variable is used to hold a file (located in the views folder) which we will be requesting to view.

once all this is done head over to the controllers folder and open the home.php file(or whatever you changed it to)
and change the line to load a file from the views folder

$data['main_content'] = 'home_view';
$this->load->view('template_view', $data);

Believe it or not but that's it. That's our basic template layout.
When we call to view a file in codeigniter we use the load->view class and the name of the file.
How the template works.
Firstly we pass the name of the file we want to open in to the $main_content variable.
We then load template_view(.php) from the views folder and pass 'main_content' to it.
The template_view has 3 includes
- includes top
- includes (whatever main_content holds) home_view
- includes bottom.
Then the template_view is displayed and we have out view.
TO test it head in to your views/includes folder and open index_top and index_bottom and write some thing in them like, hello from top, and hello from bottom. and then save it and view it in your browser you should see something like this.
Posted Image

Well done thats all for now. In the next tutorial we are going to make login and register. We are going to build the outside then move in. See you next time. If you see any mistake or any important things missed out please let me know in the comment below or drop me an email and I will get back to you


Is This A Good Question/Topic? 2
  • +

Replies To: CodeIgniter Text Based Mafia Game || Intro / Set-up Part 1

#2 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1254
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: CodeIgniter Text Based Mafia Game || Intro / Set-up Part 1

Posted 13 April 2012 - 11:54 AM

How do you feel Kohana compares to CodeIgniter? I've heard some comparisons before but nothing quite in depth. More importantly, which has the better View Engine?
Was This Post Helpful? 0
  • +
  • -

#3 Denis1  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 32
  • View blog
  • Posts: 103
  • Joined: 29-July 09

Re: CodeIgniter Text Based Mafia Game || Intro / Set-up Part 1

Posted 14 April 2012 - 08:26 AM

View PostSergio Tapia, on 13 April 2012 - 07:54 PM, said:

How do you feel Kohana compares to CodeIgniter? I've heard some comparisons before but nothing quite in depth. More importantly, which has the better View Engine?

When I was deciding my framework of choice, Kohana was in my top 5 list (along with CakePHP, YII, Zend and Codeigniter). I didnt really side by side compare Kohana to codeigniter but I can tell you the advantages and disadvantages between the two and my likes and dislikes.

Starting off with support. When it comes to support in feel that Codeigniter is more supported and documented that Kohana. click here to view the Kohana user guide and documentationand click here to view the codeigniter user guide and see for yourself. This isn't a big deal for me but when I starting learning a new system I'd like to be well inform of the workings of the system that way I can fully understand my way around the system.

When we look at the core design of both framework we can see that codeigniter has the standard MVC design pattern used by most developers and however Kohana uses Hierarchical-Model-View-Controller (HMVC). HMVC is not a bad thing, in fact most developers see it as a way expanding systems can use less resources( as I come to understand it). There is a blog post by one of the developers that work on Kohana explaining the differences between MVC and HMVC. By the looks and explanation of HMVC the main use of it is in complexing and growing system, When I was trying to archive is just a simple system and as far as I can see MVC's done the job well, I know and understand it more( and as the say "stick to your strengths"). There are still things I don't understand about HMVC and i am still learning about the usefulness and because of this issue Codeigniter made sense to me.

Design / layout
Just like support this is mainly based on preference. I find the layout and overall design of codeigniter very simple and because of this it seem like a great place to start learning about frameworks and navigations through files was also simple most of the layout of files is based on the fact that it is MVC instead of HMVC

View Engine
Since I didn't develop a full system in Kohana. I cant tell you much about the view engine in that framework. What I can tell you is that from following a few tutorials I saw how easy it is in to implement a template system. As you can see in the tutorial templating with codeigniter is very simple just the linking of series of files. In future tutorials you will see how easy the rest is to implement.

There is a lot more to the two frameworks than what I have talked about, it is all very fascinating how pages linked can make the structure and foundation for any system to be built upon. I highly recommend that if you are deciding to use a framework that you narrow you choices from look the demonstration and then download them test them out until you find one that best suits you the most. If I am doing a massive project for commercial use Zend will be my framework of choice, which just means that for different tasks there are different frameworks which you can use.

I hope that gave you an overview of the two frameworks from my point of view.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1