HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppModerate

Text-based vertical scroller game

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
scrollertextgamebasedvertical

Problem

I'm making a simple text game using the pdCurses library and a few other minor things like a vertical scroller in which you avoid the randomly-generated walls.

There are two walls on left and right made out of 'X' characters and blank, black space in which you can move around and avoid the 'X's your character is an '8' and your forced to continue forward or get touched by the X's each time a new line of the randomly generated "map" is revealed (for performance tests I'm making new lines show as fast as possible).

However, I am having performance issues and need new lines to be inserted and shown at least 3-5 times per second. Please suggest some simple ways of boosting performance.

```
#include
#include // or "ctime"
#include // for
#include
// Windows stuff
#include // GOD DAMMNIT WINDOWS WHY????
#include
// Ncurses
#include
// STL stuff
#include
#include
#include
//String/Int conversion
#include
// gives access to rand function
#include
//gives access to time functions
#include

// mySTOPWATCH i think i'm gonna cry.... =')
#include // for keeping times
#include // numb_digits() and digit_val();
using namespace std;
enum{ NUMB_LINES= 24, SEC= 1000}; // time stuff
enum{TIME= 345, HEALTH = 346, MAP= 247}; // for Refresh() command
enum{NONE= 256}; // for Refresh() transition
enum{NEW = 590, OLD = 591}; // for the old/new screen command

// Some nCurses setup
int r = 0,
c = 0; // current row and column (upper-left is (0,0))
const int nrows = 56, // number of rows in window
ncols = 79; // number of columns in window
// Timer Setup
Stopwatch myStopwatch(3, START);

/////////////////////////// RandNumb() ///////////////////////////////////////
int RandNumb(int scope){
srand(GetTickCount());
return rand() % scope;};
////////////////////////// GeneratePathStart() ///////////////////////////////
void GeneratePathStart(vector& buf

Solution

#include 
#include  // or "ctime"
#include  // for


Its best to avoid useless noise comments which contribute nothing

#include  
// Windows stuff
#include  // GOD DAMMNIT WINDOWS WHY????


As fun as complaining about Windows is, it is distracting to have comments like this. Have your comments tell me about what is going on, not your complaints about Microsoft.

#include 
// Ncurses
#include
// STL stuff
#include 
#include 
#include 
//String/Int conversion
#include 
// gives access to rand function
#include 
//gives access to time functions
#include 

// mySTOPWATCH   i think i'm gonna cry.... =')


Again, not a substantive comment

#include  // for keeping times
#include       // numb_digits() and digit_val();
using namespace std;
enum{ NUMB_LINES= 24, SEC= 1000};                   // time stuff
enum{TIME= 345, HEALTH = 346, MAP= 247};    // for Refresh() command
enum{NONE= 256};                                    // for Refresh() transition
enum{NEW = 590, OLD = 591};                         // for the old/new screen command

// Some nCurses setup
 int r = 0,
     c = 0; // current row and column (upper-left is (0,0))


If you want to split the assignment onto two lines, just make it two statements. Also I suggest using row and column or x,y because r/c aren't quite common enough that its obvious what they mean.

const int nrows = 56, // number of rows in window
           ncols = 79; // number of columns in window
// Timer Setup
 Stopwatch myStopwatch(3, START);

 ///////////////////////////        RandNumb()  ///////////////////////////////////////
int RandNumb(int scope){
    srand(GetTickCount());
    return rand() % scope;};


Many commonly used formatting rules exist. I'm not aware of any which put the } on the same line as the final statement. Also, srand should be called exactly once at the start of your program not every time you want a random number. There is also no need for the final semicolon.

//////////////////////////      GeneratePathStart() ///////////////////////////////


Some people do comments like this. I think they are silly because if I wanted to know the function's name I'd have read the function's name in the code.

void GeneratePathStart(vector& buff){
    int wall= RandNumb(80)/2,


Ok, why don't you use RandNumb(40)?

space = (RandNumb(75)/2)+5,
        wall2= 80- (wall+space);


I'm really not a fan of multiple assignments like this. it makes me hunt to try and figure out they are ints. Also, its best to adopt a consistent spacing regimine. I recommond putting spaces around all binary operators.

buff.push_back("");
    for(;wall> 0; wall--){
        buff[0].push_back('X');}


Counting down is unusual. You don't have any reason to do it here, so I recommend counting up. Otherwise its just seems odd. odd is bad.

for(;space> 0; space--){
        buff[0].push_back(' ');}
    for(; wall2 > 0; wall2--){
        buff[0].push_back('X');}
    };

//////////////////////////      GeneratePath()  ////////////////////////////////////
void GeneratePath( vector& buff){// the buff is the seed  too
    int wall= RandNumb(80)/2, 
        space = (RandNumb(75)/2)+5,
        wall2= 80-(space+wall);
    int swall= 0;
        for(char i= '0'; i!= ' ';swall++)
            i= buff[buff.size()-1][swall+1];


Its really really important that you put all statements in the same block as the same level as each other. Otherwise you WILL introduce bugs.

int sspace= 0; int I= swall+1;


This I is a really bad variable name because it provides no hints as to what it does.

for(char i= '0'; i!= 'X';sspace++, I++)
            i= buff[buff.size()-1][I];


For loops are best for simple iterations. Here you're doing a bunch of crazy stuff which makes it hard to follow what the loop is doing. A while loop can do this cleaner.

int swall2 = 80-(sspace+swall);


Variable names like swall2 suggest you were too lazy to come up with a better variable name. Come up with a more descriptive one.

// now the actual generation
    int cwall= wall-swall; 
    int cspace= space-sspace; 
    int cwall2= wall2-swall2;
    for(;cwall!= 0 || cspace!= 0 /*|| cwall2 != 0*/;){
            buff.push_back("");
//cwall


Comments are not for pieces of old code. They are for explanation. Right about now I have no idea what this piece of code is doing. Some comments explaining your algorithm or at least some meaningful variable names would be good.

```
if(cwall!= 0){
if(cwall>0){
swall++;
cwall--;}
else{ // cwall is negative
swall--;
cwall++;}}
for(int w= 0; w 0){
sspace++;
cspace--;}
else{ // cspace is negative
sspace--;
cspace++;}}
for(int s= 0; s 0){
// wall2++;
// cwal

Code Snippets

#include <iostream>
#include <time.h> // or "ctime"
#include <stdio.h> // for
#include <cstdlib> 
// Windows stuff
#include <Windows.h> // GOD DAMMNIT WINDOWS WHY????
#include <conio.h>
// Ncurses
#include<curses.h>
// STL stuff
#include <algorithm>
#include <string>
#include <vector>
//String/Int conversion
#include <sstream>
// gives access to rand function
#include <cstdlib>
//gives access to time functions
#include <ctime>

// mySTOPWATCH   i think i'm gonna cry.... =')
#include <myStopwatch.h> // for keeping times
#include <myMath.h>      // numb_digits() and digit_val();
using namespace std;
enum{ NUMB_LINES= 24, SEC= 1000};                   // time stuff
enum{TIME= 345, HEALTH = 346, MAP= 247};    // for Refresh() command
enum{NONE= 256};                                    // for Refresh() transition
enum{NEW = 590, OLD = 591};                         // for the old/new screen command

// Some nCurses setup
 int r = 0,
     c = 0; // current row and column (upper-left is (0,0))
const int nrows = 56, // number of rows in window
           ncols = 79; // number of columns in window
// Timer Setup
 Stopwatch myStopwatch(3, START);


 ///////////////////////////        RandNumb()  ///////////////////////////////////////
int RandNumb(int scope){
    srand(GetTickCount());
    return rand() % scope;};

Context

StackExchange Code Review Q#2546, answer score: 11

Revisions (0)

No revisions yet.