每当我展示蛇长时,我将如何修复这个空间的侧面?
每当我运行我的程序时,我都会检查是否会放下 3 个 os,它会转到右侧。我知道问题是我有一堆循环的 if 语句,但我有一个 for 循环来放置蛇的踪迹。我知道所有这些如果都不好。
我尝试将 for 循环的顶部更改为始终为 true 的 if 语句,但它不起作用。我对如何检查所有坐标感到困惑,但仍然没有在侧面弹出墙壁。
我是一个初学者,所以不要那么讨厌我的代码。
snake.cpp
#include <iostream>
#include <conio.h>
#include <random>
#include <time.h>
#include "snake.hpp"
void endGame(int score);
const int KEY_ARROW_CHAR1 = 224;
const int KEY_ARROW_UP = 72;
const int KEY_ARROW_DOWN = 80;
const int KEY_ARROW_LEFT = 75;
const int KEY_ARROW_RIGHT = 77;
void snake::input() {
// checks for arrow key input
if (_kbhit()) {
//stores keycode
int key = _getch();
//checks if key is arrow key
if (key == KEY_ARROW_CHAR1) {
//stores arrow key
key = _getch();
//checks if arrow key is up
if (key == KEY_ARROW_UP) {
direction = '^';
}
//checks if arrow key is down
else if (key == KEY_ARROW_DOWN) {
direction = 'v';
}
//checks if arrow key is left
else if (key == KEY_ARROW_LEFT) {
direction = '<';
}
//checks if arrow key is right
else if(key == KEY_ARROW_RIGHT) {
direction = '>';
}
}
}
switch (direction) {
case '^':
snakeCoords[2]--;
break;
case 'v':
snakeCoords[2]++;
break;
case '>':
snakeCoords[0]++;
break;
case '<':
snakeCoords[0]--;
}
checkForObjects();
}
bool snake::checkForObjects() {
if ((snakeCoords[0] == 40 || snakeCoords[0] == 0) || (snakeCoords[2] == 20 || snakeCoords[2] == 0)) {
endGame(score);
}
if (snakeCoords[0] == fruitCoords[0] && snakeCoords[2] == fruitCoords[2]) {
score++;
getFruitLoc();
return true;
}
return false;
}
void snake::getFruitLoc() {
srand(time(0));
fruitCoords[0] = rand() % 39;
fruitCoords[2] = rand() % 19;
}
void endGame(int score) {
std::cout << "you died, your score was " << score;
exit(0);
}
```
#include <iostream>
#include <conio.h>
#include <random>
#include "snake.hpp"
#include <vector>
void createArena(char direction);
//Coordinates for snake
std::vector <std::vector<int>> previousCoords{
{14, 10},
{13, 10},
{12, 10}
};
snake coordManager{};
int score{0};
int main()
{
//make terminal green
system("color 0a");
while (true) {
std::cout << "\t" << score;
//pushes ascii arena down
for (size_t i{}; i <= 5; i++) {
std::cout << std::endl;
}
score = coordManager.score;
//updates snake based on coordinates
createArena(coordManager.direction);
//manages all snake movements
coordManager.input();
//reminder this clears the screan
system("CLS");
}
}
void createArena(char direction) {
//offsets screen
std::cout << "\t\t\t\t";
for (size_t y{}; y <= 20; y++) {
for (size_t x{}; x <= 40; x++) {
//checks if it hits the snakes coordinates
for (size_t i{}; i < previousCoords.size();) {
if (x == previousCoords.at(i).at(0) && y == previousCoords.at(i).at(1)) {
std::cout << "O";
}
i++;
}
if (coordManager.snakeCoords[0] == x && coordManager.snakeCoords[2] == y) {
std::cout << direction;
}
else if (x == 0 || x == 40) {
std::cout << '|';
}
else if (y == 0 || y == 20) {
std::cout << '-';
}
else if(coordManager.fruitCoords[0] == x && coordManager.fruitCoords[2] == y) {
std::cout << "F";
}
else {
std::cout << " ";
}
}
//offsets again
std::cout << std::endl << "\t\t\t\t";
}
}
#include <conio.h>
#include <random>
#include <time.h>
class snake {
public:
void input();
bool checkForObjects();
int score{};
int snakeCoords[2]{
/* X */ 15,
/* Y */ 10
};
int fruitCoords[2]{
/* X */ rand() % 39,
/* Y */ rand() % 19
};
char direction{'O'};
private:
void getFruitLoc();
};
https://i.sstatic.net/bdSNf.png
Whenever I run my program I check to see if I would place down 3 os it goes to the right. I know that the problem is that I have a bunch of if statements that cycle through, but I have a for loop there to place down the snakes trail. I know that all those ifs aren't good.
I tried changing in the top of the for loop to be a if statement that was always true, it didn't work nothing did. I am confused on how to check for all of the coordinates, but still not have the walls pop up on the sides.
I am a begginer, so don't hate on my code that much.
snake.cpp
#include <iostream>
#include <conio.h>
#include <random>
#include <time.h>
#include "snake.hpp"
void endGame(int score);
const int KEY_ARROW_CHAR1 = 224;
const int KEY_ARROW_UP = 72;
const int KEY_ARROW_DOWN = 80;
const int KEY_ARROW_LEFT = 75;
const int KEY_ARROW_RIGHT = 77;
void snake::input() {
// checks for arrow key input
if (_kbhit()) {
//stores keycode
int key = _getch();
//checks if key is arrow key
if (key == KEY_ARROW_CHAR1) {
//stores arrow key
key = _getch();
//checks if arrow key is up
if (key == KEY_ARROW_UP) {
direction = '^';
}
//checks if arrow key is down
else if (key == KEY_ARROW_DOWN) {
direction = 'v';
}
//checks if arrow key is left
else if (key == KEY_ARROW_LEFT) {
direction = '<';
}
//checks if arrow key is right
else if(key == KEY_ARROW_RIGHT) {
direction = '>';
}
}
}
switch (direction) {
case '^':
snakeCoords[2]--;
break;
case 'v':
snakeCoords[2]++;
break;
case '>':
snakeCoords[0]++;
break;
case '<':
snakeCoords[0]--;
}
checkForObjects();
}
bool snake::checkForObjects() {
if ((snakeCoords[0] == 40 || snakeCoords[0] == 0) || (snakeCoords[2] == 20 || snakeCoords[2] == 0)) {
endGame(score);
}
if (snakeCoords[0] == fruitCoords[0] && snakeCoords[2] == fruitCoords[2]) {
score++;
getFruitLoc();
return true;
}
return false;
}
void snake::getFruitLoc() {
srand(time(0));
fruitCoords[0] = rand() % 39;
fruitCoords[2] = rand() % 19;
}
void endGame(int score) {
std::cout << "you died, your score was " << score;
exit(0);
}
```
#include <iostream>
#include <conio.h>
#include <random>
#include "snake.hpp"
#include <vector>
void createArena(char direction);
//Coordinates for snake
std::vector <std::vector<int>> previousCoords{
{14, 10},
{13, 10},
{12, 10}
};
snake coordManager{};
int score{0};
int main()
{
//make terminal green
system("color 0a");
while (true) {
std::cout << "\t" << score;
//pushes ascii arena down
for (size_t i{}; i <= 5; i++) {
std::cout << std::endl;
}
score = coordManager.score;
//updates snake based on coordinates
createArena(coordManager.direction);
//manages all snake movements
coordManager.input();
//reminder this clears the screan
system("CLS");
}
}
void createArena(char direction) {
//offsets screen
std::cout << "\t\t\t\t";
for (size_t y{}; y <= 20; y++) {
for (size_t x{}; x <= 40; x++) {
//checks if it hits the snakes coordinates
for (size_t i{}; i < previousCoords.size();) {
if (x == previousCoords.at(i).at(0) && y == previousCoords.at(i).at(1)) {
std::cout << "O";
}
i++;
}
if (coordManager.snakeCoords[0] == x && coordManager.snakeCoords[2] == y) {
std::cout << direction;
}
else if (x == 0 || x == 40) {
std::cout << '|';
}
else if (y == 0 || y == 20) {
std::cout << '-';
}
else if(coordManager.fruitCoords[0] == x && coordManager.fruitCoords[2] == y) {
std::cout << "F";
}
else {
std::cout << " ";
}
}
//offsets again
std::cout << std::endl << "\t\t\t\t";
}
}
#include <conio.h>
#include <random>
#include <time.h>
class snake {
public:
void input();
bool checkForObjects();
int score{};
int snakeCoords[2]{
/* X */ 15,
/* Y */ 10
};
int fruitCoords[2]{
/* X */ rand() % 39,
/* Y */ rand() % 19
};
char direction{'O'};
private:
void getFruitLoc();
};
https://i.sstatic.net/bdSNf.png
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我让它起作用,如果它与空格不同,我使用了一个布尔值,我会更改它。这是一个简单的修复,对不起。
I got it working, I used a boolean that I would change if it was different from whitespace. it was a simple fix sorry.