我試圖尋找我的問題的答案,但找不到任何非常正確的。我想忽略用戶的輸入,如果他們超出界限
我的問題處理有一個數組設置和基本上圍繞一個點域移動小光標。我需要能夠忽略用戶的輸入,如果他們擊中一個箭頭鍵的方向,將他們超出電網的界限。我不確定最好是做什麼。
這是我無法修改的類。我只能從它繼承來做到這一點。
//You can add or delete includes
#include <iostream>
#include <stdlib.h> //For system()
#include <conio.h> //For getche()
#include <time.h>
using namespace std;
const int MAX_HEIGHT = 20; //The height of the grid
const int MAX_WIDTH = 40; //The width of the grid
class PickUpGame
{
protected:
char Screen[MAX_HEIGHT][MAX_WIDTH]; //The grid to print to the screen
int xPos, yPos; //The current x and y position of the users cursor on the grid
public:
//Constructor that will intialize the screen and x and y positions
PickUpGame() : xPos(0), yPos(MAX_WIDTH - 1)
{
SetupScreen(); //Initalize the grid
}
//Initialize the screen with all '.' characters and set the intial user cursor position on the grid
void SetupScreen()
{
for(int height = 0; height < MAX_HEIGHT; height++) {
for(int width = 0; width < MAX_WIDTH; width++) {
Screen[height][width] = '.'; //Initialize each grid position
}
}
Screen[xPos][yPos] = '<'; //Set the users initial cursor position
}
//Print the grid to the screen
void Print()
{
for(int height = 0; height < MAX_HEIGHT; height++) {
for(int width = 0; width < MAX_WIDTH; width++) {
cout << Screen[height][width]; //Print the character at this location in the grid
}
cout << endl; //After each row is printed, print a newline character
}
}
//Take in user input to move around the grid
void Move(char Direction)
{
switch(static_cast<int>(Direction)) //Don't know the ASCII characters for the arrow keys so use the ASCII numbers
{
case 72: //Up arrow
Screen[xPos][yPos] = ' '; //Wipe out the users current cursor
xPos--; //Move the users x position on the grid
Screen[xPos][yPos] = '^'; //Move the users cursor
break;
case 80: //Down arrow
Screen[xPos][yPos] = ' ';
xPos++;
Screen[xPos][yPos] = 'V';
break;
case 75: //Left arrow
Screen[xPos][yPos] = ' ';
yPos--;
Screen[xPos][yPos] = '<';
break;
case 77: //Right arrow
Screen[xPos][yPos] = ' ';
yPos++;
Screen[xPos][yPos] = '>';
break;
}
}
};
你有試過什麼嗎? – hexacyanide