pieeater Posted April 17, 2011 Share Posted April 17, 2011 (edited) a pretty simple yet fun game for 2 people, keeps score so you can tell whos winning also i would appreciate it if you looked over my coding and gave me some tips on how to make it easyer also i left a secret key to press to make things more intresting, see if you can find it!expandcollapse popup#include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <Misc.au3> $c=Floor(Random()*0x1000000) $speed = 3 $winW = 800 $winH = 600 $winMargin = 5 $greenscore = 0 $redscore = 0 $gscorew = 21 $rscorew = 21 $aLeft = 650 $aTop = 300 $aWidth = 25 $aHeight = 33 $bLeft = 50 $bTop = 300 $bWidth = 25 $bHeight = 33 $goalX = 700 $goalY = 275 $goalW = 75 $goalH = 75 $winMain = GUICreate("2 Player Game", $winW, $winH) GUISetBkColor($c) $lbl = GUICtrlCreateLabel("use wasd keys to move the green X into the goal without the red X touching you", 50, 50) $lbl2 = GUICtrlCreateLabel("use arrow keys to move the red X into the green X", 50, 70) $label1 = GUICtrlCreateLabel("X", $bLeft, $bTop, $bWidth, $bHeight) GUICtrlSetFont($label1, 24, 1600) GUICtrlSetBkColor($label1, 0x00bb00) $label2 = GUICtrlCreateLabel("X", $aLeft, $aTop, $aWidth, $aHeight) GUICtrlSetFont($label2, 24, 1600) GUICtrlSetBkColor($label2, 0xff0000) $label3 = GUICtrlCreateLabel(" GOAL ", $goalX, $goalY, $goalW, $goalH) GUICtrlSetFont($label3, 12, 800) GUICtrlSetBkColor($label3, 0xcc9999) $label4 = GUICtrlCreateLabel($greenscore, 500, 50, $gscorew, 20) GUICtrlSetFont($label4, 12, 800) GUICtrlSetBkColor($label4, 0x00bb00) $label5 = GUICtrlCreateLabel($redscore, 700, 50, $rscorew, 20) GUICtrlSetFont($label5, 12, 800) GUICtrlSetBkColor($label5, 0xff0000) $label6 = GUICtrlCreateLabel("scores", 600, 50, 60, 20) GUICtrlSetFont($label6, 12, 800) $button = GUICtrlCreateButton( "Save Game", 727, 8) $button2 = GUICtrlCreateButton( "Load Game", 727, 33) WinSetTrans( "2 Player Game", "", 150) GUISetState() While 1 $nMsg=GUIGetMsg() If $nMsg == $GUI_EVENT_CLOSE Then Exit If _IsPressed(25) Then _LEFT() If _IsPressed(26) Then _UP() If _IsPressed(27) Then _RIGHT() If _IsPressed(28) Then _DOWN() If _IsPressed(41) Then _LEFT2() If _IsPressed(57) Then _UP2() If _IsPressed(44) Then _RIGHT2() If _IsPressed(53) Then _DOWN2() If $nMsg = $button Then _Save() If $nMsg = $button2 Then _Load() If $aLeft - 25 <= $bLeft And $bLeft <= $aLeft + 25 And $aTop - 33 <= $bTop And $bTop <= $aTop + 33 Then MsgBox(0, "results:", "green loses, red wins") $bLeft = 100 $bTop = 100 $aLeft = 650 $aTop = 300 $redscore += 1 $c=Floor(Random()*0x1000000) GUISetBkColor($c) EndIf ControlSetText("", "", $label1, "X") GUICtrlSetBkColor($label1, 0x00bb00) ControlSetText("", "", $label2, "X") GUICtrlSetBkColor($label2, 0xff0000) ControlMove($winMain, "", $label1, $bLeft, $bTop) ControlMove($winMain, "", $label2, $aLeft, $aTop) ControlSetText($winMain, "", $label4, $greenscore) ControlSetText($winMain, "", $label5, $redscore) _check_goal() Sleep(10) WEnd Func _LEFT() $aLeft -= $speed If $aLeft < $winMargin Then $aLeft = $winMargin EndFunc ;==>_LEFT Func _UP() $aTop -= $speed If $aTop < $winMargin Then $aTop = $winMargin EndFunc ;==>_UP Func _RIGHT() $aLeft += $speed If $aLeft > $winW - ($aWidth + $winMargin) Then $aLeft = $winW - ($aWidth + $winMargin) EndFunc ;==>_RIGHT Func _DOWN() $aTop += $speed If $aTop > $winH - ($aHeight + $winMargin) Then $aTop = $winH - ($aHeight + $winMargin) EndFunc ;==>_DOWN Func _LEFT2() $bLeft -= $speed If $bLeft < $winMargin Then $bLeft = $winMargin EndFunc ;==>_LEFT2 Func _UP2() $bTop -= $speed If $bTop < $winMargin Then $bTop = $winMargin EndFunc ;==>_UP2 Func _RIGHT2() $bLeft += $speed If $bLeft > $winW - ($bWidth + $winMargin) Then $bLeft = $winW - ($bWidth + $winMargin) EndFunc ;==>_RIGHT2 Func _DOWN2() $bTop += $speed If $bTop > $winH - ($bHeight + $winMargin) Then $bTop = $winH - ($bHeight + $winMargin) EndFunc ;==>_DOWN2 Func _check_goal() If $bTop > $goalY And $bLeft > $goalX Then If $bTop + $bHeight < $goalY + $goalH And $bLeft + $bWidth < $goalX + $goalW Then MsgBox(0, "Goal", "GOOOOOOOAAAAAAAAAAAAAAAAAAAAAAAAAL!!!") $bLeft = 100 $bTop = 100 $aLeft = 650 $aTop = 300 $greenscore += 1 $c=Floor(Random()*0x1000000) GUISetBkColor($c) EndIf EndIf EndFunc ;==>_check_goal Func _CHEAT() $speed = 10 EndFunc ;==>_CHEAT Func _Save() $saveRx=ControlGetPos("","",$label2) $saveGx=ControlGetPos("","",$label1) $saveGscore=ControlGetText("","",$label4) $saveRscore=ControlGetText("","",$label5) IniWrite("2 player game save.ini", "red position", $saveRx[0], $saveRx[1]) IniWrite("2 player game save.ini", "green position", $saveGx[0], $saveGx[1]) IniWrite("2 player game save.ini", "green score", "green score ", " " & $saveGscore) IniWrite("2 player game save.ini", "red score", "red score ", " " & $saveRscore) EndFunc Func _Load() BlockInput(1) $gp=IniReadSection("2 player game save.ini", "green position") If $gp = 1 Then MsgBox(0, "Error", "there is no save file.") _Save() Exit EndIf $bLeft=$gp[1][0] $bTop=$gp[1][1] ControlMove("","",$label2, $gp[1][0], $gp[1][1]) $rp=IniReadSection("2 player game save.ini", "red position") If $rp = 1 Then MsgBox(0, "Error", "there is no save file.") _Save() Exit EndIf $aLeft=$rp[1][0] $aTop=$rp[1][1] ControlMove( "", "", $label1, $rp[1][0], $rp[1][1]) $gs=IniReadSection("2 player game save.ini", "green score") $greenscore=$gs[1][1] ControlSetText("","",$label4,$gs[1][1]) $rs=IniReadSection("2 player game save.ini", "red score") $redscore=$rs[1][1] ControlSetText("","",$label5,$rs[1][1]) IniDelete("2 player game save.ini", "red position") IniDelete("2 player game save.ini", "green position") IniDelete("2 player game save.ini", "red score") IniDelete("2 player game save.ini", "green score") BlockInput(0) EndFuncsave game note: you can save your exact position and scores by clicking save but if you click load without a save game recorded in the ini file(which must be in the same directorie as the script) the game will exit and save your current game. UPDATE 2:fixed a bug, made a save game feature, yup.UPDATE 1: i made the score able to fully show a score of over 10, and made it so that every time you score a goal or reopen the script it changes the background color:) also added another secret button.previous downloads: 40 from the exe(not included anymore) and 30 from the script2 player game.au3 Edited May 12, 2011 by pieeater [spoiler]My UDFs: Login UDF[/spoiler] Link to comment Share on other sites More sharing options...
stinson6016 Posted April 30, 2011 Share Posted April 30, 2011 i don't have someone to play with, but it's a nice start i'll look at it more in a tad Gnatwork Networks Link to comment Share on other sites More sharing options...
ludocus Posted May 12, 2011 Share Posted May 12, 2011 I like it! Link to comment Share on other sites More sharing options...
dufran3 Posted May 12, 2011 Share Posted May 12, 2011 In XP, when green scores, and the screen resets, the green x is replaced by the goal grey color. Link to comment Share on other sites More sharing options...
pieeater Posted May 12, 2011 Author Share Posted May 12, 2011 (edited) thanks yah that bug did get annoying, and i fixed it, and i set the transparency because i thought it'd be a good idea heres the new code: expandcollapse popup#include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <Misc.au3> $c=Floor(Random()*0x1000000) $speed = 3 $winW = 800 $winH = 600 $winMargin = 5 $greenscore = 0 $redscore = 0 $gscorew = 21 $rscorew = 21 $aLeft = 650 $aTop = 300 $aWidth = 25 $aHeight = 33 $bLeft = 50 $bTop = 300 $bWidth = 25 $bHeight = 33 $goalX = 700 $goalY = 275 $goalW = 75 $goalH = 75 $winMain = GUICreate("2 Player Game", $winW, $winH) GUISetBkColor($c) $lbl = GUICtrlCreateLabel("use wasd keys to move the green X into the goal without the red X touching you", 50, 50) $lbl2 = GUICtrlCreateLabel("use arrow keys to move the red X into the green X", 50, 70) $label1 = GUICtrlCreateLabel("X", $bLeft, $bTop, $bWidth, $bHeight) GUICtrlSetFont($label1, 24, 1600) GUICtrlSetBkColor($label1, 0x00bb00) $label2 = GUICtrlCreateLabel("X", $aLeft, $aTop, $aWidth, $aHeight) GUICtrlSetFont($label2, 24, 1600) GUICtrlSetBkColor($label2, 0xff0000) $label3 = GUICtrlCreateLabel(" GOAL ", $goalX, $goalY, $goalW, $goalH) GUICtrlSetFont($label3, 12, 800) GUICtrlSetBkColor($label3, 0xcc9999) $label4 = GUICtrlCreateLabel($greenscore, 500, 50, $gscorew, 20) GUICtrlSetFont($label4, 12, 800) GUICtrlSetBkColor($label4, 0x00bb00) $label5 = GUICtrlCreateLabel($redscore, 700, 50, $rscorew, 20) GUICtrlSetFont($label5, 12, 800) GUICtrlSetBkColor($label5, 0xff0000) $label6 = GUICtrlCreateLabel("scores", 600, 50, 60, 20) GUICtrlSetFont($label6, 12, 800) $button = GUICtrlCreateButton( "Save Game", 727, 8) $button2 = GUICtrlCreateButton( "Load Game", 727, 33) WinSetTrans( "2 Player Game", "", 150) GUISetState() While 1 $nMsg=GUIGetMsg() If $nMsg == $GUI_EVENT_CLOSE Then Exit If _IsPressed(25) Then _LEFT() If _IsPressed(26) Then _UP() If _IsPressed(27) Then _RIGHT() If _IsPressed(28) Then _DOWN() If _IsPressed(41) Then _LEFT2() If _IsPressed(57) Then _UP2() If _IsPressed(44) Then _RIGHT2() If _IsPressed(53) Then _DOWN2() If $nMsg = $button Then _Save() If $nMsg = $button2 Then _Load() If $aLeft - 25 <= $bLeft And $bLeft <= $aLeft + 25 And $aTop - 33 <= $bTop And $bTop <= $aTop + 33 Then MsgBox(0, "results:", "green loses, red wins") $bLeft = 100 $bTop = 100 $aLeft = 650 $aTop = 300 $redscore += 1 $c=Floor(Random()*0x1000000) GUISetBkColor($c) EndIf ControlSetText("", "", $label1, "X") GUICtrlSetBkColor($label1, 0x00bb00) ControlSetText("", "", $label2, "X") GUICtrlSetBkColor($label2, 0xff0000) ControlMove($winMain, "", $label1, $bLeft, $bTop) ControlMove($winMain, "", $label2, $aLeft, $aTop) ControlSetText($winMain, "", $label4, $greenscore) ControlSetText($winMain, "", $label5, $redscore) _check_goal() WEnd Func _LEFT() $aLeft -= $speed If $aLeft < $winMargin Then $aLeft = $winMargin EndFunc ;==>_LEFT Func _UP() $aTop -= $speed If $aTop < $winMargin Then $aTop = $winMargin EndFunc ;==>_UP Func _RIGHT() $aLeft += $speed If $aLeft > $winW - ($aWidth + $winMargin) Then $aLeft = $winW - ($aWidth + $winMargin) EndFunc ;==>_RIGHT Func _DOWN() $aTop += $speed If $aTop > $winH - ($aHeight + $winMargin) Then $aTop = $winH - ($aHeight + $winMargin) EndFunc ;==>_DOWN Func _LEFT2() $bLeft -= $speed If $bLeft < $winMargin Then $bLeft = $winMargin EndFunc ;==>_LEFT2 Func _UP2() $bTop -= $speed If $bTop < $winMargin Then $bTop = $winMargin EndFunc ;==>_UP2 Func _RIGHT2() $bLeft += $speed If $bLeft > $winW - ($bWidth + $winMargin) Then $bLeft = $winW - ($bWidth + $winMargin) EndFunc ;==>_RIGHT2 Func _DOWN2() $bTop += $speed If $bTop > $winH - ($bHeight + $winMargin) Then $bTop = $winH - ($bHeight + $winMargin) EndFunc ;==>_DOWN2 Func _check_goal() If $bTop > $goalY And $bLeft > $goalX Then If $bTop + $bHeight < $goalY + $goalH And $bLeft + $bWidth < $goalX + $goalW Then MsgBox(0, "Goal", "GOOOOOOOAAAAAAAAAAAAAAAAAAAAAAAAAL!!!") $bLeft = 100 $bTop = 100 $aLeft = 650 $aTop = 300 $greenscore += 1 $c=Floor(Random()*0x1000000) GUISetBkColor($c) EndIf EndIf EndFunc ;==>_check_goal Func _CHEAT() $speed = 10 EndFunc ;==>_CHEAT Func _Save() $saveRx=ControlGetPos("","",$label2) $saveGx=ControlGetPos("","",$label1) $saveGscore=ControlGetText("","",$label4) $saveRscore=ControlGetText("","",$label5) IniWrite("2 player game save.ini", "red position", $saveRx[0], $saveRx[1]) IniWrite("2 player game save.ini", "green position", $saveGx[0], $saveGx[1]) IniWrite("2 player game save.ini", "green score", "green score ", " " & $saveGscore) IniWrite("2 player game save.ini", "red score", "red score ", " " & $saveRscore) EndFunc Func _Load() BlockInput(1) $gp=IniReadSection("2 player game save.ini", "green position") If $gp = 1 Then MsgBox(0, "Error", "there is no save file.") _Save() Exit EndIf $bLeft=$gp[1][0] $bTop=$gp[1][1] ControlMove("","",$label2, $gp[1][0], $gp[1][1]) $rp=IniReadSection("2 player game save.ini", "red position") If $rp = 1 Then MsgBox(0, "Error", "there is no save file.") _Save() Exit EndIf $aLeft=$rp[1][0] $aTop=$rp[1][1] ControlMove( "", "", $label1, $rp[1][0], $rp[1][1]) $gs=IniReadSection("2 player game save.ini", "green score") $greenscore=$gs[1][1] ControlSetText("","",$label4,$gs[1][1]) $rs=IniReadSection("2 player game save.ini", "red score") $redscore=$rs[1][1] ControlSetText("","",$label5,$rs[1][1]) IniDelete("2 player game save.ini", "red position") IniDelete("2 player game save.ini", "green position") IniDelete("2 player game save.ini", "red score") IniDelete("2 player game save.ini", "green score") BlockInput(0) EndFunc also i added a save feature to the game using ini files, however i made it delete your game once you load it, if you want to change that just take out the inidelete() stuff hope you like it Edited May 12, 2011 by pieeater [spoiler]My UDFs: Login UDF[/spoiler] Link to comment Share on other sites More sharing options...
monoscout999 Posted May 12, 2011 Share Posted May 12, 2011 i have a question... is there any way to disable the "clinck" sound of windows when i press a key(in autoit of course).. and there´s a bug(from autoit) when you hold down a key and move the mouse, the movement is much faster... this happend also when you have an event of mouse click holddown... always you move the mouse the event ocurs faster... BTW... for me i play whiout transparency Link to comment Share on other sites More sharing options...
pieeater Posted May 12, 2011 Author Share Posted May 12, 2011 (edited) i fixed the bug :/ what was happening was the mouse moving made the loop end faster, i just added a sleep at the end of my while loop to force it to wait. heres the fix(updated in the OP to). and im not sure about the noise id just say turn mute on and it goes away. ill see what i can do. expandcollapse popup#include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <Misc.au3> $c=Floor(Random()*0x1000000) $speed = 3 $winW = 800 $winH = 600 $winMargin = 5 $greenscore = 0 $redscore = 0 $gscorew = 21 $rscorew = 21 $aLeft = 650 $aTop = 300 $aWidth = 25 $aHeight = 33 $bLeft = 50 $bTop = 300 $bWidth = 25 $bHeight = 33 $goalX = 700 $goalY = 275 $goalW = 75 $goalH = 75 $winMain = GUICreate("2 Player Game", $winW, $winH) GUISetBkColor($c) $lbl = GUICtrlCreateLabel("use wasd keys to move the green X into the goal without the red X touching you", 50, 50) $lbl2 = GUICtrlCreateLabel("use arrow keys to move the red X into the green X", 50, 70) $label1 = GUICtrlCreateLabel("X", $bLeft, $bTop, $bWidth, $bHeight) GUICtrlSetFont($label1, 24, 1600) GUICtrlSetBkColor($label1, 0x00bb00) $label2 = GUICtrlCreateLabel("X", $aLeft, $aTop, $aWidth, $aHeight) GUICtrlSetFont($label2, 24, 1600) GUICtrlSetBkColor($label2, 0xff0000) $label3 = GUICtrlCreateLabel(" GOAL ", $goalX, $goalY, $goalW, $goalH) GUICtrlSetFont($label3, 12, 800) GUICtrlSetBkColor($label3, 0xcc9999) $label4 = GUICtrlCreateLabel($greenscore, 500, 50, $gscorew, 20) GUICtrlSetFont($label4, 12, 800) GUICtrlSetBkColor($label4, 0x00bb00) $label5 = GUICtrlCreateLabel($redscore, 700, 50, $rscorew, 20) GUICtrlSetFont($label5, 12, 800) GUICtrlSetBkColor($label5, 0xff0000) $label6 = GUICtrlCreateLabel("scores", 600, 50, 60, 20) GUICtrlSetFont($label6, 12, 800) $button = GUICtrlCreateButton( "Save Game", 727, 8) $button2 = GUICtrlCreateButton( "Load Game", 727, 33) WinSetTrans( "2 Player Game", "", 150) GUISetState() While 1 $nMsg=GUIGetMsg() If $nMsg == $GUI_EVENT_CLOSE Then Exit If _IsPressed(25) Then _LEFT() If _IsPressed(26) Then _UP() If _IsPressed(27) Then _RIGHT() If _IsPressed(28) Then _DOWN() If _IsPressed(41) Then _LEFT2() If _IsPressed(57) Then _UP2() If _IsPressed(44) Then _RIGHT2() If _IsPressed(53) Then _DOWN2() If $nMsg = $button Then _Save() If $nMsg = $button2 Then _Load() If $aLeft - 25 <= $bLeft And $bLeft <= $aLeft + 25 And $aTop - 33 <= $bTop And $bTop <= $aTop + 33 Then MsgBox(0, "results:", "green loses, red wins") $bLeft = 100 $bTop = 100 $aLeft = 650 $aTop = 300 $redscore += 1 $c=Floor(Random()*0x1000000) GUISetBkColor($c) EndIf ControlSetText("", "", $label1, "X") GUICtrlSetBkColor($label1, 0x00bb00) ControlSetText("", "", $label2, "X") GUICtrlSetBkColor($label2, 0xff0000) ControlMove($winMain, "", $label1, $bLeft, $bTop) ControlMove($winMain, "", $label2, $aLeft, $aTop) ControlSetText($winMain, "", $label4, $greenscore) ControlSetText($winMain, "", $label5, $redscore) _check_goal() Sleep(10) WEnd Func _LEFT() $aLeft -= $speed If $aLeft < $winMargin Then $aLeft = $winMargin EndFunc ;==>_LEFT Func _UP() $aTop -= $speed If $aTop < $winMargin Then $aTop = $winMargin EndFunc ;==>_UP Func _RIGHT() $aLeft += $speed If $aLeft > $winW - ($aWidth + $winMargin) Then $aLeft = $winW - ($aWidth + $winMargin) EndFunc ;==>_RIGHT Func _DOWN() $aTop += $speed If $aTop > $winH - ($aHeight + $winMargin) Then $aTop = $winH - ($aHeight + $winMargin) EndFunc ;==>_DOWN Func _LEFT2() $bLeft -= $speed If $bLeft < $winMargin Then $bLeft = $winMargin EndFunc ;==>_LEFT2 Func _UP2() $bTop -= $speed If $bTop < $winMargin Then $bTop = $winMargin EndFunc ;==>_UP2 Func _RIGHT2() $bLeft += $speed If $bLeft > $winW - ($bWidth + $winMargin) Then $bLeft = $winW - ($bWidth + $winMargin) EndFunc ;==>_RIGHT2 Func _DOWN2() $bTop += $speed If $bTop > $winH - ($bHeight + $winMargin) Then $bTop = $winH - ($bHeight + $winMargin) EndFunc ;==>_DOWN2 Func _check_goal() If $bTop > $goalY And $bLeft > $goalX Then If $bTop + $bHeight < $goalY + $goalH And $bLeft + $bWidth < $goalX + $goalW Then MsgBox(0, "Goal", "GOOOOOOOAAAAAAAAAAAAAAAAAAAAAAAAAL!!!") $bLeft = 100 $bTop = 100 $aLeft = 650 $aTop = 300 $greenscore += 1 $c=Floor(Random()*0x1000000) GUISetBkColor($c) EndIf EndIf EndFunc ;==>_check_goal Func _CHEAT() $speed = 10 EndFunc ;==>_CHEAT Func _Save() $saveRx=ControlGetPos("","",$label2) $saveGx=ControlGetPos("","",$label1) $saveGscore=ControlGetText("","",$label4) $saveRscore=ControlGetText("","",$label5) IniWrite("2 player game save.ini", "red position", $saveRx[0], $saveRx[1]) IniWrite("2 player game save.ini", "green position", $saveGx[0], $saveGx[1]) IniWrite("2 player game save.ini", "green score", "green score ", " " & $saveGscore) IniWrite("2 player game save.ini", "red score", "red score ", " " & $saveRscore) EndFunc Func _Load() BlockInput(1) $gp=IniReadSection("2 player game save.ini", "green position") If $gp = 1 Then MsgBox(0, "Error", "there is no save file.") _Save() Exit EndIf $bLeft=$gp[1][0] $bTop=$gp[1][1] ControlMove("","",$label2, $gp[1][0], $gp[1][1]) $rp=IniReadSection("2 player game save.ini", "red position") If $rp = 1 Then MsgBox(0, "Error", "there is no save file.") _Save() Exit EndIf $aLeft=$rp[1][0] $aTop=$rp[1][1] ControlMove( "", "", $label1, $rp[1][0], $rp[1][1]) $gs=IniReadSection("2 player game save.ini", "green score") $greenscore=$gs[1][1] ControlSetText("","",$label4,$gs[1][1]) $rs=IniReadSection("2 player game save.ini", "red score") $redscore=$rs[1][1] ControlSetText("","",$label5,$rs[1][1]) IniDelete("2 player game save.ini", "red position") IniDelete("2 player game save.ini", "green position") IniDelete("2 player game save.ini", "red score") IniDelete("2 player game save.ini", "green score") BlockInput(0) EndFunc Edited May 12, 2011 by pieeater [spoiler]My UDFs: Login UDF[/spoiler] Link to comment Share on other sites More sharing options...
monoscout999 Posted May 12, 2011 Share Posted May 12, 2011 nice... i gonna try to make the snake game Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now