Jump to content

Recommended Posts

Posted (edited)

Hi :ph34r: I am trying to make a game but i can't get the colision detector to work. Here is what i have so far and how i tried to do it and it's not working :lmao:

#include <GuiConstants.au3>
#Include <Misc.au3>

GUICreate("Game", 500, 500)

;Vars:
$can=GuiCtrlCreateButton("Lada", 270, 450, 80, 50)
$pica=GuiCtrlCreateButton("Cade", 450, 20)
$pica2=GuiCtrlCreateButton("Cade alta", 350, 10)
GuiSetState()
    While 1
        $posc=ControlGetPos("Game", "", $can)
        $posp=ControlGetPos("Game", "", $pica)
        $msg=GuiGetMsg()
        If $msg=$Gui_event_close Then Exit
        If _Ispressed("27") Then GuiCtrlSetPos($can, $posc[0]+2, $posc[1])
        If _Ispressed("25") Then GuiCtrlSetPos($can, $posc[0]-2, $posc[1])
        
    GuiCtrlSetPos($pica, $posp[0], $posp[1]+1)
    If $posp[1]=500 Then 
        GuiCtrlSetPos($pica, random(0, 470), 0)
         EndIf
        If $posp[0] + $posp[1] = $posc[0] + $posc[1] Then 
            GuiCtrlSetPos($pica, random(0, 470), 0)
            GuiCtrlCreateLabel("You Scored", 90, 10)
                EndIf
        Wend

Edit: There was a bad EndIf i putted there...but it's still not working too good. Suggestions to improve it?

Edited by Kreatorul
Posted

Try this...

This is the collision detection I used for my pacman game:

#include <GuiConstants.au3>
#Include <Misc.au3>

$window = GUICreate("Game", 500, 500)

;Vars:
$can=GuiCtrlCreateButton("Lada", 270, 450, 80, 50)
$pica=GuiCtrlCreateButton("Cade", 450, 20)
$pica2=GuiCtrlCreateButton("Cade alta", 350, 10)
GuiSetState()
    While 1
        $posc=ControlGetPos("Game", "", $can)
        $posp=ControlGetPos("Game", "", $pica)
        $msg=GuiGetMsg()
        If $msg=$Gui_event_close Then Exit
        If _Ispressed("27") Then GuiCtrlSetPos($can, $posc[0]+2, $posc[1])
        If _Ispressed("25") Then GuiCtrlSetPos($can, $posc[0]-2, $posc[1])
       
    GuiCtrlSetPos($pica, $posp[0], $posp[1]+1)
    If $posp[1]=500 Then
        GuiCtrlSetPos($pica, random(0, 470), 0)
         EndIf
        if (Intersection($window, $can, $pica)) Then
            GuiCtrlSetPos($pica, random(0, 470), 0)
            GuiCtrlCreateLabel("You Scored", 90, 10)
                EndIf
        Wend
Func Intersection($hWnd, $nControl1, $nControl2)
    ;True if they have intersected in any way.
    $rect1 = ControlGetPos($hWnd, "", $nControl1)
    $rect2 = ControlGetPos($hWnd, "", $nControl2)
    
    if ((($rect1[0] < ($rect2[0]+$rect2[2])) and ($rect2[0] < ($rect1[0]+$rect1[2])) and ($rect1[1] < ($rect2[1] + $rect2[3])))) Then
        Return ($rect2[1] < ($rect1[1]+$rect1[3]))
    Else
        Return False
    EndIf
EndFunc

the Function: Intersection($hWnd, $control1, $control2) returns true if $control1 has collided with $control2 or vice versa

;D

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
×
×
  • Create New...