PONG GAME BALLBEWEGUNG



  • // PONGGAMEPREMIUM.cpp : Diese Datei enthält die Funktion "main". Hier beginnt und endet die Ausführung des Programms.
    //
    
    #include "pch.h"
    #include <stdafx.h>
    #include "random.h"
    #include <iostream>
    #include <conio.h>
    #include "gotoxy.h"
    #include "wait.h"
    
    const int spielfeld_breite = 99;
    const int spielfeld_hoehe = 50;
    int spielfeld[spielfeld_breite][spielfeld_hoehe] = {};
    int zeichen_viereck = 219;
    int zeichen_blank = 32;
    int spieler_1[4] = { 219, 219, 219 , 219 };
    int spieler_2[4] = { 219, 219, 219 , 219 };
    const int xpos_2 = spielfeld_breite - 3;
    const int xpos_1 = 3;
    int xpos_ball = 0;
    int ypos_ball = 0;
    int dx_ball = 0;
    int dy_ball = 0;
    
    
    void setze_spielfeldzeichen(int p_x, int p_y, int p_zeichen)
    {
    	spielfeld[p_x][p_y] = p_zeichen;
    	gotoxy(p_x, p_y);
    	printf("%c", p_zeichen);
    	gotoxy(0, spielfeld_hoehe + 1);
    }
    
    
    void zeichne_spielfeld()
    {
    	int i = 0	, j = 0, rand_x = 0, rand_y = 0, dx = 0, dy = 0, check_x = 0, check_y = 0;
    
    	// Belegung des Arrays spielfeld mit "Blank"
    	for (i = 0; i < spielfeld_breite; i++)
    	{
    		for (j = 0; j < spielfeld_hoehe; j++)
    		{
    			spielfeld[i][j] = zeichen_blank;
    		}
    	}
    
    	// 1.1 Rand oben / unten
    	for (i = 0; i < spielfeld_breite; i++)
    	{
    		setze_spielfeldzeichen(i, 1, zeichen_viereck);
    		setze_spielfeldzeichen(i, spielfeld_hoehe - 1, zeichen_viereck);
    	}
    
    	// 1.2 Rand links / rechts
    	for (i = 0; i < spielfeld_hoehe - 1; i++)
    	{
    		setze_spielfeldzeichen(0, i + 1, zeichen_viereck);
    		setze_spielfeldzeichen(spielfeld_breite - 1, i + 1, zeichen_viereck);
    
    	}
    }
    
    void run()
    {
    	int startX1 = spielfeld_hoehe / 2 + 1;
    	int StartX2 = spielfeld_hoehe / 2 + 1;
    	const int xpos1 = 3;
    	const int xpos2 = spielfeld_breite - 3;
    	int ypos = spielfeld_hoehe / 2 + 1;
    	int ypos_ = spielfeld_hoehe / 2 + 2;
    	int ypos__ = spielfeld_hoehe / 2 + 3;
    	int ypos___ = spielfeld_hoehe / 2 + 3;
    
    	int ypos2 = spielfeld_hoehe / 2 + 1;
    	int ypos2_ = spielfeld_hoehe / 2 + 2;
    	int ypos2__ = spielfeld_hoehe / 2 + 3;
    	int ypos2___ = spielfeld_hoehe / 2 + 3;
    
    	const int STARTX = spielfeld_breite / 2;
    	const int STARTY = spielfeld_hoehe / 2;
    	int xposBall = STARTX;
    	int yposBall = STARTY;
    	int kb_taste = 0;
    	int kb_taste2 = 0;
    	int dy = 0;
    	int dy2 = 0;
    	int k = 0;
    	int anfangsrichtung = 0;
    	do
    	{
    		anfangsrichtung = getrandom_int(0, 8);
    		xposBall += dx_ball;
    		yposBall += dy_ball;
    			gotoxy(xposBall, yposBall);
    			printf("%c", zeichen_viereck);
    
    		if (_kbhit())
    		{
    			kb_taste = _getch();
    
    			if (kb_taste == 0xE0)
    			{	// Bei Pfeiltasten muss der Tastencode
    				// erneut abgefragt werden:
    				kb_taste = _getch();
    
    				//Mapping der Pfeiltastencodes
    				switch (kb_taste)
    				{
    
    				case 80:
    					dy2 = +1;
    
    					break;
    				case 72:
    					dy2 = -1;
    
    					break;
    				}
    			}
    		}
    
    
    
    		// Richtungsänderung? ==> dy anpassen...
    		switch (kb_taste)
    		{
    		case 'w':
    		case 'W':
    
    
    			dy = -1;
    			break;
    
    
    
    		case 's':
    		case 'S':
    
    			dy = 1;
    			break;
    
    
    		}
    		ypos += dy;
    		ypos_ += dy;
    		ypos__ += dy;
    		ypos___ += dy;
    
    		ypos2 += dy2;
    		ypos2_ += dy2;
    		ypos2__ += dy2;
    		ypos2___ += dy2;
    
    
    		gotoxy(xpos1, ypos);
    		printf("%c", zeichen_viereck);
    		gotoxy(xpos1, ypos_);
    		printf("%c", zeichen_viereck);
    		gotoxy(xpos1, ypos__);
    		printf("%c", zeichen_viereck);
    		gotoxy(xpos1, ypos___);
    		printf("%c", zeichen_viereck);
    
    		gotoxy(xpos2, ypos2);
    		printf("%c", zeichen_viereck);
    		gotoxy(xpos2, ypos2_);
    		printf("%c", zeichen_viereck);
    		gotoxy(xpos2, ypos2__);
    		printf("%c", zeichen_viereck);
    		gotoxy(xpos2, ypos2___);
    		printf("%c", zeichen_viereck);
    
    
    		wait_milliseconds(60);
    
    		if (ypos == 2)
    		{
    			dy = 1;
    		}
    		if (ypos___ == spielfeld_hoehe - 2)
    		{
    			dy = -1;
    		}
    		if (ypos2 == 2)
    		{
    			dy2 = 1;
    		}
    		if (ypos2___ == spielfeld_hoehe - 2)
    		{
    			dy2 = -1;
    		}
    
    
    		gotoxy(xpos1, ypos);
    		printf(" ");
    		gotoxy(xpos1, ypos_);
    		printf(" ");
    		gotoxy(xpos1, ypos__);
    		printf(" ");
    		gotoxy(xpos1, ypos___);
    		printf(" ");
    
    		gotoxy(xpos2, ypos2);
    		printf(" ");
    		gotoxy(xpos2, ypos2_);
    		printf(" ");
    		gotoxy(xpos2, ypos2__);
    		printf(" ");
    		gotoxy(xpos2, ypos2___);
    		printf(" ");
    
    	//	int entscheidung;
    	//	xpos_ball = spielfeld_breite / 2;
    	//	ypos_ball = spielfeld_hoehe / 2;
    	//	gotoxy(xpos_ball, ypos_ball);
    	//	printf("%c%c", zeichen_viereck, zeichen_viereck);
    	//	printf("   ");
    	//	gotoxy(xpos_ball, ypos_ball + 1); //Weil 4x4 Ballgröße
    	//	printf("%c%c", zeichen_viereck, zeichen_viereck);
    	//	printf("   "); //Alten Ball entfernen
    	//	entscheidung = getrandom_int(1, 2);
    
    	//	if (entscheidung == 1) {
    	//		dx_ball = -1;
    	//	}
    	//	else {
    	//		dx_ball = 1;
    	//	}
    
    	//	while (xpos1 != xpos_ball + 1 && xpos2 != xpos_ball) //Damit es nicht durch die Spielfigur durchgeht
    	//	{
    
    	//		xpos_ball = xpos_ball + dx_ball;
    	//		gotoxy(xpos_ball, ypos_ball); //Neue Ballposition
    	//		printf("%c%c", zeichen_viereck, zeichen_viereck);
    	//		gotoxy(xpos_ball, ypos_ball + 1); //Weil 4x4 Ballgröße
    	//		printf("%c%c", zeichen_viereck, zeichen_viereck);
    	//	}
    
    
    
    	//	//void ballbeginn aufrufen
    	//	//spielfeldbreite & spielfeldhoehe global definieren
    
    	//	//ypos_spieler1/2 MUSS der oberste spielstein sein
    
    	//	while (_kbhit() != 27) //Solange Spiel läuft
    	//	{
    	//		// Möglichkeit 1: Sehr Steil nach Oben rechts
    	//		if (xpos1 == xpos_ball + 1 && ypos == ypos_ball - 1 || xpos_2 == xpos_ball - 2 && ypos2 == ypos_ball - 1) {
    	//			dx_ball = 0 - dx_ball; //Ballrichtung umdrehen
    	//			dy_ball = -2;
    	//		}
    
    	//		// Möglichkeit 2: Leicht Steil nach Oben rechts
    	//		else if (xpos1 == xpos_ball + 1 && ypos == ypos_ball || xpos_2 == xpos_ball - 2 && ypos2 == ypos_ball) {
    	//			dx_ball = 0 - dx_ball; //Ballrichtung umdrehen
    	//			dy_ball = -1;
    	//		}
    	//		// Möglichkeit 3: Nur nach rechts
    	//		else if (xpos1 == xpos_ball + 1 && ypos == ypos_ball + 1 || xpos_2 == xpos_ball - 2 && ypos2 == ypos_ball + 1) {
    	//			dx_ball = 0 - dx_ball; //Ballrichtung umdrehen
    	//			dy_ball = 0;
    	//		}
    	//		// Möglichkeit 4: Leicht Steil nach Unten rechts
    	//		else if (xpos1 == xpos_ball + 1 && ypos == ypos_ball + 2 || xpos_2 == xpos_ball - 2 && ypos2 == ypos_ball + 2) {
    	//			dx_ball = 0 - dx_ball; //Ballrichtung umdrehen
    	//			dy_ball = 1;
    	//		}
    	//		// Möglichkeit 5: Sehr Steil nach Unten rechts
    	//		else if (xpos1 == xpos_ball + 1 && ypos == ypos_ball + 3 || xpos_2 == xpos_ball - 2 && ypos2 == ypos_ball + 3) {
    	//			dx_ball = 0 - dx_ball; //Ballrichtung umdrehen
    	//			dy_ball = 2;
    	//		}
    	//		else {
    	//			//Tor passiert (Scorezaehler)						
    	//			//Starte Unterprogramm Ballbeginn
    	//		}
    
    	//		for (int zaehler = 0; zaehler > spielfeld_breite - 7; zaehler++) //Genau abgezählt bis Ball beim anderen Spieler ist
    	//		{
    	//			if (ypos_ball == 1 || ypos_ball == spielfeld_hoehe - 2) //Wenn oberen/unteren Rand berührt
    	//			{
    	//				dy_ball = 0 - dy_ball; //Einfallswinkel = Ausfallswinkel, also Ball prallt ab
    	//			}
    	//			//Alten Ball entfernen
    	//			gotoxy(xpos_ball, ypos_ball);
    	//			printf("  ");
    	//			gotoxy(xpos_ball, ypos_ball + 1);
    	//			printf("  ");
    	//			xpos_ball = xpos_ball + dx_ball;
    	//			ypos_ball = ypos_ball + dy_ball;
    	//			//Neue Ballposition
    	//			gotoxy(xpos_ball, ypos_ball);
    	//			printf("%c%c", zeichen_viereck, zeichen_viereck);
    	//			gotoxy(xpos_ball, ypos_ball + 1); //Weil 4x4 Ballgröße
    	//			printf("%c%c", zeichen_viereck, zeichen_viereck);
    
    	//			wait_milliseconds(20);
    	//		}
    	//	}
    
    
    	} while (TRUE);
    
    
    
    }
    
    
    int main()
    {
    	zeichne_spielfeld();
    	run();
    }
    
    
    
    

    Ich bin am verzweifeln. Ich kriege es nicht hin einen Ball ins Spielfeld zu setzen, der auch eine ordentliche physik hat. Kann mir bitte jemand dabei helfen? Ich bedanke mich im Voraus.



  • @dunkin Nimm nen Debugger und schau mal, wieso du nicht Zeile 97 erreichst, ob gotoxy Blödsinn baut.
    Abgesehen davon sehe ich kein C++ beim Drüberschauen, gehört ins C- Forum.



  • @Sarkast sagte in PONG GAME BALLBEWEGUNG:

    Abgesehen davon sehe ich kein C++ beim Drüberschauen

    #include <iostream>



  • @Mechanics sagte in PONG GAME BALLBEWEGUNG:

    @Sarkast sagte in PONG GAME BALLBEWEGUNG:

    Abgesehen davon sehe ich kein C++ beim Drüberschauen

    #include <iostream>

    Was überflüssig ist da im code selbst nirgends std::cout verwendet wird.



  • Ja, ich kann dir helfen. Was ist das Problem?


  • Mod

    @dunkin Das ist ja wohl die Höhe, dass Du uns 300 Zeilen Code vor die Nase knallst und erwartest, dass wir uns damit auseinandersetzen um Dein Problem zu lösen.

    Lies mal hier, um eine grobe Idee davon zu bekommen, was wir erwarten: https://www.c-plusplus.net/forum/topic/200753/du-brauchst-hilfe

    Closed.


Anmelden zum Antworten