c - 蛇与自我和苹果的碰撞
问题描述
因此,对于我的班级,我需要将此蛇游戏编程为项目的一部分,我和我的搭档遇到了一个错误,我们的代码不会与自身或苹果发生冲突。因此,例如,如果我向上,然后按下它会自行反转,我们不太确定如何解决这个问题。我们有一个 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){
}
}
解决方案
推荐阅读
- php - 如何链接到页面的特定部分 HTML PHP
- c# - 当后端代码中的简单键值变量工作相同时,为什么要使用 redis 作为缓存层?
- google-apps-script - Google Slides Apps 脚本检索页面中的形状
- python-3.x - 在 KNeighborsClassifier 中使用自定义指标时,我不断收到“TypeError:只有整数标量数组可以转换为标量索引”
- java - JavaFX - 在使用选项卡时将 GUI 拆分为单独的类
- jquery - 通过函数调整电子窗口大小
- scheme - 匹配方案中的括号
- android - 当我使用 adb 显示 /sys/kernel/debug/tracing/available_filter_functions 时没有这样的设备
- python - 初始化以迭代抽象pyomo模型中的集合?
- angular-cli - 运行 ng serve -o 时出现错误