我在c++控制台中制作了一个移动屏幕保护程序,但当它到达角落时出现了一个错误



你知道旧dvd播放器上的屏幕保护程序吗?我在控制台上用ascii制作了它,但当它碰到一个角落时,它就看不见了。我不确定发生了什么,它应该起作用。我只是在闲逛,努力学习,所以这没什么大不了的,但如果有人有兴趣看一看,我会非常感激!任何一般性的反馈和建议也将不胜感激。

#include <iostream>
#include <windows.h>
using namespace std;
int width = 20;
int height = 10;
int iconX, iconY;
enum eDirection {UPLEFT,UPRIGHT,DOWNRIGHT,DOWNLEFT}dir;
int lastDir;
void Setup()
{
dir = UPRIGHT;                //sets the direction and a
iconX = rand() % width + 1;   //random starting point
iconY = rand() % height + 1;
}
void Draw()
{
system("cls");//clear the screen each frame
for (int y = 0; y < height + 2; y++) { // this goes from top to bottom of the grid
for (int x = 0; x < width + 2; x++) { // and then left to right to hit every square with these conditionals
if (y == 0 || y == height + 1) cout << "-"; //top and bottom border
if ((x == 0 || x == width + 1) && (y != 0 && y != height + 1)) cout << "|"; // both sides
if (x == iconX && y == iconY) cout << "0"; // this is the icon that will bounce around the screen
else if ((y != 0 && y != height + 1) && (x != 0 && x != width+1)) cout << " "; // if the icon wasn't drawn and 
}                                                               //we aren't currently on a border it makes a space
cout << endl;
}
}
void Move()
{   //bounces on the sides
if (iconX == 1 || iconX == width) {
switch (lastDir) {
case 0:
dir = UPRIGHT;   //this code checks if the icon is right
break;           //next to a wall and depending on which        
case 1:              //direction it was moving it is given a 
dir = UPLEFT;    //new direction to move in
break;
case 2:
dir = DOWNLEFT;
break;
case 3:
dir = DOWNRIGHT;
break;
}
}
//bounces on the top and bottom
if (iconY == 1 || iconY == height) {
switch (lastDir) {
case 0:
dir = DOWNLEFT;  //same thing down here but for
break;           //the top and bottom
case 1:             
dir = DOWNRIGHT;
break;
case 2:
dir = UPRIGHT;
break;
case 3:
dir = UPLEFT;
break;
}
}
switch (dir) {
case UPLEFT: 
iconX--;     //this code moves the icon 
iconY--;     //depending on which direction 
break;       //is currently saved in dir
case UPRIGHT:
iconX++;
iconY--;
break;
case DOWNLEFT:
iconX--;
iconY++;
break;
case DOWNRIGHT:
iconX++;
iconY++;
break;
}
lastDir = dir; //it saves the last direction
}                  //as a number to be used to do 
//the bouncing
int main()
{
Setup();
while (true) {
Draw();   
Move();   
}                   
}

她已经修好了!看看她。向@JaMiT大喊的想法

#include <iostream>
#include <windows.h>
using namespace std;
int width = 20;
int height = 10;
int iconX, iconY;
enum Vertical {UP,DOWN}vert;
enum Horizontal {LEFT,RIGHT}hor;
int lastVert;
int lastHor;
void Setup()
{
vert = UP;                //sets the direction and a
hor = LEFT;
iconX = rand() % width + 1;   //random starting point
iconY = rand() % height + 1;
}
void Draw()
{
system("cls");//clear the screen each frame
for (int y = 0; y < height + 2; y++) { // this goes from top to bottom of the grid
for (int x = 0; x < width + 2; x++) { // and then left to right to hit every square with these conditionals
if (y == 0 || y == height + 1) cout << "-"; //top and bottom border
if ((x == 0 || x == width + 1) && (y != 0 && y != height + 1)) cout << "|"; // both sides
if (x == iconX && y == iconY) cout << "0"; // this is the icon that will bounce around the screen
else if ((y != 0 && y != height + 1) && (x != 0 && x != width+1)) cout << " "; // if the icon wasn't drawn and 
}                                                               //we aren't currently on a border it makes a space
cout << endl;
}
}
void Move()
{
if (iconX == 1 || iconX == width)
if (lastHor == 0) //0 is left
hor = RIGHT;
else
hor = LEFT;
if (iconY == 1 || iconY == height)
if (lastVert == 0) //0 is up
vert = DOWN;
else
vert = UP;
if (vert == UP)iconY--;
else iconY++;
if (hor == LEFT)iconX--;
else iconX++;
lastVert = vert;
lastHor = hor;
}
int main()
{
Setup();
while (true) {
Draw();   
Move();   
}                   
}

相关内容

最新更新