示例#1
0
/* run : worker thread for ball control 
         along with AI generation
*/
void run(void *vsocket){
    int sock = *(int *)&vsocket;
    int echolen;
    RTIME sleep; // as nano-seconds


    if(ballThreadSpeed % 6 == 0)
	ballThreadSpeed++;
    sleep = (RTIME)(50000000 / (ballThreadSpeed % 6));

    rt_task_suspend(NULL);
    while (true){
   	ballmove();
	echolen = strlen(ballData);
	if(send(sock, ballData, echolen, 0) != echolen)
		rt_err("send()");
        /*2 View mode case*/
        if(viewMode > 1){
		if(send(viewSocket2, ballData, echolen, 0) != echolen)
     			rt_err("send()");
	}

        
	if(AIMode){
            opponentMovement(AImoveGenerate());
	    echolen = strlen(moveData[1]);
	   if(send(sock, moveData[1], echolen, 0) != echolen)
		rt_err("send()");
 	   if(viewMode > 1){
		if(send(viewSocket2, moveData[1], echolen, 0) != echolen)
     		rt_err("send()");
           } 
        }
        if(reset){
            resetBall();
            echolen = strlen(gameData);
            if(send(sock, gameData, echolen, 0) != echolen)
                   rt_err("send()");
            if(viewMode > 1){
		if(send(viewSocket2, gameData, echolen, 0) != echolen)
     		rt_err("send()");
           }  
        }
        if(bounceplayer){
            bounceDirection(player,ball.radius);
            bounceplayer = false;
        }
        if(bounceOpponent){
            bounceDirection(opponent,ball.radius);
            bounceOpponent = false;
        }
        rt_task_sleep(sleep);
    }
}
// HAUPTFUNKTION; MOTOR DES GAMES
int main(void){
	int xBall=0, weiter=0, close=1, control=1, config=1, speed=0, count1=0, count2=0, satz1=0, satz2=0; // GAMESTEUREUNGSVARIABLEN
	int direction=0, upDown=0;              // BALLRICHTUNG STEUERUNGSVARIABLEN
	int *d=&direction, *uD=&upDown;         // UND DEREN ZEIGER; STATT GLOBALE VARIABLEN
	char start, modus='0', linekiller=' ', counter1[6]={"     "}, counter2[6]={"     "};
	screen(); // MUSS NICH SEIN; SIEHT ABER SCHÖNER AUS!!
	
	// DAS GAME
	do{
		
		field(control, modus, counter1, counter2, count1, count2);       // AUSGABE DES GAME AUSGANGSLAGE

		// SPIELEINSTELLUNGEN
		// NUR BEI MATCH-BEGINN ABFRAGE DER SPIELSCHWIERIGKEIT UND DES SPIELMODUS
		if(config == 1){
			config = 0; 
			printf_s("\n\n");
			do{
				printf_s("Difficulty: very easy[1] %c easy[2] %c normal[3] %c hard[4] %c impossible[5] --> ",16,16,16,16); modus=_getche();
				if(modus=='1')speed=150;           // AUSWAHL DER BALLGESCHWINDIGKEIT DURCH DEN SPIELER
				if(modus=='2')speed=80;
				if(modus=='3')speed=50;
				if(modus=='4')speed=23;
				if(modus=='5')speed=0;
				printf_s("\r \r");          // NUR BEI MATCH-BEGINN ABFRAGE DER SPIELSCHWIERIGKEIT UND DES SPIELMODUS// SPRINGT AN ANFANG VON LETZTE ZEILE
			}while (modus!='1' && modus!='2' && modus!='3' && modus!='4' && modus!='5'); // FÄNGT GEDRÜCKTE TASTEN WÄHREND DES SPIELS AB -> ANSONSTEN modus getch() WÜRDE IRGENDWAS EINLESEN
			for(int i=0; i<=80; i++)
				printf_s("\r%*c",i,linekiller);     // LETZTE ZEILE LÖSCHEN	

			do{
				printf_s("Spielmodus: 3 S%ctze bis jeweils 5 [6] %c %c 1 Match bis 11 [7] --> ",132,17,16); modus=_getche();
				if(modus=='6'){count1=-1;count2=-1;}   // AUSWAHL DER ZÄHLWEISE DES SPIELS
				if(modus=='7'){count1=0;count2=0;}
				printf_s("\r \r");
			}while(modus!='6' && modus!='7');
			for(int i=0; i<=80; i++)p
				rintf_s("\r%*c",i,linekiller);     // LETZTE ZEILE LÖSCHEN

			printf_s("Spielstart [s] --> "); 
		}

        start=_getche();   // EINGABE S FÜR START WIRD ABGEWARTET;

		if(start=='s'||start=='S'){
			control=-1; direction=1; upDown=0;  // control REGELT VORGÄNGE IN field() UND playermove(); direction KOORDINIERT DIE BALLRICHTUNGEN in ballmove()

			// SPIELVERLAUF
			// EINE RUNDE LÄUFT SO LANGE BIS DER LINKE ODER RECHTE SPIELRAND ERREICHT WURDE
			for(xBall=XMIDDLE+1; xBall<XVALUE-2 && xBall>3; xBall++){
				Sleep(speed); // DAS MUSS REIN! COMPILER DEBUGGING IST OFT NICHT ORIGINAL SPEED! exe-DATEI OFFENBART DEN WAHREN SPEED!!
				// PAUSE
				if(GetAsyncKeyState(0x42)||weiter==1){
					if(modus=='7')
						printf_s("\n\n\nKURZE Pause!! Ball bewegt sich nur langsamer! Wenn r dann Konzentration! ^^");
					if(modus=='6')
						printf_s("KURZE Pause!! Ball bewegt sich nur langsamer! Wenn r dann Konzentration! ^^");
					weiter=1;Sleep(10000);}
				// PAUSE WIRD AUFGEHOBEN
				if(GetAsyncKeyState(0x52)){
					weiter=0;Sleep(speed);
					if(modus=='7')
						printf_s("\r \r");
						for(int i=0; i<=80; i++)
							printf_s("\r%*c",i,linekiller);} // LöSCHEN DER PAUSEINFO
				// NEUES MATCH
				if(GetAsyncKeyState(0x4E)){
					// ZURÜCKSETZTEN ALLER GAMEVARIABLEN
					for(int i=0; i<=4; i++){
						counter2[i]=' ';
						counter1[i]=' ';
					}
					count1=-1; count2=-1; config=1; satz1=0; satz2=0; modus='0';
					break;
				}
				// GAME WIRD GESCHLOSSEN
				if(GetAsyncKeyState(0x43)){
					close=0;
					break;
				}
				// FüR BALLVERLAUF IN X-RICHTUNG
				if(direction==1||direction==2||direction==3){
					ballmove(xBall, d, uD);      // BEWEGUNG DES BALLES
					playermove(control);         // BEWEGUNG DER SPIELER
					field(control, modus, counter1, counter2, count1, count2);
				}
				
				// FÜR BALLVERLAUF IN -X RICHTUNG
				if(direction==-1||direction==-2||direction==-3){
					xBall-=2;
					ballmove(xBall, d, uD);      // BEWEGUNG DES BALLES
					playermove(control);         // BEWEGUNG DER SPIELER
					field(control, modus, counter1, counter2, count1, count2);
				}
			control=0;
			}

			//SPIELSTÄNDE
			// 3 SÄTZE BIS 5
			if(modus=='6'){
				// SPIELSTAND
				if(xBall>XVALUE-5 && count1<5 && config==0){
					count1++;
					if(count1<4)
						counter1[count1]=1;
				}

				if(xBall<4 && count2<5 && config==0){
					count2++;
					if(count2<4)
						counter2[count2] = 1;
				}
				// field() MUSS AUFGERUFEN WERDEN; DAMIT ES DIE LETZTEN COUNTER-WERTE NOCH MITBEKOMMT BEVOR SIE GELÖSCHT WERDEN!!
				field(control, modus, counter1, counter2, count1, count2);
					if(count1==4)
						satz1++;
					if(count2==4)
						satz2++;
					// BEI SIEG ZURÜCKSETZTEN ALLER WERTE
					for(int i=0; i<=4 ; i++){
						counter2[i]=' ';
						counter1[i]=' ';
					}
					if(satz1==2){
						system("cls");
						printf_s("\n\n\n\n\n\n\n       Spieler 1 hat gewonnen!!!");
						PlaySoundA("winner3.wav",NULL,SND_ASYNC);
						Sleep(5000);
						config=1;
						modus='0';
						satz1=0;
						satz2=0;
					}
					if(satz2==2){
						system("cls");
						printf_s("\n\n\n\n\n\n\n       Spieler 2 hat gewonnen!!!");
						PlaySoundA("winner3.wav",NULL,SND_ASYNC);
						Sleep(5000);
						config=1;
						modus='0';
						satz2=0;
						satz1=0;
					}
					count1=-1;
					count2=-1;
			}

			// 1 MATCH BIS 11
			if(modus=='7'){
				//SPIELSTAND
				if(xBall>XVALUE-5 && count1<11 && config==0)
					count1++;
				if(xBall<4 && count2<11 && config==0)
					count2++;
				// field() MUSS AUFGERUFEN WERDEN; DAMIT ES
				field(control, modus, counter1, counter2, count1, count2);
				if(count1==11||count2==11){
					system("cls");
					// BEI SIEG ZURÜCKSETZTEN ALLER WERTE
					if(count1==11){
						printf_s("\n\n\n\n\n\n\n       Spieler 1 hat gewonnen!!!");
						PlaySoundA("winner3.wav",NULL,SND_ASYNC);
					}
					if(count2==11){
						printf_s("\n\n\n\n\n\n\n       Spieler 2 hat gewonnen!!!");
						PlaySoundA("winner3.wav",NULL,SND_ASYNC);
					}
					Sleep(5000);
					count1=0;count2=0;config=1;
				}
				
			}
		}

		fflush(stdin); 
		xBall=0; 
		control=1;
		system("cls");

	}while(close);

	return 0;
}