GDI+ Schachbrettspiel, Geschwindigkeitsprobleme

  • Moin zusammen

    Ich habe gerade angefangen, mit GDI+ zu skripten. Nun hab ich ein etwas komplizierteres Projekt pausiert, um mich mit der Funktion

    [autoit]

    _GDIPlus_GraphicsDrawRect

    [/autoit]


    auseinander zu setzen und da ich ein Fan von konzentrationsspielen bin, dachte ich mir, ich versuch mal etwas ganz einfaches zu diesem Thema auf die Beine zu stellen.
    Nun herausgekommen ist der untenstehende Code, das Programm funktioniert auch grundsätzlich.

    Regeln:
    - Klickt die erscheinenden Roten Quadrate auf dem Schachbrett an, ihr habt 5 Sekunden Zeit
    - Alle 10 Punkte wird die Anzahl der Quadrate auf dem Schachfeld verdoppelt
    - Es gibt max. 5 Runden zu spielen (weiter wollte ich aus Zeitgründen nicht gehen)

    So...wie gesagt, die Grundfunktionalität ist vorhanden, allerdings werdet ihr bemerken, dass das Schachbrett bereits ab der 3. Stufe nach jedem Punk flackert, auf der letzten Stufe dauert es beinahe 1/2 Sekunde bis das Feld geladen ist.

    Ich wäre mir froh, wenn mir jemand helfen könnte, ich glaube ich habe den Buffer irgendwie total seltsam ("schrägstrich" falsch) platziert.


    Spoiler anzeigen
    [autoit]

    #include <GuiConstants.au3>
    #include <GdiPlus.au3>;das include
    #include <Misc.au3>
    #include <ButtonConstants.au3>
    #include <StaticConstants.au3>
    #include <WindowsConstants.au3>

    [/autoit] [autoit][/autoit] [autoit]

    OnAutoItExitRegister("_end") ; die Funktion _end am Ende des Scriptes ausführen

    [/autoit] [autoit][/autoit] [autoit]

    $size = 80
    $score = 0
    $Gui = GUICreate("Schachbrett", 800, 800, -1, -1)
    GUISetState(@SW_SHOW)
    _GDIPlus_Startup() ;Gdi starten
    $graphic = _GDIPlus_GraphicsCreateFromHWND($Gui)
    $bitmap = _GDIPlus_BitmapCreateFromGraphics(800, 800, $graphic)
    $buffer = _GDIPlus_ImageGetGraphicsContext($bitmap)
    _drawChess()
    $bitmap = _GDIPlus_BitmapCreateFromGraphics(800, 800, $graphic)
    $buffer = _GDIPlus_ImageGetGraphicsContext($bitmap)
    $timer = TimerInit()

    [/autoit] [autoit][/autoit] [autoit]

    While 1
    _drawChess()
    While 1
    Do
    $xRandom = Random(0, 800 - $size, 1)
    Until IsInt($xRandom / $size)
    Do
    $yRandom = Random(0, 800 - $size, 1)
    Until IsInt($yRandom / $size)
    $redButton = GUICtrlCreateLabel("", $xRandom, $yRandom, $size, $size)
    GUICtrlSetBkColor(-1, 0xFF0000)
    While 1
    Switch GUIGetMsg()
    Case $GUI_EVENT_CLOSE
    Exit
    Case $redButton
    $timer = TimerInit()
    GUICtrlDelete($redButton)
    $score += 10
    If IsInt($score / 100) Then
    $size = $size / 2
    ExitLoop 2
    EndIf
    If $score = 500 Then
    $return = _win()
    If $return =1 Then
    Exit
    Else
    GUIDelete($Gui)
    $Gui = GUICreate("Schachbrett", 800, 800, -1, -1)
    GUISetState(@SW_SHOW)
    $timer = TimerInit()
    ExitLoop 2
    EndIf
    EndIf
    _drawChess()
    ExitLoop
    EndSwitch
    If TimerDiff($timer) > 5000 Then
    $return = _gameOver($score)
    If $return = 1 Then
    Exit
    Else
    GUIDelete($Gui)
    $Gui = GUICreate("Schachbrett", 800, 800, -1, -1)
    GUISetState(@SW_SHOW)
    $timer = TimerInit()
    ExitLoop 2
    EndIf
    EndIf
    WEnd
    WEnd
    WEnd

    [/autoit] [autoit][/autoit] [autoit]

    Func _gameOver($score)
    GUICtrlDelete($redButton)
    GUIDelete($Gui)
    _drawChess()
    $Gui = GUICreate("Schachbrett", 800, 800, -1, -1)
    GUISetState(@SW_SHOW)
    $Label1 = GUICtrlCreateLabel("Game Over!", 80, 120, 589, 122)
    GUICtrlSetFont(-1, 72, 400, 0, "MS Reference Sans Serif")
    $Label2 = GUICtrlCreateLabel("Your score is: ", 75, 295, 295, 53)
    GUICtrlSetFont(-1, 30, 400, 0, "MS Reference Sans Serif")
    $Label3 = GUICtrlCreateLabel($score, 380, 295, 295, 53, $SS_RIGHT)
    GUICtrlSetFont(-1, 30, 400, 0, "MS Reference Sans Serif")
    $Button1 = GUICtrlCreateButton("Replay", 75, 470, 280, 75)
    GUICtrlSetFont(-1, 30, 400, 0, "MS Reference Sans Serif")
    $Button2 = GUICtrlCreateButton("Quit", 395, 470, 280, 75)
    GUICtrlSetFont(-1, 30, 400, 0, "MS Reference Sans Serif")
    While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $GUI_EVENT_CLOSE
    Exit
    Case $Button1
    $return = 0
    ExitLoop
    Case $Button2
    $return = 1
    ExitLoop
    EndSwitch
    WEnd
    Return $return
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func _win()
    GUICtrlDelete($redButton)
    GUIDelete($Gui)
    _drawChess()
    $Gui = GUICreate("Schachbrett", 800, 800, -1, -1)
    GUISetState(@SW_SHOW)
    $Label1 = GUICtrlCreateLabel("Congratulations!", 80, 120, 589, 122)
    GUICtrlSetFont(-1, 50, 400, 0, "MS Reference Sans Serif")
    $Label2 = GUICtrlCreateLabel("You won!!!!!", 75, 295, 295, 53, $SS_CENTER)
    GUICtrlSetFont(-1, 30, 400, 0, "MS Reference Sans Serif")
    $Button1 = GUICtrlCreateButton("Replay", 75, 470, 280, 75)
    GUICtrlSetFont(-1, 30, 400, 0, "MS Reference Sans Serif")
    $Button2 = GUICtrlCreateButton("Quit", 395, 470, 280, 75)
    GUICtrlSetFont(-1, 30, 400, 0, "MS Reference Sans Serif")
    While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $GUI_EVENT_CLOSE
    Exit
    Case $Button1
    $return = 0
    ExitLoop
    Case $Button2
    $return = 1
    ExitLoop
    EndSwitch
    WEnd
    Return $return
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func _drawChess()
    $whitePen = _GDIPlus_BrushCreateSolid(0xFFFFFFFF)
    $blackPen = _GDIPlus_BrushCreateSolid(0xFF000000)
    $redPen = _GDIPlus_BrushCreateSolid(0xFFFF0000)
    _GDIPlus_GraphicsFillRect($graphic, 0, 0, 800, 800, $whitePen)
    For $i = 0 To 800 Step $size * 2
    For $j = $size To 800 Step $size * 2
    _GDIPlus_GraphicsFillRect($graphic, $j, $i, $size, $size, $blackPen)
    _GDIPlus_GraphicsFillRect($graphic, $j - $size, $i - $size, $size, $size, $blackPen)
    Next
    Next
    _GDIPlus_GraphicsDrawImage($graphic, $bitmap, 0, 0)
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func _end();die Exit funktion
    _GDIPlus_GraphicsDispose($graphic);Grafik Objekt freigeben
    _GDIPlus_GraphicsDispose($buffer)
    _GDIPlus_BitmapDispose($bitmap)
    _GDIPlus_Shutdown();Ressourcen freigeben
    EndFunc ;==>_end

    [/autoit]

    Bild1: Ich beim debuggen

    Einmal editiert, zuletzt von General Kaboom (30. Januar 2013 um 16:01)

  • so vieleicht

    Spoiler anzeigen
    [autoit]

    #include <GuiConstants.au3>
    #include <GdiPlus.au3>;das include
    #include <Misc.au3>
    #include <ButtonConstants.au3>
    #include <StaticConstants.au3>
    #include <WindowsConstants.au3>

    [/autoit] [autoit][/autoit] [autoit]

    OnAutoItExitRegister("_end") ; die Funktion _end am Ende des Scriptes ausführen

    [/autoit] [autoit][/autoit] [autoit]

    $size = 80
    $score = 0
    $Gui = GUICreate("Schachbrett", 800, 800, -1, -1)
    GUISetState(@SW_SHOW)
    _GDIPlus_Startup() ;Gdi starten
    $graphic = _GDIPlus_GraphicsCreateFromHWND($Gui)
    $bitmap = _GDIPlus_BitmapCreateFromGraphics(800, 800, $graphic)
    $buffer = _GDIPlus_ImageGetGraphicsContext($bitmap)
    _drawChess()
    $timer = TimerInit()

    [/autoit] [autoit][/autoit] [autoit]

    While 1
    While 1
    Do
    $xRandom = Random(0, 800 - $size, 1)
    Until IsInt($xRandom / $size)
    Do
    $yRandom = Random(0, 800 - $size, 1)
    Until IsInt($yRandom / $size)
    $redButton = GUICtrlCreateLabel("", $xRandom, $yRandom, $size, $size)
    GUICtrlSetBkColor(-1, 0xFF0000)
    While 1
    Switch GUIGetMsg()
    Case $GUI_EVENT_CLOSE
    Exit
    Case $redButton
    $timer = TimerInit()
    GUICtrlDelete($redButton)
    $score += 10
    If IsInt($score / 100) Then
    $size = $size / 2
    _drawChess()
    EndIf
    If $score = 500 Then
    $return = _win()
    If $return =1 Then
    Exit
    Else
    GUIDelete($Gui)
    $Gui = GUICreate("Schachbrett", 800, 800, -1, -1)
    GUISetState(@SW_SHOW)
    $timer = TimerInit()
    ExitLoop 2
    EndIf
    EndIf
    _GDIPlus_GraphicsDrawImage($graphic, $bitmap, 0, 0)
    ExitLoop
    EndSwitch
    If TimerDiff($timer) > 5000 Then
    $return = _gameOver($score)
    If $return = 1 Then
    Exit
    Else
    GUIDelete($Gui)
    $Gui = GUICreate("Schachbrett", 800, 800, -1, -1)
    GUISetState(@SW_SHOW)
    $timer = TimerInit()
    ExitLoop 2
    EndIf
    EndIf
    WEnd
    WEnd
    WEnd

    [/autoit] [autoit][/autoit] [autoit]

    Func _gameOver($score)
    GUICtrlDelete($redButton)
    GUIDelete($Gui)
    $Gui = GUICreate("Schachbrett", 800, 800, -1, -1)
    GUISetState(@SW_SHOW)
    $Label1 = GUICtrlCreateLabel("Game Over!", 80, 120, 589, 122)
    GUICtrlSetFont(-1, 72, 400, 0, "MS Reference Sans Serif")
    $Label2 = GUICtrlCreateLabel("Your score is: ", 75, 295, 295, 53)
    GUICtrlSetFont(-1, 30, 400, 0, "MS Reference Sans Serif")
    $Label3 = GUICtrlCreateLabel($score, 380, 295, 295, 53, $SS_RIGHT)
    GUICtrlSetFont(-1, 30, 400, 0, "MS Reference Sans Serif")
    $Button1 = GUICtrlCreateButton("Replay", 75, 470, 280, 75)
    GUICtrlSetFont(-1, 30, 400, 0, "MS Reference Sans Serif")
    $Button2 = GUICtrlCreateButton("Quit", 395, 470, 280, 75)
    GUICtrlSetFont(-1, 30, 400, 0, "MS Reference Sans Serif")
    While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $GUI_EVENT_CLOSE
    Exit
    Case $Button1
    $return = 0
    ExitLoop
    Case $Button2
    $return = 1
    ExitLoop
    EndSwitch
    WEnd
    Return $return
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func _win()
    GUICtrlDelete($redButton)
    GUIDelete($Gui)
    $Gui = GUICreate("Schachbrett", 800, 800, -1, -1)
    GUISetState(@SW_SHOW)
    $Label1 = GUICtrlCreateLabel("Congratulations!", 80, 120, 589, 122)
    GUICtrlSetFont(-1, 50, 400, 0, "MS Reference Sans Serif")
    $Label2 = GUICtrlCreateLabel("You won!!!!!", 75, 295, 295, 53, $SS_CENTER)
    GUICtrlSetFont(-1, 30, 400, 0, "MS Reference Sans Serif")
    $Button1 = GUICtrlCreateButton("Replay", 75, 470, 280, 75)
    GUICtrlSetFont(-1, 30, 400, 0, "MS Reference Sans Serif")
    $Button2 = GUICtrlCreateButton("Quit", 395, 470, 280, 75)
    GUICtrlSetFont(-1, 30, 400, 0, "MS Reference Sans Serif")
    While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $GUI_EVENT_CLOSE
    Exit
    Case $Button1
    $return = 0
    ExitLoop
    Case $Button2
    $return = 1
    ExitLoop
    EndSwitch
    WEnd
    Return $return
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func _drawChess()
    _GDIPlus_GraphicsClear($buffer,0xFFFFFFFF)
    For $i = 0 To 800 Step $size * 2
    For $j = $size To 800 Step $size * 2
    _GDIPlus_GraphicsFillRect($buffer, $j, $i, $size, $size)
    _GDIPlus_GraphicsFillRect($buffer, $j - $size, $i - $size, $size, $size)
    Next
    Next
    _GDIPlus_GraphicsDrawImage($graphic, $bitmap, 0, 0)
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func _end();die Exit funktion
    _GDIPlus_GraphicsDispose($graphic);Grafik Objekt freigeben
    _GDIPlus_GraphicsDispose($buffer)
    _GDIPlus_BitmapDispose($bitmap)
    _GDIPlus_Shutdown();Ressourcen freigeben
    EndFunc ;==>_end

    [/autoit]

    ps: das es später solange dauert liegt dann glaube ich an der geschwindigkeit von autoit

    Einmal editiert, zuletzt von gem (30. Januar 2013 um 16:06)

  • hab es nochmal geändert. es wird nur das bitmap neu gezeichnet wenn sich die größe verändert das heißt das läd später nicht so lange

  • @Make-Grafik
    ich hab es auch schon einmal geschafft :)

    ps: ich finde es sehr gut allerdings hätte ich es gemacht das wenn man falsch klickt verliert, ist dann ncoh schwieriger :)