Jump to content

help with unexplained time delayed close


Recommended Posts

I've got a game I am at the beginning of working on.

#cs ----------------------------------------------------------------------------
AutoIt Version: 3.3.6.1
Author:      Garth Bigelow
Script Function:
Template AutoIt script.
#ce ----------------------------------------------------------------------------
; Script Start - Add your code below here
#include <GUIConstantsEx.au3>
#Include <Misc.au3>
#include <Timers.au3>
#include <WindowsConstants.au3>
Global Const $WidthX = 15
Global Const $WidthY = 9
Global Const $WormHead      = 1 ; current point
Global Const $WormBody      = 2   ; one life lost
Global Const $DecayedWormBody = 3 ; no action
Global Const $Flowers        = 4 ; no action
Global Const $Death        = 5 ; one life lost
Global Const $Elevator      = 6 ; one level gained
Global Const $Gift          = 7 ; 100 to 1000 points
Global Const $PacMan          = 8 ; pacman mode (gooble items for 100 points per item value for 4 to 9 rounds) death on void regardless
Global Const $Devolve        = 9 ; all shapes (except voids, worm bodies and fields) go backwards one item value
Global Const $Angel        = 10 ; one life gained
Global Const $Devil  = 11 ; 0 to 3 lives lost
Global Const $ExpressElevator = 12 ; 4 levels gained
Global Const $Cross        = 13 ; all shapes (except voids) cleared to field
Global Const $BrickWall    = 14 ; death
Global Const $Void          = 15 ; death & never gets erased
; death leaves a void in its space
; SCORING:
;  movement one point per level
;   leveling up, 500 points * level
;   Gift, Pacman bonuses
; elevator locks in points gained during level
; Movements
Global Const $mvDownLeft = 1
Global Const $mvLeft  = 2
Global Const $mvUpLeft  = 3
Global Const $mvUp   = 4
Global Const $mvUpRight  = 5
Global Const $mvRight  = 6
Global Const $mvDownRight = 7
Global Const $mvDown  = 8
Global Const $keyForceField = 9
Global Const $keyPause   = 10
Global $Field
Dim $Field[16][10]
Dim $FieldHandle[16][10]
Global $PosX  = Rand($WidthX)
Global $PosY  = Rand($WidthY)
Global $DirectionX = 0, $DirectionY = 1
Global $Level = 0, $Score = 0, $LevelScore = 0, $ClearStrip
Global $Lives = 3, $ForceField = 0
GUICreate("Reflex", 1024, 640)
GUISetState()
InitializeField()
UpLevel()
$dll = DllOpen("user32.dll")
Do
$msg = GUIGetMsg()
$StartTime = _Timer_Init()
    While _Timer_Diff($StartTime) < (1010 - ($Level * 10))
        ReadyMovement()
    WEnd
Tick()
Move()
Evolve()
Until $msg = $GUI_EVENT_CLOSE
DllClose($dll)
;
;
;
Func Move()
$Field[$PosX][$PosY] = $WormBody
SetOnField($PosX, $PosY, $WormBody)
$PosX += $DirectionX
$PosY += $DirectionY
If $PosX < 0 Then $PosX = $WidthX
If $PosX > $WidthX Then $PosX = 0
If $PosY < 0 Then $PosY = $WidthY
If $PosY > $WidthY Then $PosY = 0
SetOnField($PosX, $PosY, $WormHead)
EndFunc
Func Tick()
Beep(500, 20)
EndFunc
Func ReadyMovement()
Switch GetKeys()
  Case $mvDownLeft
   $DirectionX = -1
   $DirectionY = 1
  Case $mvDown
   $DirectionX = 0
   $DirectionY = 1
  Case $mvDownRight
   $DirectionX = 1
   $DirectionY = 1
  Case $mvLeft
   $DirectionX = -1
   $DirectionY = 0
  Case $mvUpLeft
   $DirectionX = -1
   $DirectionY = -1
  Case $mvUp
   $DirectionX = 0
   $DirectionY = -1
  Case $mvUpRight
   $DirectionX = 1
   $DirectionY = -1
  Case $mvRight
   $DirectionX = 1
   $DirectionY = 0
EndSwitch
EndFunc
Func GetKeys()
If _IsPressed("61", $dll) Then Return $mvDownLeft
If _IsPressed("62", $dll) Then Return $mvDown
If _IsPressed("28", $dll) Then Return $mvDown
If _IsPressed("63", $dll) Then Return $mvDownRight
If _IsPressed("64", $dll) Then Return $mvLeft
If _IsPressed("25", $dll) Then Return $mvLeft
If _IsPressed("66", $dll) Then Return $mvRight
If _IsPressed("27", $dll) Then Return $mvRight
If _IsPressed("67", $dll) Then Return $mvUpLeft
If _IsPressed("68", $dll) Then Return $mvUp
If _IsPressed("26", $dll) Then Return $mvUp
If _IsPressed("69", $dll) Then Return $mvUpRight
If _IsPressed("20", $dll) Then Return $keyForceField
If _IsPressed("50", $dll) Then Return $keyPause
If _IsPressed("13", $dll) Then Return $keyPause
Return 0
EndFunc
Func UpLevel()
$Score += ($Level * 500)
$Score += $LevelScore
$LevelScore = 0
$Level += 1
$ClearStrip = Rand($WidthX)
DrawField()
EndFunc
Func InitializeField()
For $x = 0 To $WidthX
  For $y = 0 To $WidthY
   $Field[$x][$y] = $Flowers
;   $FieldHandle[$x][$y] = GUICtrlCreatePic (PictureName($Flowers), $x * 64, $y * 64, 64, 64)
  Next
Next
EndFunc
Func DrawField()
For $x = 0 To $WidthX
  For $y = 0 To $WidthY
   If $Field[$x][$y] <> $Void Then $Field[$x][$y] = $Flowers
   $FieldHandle[$x][$y] = GUICtrlCreatePic (PictureName($Flowers), $x * 64, $y * 64, 64, 64)
  Next
Next
EndFunc
Func Evolve()
Do
  $x = Rand($WidthX)
Until $x <> $ClearStrip
$y = Rand($WidthY)
If $Field[$x][$y] < $Void Then $Field[$x][$y] += 1
SetOnField($x, $y, $Field[$x][$y])
EndFunc
Func SetOnField($x, $y, $item)
GUICtrlSetImage ( $FieldHandle[$x][$y], PictureName($item))
EndFunc
Func PictureName($item)
Switch $item
  Case $WormHead
   $FileName = "01 Worm Head.bmp"
  Case $WormBody
   $FileName = "02 Worm Body.bmp"
  Case $DecayedWormBody
   $FileName = "03 Decayed Worm Body.bmp"
  Case $Flowers
   $FileName = "04 Flowers.bmp"
  Case $Death
   $FileName = "05 pigman.bmp"
  Case $Elevator
   $FileName = "06 Elevator.bmp"
  Case $Gift
   $FileName = "07 Gift.bmp"
  Case $PacMan
   $FileName = "08 PacMan.bmp"
  Case $Devolve
   $FileName = "09 Devolve.bmp"
  Case $Angel
   $FileName = "10 Angel.bmp"
  Case $Devil
   $FileName = "11 Devil.bmp"
  Case $ExpressElevator
   $FileName = "12 Atom.bmp"
  Case $Cross
   $FileName = "13 Cross.bmp"
  Case $BrickWall
   $FileName = "14 BrickWall.bmp"
  Case $Void
   $FileName = "15 Void.bmp"
  Case Else
   $FileName = False
EndSwitch
Return $FileName
EndFunc
;
; bend random() function to my needs
;
Func Rand($pMax)
Return Random(0, $pMax, 1)
EndFunc

for reasons I can not figure out the window closes 30 seconds or so after being triggered to do so.

also the beep occurs even less frequently when it should occur every second.

any help would be appreciated.

Edited by Topher

[left][hr]

$mood = "whimsy"
$mode = "confused"
$randomChaos = True
Do
Something()
Until $Tired
[/left][left]Reflex (Arcade game)[/left][left]IX (Board game)[/left][left]The Word Game (Word game)[/left][left]Plastic Sliding Squares Puzzle (Puzzle)[/left]
Link to comment
Share on other sites

Hello there :oops:

Here's my solution.

Add

Opt("GUIOnEventMode",1)

at the start of your code, then add

Func Quit()
DllClose($dll)
Exit
EndFunc

somewhere between the functions

and finally - the most important - change "Do ... Until ..." to

While 1
$StartTime = _Timer_Init()
    While _Timer_Diff($StartTime) < (1010 - ($Level * 10))
        ReadyMovement()
    WEnd
Tick()
Move()
Evolve()
GUISetOnEvent($GUI_EVENT_CLOSE, "Quit")
WEnd

It works perfectly!!! :bye:

Here you go, try:

#cs ----------------------------------------------------------------------------
AutoIt Version: 3.3.6.1
Author:   Garth Bigelow
Script Function:
Template AutoIt script.
#ce ----------------------------------------------------------------------------
; Script Start - Add your code below here
#include <GUIConstantsEx.au3>
#Include <Misc.au3>
#include <Timers.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode",1)
Global Const $WidthX = 15
Global Const $WidthY = 9
Global Const $WormHead    = 1 ; current point
Global Const $WormBody    = 2   ; one life lost
Global Const $DecayedWormBody = 3 ; no action
Global Const $Flowers       = 4 ; no action
Global Const $Death     = 5 ; one life lost
Global Const $Elevator    = 6 ; one level gained
Global Const $Gift        = 7 ; 100 to 1000 points
Global Const $PacMan          = 8 ; pacman mode (gooble items for 100 points per item value for 4 to 9 rounds) death on void regardless
Global Const $Devolve       = 9 ; all shapes (except voids, worm bodies and fields) go backwards one item value
Global Const $Angel     = 10 ; one life gained
Global Const $Devil  = 11 ; 0 to 3 lives lost
Global Const $ExpressElevator = 12 ; 4 levels gained
Global Const $Cross     = 13 ; all shapes (except voids) cleared to field
Global Const $BrickWall    = 14 ; death
Global Const $Void        = 15 ; death & never gets erased
; death leaves a void in its space
; SCORING:
;  movement one point per level
;   leveling up, 500 points * level
;   Gift, Pacman bonuses
; elevator locks in points gained during level
; Movements
Global Const $mvDownLeft = 1
Global Const $mvLeft  = 2
Global Const $mvUpLeft  = 3
Global Const $mvUp   = 4
Global Const $mvUpRight  = 5
Global Const $mvRight  = 6
Global Const $mvDownRight = 7
Global Const $mvDown  = 8
Global Const $keyForceField = 9
Global Const $keyPause   = 10
Global $Field
Dim $Field[16][10]
Dim $FieldHandle[16][10]
Global $PosX  = Rand($WidthX)
Global $PosY  = Rand($WidthY)
Global $DirectionX = 0, $DirectionY = 1
Global $Level = 0, $Score = 0, $LevelScore = 0, $ClearStrip
Global $Lives = 3, $ForceField = 0
GUICreate("Reflex", 1024, 640)
GUISetState()
InitializeField()
UpLevel()
$dll = DllOpen("user32.dll")
While 1
$StartTime = _Timer_Init()
    While _Timer_Diff($StartTime) < (1010 - ($Level * 10))
        ReadyMovement()
    WEnd
Tick()
Move()
Evolve()
GUISetOnEvent($GUI_EVENT_CLOSE, "Quit")
WEnd
;
;
;
Func Quit()
DllClose($dll)
Exit
EndFunc
Func Move()
$Field[$PosX][$PosY] = $WormBody
SetOnField($PosX, $PosY, $WormBody)
$PosX += $DirectionX
$PosY += $DirectionY
If $PosX < 0 Then $PosX = $WidthX
If $PosX > $WidthX Then $PosX = 0
If $PosY < 0 Then $PosY = $WidthY
If $PosY > $WidthY Then $PosY = 0
SetOnField($PosX, $PosY, $WormHead)
EndFunc
Func Tick()
Beep(500, 20)
EndFunc
Func ReadyMovement()
Switch GetKeys()
  Case $mvDownLeft
   $DirectionX = -1
   $DirectionY = 1
  Case $mvDown
   $DirectionX = 0
   $DirectionY = 1
  Case $mvDownRight
   $DirectionX = 1
   $DirectionY = 1
  Case $mvLeft
   $DirectionX = -1
   $DirectionY = 0
  Case $mvUpLeft
   $DirectionX = -1
   $DirectionY = -1
  Case $mvUp
   $DirectionX = 0
   $DirectionY = -1
  Case $mvUpRight
   $DirectionX = 1
   $DirectionY = -1
  Case $mvRight
   $DirectionX = 1
   $DirectionY = 0
EndSwitch
EndFunc
Func GetKeys()
If _IsPressed("61", $dll) Then Return $mvDownLeft
If _IsPressed("62", $dll) Then Return $mvDown
If _IsPressed("28", $dll) Then Return $mvDown
If _IsPressed("63", $dll) Then Return $mvDownRight
If _IsPressed("64", $dll) Then Return $mvLeft
If _IsPressed("25", $dll) Then Return $mvLeft
If _IsPressed("66", $dll) Then Return $mvRight
If _IsPressed("27", $dll) Then Return $mvRight
If _IsPressed("67", $dll) Then Return $mvUpLeft
If _IsPressed("68", $dll) Then Return $mvUp
If _IsPressed("26", $dll) Then Return $mvUp
If _IsPressed("69", $dll) Then Return $mvUpRight
If _IsPressed("20", $dll) Then Return $keyForceField
If _IsPressed("50", $dll) Then Return $keyPause
If _IsPressed("13", $dll) Then Return $keyPause
Return 0
EndFunc
Func UpLevel()
$Score += ($Level * 500)
$Score += $LevelScore
$LevelScore = 0
$Level += 1
$ClearStrip = Rand($WidthX)
DrawField()
EndFunc
Func InitializeField()
For $x = 0 To $WidthX
  For $y = 0 To $WidthY
   $Field[$x][$y] = $Flowers
;   $FieldHandle[$x][$y] = GUICtrlCreatePic (PictureName($Flowers), $x * 64, $y * 64, 64, 64)
  Next
Next
EndFunc
Func DrawField()
For $x = 0 To $WidthX
  For $y = 0 To $WidthY
   If $Field[$x][$y] <> $Void Then $Field[$x][$y] = $Flowers
   $FieldHandle[$x][$y] = GUICtrlCreatePic (PictureName($Flowers), $x * 64, $y * 64, 64, 64)
  Next
Next
EndFunc
Func Evolve()
Do
  $x = Rand($WidthX)
Until $x <> $ClearStrip
$y = Rand($WidthY)
If $Field[$x][$y] < $Void Then $Field[$x][$y] += 1
SetOnField($x, $y, $Field[$x][$y])
EndFunc
Func SetOnField($x, $y, $item)
GUICtrlSetImage ( $FieldHandle[$x][$y], PictureName($item))
EndFunc
Func PictureName($item)
Switch $item
  Case $WormHead
   $FileName = "01 Worm Head.bmp"
  Case $WormBody
   $FileName = "02 Worm Body.bmp"
  Case $DecayedWormBody
   $FileName = "03 Decayed Worm Body.bmp"
  Case $Flowers
   $FileName = "04 Flowers.bmp"
  Case $Death
   $FileName = "05 pigman.bmp"
  Case $Elevator
   $FileName = "06 Elevator.bmp"
  Case $Gift
   $FileName = "07 Gift.bmp"
  Case $PacMan
   $FileName = "08 PacMan.bmp"
  Case $Devolve
   $FileName = "09 Devolve.bmp"
  Case $Angel
   $FileName = "10 Angel.bmp"
  Case $Devil
   $FileName = "11 Devil.bmp"
  Case $ExpressElevator
   $FileName = "12 Atom.bmp"
  Case $Cross
   $FileName = "13 Cross.bmp"
  Case $BrickWall
   $FileName = "14 BrickWall.bmp"
  Case $Void
   $FileName = "15 Void.bmp"
  Case Else
   $FileName = False
EndSwitch
Return $FileName
EndFunc
;
; bend random() function to my needs
;
Func Rand($pMax)
Return Random(0, $pMax, 1)
EndFunc

Regards and happy coding!

[indent=3][/indent]

Link to comment
Share on other sites

Thank you, that did the trick.

I understand why the changes worked but not why my version failed.

Do you know why?

[left][hr]

$mood = "whimsy"
$mode = "confused"
$randomChaos = True
Do
Something()
Until $Tired
[/left][left]Reflex (Arcade game)[/left][left]IX (Board game)[/left][left]The Word Game (Word game)[/left][left]Plastic Sliding Squares Puzzle (Puzzle)[/left]
Link to comment
Share on other sites

Mainly because you had way too much lengthy operations inside your message loop.

As a general rule of thumb, you should keep the processing of messages (GUI or others) as short as possible.

Switching to OnEvent mode fixed that. Read the help file about these words.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...