Geschwindigkeits-optimierungs Vorschläge

Übersicht BlitzBasic Allgemein

Neue Antwort erstellen

 

Hendy

Betreff: Geschwindigkeits-optimierungs Vorschläge

BeitragMi, Sep 09, 2009 20:29
Antworten mit Zitat
Benutzer-Profile anzeigen
Schönen Guten Abend an alle,

wie schön die Überschrift erahnen lässt möchte ich mir ein paar möglichkeiten zeigenlassen wie ich meinen Code beschleunigen kann da bereits ab 12 Objekten (=Inf Type) die Geschwindigkeit deutlich vermindert wird.

Wer den Code ausprobieren will:

Linke Maustaste = Schießen
Mittlere Maustaste = 20 Inf Objekte erstellen
Rechte Maustaste = Alle Inf Objekte löschen
B = "kaufenmenü" wird Benötigt um mit
+ (auf dem Ziffernblock) = Waffe wechseln
Esc = beenden

Ich habe das Startgeld auf 20000 gestellt zum ausprobieren.

Hoffe auf viele Hilfreiche antworten.

P.S. wenn die rote Linie mit den Inf Objekten in Kontakt kommt verliert man Leben, links in der Mitte wird das Leben angezeigt (1000 Startwert)


BlitzBasic: [AUSKLAPPEN]
Graphics 1024, 768,0,2
SetBuffer BackBuffer()


Type Inf
Field xpos
Field ypos
Field leben
Field speed
Field xbeweg
Field ybeweg
End Type


Type schuss
Field x_Pos#
Field y_Pos#
Field winkel#
End Type


Global Spielerleben = 1000
Global killed = 0
Global Cash% = 20000
Global s.schuss
Global a.Inf
Global trigger = 0
Global revolver = 1
Global Sharpsh = 0
Global MG = 0
Global Schrot = 0
Global aktuellewaffe = 1
;------------------------------------------------------------Hauptschleife------------------------------------------------------------------

Repeat
Cls

gegner()
schuss()
Turm()


If KeyHit(48) Then kaufen()
Waffewechsel()



Flip
Until KeyHit(1)
End


;------------------------------------------------------------Funktionen---------------------------------------------------------------------
Function kaufen()
Cls
Color 255,255,255
Rect 100,100, 824,524
fntArial=LoadFont("Arial",24,True,False,True)
SetFont fntArial
Color 0,0,0
Text 400,100, "Dorena's Waffenlager"
fntArial=LoadFont("Arial",18,True,False,False)
SetFont fntArial
Text 600, 300, "Aktuelles Guthaben: " + cash%+" Euro"
FreeFont fntArial

Text 100, 300, "Scharfschützengewehr, Kosten: 1000 Euro, Kennziffer 1"
Text 100, 320, "Maschienengewehr MG 40, Kosten: 1500 Euro, Kennziffer 2"
Text 100, 340, "Schrotflinte, Kosten: 1500 Euro, Kennziffer 3"
Text 100, 500, "Zum Abrechen: A"
FlushKeys
Locate 100,400

fntArial=LoadFont("Arial",18,True,False,False)
SetFont fntArial
BuyAuswahl$ = Input$("Bitte hier Ihre Wahl: ")
FreeFont fntArial
If BuyAuswahl$ = "a" Then Return
If BuyAuswahl$ = 1 Then
If Cash% < 1000 Then
FlushKeys
Cls
Color 255,255,255
fntArial=LoadFont("Arial",18,True,False,False)
SetFont fntArial
Text 25, 300, "Aktuelles Guthaben beträgt weniger als 1000 Euro es Reicht nicht aus für dieses Scharfschützengewehr! Beliebige Taste zum Fortfahren"
WaitKey
FreeFont fntArial
kaufen()
ElseIf Cash% >= 1000 Then
Sharpsh = 1
Cash%= Cash% -1000
EndIf
EndIf

If BuyAuswahl$ = 2 Then
If Cash% < 1500 Then
FlushKeys
Cls
Color 255,255,255
fntArial=LoadFont("Arial",18,True,False,False)
SetFont fntArial
Text 80, 300, "Aktuelles Guthaben beträgt weniger als 1500 Euro es Reicht nicht aus für diese MG! Beliebige Taste zum Fortfahren"
WaitKey
FreeFont fntArial
kaufen()
ElseIf Cash% >= 1500 Then
MG = 1
Cash%= Cash% -1500
EndIf
EndIf

If BuyAuswahl$ = 3 Then
If Cash% < 1500 Then
FlushKeys
Cls
Color 255,255,255
fntArial=LoadFont("Arial",18,True,False,False)
SetFont fntArial
Text 35, 300, "Aktuelles Guthaben beträgt weniger als 1500 Euro es Reicht nicht aus für diese Schrotflinte! Beliebige Taste zum Fortfahren"
WaitKey
FreeFont fntArial
kaufen()
ElseIf Cash% >= 1500 Then
Schrot = 1
Cash%= Cash% -1500
EndIf
EndIf




FlushKeys

End Function

;--------------------------------------------------------------------------------------


Function Waffewechsel()

If KeyHit(78) Then
If aktuellewaffe = 1 Then
If sharpsh = 1 Then
aktuellewaffe = 2
ElseIf sharpsh = 0 Then
If MG = 1 Then
aktuellewaffe = 3
ElseIf MG = 0 Then
If Schrot = 1 Then
Aktuellewaffe = 4
ElseIf Schrot = 0 Then
Aktuellewaffe = 1
EndIf
EndIf
EndIf


ElseIf aktuellewaffe = 2 Then
If MG = 1 Then
aktuellewaffe=3
ElseIf MG = 0 Then
If Schrot = 1 Then
aktuellewaffe = 4
ElseIf Schrot = 0 Then
aktuellewaffe = 1
EndIf
EndIf
ElseIf aktuellewaffe = 3 Then
If schrot = 1 Then
aktuellewaffe = 4
ElseIf schrot = 0 Then
aktuellewaffe = 1
EndIf
ElseIf aktuellewaffe = 4 Then
aktuellewaffe= 1

EndIf
EndIf




End Function





;--------------------------------------------------------------------------------------


Function schuss()
x1= MouseX()
y1= MouseY()

w# = ATan2(x1-505,y1-205)


If aktuellewaffe = 1 Then
Text 0,0, "Waffe: Revolver"
EndIf
If aktuellewaffe = 2 Then
Text 0,0, "Waffe: Scharfschützengewehr"
EndIf
If aktuellewaffe = 3 Then
Text 0,0, "Waffe: MG"
EndIf
If aktuellewaffe = 4 Then
Text 0,0, "Waffe: Schrotflinte"
EndIf



If MouseHit(1) Then
If aktuellewaffe = 1 Then
s = New schuss
s\x_Pos#= 505
s\y_Pos#= 205
s\winkel# = w#
Color 255,255,0

ElseIf aktuellewaffe = 2 Then
s = New schuss
s\x_Pos#= 505
s\y_Pos#= 205
s\winkel# = w#
Color 255,255,0

ElseIf aktuellewaffe = 4 Then

For i= 1 To 10
s = New schuss
s\x_Pos#= 505
s\y_Pos#= 205
s\winkel# = w#+ Rnd(-40,40)
Color 0,0,255

Next

EndIf
EndIf

If MouseDown(1) Then
If aktuellewaffe = 3 Then
trigger = trigger +1

If trigger = 7 Then

s = New schuss
s\x_Pos#= 505
s\y_Pos#= 205
s\winkel# = w# + Rnd(-0.3,0.3)
Color 255,255,0
trigger = 0
EndIf
EndIf
EndIf




For s = Each schuss

For a = Each Inf



If LinesIntersect(s\x_Pos,s\y_Pos,s\x_Pos+Sin(s\winkel#)*10,s\y_Pos+Cos(s\winkel#)*10,a\xpos,a\ypos,a\xpos+10,a\ypos+10) Then
If aktuellewaffe = 1 Then
a\leben = a\leben - 20
EndIf

If aktuellewaffe = 2 Then
a\leben = a\leben - 100
EndIf

If aktuellewaffe = 3 Then
a\leben =a\leben -30
EndIf

If aktuellewaffe= 4 Then
a\leben =a\leben -15
EndIf
EndIf

If LinesIntersect(s\x_Pos,s\y_Pos,s\x_Pos+Sin(s\winkel#)*10,s\y_Pos+Cos(s\winkel#)*10,a\xpos,a\ypos+10,a\xpos+10, a\ypos) Then

If aktuellewaffe = 1 Then
a\leben = a\leben - 20
EndIf

If aktuellewaffe = 2 Then
a\leben = a\leben - 100
EndIf

If aktuellewaffe = 3 Then
a\leben =a\leben -30
EndIf

If aktuellewaffe= 4 Then
a\leben =a\leben -15
EndIf
EndIf




If a\Leben <= 0 Then
Cash% = Cash% + a\speed * 100
Delete a
killed=killed +1
EndIf


Next

If aktuellewaffe = 1
Line s\x_Pos,s\y_Pos,s\x_Pos+Sin(s\winkel#)*10,s\y_Pos+Cos(s\winkel#)*10
s\x_Pos#= s\x_Pos#+ Sin(s\winkel#) * 20
s\y_Pos#= s\y_Pos#+ Cos(s\winkel#) * 20
EndIf
If aktuellewaffe = 2
Line s\x_Pos,s\y_Pos,s\x_Pos+Sin(s\winkel#)*20,s\y_Pos+Cos(s\winkel#)*20
s\x_Pos#= s\x_Pos#+ Sin(s\winkel#) * 20
s\y_Pos#= s\y_Pos#+ Cos(s\winkel#) * 20
EndIf

If aktuellewaffe = 3
Line s\x_Pos,s\y_Pos,s\x_Pos+Sin(s\winkel#)*10,s\y_Pos+Cos(s\winkel#)*10
s\x_Pos#= s\x_Pos#+ Sin(s\winkel#) * 20
s\y_Pos#= s\y_Pos#+ Cos(s\winkel#) * 20
EndIf

If aktuellewaffe = 4
Line s\x_Pos,s\y_Pos,s\x_Pos+Sin(s\winkel#)*1,s\y_Pos+Cos(s\winkel#)*1
s\x_Pos#= s\x_Pos#+ Sin(s\winkel#) * 20
s\y_Pos#= s\y_Pos#+ Cos(s\winkel#) * 20
EndIf



Next

Text 300, 0, "Cash: "+Cash%
Text 0,20,"Getötet: " + killed
Color 255,255,255

End Function


;--------------------------------------------------------------------------------------



Function Turm()
x1= MouseX()
y1= MouseY()

Oval 500, 200, 12, 12 ,1


Color 255,0,0
Line 505,205,x1, y1

For a = Each Inf
If linesintersect(a\xpos,a\ypos,a\xpos+10,a\ypos+10,505,205,x1, y1) Then
Spielerleben = Spielerleben - 10
EndIf

If linesintersect(a\xpos,a\ypos+10,a\xpos+10, a\ypos,505,205,x1, y1) Then
Spielerleben = Spielerleben -10
EndIf


Next

Color 255,255,255

If spielerleben =< 0 Then
Cls

FlushKeys

fntArial=LoadFont("Arial",24,True,False,True)

SetFont fntArial

Text 400,100, "Verloren!!!!!!!!!!!!!!!!!!"
Flip
WaitKey

End
EndIf

Text 0,600, spielerleben



For s = Each schuss
If s\x_pos <= 0 Then
Delete s
ElseIf s\x_pos >= 1024 Then
Delete s
ElseIf s\y_pos <= 0 Then
Delete s
ElseIf s\y_pos >= 768 Then
Delete s
EndIf
Next
End Function





;--------------------------------------------------------------------------------------


Function Gegner()

If MouseHit(3) Then
For t = 1 To 20
a.Inf = New Inf
a\xpos# = Rnd(0,1024)
a\ypos# = Rnd(0,768)
a\leben = 100
a\speed = Rnd(1,3)
a\xbeweg = Rnd(0,1)
a\ybeweg = Rnd(0,1)
Next
End If

If MouseHit(2) Then
Delete Each Inf
End If

For a.Inf = Each Inf
Line a\xpos,a\ypos,a\xpos+10,a\ypos+10
Line a\xpos,a\ypos+10,a\xpos+10, a\ypos

If a\xpos >= 1024 Then
a\xbeweg = 1
ElseIf a\xpos =< 0 Then
a\xbeweg = 0
EndIf

If a\ypos >= 768 Then
a\ybeweg = 1
ElseIf a\ypos =< 0 Then
a\ybeweg = 0
EndIf


If a\xbeweg = 0 Then
a\xpos = a\xpos + a\speed
ElseIf a\xbeweg = 1 Then
a\xpos = a\xpos - a\speed
EndIf

If a\ybeweg = 0 Then
a\ypos = a\ypos + a\speed
ElseIf a\ybeweg = 1 Then
a\ypos = a\ypos - a\speed
EndIf



Text a\xpos-70,a\ypos+10, "Health: " + a\leben +" Speed: "+a\speed

Next



End Function









Function LinesIntersect(ax#, ay#, bx#, by#, cx#, cy#, dx#, dy#)
rn# = (ay#-cy#)*(dx#-cx#) - (ax#-cx#)*(dy#-cy#)
rd# = (bx#-ax#)*(dy#-cy#) - (by#-ay#)*(dx#-cx#)
If rd# = 0
Return False
Else
sn# = (ay#-cy#)*(bx#-ax#) - (ax#-cx#)*(by#-ay#)
intersection_ab# = rn# / rd#
intersection_cd# = sn# / rd#
intersection_x# = ax# + intersection_ab#*(bx#-ax#)
intersection_y# = ay# + intersection_ab#*(by#-ay#)

If intersection_ab#>0 And intersection_ab#<1 And intersection_cd#>0 And intersection_cd#<1 Then Return True
EndIf
End Function
Smile Very Happy Very HappyD

ozzi789

BeitragMi, Sep 09, 2009 20:48
Antworten mit Zitat
Benutzer-Profile anzeigen
1. Brauche nicht Text, schau dir Speedtext an (forum suche;))
2. Lade die Fonts am Anfang und lasse sie im Speicher (besser als jedesmal neu laden afaik)
3. Rücke den Code ein
4. Programmiere übersichtlicher (zmb =>)

Code: [AUSKLAPPEN]

If BuyAuswahl$ = 2 Then gun$="MG": kosten%=1500
If BuyAuswahl$ = 1 Then gun$="ROCKETWOUF0R":kosten%=1000
; USW ....

   If Cash% < 1500 Then   
      FlushKeys
      Cls
      Color 255,255,255
      fntArial=LoadFont("Arial",18,True,False,False)
      SetFont fntArial
      Text 80, 300, "Aktuelles Guthaben beträgt weniger als 1500 Euro es Reicht nicht aus für diese "+gun$+" Beliebige Taste zum Fortfahren"
      WaitKey
      FreeFont fntArial
         kaufen()
      ElseIf Cash% >= 1500 Then
      MG = 1
      Cash%= Cash% - kosten%
   EndIf


Anstatt für jede Waffe so nen Codeblock zu erstellen, ist sonst sehr hardgecodet, naja ist es immer noch aber flexibler


5. Frage MouseX() / MouseY() nur einmal pro Schleife ab und speichere die Werte in Variablen
6. Was soll 3 mal CLS? XD

Code: [AUSKLAPPEN]

If spielerleben =< 0 Then
Cls

Cls

Cls
FlushKeys


7.Rnd ist für Floats, nimm Rand für Integer
8.Wieso brauchst du das Lineintersect, es genügt ja auch eine rechteck kollision für den Schuss/Gegner (denke mal die Function ist für das)


mfg ozzi
0x2B || ! 0x2B
C# | C++13 | Java 7 | PHP 5
 

Hendy

BeitragMi, Sep 09, 2009 20:54
Antworten mit Zitat
Benutzer-Profile anzeigen
Danke für deine schnelle Antwort werde das erstmal umsetzen.
Die 3 mal CLS hatte ich vergessen zu löschen war noch von etwas vorherigem. XD

Ich hatte, als ich begonnen hatte diesen Code zu schreiben, nicht daran gedacht das ich auch Rectcollide benutzen kann, aber ich finde Linesintersect ist ne tolle sache Wink.

EDIT1:
MFG
Hendy
Smile Very Happy Very HappyD

Eingeproggt

BeitragMi, Sep 09, 2009 22:16
Antworten mit Zitat
Benutzer-Profile anzeigen
Und ganz wichtig: Verwende in deiner Hauptschleife einen Timer (CreateTimer, Waittimer) und verwende statt Flip Flip 0

Warum und wieso?
--> Deshalb

mfG, Christoph.
Gewinner des BCC 18, 33 und 65 sowie MiniBCC 9
 

Hendy

BeitragDo, Sep 10, 2009 21:51
Antworten mit Zitat
Benutzer-Profile anzeigen
jap das macht sinn:)
Smile Very Happy Very HappyD

Neue Antwort erstellen


Übersicht BlitzBasic Allgemein

Gehe zu:

Powered by phpBB © 2001 - 2006, phpBB Group