Thread: 2d game

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    43

    2d game

    does any one have a 2d game with the source code and all files so I can take a look at it to understand how it works
    I’m Dyslexic and I know that I don’t spell well so quit telling to learn my English because I do my best at it all right.

    Windows XP with Dev-C++ for now.

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    sourceforge.net
    freshmeat.net

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    You don't want much do you?

  4. #4
    Registered User
    Join Date
    Nov 2007
    Location
    Free Country, USA
    Posts
    105
    Look at the "Post your games here..." thread. It has lost of board-member submitted games. Although some of the links are broken, several 2d graphics games have been submitted.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > so I can take a look at it to understand how it works
    This is a bit like looking at someone else's weeds in order to learn how to garden
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    Here is the source code of the simple console blackjack game I'm working on right now, it's pretty incomplete right now but this basically will show all my current systems working together.

    Code:
    #ifndef GAME_H
    #define GAME_H
    #include "Dealer.h"
    #include <vector>
    #include <iostream>
    #include "IO.H"
    #include "Player.h"
    class Game
    {
    public:
    
    	Game()
    	{
    		init_players(1);
    	}
    
    	void play_game()
    	{
    		dealer.shuffle_deck(deck);
    		dealer.draw(deck, 1);
    		players[0].draw(deck, 2);
    		int gameloop = 1;
    		io.init_output(tmenu);
    		while(gameloop != 0)
    		{
    			
    			io.update_output(2, dealer.hand->read_cards());
    			io.update_output(6, players[0].hand->read_cards());
    			io.display_output();
    			gameloop = blackjack(io.get_input()); 
    			io.clear_screen();
    		}	
    		
    	}
    
    private:
    	int blackjack(int result)
    	{
    		if (result == 1)
    		{
    			players[0].draw(deck, 1);
    			if (get_hand_value(players[0].hand->card_list) > 21)
    			{
    				return 0;
    			}
    			else { return 1; }
    		}
    		else if (result == 2)
    		{
    			if(get_hand_value(players[0].hand->card_list ) > get_hand_value(dealer.hand->card_list))  
    			{
    				return 0;
    			}
    			else { return 0; }
    		}
    		else if (result == 3)
    		{	
    			return 0;
    		}
    		return 1;
    	}
    
    
    	 int get_hand_value(vector<Card> hand)
    	 {
    		 sum = 0;
    		 for (unsigned int loop = 0; loop < hand.size(); loop++)
    		 {
    			 sum += hand[loop].read_numeric_data();
    		 }
    		 return sum;
    	 }
    
    	 void init_players(unsigned int number)
    	 {
    		 for (unsigned int loop = 0; loop < number; loop++)
    		 {
    			 Player player;
    			 //player id will increment with the number of players
    			 player.playerid = loop + 1;		
    			 players.push_back(player);
    		 }
    
    	 }
    	 int sum;
    	 Dealer dealer;
    	 Deck deck;
    	 text_menu tmenu;
    	 IO io;
    	 vector<Player> players;
    
    };
    #endif
    Code:
    // This header file declares the deck class and all of its functions,
    // as well as the card structure.
    #ifndef DECK_H
    #define DECK_H
    #include <vector>
    #include <stdio.h>
    #include <stdlib.h>
    #include <algorithm>
    #include <cassert>
    #include "Data.h"
    
    using namespace std;
    
    class Card : public Data
    {
    public:
    	Card(int suit, int value)
    	{
    		initialize();
    		text_data.erase();
    		text_data.append(values[value]);
    		text_data.append(" of ");
    		text_data.append(suits[suit]);
    		numeric_data = points[value];
    	}
    
    private:
    
    	void initialize()
    	{
    		add_to_string_lookup(suits, "Clubs");
    		add_to_string_lookup(suits, "Diamonds");
    		add_to_string_lookup(suits, "Hearts");
    		add_to_string_lookup(suits, "Spades");
    		add_to_string_lookup(values, "Two");
    		add_to_string_lookup(values, "Three");
    		add_to_string_lookup(values, "Four");
    		add_to_string_lookup(values, "Five");
    		add_to_string_lookup(values, "Six");
    		add_to_string_lookup(values, "Seven");
    		add_to_string_lookup(values, "Eight");
    		add_to_string_lookup(values, "Nine");
    		add_to_string_lookup(values, "Ten");
    		add_to_string_lookup(values, "Jack");
    		add_to_string_lookup(values, "Queen");
    		add_to_string_lookup(values, "King");
    		add_to_string_lookup(values, "Ace");
    		add_to_int_lookup(points, 2);
    		add_to_int_lookup(points, 3);
    		add_to_int_lookup(points, 4);
    		add_to_int_lookup(points, 5);
    		add_to_int_lookup(points, 6);
    		add_to_int_lookup(points, 7);
    		add_to_int_lookup(points, 8);
    		add_to_int_lookup(points, 9);
    		add_to_int_lookup(points, 10);
    		add_to_int_lookup(points, 10);
    		add_to_int_lookup(points, 10);
    		add_to_int_lookup(points, 10);
    		add_to_int_lookup(points, 11);
    
    	}	
    
    	string_lookup suits;
    	string_lookup values;
    	int_lookup points;
    	int Suit;
    	int Value;
    };
    
    class Deck
    {
    public:	
    
    	Deck()
    	{
    		unsigned int x = 0;
    		unsigned int y = 0;
    		unsigned int z = 0;
    			for (y = 0; y < 4;  y++)
    			{
    				
    				for (x = 0; x < 13; x++)
    				{
    					Card c(y,x);
    					cards.push_back(c);
    				}
    			}
    			assert(cards.size() == 52);
    	}
    	vector<Card> cards;
    };
    #endif
    Code:
    #ifndef DATA_H
    #define DATA_H
    #include <string>
    #include <vector>
    
    class Data // this is the class that will define how we output certain data sets on the screen
    {
    public:
    	typedef std::vector<std::string> string_lookup;
    	typedef std::vector<int> int_lookup;
    	// everytime we create a class that is derived from Data it will run this function
    	Data()
    	{
    		initialize();
    	}
    
    	void add_to_string_lookup(string_lookup & string_table, std::string word)
    	{
    		string_table.push_back(word);
    	}
    
    	void add_to_int_lookup(int_lookup & int_table, int value)
    	{
    		int_table.push_back(value);
    	}
    
    	virtual void initialize(){}// function to initialize lookups.
    	int & read_numeric_data(){return numeric_data;} // function to output numeric data
    	std::string & read_string_data(){return text_data;} // function to output string data
    	std::string text_data;
    	int numeric_data;
    
    };
    #endif
    Code:
    #ifndef IO_H
    #define IO_H
    #include <iostream>
    #include <string>
    #include <map>
    #include <string>
    
    struct text_menu
    {
    	typedef std::vector<std::string> tmenu;
    	text_menu()			//Construct Blackjack text interface
    	{
    		std::string output;		//declare the actual output item
    		output = "Dealer's Cards";
    		text.push_back(output);
    		output = "--------------";
    		text.push_back(output);
    		output = "Nothing Here.";
    		text.push_back(output);
    		output = "--------------";
    		text.push_back(output);
    		output = "Player's Cards";
    		text.push_back(output);
    		output = "--------------";
    		text.push_back(output);
    		output = "Nothing Here.";
    		text.push_back(output);
    		output = "--------------";
    		text.push_back(output);
    		output = "What would you like to do?\n1) Hit me\n2) Stay\n3) Fold\n";
    		text.push_back(output);
    	}
    	tmenu text;
    };
    
    class IO
    {
    public:
    	typedef	std::vector<std::string> output;
    	
    	void update_output(int id, std::string & update)
    	{
    		coutput[id] = update;
    	}
    
    	void init_output(text_menu tmenu)
    	{
    		coutput = tmenu.text;
    	}
    
    	void display_output()
    	{
    		for (int index = 0; index < coutput.size(); index++)
    		{
    			cout << coutput[index] << std::endl;
    		}
    	}
    	int get_input()
    	{
    		std::cin >> input;
    		return input;
    	}
    	void clear_screen()
    	{
    		system("cls");
    	}
    	output coutput;
    	int input;
    	
    };
    #endif
    
    //every time some output is updated we rebuild the output vector
    Code:
    #ifndef PLAYER_H
    #define PLAYER_H
    #include "Data.h"
    #include "Deck.h"
    #include "IO.H"
    class Hand
    {
    public:
    	Hand()
    	{
    	}
    
    	string & read_cards()
    	{
    		show_hand.erase();
    		for(unsigned int loop = 0; loop < card_list.size(); loop++)
    		{
    			show_hand.append(card_list[loop].read_string_data());
    			show_hand.append("	");
    		}
    		show_hand.append("\n");
    		return show_hand;
    	}
    
    	vector<Card> card_list;	
    	friend class Dealer;
    	friend class Game;
    
    	string show_hand;
    
    };
    
    class Player
    {
    public:
    	Player()
    	{
    		hand = new Hand;
    	}
    
    	void draw(Deck & deck, int number)
    	{
    		for(int loop = 0; loop < number; loop++)
    		{
    			hand->card_list.push_back(deck.cards.back());
    			deck.cards.pop_back();
    		}
    	}
    
    	int playerid;
    	Hand * hand;	
    };
    
    
    #endif
    Code:
    #ifndef DEALER_H
    #define DEALER_H
    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream>
    #include "Deck.h"
    #include "Player.h"
    
    class Dealer
    {
    public:
    
    	Dealer()
    	{
    		hand = new Hand;
     		playerid = 0;
    	}
    	void shuffle_deck(Deck & deck)
    	{
    		random_shuffle(deck.cards.begin(), deck.cards.end());
    	} 
    
    	void draw(Deck & deck, int number)
    	{
    		for(int loop = 0; loop < number; loop++)
    		{
    			hand->card_list.push_back(deck.cards.back());
    			deck.cards.pop_back();
    		}
    	}
    
    	Hand * hand;
    	int playerid;
    	friend class Game;
    };
    #endif




    Here is how I implement all this in main:
    Code:
    // Blackjack.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <time.h>
    #include "Game.h"
    int _tmain(int argc, _TCHAR* argv[])
    {
    	srand ( time(NULL) );
    	Game game;
    	game.play_game();
    	return 0;
    }
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need book to program game into multiplayer...
    By edomingox in forum Game Programming
    Replies: 3
    Last Post: 10-02-2008, 09:26 AM
  2. 2D RPG Online Game Project. 30% Complete. To be released and marketed.
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 10-28-2006, 12:48 AM
  3. Game Programmer's AIM Circle: Join Today
    By KingZoolerius66 in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 12-20-2003, 12:12 PM
  4. 2d game ideas
    By BeholderOf in forum Game Programming
    Replies: 5
    Last Post: 10-27-2003, 06:06 PM
  5. how to make a racing 2d game
    By juandy in forum Game Programming
    Replies: 3
    Last Post: 10-28-2001, 09:47 PM