Pong buggt. :<

Übersicht BlitzMax, BlitzMax NG Beginners-Corner

Neue Antwort erstellen

The_Nici

Betreff: Pong buggt. :<

BeitragMi, Sep 10, 2008 20:19
Antworten mit Zitat
Benutzer-Profile anzeigen
Hallo Leute!
Ich schreibe gerade mein erstes Blitzmax-Spiel, und leider musste ich feststellen dass meine Randkollision fehlerhaft ist.
Der Ball verfängt sich und spinnt rum. :/

Hier der Code: [AUSKLAPPEN]

Graphics 640,480

AppTitle="ASCII Pong :D"
Global ballx%=320,bally%=240,ballr%=180,s1y%=240,s2y%=240,p1%,p2%,speed%=2

millis=MilliSecs()
Repeat
   Cls
   'Spieler
   If KeyDown(KEY_UP) And s1y>=12 Then
      s1y=s1y-3
   EndIf
   If KeyDown(KEY_DOWN) And s1y<=456 Then
      s1y=s1y+3
   EndIf
   'Gegner
   If bally<s2y Then s2y=s2y-3
   If bally>s2y Then s2y=s2y+3
   'Ball
   ballx=ballx+Cos(ballr)*speed
   bally=bally+Sin(ballr)*speed
   'Kollision
      '1
   If ballx<=10 And bally>=s1y-12 And bally<=s1y+24
      ballr=(ballr + 180) Mod 360 + (s1y+6-bally)*-1
      speed=speed+1
   ElseIf ballx<10
      speed=2
      p2=p2+1
      ballx=320
      bally=240
      ballr=180
   EndIf
      '2
   If ballx>=632 And bally>=s2y-12 And bally<=s2y+24
      ballr=(ballr + 180) Mod 360 + (s2y+6-bally)*-1
      speed=speed+1
   ElseIf ballx>632
      speed=2
      p1=p1+1
      ballx=320
      bally=240
      ballr=0
   EndIf
   'Rand
   If bally<=10 Then
      ballr=(ballr + 180) Mod 360
      ballx=ballx+Cos(ballr)*speed
      bally=bally+Sin(ballr)*speed
   EndIf
   If bally>=470 Then
      ballr=(ballr + 180) Mod 360
      ballx=ballx+Cos(ballr)*speed
      bally=bally+Sin(ballr)*speed
   EndIf

   'Ball malen
   DrawText "O",ballx-6,bally-6
   'Spieler malen
   DrawText "|",0,s1y-12
   DrawText "|",0,s1y
   DrawText "|",0,s1y+12
   'Gegner malen
   DrawText "|",632,s2y-12
   DrawText "|",632,s2y
   DrawText "|",632,s2y+12
   'Punkte
   DrawText p1,0,0
   DrawText p2,632,0
   Flip
Until KeyHit(KEY_ESCAPE)
End


Ich hoffe ihr bringt mir gute Vorschläge!

MfG

amon

BeitragDo, Sep 11, 2008 17:04
Antworten mit Zitat
Benutzer-Profile anzeigen
ich nehm an - wenn die y koordinate kleiner 10 ist:

- änderst du den winkel, so dass der ball wieder vom rand wegfliegt

du kannst dir aber nicht sicher sein, dass du nur mit der richtungsänderung einen zustand erreichst wo y dann grösser 10 ist.

dh. beim nexten schleifendurchlauf bist immer noch kleiner 10, drehst die richtung und hängst somit endlos lange drin

The_Nici

BeitragDo, Sep 11, 2008 17:06
Antworten mit Zitat
Benutzer-Profile anzeigen
Jep, das ist das Problem. Btw ich hab jetzt einfallswinkel=auswallswinkel eingebaut, buggt jetzt ETWAS weniger.
Aber manchmal schon. Wie soll ich das am besten machen? Wenn der Ball weg fliegt einfach nicht diese Bedingung ausführen? Kay.

BtbN

BeitragDo, Sep 11, 2008 19:33
Antworten mit Zitat
Benutzer-Profile anzeigen
Bitte benutze in Zukunft SuperStrict, ansonsten ist es nicht möglich, den Code effektiv zu debuggen.

amon

BeitragDo, Sep 11, 2008 20:45
Antworten mit Zitat
Benutzer-Profile anzeigen
Geschwinder Hack einer OOP orientierten Lösung - der Weisheit letzter Schluss isses sicher auch ned:

Code: [AUSKLAPPEN]
SuperStrict

Type TEntity

   Global entities:TList = New TList

   Field x:Double
   Field y:Double
   
   Method New()
      entities.AddLast(Self)
   EndMethod
   
   Method Draw() Abstract
   
   Method Update() Abstract
      
   Method Reset() Abstract
      
   Function DrawAll()
      For Local e:TEntity =EachIn entities
         e.Draw()
      Next
   EndFunction
   
   Function UpdateAll()
      For Local e:TEntity =EachIn entities
         e.Update()
      Next
   EndFunction
   
   Function ResetAll()
      For Local e:TEntity =EachIn entities
         e.Reset()
      Next      
   EndFunction
   
EndType


Type TBall Extends TEntity
   
   Field speed:Double
   Field direction:Double
   
   Method Draw()
         DrawText "O", x-6, y-6    
         'DrawText direction, 320, 0
   EndMethod
   
   Method Update()
      
      Self.UpdatePostion()
   
      If y <  10 Then Self.TurnWall()
      If y > 470 Then Self.TurnWall()
      
   EndMethod
   
   Method TurnWall()
      direction = (360 - direction) Mod 360.0
      Self.UpdatePostion()
   EndMethod
   
   Method TurnPaddle(paddlepos:Double)
      direction = (180 - direction) Mod 360.0 - ( paddlepos + 6 - y)
      Self.UpdatePostion()
   EndMethod
   
   Method UpdatePostion()
      x = x + Cos(direction) * speed
          y = y + Sin(direction) * speed   
   EndMethod
   
   Method Reset()
      Self.x = 320
      Self.y = 240
      Self.speed = 2
      Self.direction = direction
   EndMethod
   
   
EndType


Type THuman Extends TEntity
   
   Field score:Int
   
   Field ball:TBall
   Field opponent:TComputer
   
   Method Draw()
      DrawText "|",0,y-12
         DrawText "|",0,y
         DrawText "|",0,y+12

      DrawText score, 0, 0
   
   EndMethod
   
   Method Update()

         If KeyDown(KEY_UP) And y >= 12
            y :- 3
         EndIf
         If KeyDown(KEY_DOWN) And y <= 456
            y :+ 3
         EndIf

      If ball.x <= 10
         If ball.direction > 90 And ball.direction < 270
            If ball.y > y-12 And ball.y < y+24
               ball.TurnPaddle(y)
            Else
               opponent.WinMatch()
            EndIf
         EndIf
      EndIf
 
   EndMethod
   
   Method RegisterBall(ball:TBall)
      Self.ball = ball
   EndMethod
   
   Method RegisterOpponent(opponent:TComputer)
      Self.opponent = opponent
   EndMethod
   
   Method Reset()
      Self.y = 240
   EndMethod
   
   Method WinMatch()
      score :+ 1
      Self.Reset()
      opponent.Reset()
      ball.Reset()
      ball.direction = 240
   EndMethod
   
EndType

Type TComputer Extends TEntity
   
   Field score:Int
   
   Field ball:TBall
   Field opponent:THuman
   
   Method Draw()
      DrawText "|",632,y-12
         DrawText "|",632,y
         DrawText "|",632,y+12

      DrawText score, 632, 0
   EndMethod
   
   Method Update()
         If ball.y < y Then y=y-3
         If ball.y > y Then y=y+3

      If ball.x >= 632
         If ball.direction < 90 Or ball.direction > 270
            If ball.y > y-12 And ball.y < y+24
               ball.TurnPaddle(y)
            Else
               opponent.WinMatch()
            EndIf
         EndIf
      EndIf

   EndMethod
   
   Method RegisterBall(ball:TBall)
      Self.ball = ball
   EndMethod
   
   Method RegisterOpponent(opponent:THuman)
      Self.opponent = opponent
   EndMethod
   
   Method Reset()
      Self.y = 240
   EndMethod
   
   Method WinMatch()
      score :+ 1
      Self.Reset()
      opponent.Reset()
      ball.Reset()
      ball.direction = 180
   EndMethod
   
EndType


Graphics 640,480

AppTitle="ASCII Pong :D"

Local ball:TBall =          New TBall
Local human:THuman =       New THuman
Local computer:TComputer  =    New TComputer

human.RegisterBall(ball)
computer.RegisterBall(ball)

human.RegisterOpponent(computer)
computer.RegisterOpponent(human)

TEntity.ResetAll()
ball.direction = 45

Repeat
   Cls
   TEntity.UpdateAll()
   TEntity.DrawAll()
   Flip
Until KeyHit(KEY_ESCAPE)
End

Neue Antwort erstellen


Übersicht BlitzMax, BlitzMax NG Beginners-Corner

Gehe zu:

Powered by phpBB © 2001 - 2006, phpBB Group