Snake

#include <iostream> #include <time.h> #include <conio.h> #include <string> #include <Windows.h> #include "console.h" #define consoleWidth 85; #define consoleHeight 25; using namespace std; enum TrangThai { UP, DOWN, LEFT, RIGHT }; typedef struct { int x, y; }ToaDo; typedef struct { ToaDo dot[31]; int n; // so dot <=> chieu dai ran TrangThai tt; }Snake; void khoitao(Snake &snake) { snake.n = 1; snake.tt = RIGHT; snake.dot[0].x = 0; snake.dot[0].y = 0; } void hienthi(Snake snake) { clrscr(); for (int i = 0;i < snake.n;i++) { gotoXY(snake.dot[i].x, snake.dot[i].y); cout << "*"; } } void dieukhien_dichuyen(Snake &snake) { //Di chuyen phan than, theo sau phan dau for (int i = snake.n - 1;i > 0;i--) snake.dot[i] = snake.dot[i - 1]; // Dieu khien phan dau if (_kbhit) { int key=_getch(); if (key == 'A' || key == 'a') snake.tt = LEFT; else if (key == 'D' || key == 'd') snake.tt = RIGHT; else if (key == 'W' || key == 'w') snake.tt = UP; else if (key == 'S' || key == 's') snake.tt = DOWN; } if (snake.tt == DOWN) snake.dot[0].y++; else if (snake.tt == UP) snake.dot[0].y--; else if (snake.tt == RIGHT) snake.dot[0].x++; else if (snake.tt == LEFT) snake.dot[0].x--; } int main() { Snake snake; khoitao(snake); while (1) { hienthi(snake); dieukhien_dichuyen(snake); Sleep(200); } return 0; }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.