首页 > 解决方案 > 蛇与自我和苹果的碰撞

问题描述

因此,对于我的班级,我需要将此蛇游戏编程为项目的一部分,我和我的搭档遇到了一个错误,我们的代码不会与自身或苹果发生冲突。因此,例如,如果我向上,然后按下它会自行反转,我们不太确定如何解决这个问题。我们有一个 if 语句来测试是否蛇[1].y-1 = snake[0].y,我们想知道为什么当我们的语句与蛇碰撞时苹果不移动。

#include <stdio.h>
#include <stdlib.h>
#include <ncurses.h>

//Defines the rate at which the world refreshes
#define TICKRATE 100
//Defines the width of the world
#define WIDTH 50
//Defines the height of the world
#define HEIGHT 20
//Defines the length of the snake
#define SNAKE_LNGTH 10

//Defines an enum with the directions
enum direction { UP, DOWN, RIGHT, LEFT };

//Defines the structure "Snake Part" for the parts of the snake
typedef struct spart {
    int x;
    int y;
} snakepart;

//Initalizes the function for moving the snake
int move_snake(WINDOW *win, int direction,
                snakepart snake[], snakepart app);

//Initalizes the function for checking where the snake is moving
int collision(snakepart snake[], snakepart app);

//Main argument
int main(int argc, char *argv[]) {

    //Establishes the Window for the snake world
    WINDOW *snake_wrld;
    //
    int offsetx, offsety, i, ch;

    initscr();
    noecho();
    cbreak();
    timeout(TICKRATE);
    keypad(stdscr, TRUE);

    printw("Classic Snake (Optimized for Linux/Unix Only)  -  Press q to quit...");

    refresh();

    //Offsets the screen to center of the window
    offsetx = (COLS - WIDTH) / 2;
    offsety = (LINES - HEIGHT) / 2;

    //Establishes windows height and width, with offset
    snake_wrld = newwin(HEIGHT,WIDTH,offsety,offsetx);

    //Creates snake with (x,y) cordinates and SNAKE_LNGTH
    snakepart snake[SNAKE_LNGTH];
    snakepart app;

    //Establishes apple with random
    app.x = rand() % (WIDTH-1);
    app.y = rand() % (HEIGHT-1);

    //Establishes snake creation location
    int sbegx = (WIDTH - SNAKE_LNGTH) / 2;
    int sbegy = (HEIGHT - 1) / 2;

    //Establishes each piece of the snake array
    for (i = 0; i < SNAKE_LNGTH; i++) {
        snake[i].x = sbegx + i;
        snake[i].y = sbegy;
    }

    int cur_dir = RIGHT;

    while ((ch = getch()) != 'q') {
        //Calls the move_snake function
        move_snake(snake_wrld, cur_dir, snake, app);
        //collision();
        if(ch != ERR) {
            switch(ch) {
                case KEY_UP:
                    if(snake[1].y-1 == snake[0].y) break;
                    cur_dir = UP;
                    break;
                case KEY_DOWN:
                    if(snake[1].y+1 == snake[0].y) break;
                    cur_dir = DOWN;
                    break;
                case KEY_RIGHT:
                    if(snake[1].x+1 == snake[0].y) break;
                    cur_dir = RIGHT;
                    break;
                case KEY_LEFT:
                    if(snake[1].x-1 == snake[0].y) break;
                    cur_dir = LEFT;
                    break;
                default:
                    break;
            }

        }
    }

    delwin(snake_wrld);

    endwin();

    return 0;

}

int move_snake(WINDOW *win, int direction,
                snakepart snake[], snakepart app) {

    wclear(win);

    mvwaddch(win, app.y, app.x, '@');
    //collision(snake, app);

    for (int i = 0; i < SNAKE_LNGTH - 1; i++) {
      if(snake[i].y == app.y && snake[i].x == app.x){
        app.x = rand() % (WIDTH-1);
        app.y = rand() % (HEIGHT-1);
        mvwaddch(win, app.y, app.x, '@');
        //SnakeLength ++
      }

    for (int i = 0; i < SNAKE_LNGTH - 1; i++) {
        snake[i] = snake[i + 1];
        mvwaddch(win, snake[i].y, snake[i].x, '$');

    }

    int x = snake[SNAKE_LNGTH - 1].x;
    int y = snake[SNAKE_LNGTH - 1].y;
    switch (direction) {
        case UP:
            y - 1 == 0 ? y = HEIGHT - 2 : y--;
            break;
        case DOWN:
            y + 1 == HEIGHT - 1 ? y = 1 : y++;
            break;
        case RIGHT:
            x + 1 == WIDTH - 1 ? x = 1 : x++;
            break;
        case LEFT:
            x - 1 == 0 ? x = WIDTH - 2 : x--;
            break;
        default:
            break;
    }

    snake[SNAKE_LNGTH - 1].x = x;
    snake[SNAKE_LNGTH - 1].y = y;

    mvwaddch(win, y, x, '$');

    box(win, 0 , 0);

    wrefresh(win);

    return 0;
}

int collision(snakepart snake[], snakepart app){

  }
}

标签: cstructncursesstdio

解决方案


推荐阅读