cuberz.com

Kategori: C

Protected: [Kod] C++ : Catatan pembelian kereta

Komen Enter your password to view comments.
Penulis M450K1S, 06/08/2010 1:24 AM

This post is password protected. To view it please enter your password below:


[Kod] C++ : Snake Game / Oleh : BlueMelon

Penulis M450K1S, 15/07/2010 3:56 AM

C++ SNAKE GAME

/*
Snake
Author: BlueMelon

bluemelon.info
*/

#include <iostream>
#include <windows.h>
#include <list>

char Map[20][60] = {"###########################################################",
                    "#                                                         #",
                    "#                                                         #",
                    "#                                                         #",
                    "#                                                         #",
                    "#                                                         #",
                    "#                                                         #",
                    "#                                                         #",
                    "#                                                         #",
                    "#                                                         #",
                    "#                                                         #",
                    "#                                                         #",
                    "#                                                         #",
                    "#                                                         #",
                    "#                                                         #",
                    "#                                                         #",
                    "#                                                         #",
                    "#                                                         #",
                    "#                                                         #",
                    "###########################################################"};

/* -=- Fruit Spawn -=- */
bool Fruit = true;
/* -=- Interval -=- */
unsigned short MovementInterval = 1; // 1 millisecond = 0.001 seconds
/* -=- Player Score -=- */
unsigned int PScore = 0;
/* -=- Functions -=- */
void gotoxy(unsigned short x, unsigned short y);
void DrawMap();
void DropFruit();
void AddScore(int points);

/* -=- Key Press -=- */
#define RIGHT 0
#define LEFT 1
#define DOWN 2
#define UP    3

class Snake
{

private:
bool Dead;
unsigned short Dir;
unsigned short Hx,Hy;
std::list<unsigned int> Lx,Ly;

public:
    void SetDirection(unsigned short direction){Dir = direction;}
    void HandleKeys();
    void SnakeMove();
    bool CheckDead(){return Dead;};
    void Grow();
    void SetSnake();
};
void Snake::Grow()
{
    Lx.push_front(Lx.front());
    Ly.push_front(Ly.front());
    return;
}
void Snake::SnakeMove()
{
    if(Dir == RIGHT){

        if(Map[Hy][Hx+1] == '#' || Map[Hy][Hx+1] == 'o') //Collision
        {
            Dead = true;
        }else
        {
            if(Map[Hy][Hx+1] == '*') // Fruit
            {
                Fruit = true;
                AddScore(100);
            }
            Hx++;
        }

    }else if(Dir == LEFT){

        if(Map[Hy][Hx-1] == '#' || Map[Hy][Hx-1] == 'o')
        {
            Dead = true;
        }else
        {
            if(Map[Hy][Hx-1] == '*')
            {
                Fruit = true;
                AddScore(100);
            }
            Hx--;
        }

    }else if(Dir == DOWN){

        if(Map[Hy+1][Hx] == '#' || Map[Hy+1][Hx] == 'o')
        {
            Dead = true;
        }else
        {
            if(Map[Hy+1][Hx] == '*')
            {
                Fruit = true;
                AddScore(100);
            }
            Hy++;
        }

    }
    else if(Dir == UP){

        if(Map[Hy-1][Hx] == '#' || Map[Hy-1][Hx] == 'o')
        {
            Dead = true;
        }else
        {
            if(Map[Hy-1][Hx] == '*')
            {
                Fruit = true;
                AddScore(100);
            }
            Hy--;
        }
    }

    if(Dead != true)
    {
        if(Fruit == true)
        {
            Grow();
        }

        Ly.push_back(Hy);
        Lx.push_back(Hx);

        Map[Ly.back()][Lx.back()] = 'o';
        Map[Ly.front()][Lx.front()] = ' ';

        Ly.pop_front();
        Lx.pop_front();

        gotoxy(0,0);
        DrawMap();
    }

    return;
}
void Snake::HandleKeys()
{

    if(GetAsyncKeyState(VK_RIGHT) != 0)
        {
            if(Dir != LEFT)
            SetDirection(RIGHT);
        }
        else if(GetAsyncKeyState(VK_UP) != 0)
        {
            if(Dir != DOWN)
            SetDirection(UP);
        }
        else if(GetAsyncKeyState(VK_DOWN) != 0)
        {
            if(Dir != UP)
            SetDirection(DOWN);
        }
        else if(GetAsyncKeyState(VK_LEFT) != 0)
        {
            if(Dir != RIGHT)
            SetDirection(LEFT);
        }
    return;
}
void Snake::SetSnake()
{
    Hx = 10;
    Hy = 15;
    Lx.push_back(8);
    Lx.push_back(9);
    Lx.push_back(10);

    Ly.push_back(15);
    Ly.push_back(15);
    Ly.push_back(15);

    Map[Ly.back()][Lx.back()] = 'o';
    Map[Ly.back()][Lx.back()-1] = 'o';
    Map[Ly.back()][Lx.back()-2] = 'o';
    return;
}

int main()
{
    SetConsoleTitleA("BlueMelon's Snake");
    Snake PSnake;

    PSnake.SetSnake ();
    PSnake.SetDirection(RIGHT);

    while(PSnake.CheckDead() != true)
    {
        Sleep(MovementInterval);
        if(Fruit == true)
        {
            DropFruit();
        }

        PSnake.HandleKeys();
        PSnake.SnakeMove();

            if(PSnake.CheckDead() == true)
            {
                using namespace std;
                system("cls"); // This is only a one time thing, do not use this on a loop, bad practice.
                cout << "Game Over!\n";
                cout << "Press \'Return\' Key to Continue...";
                cin.get();
                return 0;
            }

    }

    return 0;
}

void DropFruit()
{
    srand(GetTickCount());
    unsigned short Fruitx = rand() % 57 +1;
    unsigned short Fruity = rand() % 18 +1;

    if(Map[Fruity][Fruitx] == 'o') // Check if its going to drop on snake
    {
        DropFruit();
    }else
    {
        Map[Fruity][Fruitx] = '*';
    }

    Fruit = false;
    return;
}

void gotoxy(unsigned short x, unsigned short y)
{
    static HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    static COORD WritePos;
    WritePos.X = x;
    WritePos.Y = y;
    SetConsoleCursorPosition(hStdOut,WritePos);
}

void DrawMap()
{
    using std::cout;
    for(unsigned short rows=0; rows < 20 ; rows++)
                cout << Map[rows] << "\n";

    //Menu Points etc.
    cout << "#                                                         #\n";
    cout << "#                                                         #\n";
    cout << "#                                                         #\n";;
    cout << "###########################################################";
    gotoxy(46,22);
    cout << "Score: " << PScore;
	gotoxy(5,25);
	cout << "<-- BY : BlueMelon | DOWNLOAD : MASOKIS.COM -->";
    return;
}

void AddScore(int points)
{
    PScore += points;
    return ;
}

Pesanan :
>> Kalau yang nak guna kod ni, jaranglah buang kreadit tu. Letih-letih si Bluemelon ni tulis.
>> Bak kata mat salleh “Give a nice support to Bluemelon”. Orait..Wassalam..

[Kod] C++ : Struct untuk mengira hari dalam tahun

Penulis M450K1S, 14/07/2010 1:19 PM

Assalamulaikum & salam sejahtera..
Hari ni nak berkongsi kod aturcara C++. Menggunakan struktur (structure). Tapi dipadatkan dengan sedikit fungsi. Kod ni takde kat mana-mana sumber internet. masokis buat ni semasa belajar, buat latihan untuk “algorithm and data structure”. Ringkas je.. bleh kembangkan lagi ni..

// Oleh    : MASOKIS
// Tarikh  :14.07.2010
// Tajuk   : Struct untuk mengira hari dalam tahun
#include<iostream.h>
//declare structure
struct dte
{
	int date;
	int month;
	int year;
};

//prototype function..muahaahaaa
struct dte readDate();

int calcDays(struct dte);
int daysInMonth (int, int);

void main ()
{
	struct dte tkh;
	int bil;
	tkh = readDate();
	bil = calcDays(tkh);
	cout<<tkh.date<<"/"<<tkh.month<<"/"<<tkh.year<<"is the"<<bil<<"days on year"<<tkh.year<<endl;
}

struct dte readDate()
{
	struct dte t;
	cout<<"Enter dat with format dd/mm/yyyy : ";
	cin>>t.date>>t.month>>t.year;
	return t;
}

int calcDays(struct dte t)
{
	int n=0,i;
	for(i=1;i<t.month;i++)
		n+=daysInMonth(i, t.year);
	n+=t.date;
	return n;
}

int daysInMonth(int month, int yr)
{
	switch (month)
	{
	case 2:if (yr %4==0)
			   return 29;
		else
			return 28;
	case 4: case 6: case 9: case 11: return 30;

	default: return 31;
	}
}

//P/S : mauaahahahhhahaaaaa..... xde kerja :P

Orait..C++ memang r0x! Wassalam…

[C++] Kod : Latihan Algoritma

Penulis M450K1S, 12/07/2010 2:00 AM

Assalamulaikum, hari ni tak ada apa nak update. Cuma share snippet sendiri. Pasal kalau masukkan kat pastebin, susah nak trace. Jadi masokis conteng la kat blog ni. xpe kan? Nama pun programmer.. serabut sikit. Jangan marah… :D
Teruskan membaca '[C++] Kod : Latihan Algoritma'»


HALAMAN 11

Tema yang diubahsuai oleh MASOKIS.COM