[ create a new paste ] login | about

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

C, pasted on Jun 20:
#include<iostream>
#include<time.h>
#include"Util.h"

using namespace std;

// Trước tiên ta tạo một kiểu dữ liệu tên Pos2D lưu giữ tọa độ của 1 ô
// Con rắn là tập hợp của nhiều ô Pos2D

// Đầu tiên, ta khai báo struct Pos2D
struct Pos2D
{
	int x, y;  // Tọa độ hiện tại
	int ox, oy;  // Tọa độ trước đó
};

int boardW, boardH; // Kích thước của màn hình game
Pos2D snake[100]; // Mảng các ô của con rắn, tối đa dài 100 ô
int snakeLength; // Độ dài mảng snake đó
Pos2D direction; // Hướng di chuyển
bool endGame; // Biến kiểm tra kết thúc game

//Để biểu diễn thức ăn (food), chúng ta cần một biến kiểu Pos2D cho biết vị trí cục Food trên màn hình.
// đồ ăn đây
Pos2D food;
int tickSpeed;

// Hàm khởi tạo giá trị ban đầu
void init()
	// khởi tạo một số giá trị ban đầu
{
	endGame = false;
	 tickSpeed = 10;
	boardW  = 50; boardH = 20;
	// Khởi tạo con rắn gồm có 5 ô
	snake[0].x = 5;  snake[0].y = 5;  // độ dài nằm ngang của con rắn là năm ô
	snake[1].x = 4;  snake[1].y = 5;  // con rắn nằm ngang ở tung độ 5
	snake[2].x = 3;  snake[2].y = 5;
	snake[3].x = 2;  snake[3].y = 5;
	snake[4].x = 1;  snake[4].y = 5;
	snakeLength = 5;
	// hướng di chuyển ban đầu là đi xuống
	direction.x = 0; direction.y = 1;
	// Đặt màu nền cho màn hình
	SetBGColor(8);
	for(int i = 0; i < boardW; i++)  // Duyệt dòng
	{
		for(int j = 0; j < boardH; j++)
		{
			gotoxy(i, j);
            printf(" ");
		}
	}
	 // đặt màu chữ
    SetColor(14);
	// vị trí ban đầu cho food
    food.x = 25; food.y = 1;
}
void moveSnake(Pos2D dir)  // Biến dir để chỉ định hướng di chuyển của con rắn
{
	for(int i = 0; i < snakeLength; i++)
	{
		if(i == 0)
		{
			// Di chuyển đầu rắn
			snake[0].ox = snake[0].x; snake[0].oy = snake[0].y;  //  Lưu lại vị trí cũ của Snake[0], 
			                                                     // nhằm mục đích di chuyển phần thân rắn.

			// để di chuyển con rắn, ta di chuyển phần đầu của con rắn (tức là ô Snake[0]) bằng cộng giá trị của Snake[0] với biến dir
			snake[0].x += dir.x; snake[0].y += dir.y;  
		}
		else
		{
			// di chuyển phần thân rắn
			/*
			Ô sau sẽ lấy giá trị của ô trước, 
			đồng thời trước đó ta cũng phải lưu lại vị trí cũ vào biến ox và oy của từng Snake[i] như đã làm với Snake[0].
			*/
			snake[i].ox = snake[i].x; snake[i].oy = snake[i].y;  //Lưu lại vị trí cũ của Snake[i]
			snake[i].x = snake[i - 1].ox; snake[i].y = snake[i - 1].oy;  // Gán giá trị của ô sau cho ô trước
		}
		// kiểm tra coi con rắn có cắn trúng nó không
		if(i != 0 && (snake[0].x == snake[i].x && snake[0].y == snake[i].y)) // Với độ dài khác 0 thì khi đầu con rắn đụng vào
			endGame = true;                                                   // bất kỳ chỗ nào ở thân nó thì kết thúc game

		 // khi rắn vượt ra khỏi màn hình thì cho nó xuất hiện lại
        if (snake[i].x >= boardW) snake[i].x = 0; // Nếu rắn vượt qua biên phải thì xuất hiện lại ở đầu biên trái
        if (snake[i].x < 0) snake[i].x = boardW - 1;  // Nếu rắn vượt qua biên trái thì xuất hiện lại ở biên phải
        if (snake[i].y >= boardH) snake[i].y = 0;
        if (snake[i].y < 0) snake[i].y = boardH - 1;

		 

		// kiểm tra coi con rắn có ăn trúng food chưa
    if (snake[0].x == food.x && snake[0].y == food.y) {
        snake[snakeLength].x = snake[snakeLength - 1].ox; snake[snakeLength].y = snake[snakeLength - 1].oy;
        snakeLength++;
        srand 
( time(NULL) );
        food.x = rand() % boardW; 
        food.y = rand() % boardH;
        if (tickSpeed > 5)
            tickSpeed -= 5;
    }
		
	}
}
// Vẽ thức ăn
void drawFood() {
    // dọn đồ ăn ra nào
    SetColor(12);
    gotoxy(food.x, food.y);
    printf("%c", 2);
}
// Hàm vẽ con rắn lên màn hình
void drawSnake()
{
	 SetColor(14);
	  for (int i = 0; i < snakeLength; i++)
	  {
		  gotoxy(snake[i].x, snake[i].y);
		  printf("%c", 4); // dùng kí tự hình kim cương để vẽ con rắn
	  }
	   gotoxy(snake[snakeLength-1].ox, snake[snakeLength-1].oy);
    printf(" "); // xóa phần đuôi trước đó của nó
}
void drawgame() {
	drawFood();
    drawSnake();

	SetColor(10);
    gotoxy(0, 0);
    printf("Diem: %d", snakeLength*100);
}

void mainloop() {
    moveSnake(direction);
 
    if (checkKey(KEY_LEFT)) {
        if (direction.x != 1) {
            direction.x = -1; direction.y = 0;
        }
    }
    else if (checkKey(KEY_RIGHT)) {
        if (direction.x != -1) {
            direction.x = 1; direction.y = 0;
        }
    }
    else if (checkKey(KEY_UP)) {
        if (direction.y != 1) {
            direction.y = -1; direction.x = 0;
        }
    }
    else if (checkKey(KEY_DOWN)) {
        if (direction.y != -1) {
            direction.y = 1; direction.x = 0;
        }
    }
 
    if (checkKey(KEY_ESC)) {
        endGame = true;
    }
}
int main() {
    init();
    ShowCur(false); // Ẩn con trỏ
    while (!endGame){
		Tick(tickSpeed, mainloop, drawgame); // Mainloop
    }
    return 0;
}


Output:
Line 18: error: iostream: No such file or directory
Line 16: error: Util.h: No such file or directory
Line 5: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'namespace'
Line 18: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'snake'
Line 20: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'direction'
Line 21: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'endGame'
Line 25: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'food'
In function 'init':
Line 32: error: 'endGame' undeclared (first use in this function)
Line 32: error: (Each undeclared identifier is reported only once
Line 32: error: for each function it appears in.)
Line 32: error: 'false' undeclared (first use in this function)
Line 36: error: 'snake' undeclared (first use in this function)
Line 43: error: 'direction' undeclared (first use in this function)
Line 46: error: 'for' loop initial declaration used outside C99 mode
Line 48: error: 'for' loop initial declaration used outside C99 mode
Line 57: error: 'food' undeclared (first use in this function)
t.c: At top level:
Line 59: error: expected ')' before 'dir'
In function 'drawFood':
Line 112: error: 'food' undeclared (first use in this function)
In function 'drawSnake':
Line 119: error: 'for' loop initial declaration used outside C99 mode
Line 121: error: 'snake' undeclared (first use in this function)
In function 'mainloop':
Line 137: error: 'direction' undeclared (first use in this function)
Line 139: error: 'KEY_LEFT' undeclared (first use in this function)
Line 144: error: 'KEY_RIGHT' undeclared (first use in this function)
Line 149: error: 'KEY_UP' undeclared (first use in this function)
Line 154: error: 'KEY_DOWN' undeclared (first use in this function)
Line 160: error: 'KEY_ESC' undeclared (first use in this function)
Line 161: error: 'endGame' undeclared (first use in this function)
Line 161: error: 'true' undeclared (first use in this function)
In function 'main':
Line 166: error: 'false' undeclared (first use in this function)
Line 167: error: 'endGame' undeclared (first use in this function)


Create a new paste based on this one


Comments: