Probleme mit Pong Programm-Code

Ich habe Probleme mit meinem Programm-Code von Pong. Und zwar ist der Ball nicht sichtbar und das "Spieler 1" Paddel bewegt sich ohne dass ich was mache nach oben (in Richtung 0;0). Außerdem flackert das "Spieler 2" Paddel auch noch. Ich habe allerdings alles duchgeschaut und konnte keine Fehler entdecken. Wäre Toll wenn mir da jemand helfen könnte.
Hier der Code:

#include <Adafruit_SSD1306.h>
#include <splash.h>

#include <Adafruit_GFX.h>
#include <Adafruit_GrayOLED.h>
#include <Adafruit_SPITFT.h>
#include <Adafruit_SPITFT_Macros.h>
#include <gfxfont.h>



#define SCREEN_WIDTH 128 
#define SCREEN_HEIGHT 64 

 #define OLED_RESET     -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

int x=63;
int y=31;
 int vy=((random(0,2)*2-1)*1,1);
 int vx=((random(0,2)*2-1)*1,1);
int D=2;
int A=25;
int B=35;
int G=35;
int F=25;
const int X_Pin=A0;  
const int X2_Pin=A2; 

 int b=0;
 int m=0;
int h=10;    //zum ändern von paddel aus



     


void setup() {
  // put your setup code here, to run once:
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); 
display.clearDisplay(); 
    
}

void loop() {
  // put your main code here, to run repeatedly:
  
display.setTextColor(WHITE);
 display.setCursor(54,0);
display.print(b);
display.setCursor(72,0);
display.print(m);

        //SPIELER 1
  display.drawLine(0,F,0,G,WHITE);
  
  if(analogRead(X_Pin)>1000){
    display.drawLine(0,F-D,0,G-D,BLACK);
    F=F+D;
    G=G+D;
    display.drawLine(0,F,0,G, WHITE);
    display.display();
      }
  if(analogRead(X_Pin)<500){
    display.drawLine(0,F+D,0,G+D,BLACK);
    F=F-D;
    G=G-D;
    display.drawLine(0,F,0,G, WHITE);
    display.display();
    }
    display.drawLine(0,F,0,G, WHITE);
      if(F<1){
        F=1;
        G=F+h;}
      if(G>62){
        G=62;
        F=G-h;}

display.display();



       //SPIELER 2
    
    display.drawLine(127,A,127,B,WHITE);
    
  if(analogRead(X2_Pin)>1000){
    display.drawLine(127,A-D,127,B-D,BLACK);
    A=A+D;
    B=B+D;
    display.drawLine(127,A,127,B, WHITE);
    display.display();
      }
  if(analogRead(X2_Pin)<500){
  display.drawLine(127,A+D,127,B+D,BLACK);        
    A=A-D;
    B=B-D;
    display.drawLine(127,A,127,B, WHITE);
    display.display();
    }
    display.drawLine(127,A,127,B, WHITE);

      if(A<1){
        A=1;
        B=A+h;}
      if(B>62){
        A=B-h;
        B=62;}

display.display();
             //BALL
    display.drawPixel(x,y,WHITE);

x=x+vx;
y=y+vy;
display.drawPixel(x,y,WHITE);
display.drawPixel(x-vx,y-vy,BLACK);
if(y>62){
  vy=-vy;
}
if(y<1){
 vy=-vy;
}

  
   
  
        if(x>125){
          if(A<=y&&B>=y){
             vx=-vx;
          }
         
        }
       
     if(x<2){
      if(F<=y&&G>=y){
        vx=-vx;
    } }
     
     x=x+vx;
     y=y+vy;
     //hier war ball


     if(x<1){
      x=63;
      y=31;
      display.drawPixel(x,y,WHITE);
      m=m+1;
      delay(500);
     }
     if(x>125){
      x=63;
      y=31;
      display.drawPixel(x,y,WHITE);
      b=b+1;
      delay(500);
     }

    if(b==11){
      display.clearDisplay();
      delay(100);
      display.setCursor(40,26);
      display.print("Spieler 1");
      display.setCursor(30,38);
      display.print("hat gewonnen!");
      b=b-b;
      m=m-m;
      display.display();
      delay(5000);
    }
    

      if(m==11){
      display.clearDisplay();
      display.setCursor(40,26);
      display.print("Spieler 2");
      display.setCursor(30,38);
      display.print("hat gewonnen!");
      b=b-b;
      m=m-m;
      display.display();
      delay(5000);
    }
    display.clearDisplay();
   
   }

Im englischen Teil des Forum müssen die Beiträge und Diskussionen in englischer Sprache verfasst werden. Deswegen wurde diese Diskussion in den deutschen Teil des Forums verschoben.

mfg ein Moderator.

Hast Du früher in Basic programmiert? Die Zeiten von einbuchstabigen Variablennamen sind lange vorbei. Gib deinen Variablen sinnvolle Namen, aus denen die Funktion der Variable hervorgeht. Das macht den Code wesentlich besser verständlich. Vor allem wenn man mit Kommentaren extrem sparsam umgeht ...
Dann kann man den Code ordentlich nachvollziehen und helfen.

P.S. es macht auch wenig Sinn, für jede Frage ein neues Thema anzufangen. Ein Projekt -> ein Thema. So bleibt alles beieinander und der Zusammenhang ist verständlich.

1 Like

Bist Du Dir sicher, dass das richtig ist?

usw...
Und dann drück mal STRG-T

Dein clear.display() am Ende von loop() macht Dir Dein Bild kaputt.

Vielleicht als Ansatz:

#include <Adafruit_SSD1306.h>
#include <splash.h>

#include <Adafruit_GFX.h>
#include <Adafruit_GrayOLED.h>
#include <Adafruit_SPITFT.h>
#include <Adafruit_SPITFT_Macros.h>
#include <gfxfont.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

#define OLED_RESET     -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

int x = 63;
int y = 31;
int vy = ((random(0, 2) * 2 - 1) * 1, 1);
int vx = ((random(0, 2) * 2 - 1) * 1, 1);
int D = 2;
int A = 25;
int B = 35;
int G = 35;
int F = 25;
const int X_Pin = A0;
const int X2_Pin = A2;

int b = 0;
int m = 0;
int h = 10;  //zum ändern von paddel aus

void setup()
{
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
}

void loop()
{
  spielstand();
  spielerOne();
  spielerTwo();
  ball();
  checkPoint();
}

void spielstand()
{
  display.setTextColor(WHITE);
  display.setCursor(54, 0);
  display.print(b);
  display.setCursor(72, 0);
  display.print(m);
}

void spielerOne()
{
  //SPIELER 1
  if (analogRead(X_Pin) > 1000)
  {
    display.drawLine(0, F, 0, G, BLACK);
    F = F + D;
    G = G + D;
  }

  if (analogRead(X_Pin) < 500)
  {
    display.drawLine(0, F, 0, G, BLACK);
    F = F - D;
    G = G - D;
  }

  if (F < 1)
  {
    F = 1;
    G = F + h;
  }

  if (G > 62)
  {
    G = 62;
    F = G - h;
  }

  display.drawLine(0, F, 0, G, WHITE);
  display.display();
}

void spielerTwo()
{
  //SPIELER 2
  if (analogRead(X2_Pin) > 1000)
  {
    display.drawLine(127, A, 127, B, BLACK);
    A = A + D;
    B = B + D;
  }

  if (analogRead(X2_Pin) < 500)
  {
    display.drawLine(127, A, 127, B, BLACK);
    A = A - D;
    B = B - D;
  }

  if (A < 1)
  {
    A = 1;
    B = A + h;
  }

  if (B > 62)
  {
    A = B - h;
    B = 62;
  }

  display.drawLine(127, A, 127, B, WHITE);
  display.display();
}

void ball()
{
  //BALL
  display.drawPixel(x, y, WHITE);
  x = x + vx;
  y = y + vy;
  display.drawPixel(x, y, WHITE);
  display.drawPixel(x - vx, y - vy, BLACK);

  if (y > 62)
  {
    vy = -vy;
  }

  if (y < 1)
  {
    vy = -vy;
  }

  if (x > 125)
  {
    if (A <= y && B >= y)
    {
      vx = -vx;
    }
  }

  if (x < 2)
  {
    if (F <= y && G >= y)
    {
      vx = -vx;
    }
  }

  x = x + vx;
  y = y + vy;
  //hier war ball

  if (x < 1)
  {
    x = 63;
    y = 31;
    display.drawPixel(x, y, WHITE);
    m = m + 1;
    delay(500);
  }

  if (x > 125)
  {
    x = 63;
    y = 31;
    display.drawPixel(x, y, WHITE);
    b = b + 1;
    delay(500);
  }
}

void checkPoint()
{
  if (b >= 11 || m >= 11)
  {
    display.clearDisplay();
    display.setCursor(40, 26);
    display.print(F("Spieler "));

    if (b >= 11)
    { Serial.print(1); }
    else if (m >= 11)
    { Serial.print(2); }

    display.setCursor(30, 38);
    display.print(F("hat gewonnen!"));
    display.display();
    b = 0;
    m = 0;
    delay(5000);
    display.clearDisplay();
    display.display();
  }
}

Und im Übrigen glt das von @MicroBahner gesagte.
Wenn Du den Dingen etwas sprechenderes, mindestens aber erklärende Kommentare vergibst, dann kann man das auch besser bearbeiten.

Die Codedopplungen habe ich mal am Beispiel des Spielendes gelöscht.

Das solltest Du auch mal erklären.
Bei mir erzeugt das eine Warnung:

C:\...\sketch_may18a.ino:20:34: warning: value computed is not used [-Wunused-value]
 int vy = ((random(0, 2) * 2 - 1) * 1, 1);
           ~~~~~~~~~~~~~~~~~~~~~~~^~~
Was da gerechnet wird...

Denn da steht sinngemäß

int vy=((zufallszahl*2-1)*1,1);

Da random() zwei Parameter hat

kann das in zwei Varianten enden:

int vy=((0*2-1)*1,1);  // --> (-1,1) --> 1 wegen des Comma-Operators
int vy=((1*2-1)*1,1);  // --> ( 2-1,1) --> (1,1) --> 1 (dito)

Ist es das, was Du wolltest?