Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h>
- #include <cstdlib>
- #include <conio.h>
- using namespace std;
- char **board;
- int width = 32, height = 16;
- typedef struct SnakePiece {
- int x;
- int y;
- SnakePiece *next;
- } SnakePiece;
- SnakePiece *head = NULL;
- int headx, heady,
- tailx, taily,
- len;
- int x_increment, y_increment;
- void initialize_board () {
- //printf ("Height: ");
- //scanf ("%d", &height);
- //printf ("Width: ");
- //scanf("%d", &width);
- board = (char **) malloc (sizeof(char *) * height);
- for (int i = 0; i < height; i++) {
- board[i] = (char *) malloc (sizeof(char) * width);
- }
- for (int i = 0; i < height; i++) {
- for (int j = 0; j < width; j++) {
- board[i][j] = ' ';
- }
- }
- for (int i = 0; i < width; i++) {
- board[height-1][i] = '#';
- board[0][i] = '#';
- }
- for (int i = 0; i < height; i++) {
- board[i][width-1] = '#';
- board[i][0] = '#';
- }
- }
- void show_board () {
- for (int i = 0; i < height; i++) {
- for (int j = 0; j < width; j++) {
- printf ("%c", board[i][j]);
- }
- printf ("\n");
- }
- }
- void add_piece (int x_data, int y_data) {
- SnakePiece *new_piece = (SnakePiece *) malloc (sizeof(SnakePiece));
- new_piece->x = x_data;
- new_piece->y = y_data;
- tailx = x_data;
- taily = y_data;
- new_piece->next = head;
- head = new_piece;
- len++;
- }
- void initialize_snake () {
- headx = tailx = height/2-1;
- heady = taily = width/2-1;
- add_piece (headx, heady);
- for (int i = 0; i < 3; i++) {
- add_piece (tailx+1, taily);
- }
- }
- void move_snake () {
- SnakePiece *ptr;
- ptr = head;
- for (int i = 0; i < len; i++) {
- board[ptr->x][ptr->y] = ' ';
- ptr = ptr->next;
- }
- ptr = head;
- int old_x, old_y;
- for (int i = 0; i < len; i++) {
- if (i == len-1) {
- old_x = ptr->x;
- old_y = ptr->y;
- if (x_increment == -1 && (headx + x_increment) == 0) {
- ptr->x = height-2;
- headx = ptr->x;
- } else if (x_increment == 1 && (headx + x_increment) == height-1) {
- ptr->x = 1;
- headx = ptr->x;
- } else if (y_increment == -1 && (heady + y_increment) == 0) {
- ptr->y = width-2;
- heady = ptr->y;
- } else if (y_increment == 1 && (heady + y_increment) == width-1) {
- ptr->y = 1;
- heady = ptr->y;
- } else {
- ptr->x += x_increment;
- ptr->y += y_increment;
- headx = ptr->x;
- heady = ptr->y;
- break;
- }
- }
- ptr = ptr->next;
- }
- ptr = head;
- int old_x2, old_y2;
- for (int i = 0; i < len-1; i++) {
- for (int j = 0; j < (len-1)-i; j++) {
- if (j == (len-2)-i) {
- old_x2 = ptr->x;
- old_y2 = ptr->y;
- ptr->x = old_x;
- ptr->y = old_y;
- old_x = old_x2;
- old_y = old_y2;
- }
- ptr = ptr->next;
- }
- ptr = head;
- }
- tailx = ptr->x;
- taily = ptr->y;
- }
- void convert_move (char c) {
- if (c == 'w') {
- x_increment = -1;
- y_increment = 0;
- } else if (c == 's') {
- x_increment = 1;
- y_increment = 0;
- } else if (c == 'a') {
- x_increment = 0;
- y_increment = -1;
- } else if (c == 'd') {
- x_increment = 0;
- y_increment = 1;
- }
- }
- void show_coords() {
- SnakePiece *ptr;
- ptr = head;
- while (ptr) {
- printf ("%d %d\n", ptr->x, ptr->y);
- ptr = ptr->next;
- }
- /*for (int i = 0; i < len; i++) {
- for (int j = 0; j < len-i; j++) {
- if (j == (len-1)-i) {
- printf ("%d %d\n", ptr->x, ptr->y);
- }
- ptr = ptr->next;
- }
- ptr = head;
- }*/
- printf ("\n");
- }
- void draw_snake () {
- SnakePiece *ptr;
- ptr = head;
- int x, y;
- for (int i = 0; i < len; i++) {
- x = ptr->x;
- y = ptr->y;
- board[x][y] = 'o';
- ptr = ptr->next;
- }
- board[headx][heady] = '@';
- }
- int main()
- {
- initialize_board();
- initialize_snake();
- char c = 'w';
- convert_move(c);
- while (true) {
- if (_kbhit()) {
- c = _getch();
- convert_move(c);
- }
- move_snake();
- draw_snake();
- show_board();
- system("cls");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement