Jump to content

tank game


Recommended Posts

hi,

i've done a litle tank-game... everytime, when you shoot, the bullet should disappear, if it collides with the walls... the walls are black, so i made a condition, if the pixel in front of the bullet is black, the bullet should disappear, but the bulletcoordinates don't fit and i don't know why o.O...

;Name: Battle.au3
;Author: Lord_Doominik
;Status: In Progress (50%)
;Version: 0

$gui = WinGetPos("Robot Wars")

$x1 = 330
$xmove1 = $gui[0] + $x1
$y1 = 180
$ymove1 = $gui[1] + $y1
Dim $bullet1[1000]
$bullet1[0] = 0
Dim $bulletx1[1000]
Dim $bullety1[1000]

$x2 = 203
$xmove2 = $gui[0] + $x1
$y2 = 550
$ymove2 = $gui[1] + $y1
Dim $bullet2[1000]
$bullet2[0] = 0
Dim $bulletx2[1000]
Dim $bullety2[1000]

$i = 0
$reloadtime = TimerInit()

$Arena = GUICtrlCreatePic("Battlefields/Arena1.map", 0, 0, 800, 600)
GUICtrlSetState(-1, $GUI_DISABLE)

$Roboter1 = GUICreate("Roboter1", 32, 32, $x1, $y1, $WS_POPUP, BitOr($WS_EX_LAYERED, $WS_EX_MDICHILD), $mainGUI)
GUICtrlCreatePic("Vehicles/Panzer1 N.gif", 0, 0, 32, 32)

GUISetState()

$Roboter2 = GUICreate("Roboter2", 11, 21, $x2, $y2, $WS_POPUP, BitOr($WS_EX_LAYERED, $WS_EX_MDICHILD), $mainGUI)
GUICtrlCreatePic("Vehicles/Roboter2.gif", 0, 0, 11, 21)

GUISetState()

While 1
    $bmsg = GUIGetMsg()
    Select
    Case $bmsg = $GUI_EVENT_CLOSE
        Exit
    EndSelect
    
    Action1()
    Shot1()
WEnd

Func Action1()
    $gui = WinGetPos("Robot Wars")
    $xmove1 = $gui[0] + $x1
    $ymove1 = $gui[1] + $y1
    
    If _IsPressed("25") Then
        If Hex(PixelGetColor($xmove1+4, $ymove1), 6) <> "000000" And Hex(PixelGetColor($xmove1+4, $ymove1+27), 6) <> "000000" Then
            $xmove1 = $xmove1 - 1
            $x1 = $x1 - 1
        EndIf
    EndIf
    
    If _IsPressed("27") Then
        If Hex(PixelGetColor($xmove1+27, $ymove1), 6) <> "000000" And Hex(PixelGetColor($xmove1+27, $ymove1+27), 6) <> "000000" Then
            $xmove1 = $xmove1 + 1
            $x1 = $x1 + 1
        EndIf
    EndIf
    
    If _IsPressed("26") Then
        If Hex(PixelGetColor($xmove1+16, $ymove1-1), 6) <> "000000" Or Hex(PixelGetColor($xmove1+17, $ymove1-1), 6) <> "000000" Or Hex(PixelGetColor($xmove1+18, $ymove1-1), 6) <> "000000" And Hex(PixelGetColor($xmove1+5, $ymove1+2), 6) <> "000000" And Hex(PixelGetColor($xmove1+26, $ymove1+2), 6) <> "000000" Then
            $ymove1 = $ymove1 - 1
            $y1 = $y1 - 1
        EndIf
    EndIf
    
    If _IsPressed("28") Then
        If Hex(PixelGetColor($xmove1+5, $ymove1+29), 6) <> "000000" And Hex(PixelGetColor($xmove1+26, $ymove1+29), 6) <> "000000" Then
            $ymove1 = $ymove1 + 1
            $y1 = $y1 + 1
        EndIf
    EndIf
    
    If _IsPressed("20") Then
        If TimerDiff($reloadtime) >= 750 Then
            GUISwitch($mainGUI)
            $bullet1[0] = $bullet1[0] + 1
            $i = $bullet1[0]
            $bulletx1[$i] = $x1 + 11
            $bullety1[$i] = $y1 - 27
            $bullet1[$i] = GUICtrlCreateLabel("", $bulletx1[$i], $bullety1[$i], 3, 2)
            GUICtrlSetBkColor(-1, 0xFF0000)
            $reloadtime = TimerInit()
        EndIf
    EndIf
    
    WinMove("Roboter1", "", $xmove1, $ymove1)
EndFunc

Func Shot1()
    For $j = 1 To $i
        If Hex(PixelGetColor($bulletx1[$j], $bullety1[$j]-1), 6) = "000000" Then
            GUICtrlDelete($bullet1[$j])
        EndIf
        $bullety1[$j] = $bullety1[$j] - 2
        GUICtrlSetPos($bullet1[$j], $bulletx1[$j], $bullety1[$j])
    Next
EndFunc

Robot Wars Source Code + Graphics

Edited by Lord_Doominik
Link to comment
Share on other sites

  • Moderators

Try just:

(PixelGetColor($bulletx1[$j], $bullety1[$j]-1) == 0x000000)

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

  • Moderators

Well, actually it's quite confusing... Is there a reason your not supplying the whole code for debugging?

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

  • Moderators

that's the WHOLE code for debugging :o the other script is running well...

I can't run the script to try and help, so it's not the whole script, I have errors when I try to figure stuff out that the main GUI is missing. If you can't provide it all, then there's not much that anyone can do but take guesses and hope that their educated guesses are correct.

Help yourself here!

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

lol, theres just another little script, but everything is fine with it...

and if you see its jsut the guy... not more -.-*

;Name: RobotWars.au3
;Author: Lord_Doominik
;Status: In Progress (15%)
;Version: 0

#include <GUIConstants.au3>
#include <Misc.au3>

$mainGUI = GUICreate("Robot Wars", 800, 625, (@DesktopWidth-800)/2, (@DesktopHeight-600)/2, $WS_MINIMIZEBOX + $WS_SYSMENU)

$Label = GUICtrlCreateLabel("Press Enter to Start", 0, (@DesktopHeight-200)/2, 800, -1, $SS_CENTER)

GUISetState()

While 1
    $msg = GUIGetMsg()
    Select
    Case $msg = $GUI_EVENT_CLOSE
        Exit
    Case _IsPressed("0D")
        ExitLoop
    EndSelect
WEnd

GUICtrlDelete($Label)
#include <Battle.au3>
Link to comment
Share on other sites

  • Moderators

lol, theres just another little script, but everything is fine with it...

and if you see its jsut the guy... not more -.-*

;Name: RobotWars.au3
;Author: Lord_Doominik
;Status: In Progress (15%)
;Version: 0

#include <GUIConstants.au3>
#include <Misc.au3>

$mainGUI = GUICreate("Robot Wars", 800, 625, (@DesktopWidth-800)/2, (@DesktopHeight-600)/2, $WS_MINIMIZEBOX + $WS_SYSMENU)

$Label = GUICtrlCreateLabel("Press Enter to Start", 0, (@DesktopHeight-200)/2, 800, -1, $SS_CENTER)

GUISetState()

While 1
    $msg = GUIGetMsg()
    Select
    Case $msg = $GUI_EVENT_CLOSE
        Exit
    Case _IsPressed("0D")
        ExitLoop
    EndSelect
WEnd

GUICtrlDelete($Label)
#include <Battle.au3>
Just a Blank GUI ... (not included: #include <Battle.au3>)...

I hope you get it working.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

hugh??? look at the filenames... the game is splitted into 2 files... the game.au3 file, which later gets the menu and this stuff and the battle.au3, which has got the game... it's jsut included there, it doesn't matter if i copy it there or include???

don't know what exactly you mean...

Link to comment
Share on other sites

  • Moderators

Just hard to help doom, if I don't have anything but a 'blank' gui to look at... I can't replicate anything.

I mean personally, looking at the code, I would have written it differently for the pixelgetcolor options, but nothing is commented or anything, on what it does, you leave it up to us to try and debug a problem that your having that only you can replicate, and we have to go off of variable names to try and understand what could be happening.

I'll leave you alone, maybe someone else has a clue... but as I said... I personally need to be able to recreate a situation to be able to debug one.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Hmmm... game idea?

I think you should try using some simple collision detection like: (Assuming your battle arena is a rectangle)

If $bullet_X < $arena_leftwall_x Or $bullet_X > $arena_rightwall_x Or $bullet_Y < $arena_topwall_y Or $bullet_Y > $arena_bottomwall_y Then
    ;;; $bullet has collided with a wall
EndIf

#)

EDIT: code tags

Edited by nfwu
Link to comment
Share on other sites

  • Moderators

I didn't even know this was still an open issue, so I downloaded the file... Well to be honest, you guys really make it too much of a pain in the rear to even try to help debug. I mean, I don't know about your computers, but when I am using a file path, don't work so good with a forward slash (Battlefields/Arena1.map)... I tend to use a back slash.

Pic's were in the wrong place to even make the background show... I mean, if this was a edit job to try and hide any trade secrets... you did a good job on that, but hacked up the rest... I mean ya'll didn't even comment anything.

Anyway, the problem your having... (After Debuging, is that the window you are wanting to get the 0x000000 color from doesn't have focus for PixelGetColor() to work... Just run my example below (change the file locations for the pictures of course) and you'll see what I'm talking about, the mouse will move where ever PixelGetColor() is searching:

#include <GUIConstants.au3>
#include <Misc.au3>

$mainGUI = GUICreate("Robot Wars", 800, 625, (@DesktopWidth-800)/2, (@DesktopHeight-600)/2, $WS_MINIMIZEBOX + $WS_SYSMENU)

$Label = GUICtrlCreateLabel("Press Enter to Start", 0, (@DesktopHeight-200)/2, 800, -1, $SS_CENTER)

GUISetState()

While 1
    $msg = GUIGetMsg()
    Select
    Case $msg = $GUI_EVENT_CLOSE
        Exit
    Case _IsPressed("0D")
        ExitLoop
    EndSelect
WEnd

GUICtrlDelete($Label)

$Arena = GUICtrlCreatePic(@DesktopDir & "\Test_Folder\Robot_Wars\Battlefields\Arena2.bmp", 0, 0, 800, 600)
GUICtrlSetState(-1, $GUI_DISABLE)

$gui = WinGetPos("Robot Wars")

$x1 = 330
$xmove1 = $gui[0] + $x1
$y1 = 180
$ymove1 = $gui[1] + $y1
Dim $bullet1[1000]
$bullet1[0] = 0
Dim $bulletx1[1000]
Dim $bullety1[1000]

$x2 = 203
$xmove2 = $gui[0] + $x1
$y2 = 550
$ymove2 = $gui[1] + $y1
Dim $bullet2[1000]
$bullet2[0] = 0
Dim $bulletx2[1000]
Dim $bullety2[1000]

$i = 0
$reloadtime = TimerInit()

$Roboter1 = GUICreate("Roboter1", 32, 32, $x1, $y1, $WS_POPUP, BitOr($WS_EX_LAYERED, $WS_EX_MDICHILD), $mainGUI)
GUICtrlCreatePic(@DesktopDir & "\Test_Folder\Robot_Wars\Vehicles\Panzer1 N.gif", 0, 0, 32, 32)

GUISetState()

$Roboter2 = GUICreate("Roboter2", 11, 21, $x2, $y2, $WS_POPUP, BitOr($WS_EX_LAYERED, $WS_EX_MDICHILD), $mainGUI)
GUICtrlCreatePic(@DesktopDir & "\Test_Folder\Robot_Wars\Vehicles\Roboter2.gif", 0, 0, 11, 21)

GUISetState()

While 1
    $bmsg = GUIGetMsg()
    Select
    Case $bmsg = $GUI_EVENT_CLOSE
        Exit
    EndSelect
    
    Action1()
    Shot1()
WEnd

Func Action1()
    $gui = WinGetPos("Robot Wars")
    $xmove1 = $gui[0] + $x1
    $ymove1 = $gui[1] + $y1
    
    If _IsPressed("25") Then
        If (PixelGetColor($xmove1+4, $ymove1) <> 0x000000) And (PixelGetColor($xmove1+4, $ymove1+27) <> 0x000000) Then
            $xmove1 = $xmove1 - 1
            $x1 = $x1 - 1
        EndIf
    EndIf
    
    If _IsPressed("27") Then
        If (PixelGetColor($xmove1+27, $ymove1) <> 0x000000) And (PixelGetColor($xmove1+27, $ymove1+27) <> 0x000000) Then
            $xmove1 = $xmove1 + 1
            $x1 = $x1 + 1
        EndIf
    EndIf
    
    If _IsPressed("26") Then
        If (PixelGetColor($xmove1+16, $ymove1-1) <> 0x000000) Or (PixelGetColor($xmove1+17, $ymove1-1) <> 0x000000) Or (PixelGetColor($xmove1+18, $ymove1-1) <> 0x000000) And (PixelGetColor($xmove1+5, $ymove1+2) <> 0x000000) And (PixelGetColor($xmove1+26, $ymove1+2) <> 0x000000) Then
            $ymove1 = $ymove1 - 1
            $y1 = $y1 - 1
        EndIf
    EndIf
    
    If _IsPressed("28") Then
        If (PixelGetColor($xmove1+5, $ymove1+29) <> 0x000000) And (PixelGetColor($xmove1+26, $ymove1+29) <> 0x000000) Then
            $ymove1 = $ymove1 + 1
            $y1 = $y1 + 1
        EndIf
    EndIf
    
    If _IsPressed("20") Then
        If TimerDiff($reloadtime) >= 750 Then
            GUISwitch($mainGUI)
            $bullet1[0] = $bullet1[0] + 1
            $i = $bullet1[0]
            $bulletx1[$i] = $x1 + 11
            $bullety1[$i] = $y1 - 27
            $bullet1[$i] = GUICtrlCreateLabel("", $bulletx1[$i], $bullety1[$i], 3, 2)
            GUICtrlSetBkColor(-1, 0xFF0000)
            $reloadtime = TimerInit()
        EndIf
    EndIf
    
    WinMove("Roboter1", "", $xmove1, $ymove1)
EndFunc

Func Shot1()
    For $j = 1 To $i
        Local $PixColor = PixelGetColor($bulletx1[$j], $bullety1[$j]-1)
        If ($PixColor == 0x000000) Then
            GUICtrlDelete($bullet1[$j])
        EndIf
        If ($PixColor <> 0xFFFFFF) Then 
            ConsoleWrite('0x' & Hex(PixelGetColor($bulletx1[$j], $bullety1[$j]-1), 6))
            MouseMove($bulletx1[$j], $bullety1[$j]-1)
        EndIf
        $bullety1[$j] = $bullety1[$j] - 2
        GUICtrlSetPos($bullet1[$j], $bulletx1[$j], $bullety1[$j])
    Next
EndFunc
Not the exact answer your looking for I'm sure, but further than what has been explained so far as far as I know.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

  • Moderators

This is sloppy, but a works a bit... I think nfwu is on the right path, doesn't matter if the arena is a 'bitmap' file, you gave it a specific size (lol). C'mon now... You might think of using ControlGetPos() your lives may be alot easier.

#include <GUIConstants.au3>
#include <Misc.au3>

Opt('PixelCoordMode', 2)

$mainGUI = GUICreate("Robot Wars", 800, 625, (@DesktopWidth-800)/2, (@DesktopHeight-600)/2, $WS_MINIMIZEBOX + $WS_SYSMENU)

$Label = GUICtrlCreateLabel("Press Enter to Start", 0, (@DesktopHeight-200)/2, 800, -1, $SS_CENTER)

GUISetState()

While 1
    $msg = GUIGetMsg()
    Select
    Case $msg = $GUI_EVENT_CLOSE
        Exit
    Case _IsPressed("0D")
        ExitLoop
    EndSelect
WEnd

GUICtrlDelete($Label)

$Arena = GUICtrlCreatePic(@DesktopDir & "\Test_Folder\Robot_Wars\Battlefields\Arena2.bmp", 0, 0, 800, 600)
GUICtrlSetState(-1, $GUI_DISABLE)

$gui = WinGetPos("Robot Wars")

$x1 = 330
$xmove1 = $gui[0] + $x1
$y1 = 180
$ymove1 = $gui[1] + $y1
Dim $bullet1[1000]
$bullet1[0] = 0
Dim $bulletx1[1000]
Dim $bullety1[1000]

$x2 = 203
$xmove2 = $gui[0] + $x1
$y2 = 550
$ymove2 = $gui[1] + $y1
Dim $bullet2[1000]
$bullet2[0] = 0
Dim $bulletx2[1000]
Dim $bullety2[1000]
$i = 0
$reloadtime = TimerInit()

$Roboter1 = GUICreate("Roboter1", 32, 32, $x1, $y1, $WS_POPUP, BitOr($WS_EX_LAYERED, $WS_EX_MDICHILD), $mainGUI)
GUICtrlCreatePic(@DesktopDir & "\Test_Folder\Robot_Wars\Vehicles\Panzer1 N.gif", 0, 0, 32, 32)

GUISetState()

$Roboter2 = GUICreate("Roboter2", 11, 21, $x2, $y2, $WS_POPUP, BitOr($WS_EX_LAYERED, $WS_EX_MDICHILD), $mainGUI)
GUICtrlCreatePic(@DesktopDir & "\Test_Folder\Robot_Wars\Vehicles\Roboter2.gif", 0, 0, 11, 21)

GUISetState()

While 1
    $bmsg = GUIGetMsg()
    Select
    Case $bmsg = $GUI_EVENT_CLOSE
        Exit
    EndSelect
    
    Action1()
    Shot1()
WEnd

Func Action1()
    $gui = WinGetPos("Robot Wars")
    $xmove1 = $gui[0] + $x1
    $ymove1 = $gui[1] + $y1
    
    If _IsPressed("25") Then
        If (PixelGetColor($xmove1+4, $ymove1) <> 0x000000) And (PixelGetColor($xmove1+4, $ymove1+27) <> 0x000000) Then
            $xmove1 = $xmove1 - 1
            $x1 = $x1 - 1
        EndIf
    EndIf
    
    If _IsPressed("27") Then
        If (PixelGetColor($xmove1+27, $ymove1) <> 0x000000) And (PixelGetColor($xmove1+27, $ymove1+27) <> 0x000000) Then
            $xmove1 = $xmove1 + 1
            $x1 = $x1 + 1
        EndIf
    EndIf
    
    If _IsPressed("26") Then
        If (PixelGetColor($xmove1+16, $ymove1-1) <> 0x000000) Or (PixelGetColor($xmove1+17, $ymove1-1) <> 0x000000) Or (PixelGetColor($xmove1+18, $ymove1-1) <> 0x000000) And (PixelGetColor($xmove1+5, $ymove1+2) <> 0x000000) And (PixelGetColor($xmove1+26, $ymove1+2) <> 0x000000) Then
            $ymove1 = $ymove1 - 1
            $y1 = $y1 - 1
        EndIf
    EndIf
    
    If _IsPressed("28") Then
        If (PixelGetColor($xmove1+5, $ymove1+29) <> 0x000000) And (PixelGetColor($xmove1+26, $ymove1+29) <> 0x000000) Then
            $ymove1 = $ymove1 + 1
            $y1 = $y1 + 1
        EndIf
    EndIf
    
    If _IsPressed("20") Then
        If TimerDiff($reloadtime) >= 750 Then
            GUISwitch($mainGUI)
            $bullet1[0] = $bullet1[0] + 1
            $i = $bullet1[0]
            $bulletx1[$i] = $x1 + 11
            $bullety1[$i] = $y1 - 27
            $bullet1[$i] = GUICtrlCreateLabel("", $bulletx1[$i], $bullety1[$i], 3, 2)
            GUICtrlSetBkColor(-1, 0xFF0000)
            $reloadtime = TimerInit()
        EndIf
    EndIf
    
    WinMove("Roboter1", "", $xmove1, $ymove1)
EndFunc

Func Shot1()
    For $j = 1 To $i
        ControlFocus($mainGUI, '', '')
        Local $PixColor = PixelGetColor($bulletx1[$j], $bullety1[$j]-1)
        If ($PixColor == 0x000000) Then
            GUICtrlDelete($bullet1[$j])
            $ShotsFired = 0
        EndIf
        $bullety1[$j] = $bullety1[$j] - 2
        GUICtrlSetPos($bullet1[$j], $bulletx1[$j], $bullety1[$j])
    Next
    ControlFocus($Roboter1, '', '')
EndFunc

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

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