Пример #1
0
int main(){
    ball b;
    wall w[50];
    int i=0;
    for(;i<15;++i){
        w[i].y=15;
        w[i].x=10+i;
        w[i].flag=URWALL;
    }
    for(;i<30;++i){
        w[i].y=w[14].y+i-15;
        w[i].x=w[14].x;
        w[i].flag=WALL;
    }
    for(;i<50;++i){
        w[i].y=w[29].y;
        w[i].x=w[29].x+i-30;
        w[i].flag=DRWALL;
    }

    initscr();
    raw();
    keypad(stdscr,TRUE);
    noecho();
    curs_set(FALSE);

    initball(&b,DELAY,'O');
    b.direction_x=1;
    drawMap(w,50);

    timeout(DELAY);

    while(1){
        /* getmaxyx(stdscr,max_y,max_x); */
        /* clear(); */

        /* drawMap(NULL,0); */
        /* collision(&b); */
        drawball(&b);

        refresh();

        changedirection(&b);
        updateball(&b);
        /* ch=halfdelay(190); */
        if(quit)
            break;
    }

    endwin();
    exit(EXIT_SUCCESS);
}
Пример #2
0
/*
 //--------------------------------------------------------------
 //updatePosition() was used for updating the cells position. When the cell has any neighbors, it will change direction.
 // When there is no cell that is alive on the next position then it will update the cell position.
 // When the cell hit the border, then it will change direction.
 //--------------------------------------------------------------
 void testApp::updatePosition(){
 
 //create a temp circle
 tempCircle.resize(myCircle.size());
 
 //if the cell has any neighbors then change direction 
 for(int i=0;i<myCircle.size();i++){
 if(pCircle[i]->neighbor>0||pCircle[i]->getNextRing()<0||pCircle[i]->getNextRing()>maxRingNum){
 changedirection(i);
 }
 }
 
 for(int i=0;i<myCircle.size();i++){
 //if the next position is in range
 if(pCircle[i]->getNextRing()>=0 && pCircle[i]->getNextRing() < maxRingNum){
 //change the position if the next position is empty	
 if(!tempisExisted(pCircle[i]->getNextRing(), pCircle[i]->getNextStep())){
 tempCircle[i].setCell(pCircle[i]->getNextRing(), pCircle[i]->getNextStep());
 tempCircle[i].setMaxRing(maxRingNum);
 tempCircle[i].direction=pCircle[i]->direction;
 tempCircle[i].setColor(pCircle[i]->color.r,pCircle[i]->color.g,pCircle[i]->color.b);
 
 }
 
 //position unchange
 else{
 changedirection(i);
 tempCircle[i]=myCircle[i];
 }
 
 }
 //out of the ring ; nextring<0 and nextring< maxRingNum
 else{
 changedirection(i);
 tempCircle[i]=myCircle[i];
 }
 }
 
 for(int i=0; i<myCircle.size();i++){
 pCircle[i]->setCell(tempCircle[i].ring,tempCircle[i].steps);
 pCircle[i]->direction=tempCircle[i].direction;
 pCircle[i]->color=tempCircle[i].color;
 }
 }	
 */
void cell::updatePosition2(){
	//create a temp circle
	//tempCircle.resize(myCircle.size());
	
	//if the cell has any neighbors then change direction 
	for(int i=0;i<pCircle.size();i++){
		if(pCircle[i]->neighbor>0||pCircle[i]->ring<0||pCircle[i]->ring>maxRingNum){
			pCircle[i]->numOfHit++;
			
			if(pCircle[i]->condition==0){
				if(pCircle[i]->numOfHit%5==0 && pCircle[i]->numOfHit%7==0){
					changedirection(i);
				}
				else if(pCircle[i]->numOfHit%5==0){
					pCircle[i]->increasering();
				}
				else if(pCircle[i]->numOfHit%7==0){
					pCircle[i]->decreasering();
				}
				else{
					changedirection(i);
				}
			}
			
			else if(pCircle[i]->condition==1){
				if(pCircle[i]->numOfHit%5==0 && pCircle[i]->numOfHit%7==0){
					changedirection(i);
				}
				else if(pCircle[i]->numOfHit%5==0){
					pCircle[i]->increasering();
				}
				else if(pCircle[i]->numOfHit%7==0){
					pCircle[i]->decreasering();
				}
				else{
					changedirection(i);
				}
			}
		}
	}
	
	for(int i=0;i<myCircle.size();i++){
		//if the next position is in range
		if(pCircle[i]->ring>=0 && pCircle[i]->ring < maxRingNum){
			//change the position if the next position is empty	
			if(!tempisExisted(pCircle[i]->getNextRing(), pCircle[i]->getNextStep())){
				tempCircle[i].setCell(pCircle[i]->getNextRing(), pCircle[i]->getNextStep());
				tempCircle[i].setMaxRing(maxRingNum);
				tempCircle[i].direction=pCircle[i]->direction;
				tempCircle[i].setColor(pCircle[i]->color.r,pCircle[i]->color.g,pCircle[i]->color.b);
				tempCircle[i].condition=pCircle[i]->condition;
				tempCircle[i].numOfHit=pCircle[i]->numOfHit;
			}
			
			//position unchange direction
			else{
				changedirection(i);
				tempCircle[i].setCell(pCircle[i]->ring, pCircle[i]->steps);
				tempCircle[i].setMaxRing(maxRingNum);
				tempCircle[i].direction=pCircle[i]->direction;
				tempCircle[i].setColor(pCircle[i]->color.r,pCircle[i]->color.g,pCircle[i]->color.b);
				tempCircle[i].condition=pCircle[i]->condition;
				tempCircle[i].numOfHit=pCircle[i]->numOfHit;
				
			}
			
		}
		//out of the ring ; nextring<0 and nextring< maxRingNum
		else{
			
			if(pCircle[i]->ring<0){
				pCircle[i]->increasering();
				pCircle[i]->condition=0;
			}
			else{
				pCircle[i]->decreasering();
				pCircle[i]->condition=1;
			}
			
			//changecondition(i);
			tempCircle[i].setCell(pCircle[i]->ring, pCircle[i]->steps);
			tempCircle[i].setMaxRing(maxRingNum);
			tempCircle[i].direction=pCircle[i]->direction;
			tempCircle[i].setColor(pCircle[i]->color.r,pCircle[i]->color.g,pCircle[i]->color.b);
			tempCircle[i].condition=pCircle[i]->condition;
			tempCircle[i].numOfHit=pCircle[i]->numOfHit;
		}
	}
	
	for(int i=0; i<myCircle.size();i++){
		pCircle[i]->setCell(tempCircle[i].ring,tempCircle[i].steps);
		pCircle[i]->direction=tempCircle[i].direction;
		pCircle[i]->color=tempCircle[i].color;
	}
	tempCircle.erase(tempCircle.begin(),tempCircle.end()); 
}
Пример #3
0
//--------------------------------------------------------------
//updatePosition() was used for updating the cells position. When the cell has any neighbors, it will change direction.
// When there is no cell that is alive on the next position then it will update the cell position. When the cell hit the border
// then it will change direction.
//--------------------------------------------------------------
void testApp::updatePosition(){
	for (int i=0; i<windowcol;i++){
		for(int j=0; j<windowrow;j++){
			map[i][j].col=Life[i][j].col;
			map[i][j].row=Life[i][j].row;
			map[i][j].alive=Life[i][j].alive;
			map[i][j].dx=Life[i][j].dx;
			map[i][j].dy=Life[i][j].dy;
			map[i][j].nextx=Life[i][j].nextx;
			map[i][j].nexty=Life[i][j].nexty;
			map[i][j].numItems=Life[i][j].numItems;
			map[i][j].counter=Life[i][j].counter;
			Life[i][j].dspeed=deadspeed;
			map[i][j].dspeed=Life[i][j].dspeed;
		}
	}
	
	int tempx,tempy;
	int tr,tg,tb;
	int NumOfALiveCircle=0;
	for (int x=0; x<windowcol;x++){
		for(int y=0; y<windowrow;y++){
			//if the cell has any neighbors then change direction 
			if(map[x][y].numItems>0){
				changedirection(x,y);
			}
			//the temp position is next position of the current cell
			tempx=map[x][y].col+map[x][y].dx;
			tempy=map[x][y].row+map[x][y].dy;
			//if the current cell is alive
			if(	map[x][y].alive){
				NumOfALiveCircle++;
				//if the next position is in range
				if(tempx<windowcol && tempy<windowrow&&tempx>=0 &&tempy>=0){
					//change the position if the current cell is alive and if the next position dont have neighbour
					if(Life[tempx][tempy].alive==false){
					Life[tempx][tempy].alive= true;
					Life[x][y].alive=false;
					tr=Life[tempx][tempy].red;
					tg=Life[tempx][tempy].green;
					tb=Life[tempx][tempy].blue;
					Life[tempx][tempy].dx=map[x][y].dx;
					Life[tempx][tempy].dy=map[x][y].dy;
					Life[tempx][tempy].red=Life[x][y].red;
					Life[tempx][tempy].green=Life[x][y].green;
					Life[tempx][tempy].blue=Life[x][y].blue;
					Life[tempx][tempy].cellcolor=map[x][y].cellcolor;
					Life[tempx][tempy].counter=map[x][y].counter;
					Life[x][y].red=tr;
					Life[x][y].green=tg;
					Life[x][y].blue=tb;
					}
				}
				//when they hit the borders, change direction
				else{
					if(tempx<0 || tempx> windowcol-1){
						Life[x][y].dx*=-1;
						//Life[x][y].addCrashes(Life[x][y].cellcolor);
					}
					if( tempy<0 || tempy>windowrow-1){
						Life[x][y].dy*=-1;
						//Life[x][y].addCrashes(Life[x][y].cellcolor);
					}
				}
			}
		}
	}
}