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

问题描述 投票:0回答:1

您知道旧的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();   
    }                   
}
c++ console-application game-physics
1个回答
0
投票

她是固定的!检查她。向@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();   
   }                   
}
© www.soinside.com 2019 - 2024. All rights reserved.