BPS #6: Synchrone Bewegung - Auswertung

Übersicht BlitzBasic Beginners-Corner

Neue Antwort erstellen

Xeres

Moderator

Betreff: BPS #6: Synchrone Bewegung - Auswertung

BeitragMi, Apr 20, 2011 17:30
Antworten mit Zitat
Benutzer-Profile anzeigen
Wieder ist eine Runde um!

Das war die Aufgabe

Postet hier eure Ergebnisse, Codes, Gedanken. Lernt von den anderen, seht euch deren Quelltext an und versucht euren eigenen zu verbessern.

Diskussion
Postet zu euren Codes stets eine kurze Erklärung mit euren Gedanken in denen ihr simpel gesagt die Frage "Wieso habe ich XY auf diese Art gelöst?" beantwortet. Beiträge, die nur den Code enthalten werden wir aus dem Thread entfernen.

Nächste Aufgabe
In drei Tagen, am 23. April wird die Musterlösung nach editiert und die nächste Aufgabe eingestellt.

Viel Spaß & viel Erfolg!

Musterlösung:
BlitzBasic: [AUSKLAPPEN]
Graphics 320,240, 0,2						; Grafikfenster
SetBuffer BackBuffer() ; Doublebuffering
SeedRnd MilliSecs() ; Zufallszahlen streuen
Global timer = CreateTimer(50), ticks ; Timer, synchrone Referenz

; Ball
Type TBall
Field x#, y# ; Position
Field sx#, sy# ; Geschwindigkeit
Field col ; Farbe
End Type
Function CreateBall()
Local ball.TBall = New TBall
ball\x = Rand(10,310) ; X-Position 10 .. 310
ball\y = Rand(10,230) ; Y-Position 10 .. 230
ball\sx = Rnd(-1,1) ; X-Geschwindigkeit -1 .. 1
ball\sy = Rnd(-1,1) ; Y-Geschwindigkeit -1 .. 1
ball\col = Rnd(0,$FFFFFF) ; Farbe, völlig zufällig
End Function
Function UpdateBalls()
; bälle bewegen
Local ball.TBall
For ball = Each TBall
ball\x = ball\x +ball\sx
ball\y = ball\y +ball\sy
If ball\x<10 ; Bandenkollision, X
ball\sx = -ball\sx
ball\x = 10
ElseIf ball\x>310
ball\sx = -ball\sx
ball\x = 310
EndIf
If ball\y<10 ; Bandenkollision, Y
ball\sy = -ball\sy
ball\y = 10
ElseIf ball\y>230
ball\sy = -ball\sy
ball\y = 230
EndIf
Next
; kollisionen prüfen
For ball = Each TBall ; Doppelt verschachtelte Schleife
Local collBall.TBall
For collBall = Each TBall
If collBall = ball Then Exit ; selbstprüfung verhindern
; distanz berechnen
Local dx# = collBall\x -ball\x
Local dy# = collBall\y -ball\y
If (dx*dx +dy*dy)<400 ; Distanzprüfung, optimiert
Local tsx# = collBall\sx ; Tauschen der Geschwindigkeiten
Local tsy# = collBall\sy
collBall\sx = ball\sx
collBall\sy = ball\sy
ball\sx = tsx
ball\sy = tsy
EndIf
Next
Next
End Function
Function DrawBalls()
Local ball.TBall
For ball = Each TBall
Color 0,0,ball\col ; individuelle Farbe pro Ball
Oval ball\x-10, ball\y-10, 20,20 ; 20x20Px Oval
Next
End Function

Local i
For i = 0 To 9
CreateBall() ; 10 Bälle erstellen
Next

While Not KeyDown(1) ; Hauptschleife, per ESC beendbar
Cls
For i = 1 To ticks ; synchrone Bewegung
UpdateBalls()
Next
DrawBalls()
Flip 0 ; low-CPU Ausgabe
ticks = WaitTimer(timer) ; FPS-Begrenzung
Wend
End
Win10 Prof.(x64)/Ubuntu 16.04|CPU 4x3Ghz (Intel i5-4590S)|RAM 8 GB|GeForce GTX 960
Wie man Fragen richtig stellt || "Es geht nicht" || Video-Tutorial: Sinus & Cosinus
T
HERE IS NO FAIR. THERE IS NO JUSTICE. THERE IS JUST ME. (Death, Discworld)
  • Zuletzt bearbeitet von Xeres am Sa, Apr 23, 2011 19:25, insgesamt einmal bearbeitet

ToeB

BeitragMi, Apr 20, 2011 17:49
Antworten mit Zitat
Benutzer-Profile anzeigen
Hier mein Beitrag :
BlitzBasic: [AUSKLAPPEN]
Graphics 320, 240, 16, 2
SetBuffer BackBuffer( )

Global gfx_w = GraphicsWidth( )
Global gfx_h = GraphicsHeight( )

;Ball-Typ
Type TBall
Field pos_x#, pos_y#, v_x#, v_y# ;positions und geschwindigkeits-Daten
Field radius
Field r, g, b ;Farbe
End Type

;Timer erstellen
Global timer = CreateTimer( 50 ), frames

;10 Bälle erstellen
SeedRnd MilliSecs( )
For i = 1 To 10
tmp.TBall = Create( Rand( 10, gfx_w-10 ), Rand( 10, gfx_h-10 ), Rnd( 1.0, -1.0 ), Rnd( 1.0, -1.0 ), 10 )
SetColor( tmp, Rand( 32, 255 ), Rand( 32, 255 ), Rand( 32, 255 ) )
Next

Repeat
For i = 1 To frames ;Für evtl. Veräumte Frames die wiederholt werden müssten
Update( ) ;Alle Bälle Updaten (Bewegen + Kollision)
Next
Draw( ) ;Alle Bälle Malen
Flip 0
frames = WaitTimer( timer ) ;Timer ausführen und versäumte Frames speichern
Cls
Until KeyHit( 1 )
End

;Ball mit Position und Geschwindigkeit erstellen
Function Create.TBall( x#, y#, vx#, vy#, rad=10 )
Local tmp.TBall = New TBall
tmp\pos_x = x
tmp\pos_y = y
tmp\v_x = vx
tmp\v_y = vy
tmp\radius = rad
Return tmp
End Function


Function Update( )
For tmp.TBall = Each TBall
;Bewegen
tmp\pos_x = tmp\pos_x + tmp\v_x
tmp\pos_y = tmp\pos_y + tmp\v_y
;Am Bildschirmrand abprallen
If tmp\pos_x+tmp\radius > gfx_w Then tmp\pos_x = gfx_w-tmp\radius : tmp\v_x = -tmp\v_x
If tmp\pos_x-tmp\radius < 0 Then tmp\pos_x = tmp\radius : tmp\v_x = -tmp\v_x
If tmp\pos_y+tmp\radius > gfx_h Then tmp\pos_y = gfx_h-tmp\radius : tmp\v_y = -tmp\v_y
If tmp\pos_y-tmp\radius < 0 Then tmp\pos_y = tmp\radius : tmp\v_y = -tmp\v_y
;Kollision auf andere Bälle prüfen
Local tmp2.TBAll = After tmp ;In der Liste das Element nach diesem Hier nehmen
While tmp2 <> Null
Local dist_x# = tmp\pos_x - tmp2\pos_x
Local dist_y# = tmp\pos_y - tmp2\pos_y
Local dist# = Sqr( dist_x*dist_x + dist_y*dist_y )
Local rad# = tmp\radius + tmp2\radius
If dist <= rad Then
;Geschwindigkeiten Tauschen
Local tmp_v_x# = tmp\v_x, tmp_v_y# = tmp\v_y
tmp\v_x = tmp2\v_x : tmp\v_y = tmp2\v_y
tmp2\v_x = tmp_v_x : tmp2\v_y = tmp_v_y
;den anderen Ball soweit zurücksetzten dass er den Ball nicht mehr brührt
Local ang# = ATan2( dist_y, dist_x )
tmp\pos_x = tmp2\pos_x + Cos( ang ) * rad
tmp\pos_y = tmp2\pos_y + Sin( ang ) * rad
EndIf
tmp2 = After tmp2 ;Nächsten auswählen
Wend
Next
End Function

Function Draw( )
For tmp.TBall = Each TBall
Color( tmp\r, tmp\g, tmp\b )
Oval( tmp\pos_x-tmp\radius, tmp\pos_y-tmp\radius, tmp\radius*2, tmp\radius*2 )
Next
End Function

;Um farbe zu setzten
Function SetColor( tmp.TBall, r, g, b )
tmp\r = r
tmp\g = g
tmp\b = b
End Function


Erklärung in den Kommentaren, sonst einfach Fragen Wink

mfg ToeB
Religiöse Kriege sind Streitigkeiten erwachsener Männer darum, wer den besten imaginären Freund hat.
Race-Project - Das Rennspiel der etwas anderen Art
SimpleUDP3.0 - Neuste Version der Netzwerk-Bibliothek
Vielen Dank an dieser Stelle nochmal an Pummelie, welcher mir einen Teil seines VServers für das Betreiben meines Masterservers zur verfügung stellt!

Neue Antwort erstellen


Übersicht BlitzBasic Beginners-Corner

Gehe zu:

Powered by phpBB © 2001 - 2006, phpBB Group