Jump to content

Not sure what to make the title...


Recommended Posts

I started to play around with the _IsPressed function and it's a lot of fun.

I made this script below for fun and I want to stop the Gray box from going through the Red box any ideas?

#include<misc.au3>
#include<windowsconstants.au3>
#include<guiconstants.au3>
$gui = GUICreate("Gui",300,300,-1,-1,BitOR($WS_EX_WINDOWEDGE,$WS_POPUP))
$pic = GUICtrlCreateInput("",50,50,36,36)
GUICtrlSetState($pic,$GUI_DISABLE)
GUICtrlSetBkColor($pic,0xFF0000)
GUISetBkColor(0x000000)
$inp = GUICtrlCreateInput("",200,200,50,50)
GUICtrlSetState($inp,$GUI_DISABLE)
GUISetState()

while 1
   If _IsPressed("25") Then
     $cp = ControlGetPos($gui,"",$pic)
     $cpx = $cp[0] - 1
     GUICtrlSetPos($pic,$cpx,$cp[1])
  EndIf
  If _IsPressed("28") Then
     $cp = ControlGetPos($gui,"",$pic)
     $cpy = $cp[1] + 1
     GUICtrlSetPos($pic,$cp[0],$cpy)
  EndIf
  If _IsPressed("27") Then
     $cp = ControlGetPos($gui,"",$pic)
     $cpx = $cp[0] + 1
     GUICtrlSetPos($pic,$cpx,$cp[1])
  EndIf
  If _IsPressed("26") Then
     $cp = ControlGetPos($gui,"",$pic)
     $cpy = $cp[1] - 1
     GUICtrlSetPos($pic,$cp[0],$cpy)
     EndIf
   $msg = GUIGetMsg(1)
   Switch $msg[1]
   Case $gui
      Switch $msg[0]
      Case -3
         Exit
      EndSwitch
   EndSwitch
WEnd

Thanks. :)

Edited by gottygolly
Link to comment
Share on other sites

You could try putting the x,y of $pic in a variable and then set an If...Then condition, if $x = xx then reset.

 

EDIT: you already got the $cpy and $cpx so no need for $x,$y, you could try something like this:

     $cp = ControlGetPos($gui,"",$pic)
     $cpx = $cp[0] + 1
     If $cpx > 150 Then $cpx = 150
     GUICtrlSetPos($pic,$cpx,$cp[1])
Edited by Palestinian
Link to comment
Share on other sites

What you really want to do is create an _Is_collision() function in which all edges are calculated to see if any are overlapped.

For example if the bottom of red is equal to top of grey and (right of red is equal to left of grey or left of red is equal to right of grey) then red cannot move down.

A whole lot of calculations but a decent exercise for beginner.

EDIT:

Example to control a down movement.

#include<misc.au3>
#include<windowsconstants.au3>
#include<guiconstants.au3>
$gui = GUICreate("Gui", 300, 300, -1, -1, BitOR($WS_EX_WINDOWEDGE, $WS_POPUP))
$pic = GUICtrlCreateInput("", 50, 50, 36, 36)
GUICtrlSetState($pic, $GUI_DISABLE)
GUICtrlSetBkColor($pic, 0xFF0000)
GUISetBkColor(0x000000)
$inp = GUICtrlCreateInput("", 200, 200, 50, 50)
GUICtrlSetState($inp, $GUI_DISABLE)
GUISetState()

While 1
    If _IsPressed("25") Then
        $cp = ControlGetPos($gui, "", $pic)
        $cpx = $cp[0] - 1
        GUICtrlSetPos($pic, $cpx, $cp[1])
    EndIf
    If _IsPressed("28") Then
        $cp = ControlGetPos($gui, "", $pic)
        If _CanMoveDown($cp) Then
            $cpy = $cp[1] + 1
            GUICtrlSetPos($pic, $cp[0], $cpy)
        EndIf

    EndIf
    If _IsPressed("27") Then
        $cp = ControlGetPos($gui, "", $pic)
        $cpx = $cp[0] + 1
        GUICtrlSetPos($pic, $cpx, $cp[1])
    EndIf
    If _IsPressed("26") Then
        $cp = ControlGetPos($gui, "", $pic)

        $cpy = $cp[1] - 1
        GUICtrlSetPos($pic, $cp[0], $cpy)

    EndIf
    $msg = GUIGetMsg(1)
    Switch $msg[1]
        Case $gui
            Switch $msg[0]
                Case -3
                    Exit
            EndSwitch
    EndSwitch
WEnd

Func _CanMoveDown($cp)
    $red_B = $cp[1] + $cp[3]
    $red_R = $cp[0] + $cp[2]
    $red_L = $cp[0]
    If $red_B >= 200 And ($red_R >= 200 And $red_L <= 250) Then
        Return False
    EndIf
    Return True
EndFunc   ;==>_CanMoveDown

Just need to repeat with left right and up.

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Thanks JohnOne it works like a charm :)

And Palestinian it works but it wasn't what I was looking for thanks for the info though I'll probably end up using it in the future.

Edit:

JohnOne I ran into a problem with the code you provided me...When ever the blocks touch each other it disables all the other arrow keys.

(I probably did something wrong but could you enlighten me on what I did wrong?)

#include<misc.au3>
#include<windowsconstants.au3>
#include<guiconstants.au3>
$gui = GUICreate("Gui", 300, 300, -1, -1, BitOR($WS_EX_WINDOWEDGE, $WS_POPUP))
$pic = GUICtrlCreateInput("", 50, 50, 36, 36)
GUICtrlSetState($pic, $GUI_DISABLE)
GUICtrlSetBkColor($pic, 0xFF0000)
GUISetBkColor(0x000000)
$inp = GUICtrlCreateInput("", 200, 200, 50, 50)
GUICtrlSetState($inp, $GUI_DISABLE)
GUISetState()

While 1
    If _IsPressed("25") Then
        $cp = ControlGetPos($gui, "", $pic)
        If _CanMoveLeft($cp) Then
        $cpx = $cp[0] - 1
        GUICtrlSetPos($pic, $cpx, $cp[1])
        EndIf
    EndIf
    If _IsPressed("28") Then
        $cp = ControlGetPos($gui, "", $pic)
        If _CanMoveDown($cp) Then
            $cpy = $cp[1] + 1
            GUICtrlSetPos($pic, $cp[0], $cpy)
        EndIf

    EndIf
    If _IsPressed("27") Then
        $cp = ControlGetPos($gui, "", $pic)
        If _CanMoveRight($cp) Then
        $cpx = $cp[0] + 1
        GUICtrlSetPos($pic, $cpx, $cp[1])
        EndIf
    EndIf
    If _IsPressed("26") Then
        $cp = ControlGetPos($gui, "", $pic)
         If _CanMoveUp($cp) Then
        $cpy = $cp[1] - 1
        GUICtrlSetPos($pic, $cp[0], $cpy)
      EndIf
    EndIf
    $msg = GUIGetMsg(1)
    Switch $msg[1]
        Case $gui
            Switch $msg[0]
                Case -3
                    Exit
            EndSwitch
    EndSwitch
WEnd

Func _CanMoveDown($cp)
    $red_B = $cp[1] + $cp[3]
    $red_R = $cp[0] + $cp[2]
    $red_L = $cp[0]
    If $red_B >= 200 And ($red_R >= 200 And $red_L <= 250) Then
        Return False
    EndIf
    Return True
 EndFunc   ;==>_CanMoveDown
 Func _CanMoveLeft($cp)
    $red_B = $cp[1] + $cp[3]
    $red_R = $cp[0] + $cp[2]
    $red_L = $cp[0]
    If $red_B >= 200 And ($red_R >= 200 And $red_L <= 250) Then
        Return False
    EndIf
    Return True
 EndFunc
  Func _CanMoveRight($cp)
    $red_B = $cp[1] + $cp[3]
    $red_R = $cp[0] + $cp[2]
    `$red_L = $cp[0]
    If $red_B >= 200 And ($red_R >= 200 And $red_L <= 250) Then
        Return False
    EndIf
    Return True
 EndFunc
  Func _CanMoveUp($cp)
    $red_B = $cp[1] + $cp[3]
    $red_R = $cp[0] + $cp[2]
    $red_L = $cp[0]
    If $red_B >= 200 And ($red_R >= 200 And $red_L <= 250) Then
        Return False
    EndIf
    Return True
EndFunc

Sorry for the inconvenience.

Edited by gottygolly
Link to comment
Share on other sites

JohnOne I ran into a problem with the code you provided me...When ever the blocks touch each other it disables all the other arrow keys.

I probably did something wrong but could you enlighten me on what I did wrong?

 

You simply renamed the function for checking the down movement for other directions.

You need different logic for each direction.

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

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...