[ create a new paste ] login | about

Link: http://codepad.org/clRrwaOw    [ raw code | fork ]

hieunv1996 - C++, pasted on Apr 3:
#include <iostream>
#include <conio.h>
using namespace std;
#include <winbgim.h>
#include <time.h>

#define 	MAXW 		400
#define 	MAXH 		200
#define 	DIRECTION	10

void DrawFood();




struct Point {
	int x,y,x0,y0;
};

bool endGame;
int snakeLength;
Point snake[100];
Point direction;
Point food;
int score = 0;
char* str = new char[20];

void initialize (){
	initwindow (800,400);
	setbkcolor (15);
	cleardevice ();
	endGame = false;
	setfillstyle (1,5);
	bar (0,0,MAXW,5);
	bar (MAXW,0,MAXW-5,MAXH);
	bar (MAXW,MAXH,0,MAXH-5);
	bar (0,0,5,MAXH);
	setcolor (0);
	// Create snake with 3 point
	snake[0].x = 40;snake[0].y = 30;
	snake[1].x = 30;snake[1].y = 30;
	snake[2].x = 20;snake[2].y = 30;
	snakeLength = 3;
	// Direction	
	direction.x = 10;direction.y = 0; // Khoi tao di theo huong trai qua phai
	setcolor (2);//Mau xanh
	//Init food
	food.x = 50;food.y = 50;
}
void DrawPoint (int x,int y){
	circle (x,y,4);
	floodfill (x,y,getcolor());
}
// Snake is moving
void MoveSnake (){
	for (int i = 0;i < snakeLength;i++){
		if (i == 0){
			snake[0].x0 = snake[0].x;snake[0].y0 = snake[0].y;
			snake[0].x += direction.x;
			snake[0].y += direction.y;
		}
		else {
			snake[i].x0 = snake[i].x;snake[i].y0 = snake[i].y;
			snake[i].x = snake[i-1].x0;snake[i].y = snake[i-1].y0;
		}
		if (snake[0].x < 5 || snake[0].y < 5 || snake[0].x > MAXW-5 || snake[0].y > MAXH - 5 )
			endGame = true;
		if (i != 0 && snake[0].x == snake[i].x && snake[0].y == snake[i].y && snakeLength > 3)	 endGame = true;
	}
	if (snake[0].x == food.x && snake[0].y == food.y){
		snake[snakeLength].x = snake[snakeLength-1].x0;snake[snakeLength].y = snake[snakeLength-1].y0;
		snakeLength++;
		srand ( time(NULL));
        food.x = (rand() % (39) + 1)*10;
        food.y = (rand() % (19) + 1)*10;
	}
}

void DrawSnake (){
	setfillstyle (1,10);
	int count = 0;
	for (int i = 0;count < snakeLength;i++){
		DrawPoint(snake[i].x, snake[i].y);
		count++;
	}
	//delete point when snake move
	int x = getcolor();
	setfillstyle (1,15);
	DrawPoint(snake[snakeLength-1].x0,snake[snakeLength-1].y0);
	setcolor (15);
	circle (snake[snakeLength-1].x0,snake[snakeLength-1].y0,4);
	setcolor(x);
	setfillstyle (1,5);
}

void DrawFood (){
	int x = getcolor ();
	setcolor (1);
	DrawPoint(food.x, food.y);
	//outtextxy (food.x,food.y,"+");
	setcolor (x);
}

//Ve toan bo giao dien game
void DrawGame (){
	DrawSnake();
	DrawFood();
	int x = getcolor ();
	setcolor (14);
	sprintf(str,"%d",snakeLength*100-300);    //hien thi ra diem choi
    settextstyle(4,0,3);
    outtextxy(450,20,"SCORE:");
    setcolor(12);
    outtextxy(600,20,str);
    setcolor (x);
}

void MainLoop (){
	if (!kbhit()){
		MoveSnake();
	}else {
		char ch;
        ch = getch();
            ch = getch();
            switch(ch){
                case 72: 
                    if (direction.y != DIRECTION) {
            			direction.y = -DIRECTION; direction.x = 0;
        			}
                    break;
                case 80: 
                	if (direction.y != -DIRECTION) {
            			direction.y = DIRECTION; direction.x = 0;
        			}
                   break;
                case 77:
                    if (direction.x != -DIRECTION) {
            			direction.x = DIRECTION; direction.y = 0;
        			}
                    break;
                case 75:
                    if (direction.x != DIRECTION) {
            			direction.x = -DIRECTION; direction.y = 0;
        			}
                    break;
                case 32:
                	endGame = true;
					break;
            }
        }
}

int main (){
	
	initialize();
	while (!endGame){
		delay (100);
		MainLoop();
		DrawGame();
		
	}
	getch();
	closegraph ();
    return 0;
}


Create a new paste based on this one


Comments: