AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Pong - the ultimate retro game
Goto page 1, 2  Next
 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
Lequi*
Guest





PostPosted: Fri Aug 31, 2007 1:16 pm    Post subject: Pong - the ultimate retro game Reply with quote

its one or my first scripts done with ahk so i wanted to ask if i can optimize sth, i can for sure let the code look better with more comments etc but i dont comment much normaly

at first u can simply test the game and post suggestions or improvements here. many thanks in advance Smile

The lastest version of this script is now further below in this same topic.
Back to top
Charon the Hand



Joined: 07 Aug 2007
Posts: 4

PostPosted: Fri Aug 31, 2007 3:18 pm    Post subject: Reply with quote

This is pretty impressive. One minor problem is when the paddle is at the bottom of the screen, half of it wraps around to the top. Also the bounces were a little wierd when I got my paddle there almost too late to catch the ball. Overall, a very cool little script.
Back to top
View user's profile Send private message
aCkRiTe



Joined: 21 Jul 2006
Posts: 502

PostPosted: Fri Aug 31, 2007 3:25 pm    Post subject: Reply with quote

Very Nice! I like it. I havent had a chance to play it a lot, but does the computer ever miss? I havent seen it miss once...
_________________

HTH...

Back to top
View user's profile Send private message
Lequi
Guest





PostPosted: Fri Aug 31, 2007 5:54 pm    Post subject: Reply with quote

Code:
settimer, pcmove, 1    ;<--- sets pc difficult


thats the timer triggering the pc movement, 1 ms is quite fast Wink if its too hard you can change 1 ms to 15 (beatable) or even 30, that would be quite easy.
you can also change the ball base speed and its increasing speed every 3 sek if you want
Code:
if SW < 1300       ;<--- sets the ball speed
   speed = 10
else
   speed = 15

if your monitor is 1280 x xxx or below u need to change the upper line, in my case, 1680x 1050, i need to change the lower one
Back to top
aCkRiTe



Joined: 21 Jul 2006
Posts: 502

PostPosted: Fri Aug 31, 2007 6:48 pm    Post subject: Reply with quote

Thanks! Guess I should have taken a look at you code comments... Razz
_________________

HTH...

Back to top
View user's profile Send private message
Lequi
Guest





PostPosted: Fri Aug 31, 2007 7:02 pm    Post subject: Reply with quote

i got sounds too, which truly make this game retro style, is there a way to attach them to this forum? i got no space where i could upload them :/
Back to top
SKAN



Joined: 26 Dec 2005
Posts: 5884

PostPosted: Fri Aug 31, 2007 7:24 pm    Post subject: Reply with quote

Impressive! Very Happy

Lequi wrote:
i got no space where i could upload them :/


http://www.autohotkey.net/file/index.php?act=reg

Smile
Back to top
View user's profile Send private message
Lequi



Joined: 31 Aug 2007
Posts: 29

PostPosted: Fri Aug 31, 2007 8:25 pm    Post subject: Reply with quote

right click, save target as, dont change name and save the files where the pong file is
http://www.autohotkey.net/~Lequi/pong_board_bounce.wav
http://www.autohotkey.net/~Lequi/pong_goal.wav
http://www.autohotkey.net/~Lequi/pong_wall_bounce.wav

edit: any suggestions for further features or code improvements are welcome Smile and another game is coming soon Wink
a suggestion from me is to hide the mouse cursor, anyone knows how to manage that?
Back to top
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 5884

PostPosted: Fri Aug 31, 2007 9:09 pm    Post subject: Reply with quote

Lequi wrote:
right click, save target as, dont change name and save the files where the pong file is
http://www.autohotkey.net/~Lequi/pong_board_bounce.wav
http://www.autohotkey.net/~Lequi/pong_goal.wav
http://www.autohotkey.net/~Lequi/pong_wall_bounce.wav

edit: any suggestions for further features or code improvements are welcome Smile


Add this to the top of the script:

Code:
SetWorkingDir, %A_ScriptDir%

IfNotExist, pong_board_bounce.wav, UrlDownloadToFile
          , http://www.autohotkey.net/~Lequi/pong_board_bounce.wav
          , pong_board_bounce.wav

IfNotExist, pong_goal.wav, UrlDownloadToFile
          , http://www.autohotkey.net/~Lequi/pong_goal.wav
          , pong_goal.wav


IfNotExist, pong_wall_bounce.wav, UrlDownloadToFile
          , http://www.autohotkey.net/~Lequi/pong_wall_bounce.wav
          , pong_wall_bounce.wav


Smile
Back to top
View user's profile Send private message
Lequi



Joined: 31 Aug 2007
Posts: 29

PostPosted: Fri Aug 31, 2007 9:26 pm    Post subject: Reply with quote

cause i cant edit my first post, (wasnt registered to that point of time) here the new version: this now downloads the sound files if you dont have them alrdy and the hotkeys only work if pong is the active window

Code:
Scroll down a few posts to see the latest script


thx skan Smile

edit: changed buttons to progress bar in order to change colors, looks a bit more retro Wink


Last edited by Lequi on Fri Aug 31, 2007 9:33 pm; edited 1 time in total
Back to top
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 5884

PostPosted: Fri Aug 31, 2007 9:33 pm    Post subject: Reply with quote

Lequi wrote:
cause i cant edit my first post, (wasnt registered to that point of time)


A moderator can alter the ownership of the title post.
Send a PM to one of them ( maybe Titan )

Lequi previously wrote:
a suggestion from me is to hide the mouse cursor, anyone knows how to manage that?


Here is a working demo. Adapt it to your needs:

Code:
DllCall("ShowCursor", UInt,False )
Gui, Show, w400 h300, No Cursor Zone
OnExit, QuitScript
Return

GuiClose:
QuitScript:
 DllCall("ShowCursor", UInt,True )
 ExitApp
Return


Smile
Back to top
View user's profile Send private message
Lexikos



Joined: 17 Oct 2006
Posts: 2558
Location: Australia, Qld

PostPosted: Sat Sep 01, 2007 5:16 am    Post subject: Reply with quote

Heh, nice. The first thing I did after trying it - I added:
Code:
Gui, +LastFound
WinSet, TransColor, Black
above
Code:
gui, +toolwindow +alwaysontop
Cool
Back to top
View user's profile Send private message
Lequi



Joined: 31 Aug 2007
Posts: 29

PostPosted: Sat Sep 01, 2007 11:04 am    Post subject: Reply with quote

this is the latest version

change some more things and now its easier to configurate it like you want
edit: included ingame difficult config for those who cant change the code (compiled version)
- added impossible mode (believe me, it is Wink )
Code:

;configuration area:
color1 = Lime             ; color of the players paddle, colorname or hexcode
color2 = Lime             ; color of the computer paddle, "
color3 = Lime             ; color of the ball,       "
transparent_background = 0      ; enter 1 to have a complete transparent background, 0 for a black bg
random_angle = 15         ; determines angle of incidence = angle of reflection, if 0 it is mathematicaly      

               ; correct, 50 or more is totaly random

;end of configuration

;ingame config
gui, 3:default
gui, margin, 15 ,15
gui, font, s13
Gui, Add, Text,, Instructions:
gui, font, s10
gui, add, text,, F1 for help.`nLeft Mouseclick to start the ball`nP for Pause`nCtrl+x for exit`nS to toggle sound on/off
gui, font, s13
Gui, Add, Text,, Choose your difficulty:
gui, font, s10
Gui, Add, Button, gBeg w350 h25 x20 y200, Beginner
Gui, Add, Button, gInt wp hp xp yp+30, Intermediate
Gui, Add, Button, gHard wp hp xp yp+30, Hard
Gui, Add, Button, gimp wp hp xp yp+30, Impossible
Gui, Show,autosize, Ping Pong
return

Beg:
pc_difficulty = 30    
pc_board_speed = 15      
ball_speed = 15            
ball_increasing_speed = 2
gui, destroy
goto go_ahead
return
Int:
pc_difficulty = 20    
pc_board_speed = 23      
ball_speed = 23            
ball_increasing_speed = 4
gui, destroy
goto go_ahead
return
Hard:
pc_difficulty = 1
pc_board_speed = 30         
ball_speed = 30            
ball_increasing_speed = 5
gui, destroy
goto go_ahead
return
imp:
imp =1
pc_difficulty = 1
pc_board_speed = 30         
ball_speed = 35            
ball_increasing_speed = 7
gui, destroy
goto go_ahead
return

go_ahead:
#persistent
SetWorkingDir, %A_ScriptDir%

IfNotExist, pong_board_bounce.wav, UrlDownloadToFile
          , http://www.autohotkey.net/~Lequi/pong_board_bounce.wav
          , pong_board_bounce.wav

IfNotExist, pong_goal.wav, UrlDownloadToFile
          , http://www.autohotkey.net/~Lequi/pong_goal.wav
          , pong_goal.wav


IfNotExist, pong_wall_bounce.wav, UrlDownloadToFile
          , http://www.autohotkey.net/~Lequi/pong_wall_bounce.wav
          , pong_wall_bounce.wav


SW := A_ScreenWidth
SH := A_ScreenHeight
SW_2 := A_ScreenWidth/2
SH_2 := A_ScreenHeight/2
right_goal := SW-30
right_block := SW-80
lower_bounce := SH-20
xpos_board2 := SW-50
points0= 0
points1= 0
check= 1
sound = 1
gui,1:default
DllCall("ShowCursor", UInt,False )

if transparent_background
{
   Gui, +LastFound
   WinSet, TransColor, Black
}
gui, +toolwindow +alwaysontop
gui, color, black
temp := SW_2-0.1*SW
gui, font, s35
Gui, Add, Text, -wrap vstats cLime x%temp% y50 w200, %points0%  :  %points1%
temp := SW_2-100
temp2 := SH_2-50
Gui, add, text, vpause cLime x%temp% y%temp2%, Paused
guicontrol, hide, pause
gui, add, progress, Background%color3% vball w20 h20 x%sw_2% y%sh_2%
temp := SH_2-50
gui, add, progress, Background%color1% vboard1 x50 y%temp% w30 h100
gui, add, progress, Background%color2% vboard2 x%xpos_board2% y%temp% w30 h100
temp := SW-0.1*SW
gui, font, s15
gui, add, text, vvsound cgray x%temp% y30, Sound: On
gui, show, w%sw% h%sh%

settimer, mousemove, 10
settimer, pcmove, %pc_difficulty%
settimer, balance, 3000

if SW < 1300       
   offset := ball_speed-5
else
   offset := ball_speed
goto init
return

init:
KeyWait, LButton, D
if check
   goto start
return

start:
check=0
pi := (4*Atan(1))
loop
{
   random, angle, 0, 360
   ;msgbox %angle%
   if angle between 45 and 135
      continue
   if angle between 225 and 315
      continue
   else
      break
}
goto move
return


mousemove: ;player board moving
if not check
{
   mousegetpos,, mousey
   mousey -= 65
   guicontrol, move, board1, x50 y%mousey%
}
return


pcmove:
GuiControlGet, board2, Pos
GuiControlGet, ball, Pos
bally -= 50


if xpos < %SW_2%          ; ball in player half
{
   dif := abs(board2y - SH_2)
   
   if dif < 20
      pc_offset =0
   else
      pc_offset = %pc_board_speed%
   if board2y < %SH_2%
   {
      pc_ypos := board2y + pc_offset
   }
   else
   {
      pc_ypos := board2y - pc_offset
   }
   guicontrol, move, board2, x%xpos_board2% y%pc_ypos%
}
else               ; ball in pc's half
{
   dif := abs(board2y - bally)
   
   if dif < 20
      pc_offset =0
   else
      pc_offset = 20

   if board2y < %bally%
   {
      pc_ypos := board2y + pc_offset
   }
   else
   {
      pc_ypos := board2y - pc_offset
   }
   if imp
      guicontrol, move, board2, x%xpos_board2% y%bally%
   else
      guicontrol, move, board2, x%xpos_board2% y%pc_ypos%
}
return

move:  ;ball moving
loop
{
   rad := angle * pi/180
   GuiControlGet, ball, Pos
   xpos := ballx + cos(rad)*offset
   ypos := bally + sin(rad)*offset
   guicontrol, move, ball, x%xpos% y%ypos%
   if bounce
   {
      offset -=30
      bounce =0
   }
   sleep 1

   ;bounce
   random, rnd, -%random_angle%, %random_angle%

   
   if xpos > %right_block% ;right side block
   {
      diff := ypos-pc_ypos
      if  diff between 0 and 100
      {
         bounce = 1
         offset += 30
         if sound
            soundplay, %a_workingdir%\pong_board_bounce.wav
         angle := 180-angle+rnd
      }
   }
   if xpos > %right_goal% ;right side goal
   {
      if sound
         soundplay, %a_workingdir%\pong_goal.wav
      points0++
      GuiControl,, stats, %points0%  :  %points1%
      guicontrol, move, ball, x%sw_2% y%sh_2%
      bounce =0
      if SW < 1300       
         offset := ball_speed-5
      else
         offset := ball_speed
      KeyWait, LButton, D
         goto start

   }


   if xpos < 90
   {
      diff := ypos-mousey
      if  diff between -30 and 100 ;left side block
      {
         bounce = 1
         offset += 30
         if sound
            soundplay, %a_workingdir%\pong_board_bounce.wav
         angle := 180-angle+rnd
      }
   }
   if xpos < 30 ;left side goal
   {
      if sound
         soundplay, %a_workingdir%\pong_goal.wav
      points1++
      GuiControl,, stats, %points0%  :  %points1%
      guicontrol, move, ball, x%sw_2% y%sh_2%
      bounce =0
      if SW < 1300       
         offset := ball_speed-5
      else
         offset := ball_speed
      KeyWait, LButton, D
         goto start
   }


   if ypos > %lower_bounce% ;lower side
   {
      bounce = 1
      offset += 30
      if sound
         soundplay, %a_workingdir%\pong_wall_bounce.wav
      angle := 360-angle+rnd
   }
   if ypos < 0 ;upper side
   {
      bounce = 1
      offset += 30
      if sound
         soundplay, %a_workingdir%\pong_wall_bounce.wav
      angle := 360-angle+rnd
   }
      

}
return

balance:
offset+= %ball_increasing_speed%
return

#IfWinActive, ahk_class AutoHotkeyGUI
s::             ;sound toggle
if sound
{
   sound = 0
   guicontrol, , vsound, Sound:%a_space%Off
}
else
{
   sound = 1
   guicontrol, , vsound, Sound:%a_space%On
}
return

#IfWinActive, ahk_class AutoHotkeyGUI ;help
F1::            
gui, 1:-alwaysontop
gui, 2:margin, 15 ,15
gui, 2:destroy
gui, 2:font, s13
gui, 2:add, text, , Left Mouseclick to start the ball`nP for Pause`nStrg+x for exit`nS to toggle sound on/off
gui, 2:add, button, default yp+90 x100, Close
gui, 2:show, autosize, Help
return
2ButtonClose:
2guiclose:
2guiescape:
gui, 2:destroy
gui,1: +alwaysontop
return

#IfWinActive, ahk_class AutoHotkeyGUI  ;pause
p::            
gui, 1:-alwaysontop
DllCall("ShowCursor", UInt,True )
guicontrol, show, pause
settimer, mousemove, off
settimer, pcmove, off
settimer, balance, off
sleep, 200
KeyWait, P, D
gui, 1:+alwaysontop
DllCall("ShowCursor", UInt,false )
guicontrol, hide, pause
settimer, mousemove, on
settimer, pcmove, on
settimer, balance, on
return

#IfWinActive, ahk_class AutoHotkeyGUI ;exit
3guiclose:
guiclose:
^x::            
DllCall("ShowCursor", UInt,True )
exitapp
return


Last edited by Lequi on Mon Sep 03, 2007 12:07 pm; edited 3 times in total
Back to top
View user's profile Send private message
Chris
Site Admin


Joined: 02 Mar 2004
Posts: 10467

PostPosted: Sun Sep 02, 2007 3:11 am    Post subject: Reply with quote

Although I can't easily assign a guest post to a registered user, I did edit the topmost post to refer to the new version of the script. If you prefer, I could split this topic so that your registered post becomes the topmost post of a new topic.
Back to top
View user's profile Send private message Send e-mail
Lequi



Joined: 31 Aug 2007
Posts: 29

PostPosted: Sun Sep 02, 2007 11:32 am    Post subject: Reply with quote

thats already perfect, thank you chris Smile
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group