Snake: bewegung der Schlange und Futterproblem

Übersicht BlitzBasic Beginners-Corner

Neue Antwort erstellen

The_Nici

Betreff: Snake: bewegung der Schlange und Futterproblem

BeitragMi, Aug 13, 2008 14:54
Antworten mit Zitat
Benutzer-Profile anzeigen
Hai guys,
Ich programmier grad so n'kleines Snake. Meine beiden Probleme (bzw. die des Programmes):

-Falsche Bewegung
Wie man erkennt, wenn man ganz schnell P drückt, zwängen sich die Teile der Schlange immer hinten ran, wenn sie also eine Biegung macht ist die Schlange mehr wie ein elastischer Stock als eine Schlange. Halp pls kthxbai?

-Futterkollision
Manchmal geschieht es, das wenn man ein paar lustige gelbe Punkte gefressen hat, das Futter plötzlich nichtmehr kollidiert. wtf?
Halp pls thxbai?

Hier der Kot..äh Code:
Code: [AUSKLAPPEN]

Type bodypart
   Field x%
   Field y%
End Type

Type food
   Field x%
   Field y%
End Type

Graphics 320,240,16,3
SetBuffer BackBuffer()

;Kopf
Global b.bodypart=New bodypart
b\x=GraphicsWidth()/2
b\y=GraphicsHeight()/2

;Anfangsteile
For i=1 To 4
   b.bodypart=New bodypart
   b\x=GraphicsWidth()/2+i
   b\y=GraphicsHeight()/2
Next

;Variablen
Global level%=1
Global gimmefood=True
Global richtung=1


;Richtungen:
;1=Links
;2=Rechts
;3=Hoch
;4=Runter

;Mainloop
While Not KeyHit(1)
   UpdateSnake()
   UpdateFood()
   NewFood()
   Flip
   Cls
   If KeyHit(25) Then FlushKeys():WaitKey()
Wend
End

Function UpdateFood()
   head.bodypart=First bodypart
   LockBuffer BackBuffer()
   For f.food=Each food
      For w=-1 To 1
         For h=-1 To 1
            WritePixelFast f\x+w, f\y+h, $FFFFFF00   ;Gelber Punkt zeichnen
         Next
      Next
      If head\x-1<=f\x+1 And head\x+1>=f\x-1 And head\y-1<=f\y+1 And head\y+1>=f\y-1 Then
         b.bodypart=New bodypart
         lastbodypart.bodypart= Before b
         b\x=lastbodypart\x
         b\y=lastbodypart\y
         If richtung=1 Then
            b\x=b\x+1
         ElseIf richtung=2 Then
            b\x=b\x-1
         ElseIf richtung=3 Then
            b\y=b\y+1
         ElseIf richtung=4 Then
            b\y=b\y-1
         EndIf
         Delete f
         gimmefood=True
      EndIf
   Next
   UnlockBuffer BackBuffer()
End Function

Function NewFood()
   Local check%=0
   If gimmefood=True Then
      f.food=New food
      LockBuffer BackBuffer()
      Repeat
         foodx=Rand(10,310)
         foody=Rand(10,230)
         check=CheckFoodpos(foodx,foody)
      Until check=1
      UnlockBuffer BackBuffer()
      f\x=foodx
      f\y=foody
      gimmefood=False
   EndIf
End Function

Function CheckFoodpos%(x%,y%)
   checkpixel=ReadPixelFast(x,y)
   If checkpixel=-16777216 Then
      Return 1
   Else
      Return 0
   EndIf
End Function

Function UpdateSnake()
   head.bodypart=First bodypart
   ;Input
   If KeyHit(203) And richtung<>2 Then richtung=1
   If KeyHit(205) And richtung<>1 Then richtung=2
   If KeyHit(200) And richtung<>4 Then richtung=3
   If KeyHit(208) And richtung<>3 Then richtung=4
   Select richtung
      Case 1
         head\x=head\x-1
      Case 2
         head\x=head\x+1
      Case 3
         head\y=head\y-1
      Case 4
         head\y=head\y+1
   End Select
   
   ;Schlange berechnen
   nextpart.bodypart=Last bodypart
   While nextpart<>Null
      thispart.bodypart=nextpart
      nextpart=Before thispart
      If nextpart <> Null
         Select richtung
            Case 1
               thispart\x=nextpart\x+2
               thispart\y=nextpart\y
            Case 2
               thispart\x=nextpart\x-2
               thispart\y=nextpart\y
            Case 3
               thispart\x=nextpart\x
               thispart\y=nextpart\y+2
            Case 4
               thispart\x=nextpart\x
               thispart\y=nextpart\y-2
         End Select

      EndIf
   Wend
   LockBuffer BackBuffer()
   For b.bodypart=Each bodypart
      For w=-1 To 1
         For h=-1 To 1
            WritePixelFast b\x+w,b\y+h,$FFFFFFFF
         Next
      Next
   Next
   UnlockBuffer BackBuffer()
End Function


Danke schonmal für die Hilfe im Vorraus!

MfG

hectic

Sieger des IS Talentwettbewerb 2006

BeitragMi, Aug 13, 2008 16:03
Antworten mit Zitat
Benutzer-Profile anzeigen
Ich hatte grad nicht die richtigen Worte zusammen bekommen.

Bin auch nicht zu Hause. Daher einfach mal ein Code:

Code: [AUSKLAPPEN]
Graphics 320,240,0,3
SetBuffer BackBuffer()


Local Timer=CreateTimer(20)

Type TSnake
   Field X%
   Field Y%
End Type

Local XP=20
Local YP=20
Local XG=1
Local YG=0
Local Sw=0
Local Co=0


While Not KeyHit(1)
   
   Text 20,20,"Drücke SPACE"
   Text 20,40,"Kollision: "+(Co>1)
   
   If KeyHit(57) Then Sw=1-Sw
   If KeyHit(203) Then XG=-1:YG=0
   If KeyHit(205) Then XG=+1:YG=0
   If KeyHit(200) Then XG=0:YG=-1
   If KeyHit(208) Then XG=0:YG=+1
   
   XP=XP+XG
   YP=YP+YG
   
   If XP<0 Then XP=79
   If XP>79 Then XP=0
   If YP<0 Then YP=59
   If YP>59 Then YP=0
   
   T.TSnake=New TSnake
   T\X=XP
   T\Y=YP
   
   For T.TSnake=Each TSnake
      Rect T\X*4,T\Y*4,4,4,1
   Next
   
   
   Co=0
   T.TSnake=Last TSnake
   For U.TSnake=Each TSnake
      If T\X=U\X Then
         If T\Y=U\Y Then
            Co=Co+1
         End If
      End If
   Next
   
   If Sw=1 Then
      T.TSnake=First TSnake
      Delete T.TSnake
   End If
   
   WaitTimer(Timer)
   Flip 0
   Cls
Wend
End
Download der Draw3D2 V.1.1 für schnelle Echtzeiteffekte über Blitz3D

The_Nici

BeitragMi, Aug 13, 2008 16:06
Antworten mit Zitat
Benutzer-Profile anzeigen
Ah danke, jetzt verstehe ich, du erstellst immer neue teile, löschst aber die alten dann wieder! danke!

Neue Antwort erstellen


Übersicht BlitzBasic Beginners-Corner

Gehe zu:

Powered by phpBB © 2001 - 2006, phpBB Group