Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

brAHKout


  • Please log in to reply
8 replies to this topic
ton80
  • Members
  • 74 posts
  • Last active: Oct 26 2017 12:20 AM
  • Joined: 18 Dec 2009
breakout game in AHK.. for now, the math is generic. a script i did to better understand regions and region offsets.

screenshot
Posted Image

;brAHKout by Ton80
;11-23-11
SetBatchLines, -1
#NoEnv

Green    = 0x008000 
Silver    = 0xC0C0C0 
Lime       = 0x00FF00 
Gray       = 0x808080
Olive       = 0x008080 
White    = 0xFFFFFF
Yellow    = 0x00FFFF
Maroon    = 0x000080
Navy    = 0x800000
Red       = 0x0000FF
Blue       = 0xFF0000
Purple    = 0x800080 
Teal       = 0x808000 
Fuchsia    = 0xFF00FF
Aqua    = 0xFFFF00
Black	= 0x000000

Score := 0
Lives := 5
FullScreen := 0
boardHeight := 768
boardWidth := 1024
If (FullScreen)
	boardHeight := A_ScreenHeight, boardWidth := A_ScreenWidth
backgroundColor := Black				

startBallX := BoardWidth / 2
startBallY := BoardHeight / 2
ballHeight := boardWidth * .02
ballWidth := boardWidth * .02
ballColor := Gray
ballPenWidth := 2
ballPenColor := Silver
ballSpeedX := boardwidth *.001
ballSpeedY := boardHeight *.003
ballAngle := 180
ballX := startBallX
ballY := startBallY
ballDirection = Down

paddleX := BoardWidth/2
paddleY := BoardHeight * .94
paddleHeight := boardHeight *.03		
paddleWidth := BoardWidth *.12					

paddleColor := Blue
paddlePenWidth := 1
paddlePenColor := Yellow

bricksPerRow := 15
brickRows	:= 5
brickHeight := boardHeight /30
brickWidth := boardWidth / bricksPerRow
brickPenWidth := 3
brickPenColor := backGroundColor
brickColor := Green
bottomBrickY := (brickRows + 1) * BrickHeight

scoreColor := Red
scoreX := 20
scoreY := boardHeight *.975

LivesX := boardWidth * .8
LivesY := boardHeight * .975

VarSetCapacity(ptWin, 16, 0)
NumPut(BoardWidth, ptWin, 8) , NumPut(BoardHeight, ptWin, 12) 
gui,show, h%BoardHeight% w%BoardWidth%, brAHKout																						; shows the gui

hdcWin := DllCall("GetDC", "UInt", hwnd:=WinExist("A"))
hdcMem := DllCall("CreateCompatibleDC", "Uint", hdcWin)
hbm := DllCall("CreateCompatibleBitmap", "uint", hdcWin, "int", BoardWidth, "int", BoardHeight)
DllCall("SelectObject", "uint", hdcMem, "uint", hbm)

paddlePen := DllCall("CreatePen", UInt, 0, UInt, paddlePenWidth, UInt, paddlePenColor)
ballPen := DllCall("CreatePen", UInt, 0, Uint, ballPenWidth, UInt, ballPenColor)
brickPen := DllCall("CreatePen", UInt, 0, UInt, brickPenWidth, UInt, brickPenColor)
paddleBrush:= DllCall("CreateSolidBrush", "int", paddleColor)
ballBrush:= DllCall("CreateSolidBrush", "int", ballColor)
backgroundBrush := DllCall("CreateSolidBrush", "Int", backgroundColor)
brickBrush := DllCall("CreateSolidBrush", "Int", brickColor)
DllCall("SetTextColor", "Int", hdcMem, "Int", scoreColor)

MouseMove, paddleX, paddleY
gosub, CreateBrickArray
gosub, CreateBrickRegions
gosub, CreatePaddleRegions
gosub, CreateBallRegion

Loop
	{
		paddlePrev := paddleX|paddleY<<16
		ballPrev := ballX|ballY<<16
		
		MouseGetPos, PaddleX
			If (PaddleX >= BoardWidth - PaddleWidth)																												;keep paddle from going off right edge of screen
				PaddleX := BoardWidth - PaddleWidth
			If (PaddleX <= 0)
				PaddleX := 0
			
		If (ballDirection = "Up")
			ballY -= BallSpeedY
		else If (ballDirection = "Down")
			ballY += BallSpeedY
		else If (ballDirection = "RightUp")
			ballY -= ballSpeedY, ballX += ballSpeedX
		else If (ballDirection = "LeftUp")
			ballY -= ballSpeedY, ballX -= ballSpeedX
		else If (ballDirection = "RightDown")
			ballY += ballSpeedY, ballX += ballSpeedX
		else If (ballDirection = "LeftDown")
			ballY += ballSpeedY, ballX -= ballSpeedX
			
		paddleCur := paddleX|paddleY<<16
		paddleOffset := paddleCur - PaddlePrev
		ballCur := ballX|ballY<<16
		ballOffset := ballCur - ballPrev
			
		;background
		DllCall("SelectObject", "Uint", hdcMem, "UInt", backgroundBrush)
		DllCall("FillRect", "uint", hdcMem, "uint", &ptWin, "uint", 0)
		
		
		;paddle
		DllCall("SelectObject", "Uint", hdcMem, "UInt", paddleBrush)
		DllCall("SelectObject", UInt,hdcMem, UInt,paddlePen)
		DllCall("Rectangle", uint, hdcMem, uint, PaddleX, uint, paddleY, uint, PaddleX+paddleWidth, uint,paddleY + paddleHeight)
		DllCall("OffsetRgn", "Int", PaddleC, "Int", paddleOffset, "Int", 0)
		DllCall("OffsetRgn", "Int", PaddleL, "Int", paddleOffset, "Int", 0)
		DllCall("OffsetRgn", "Int", PaddleR, "Int", paddleOffset, "Int", 0)
		
		;ball
		DllCall("SelectObject", "Uint", hdcMem, "UInt", ballBrush)
		DllCall("SelectObject", UInt,hdcMem, UInt,ballPen)
		DllCall("RoundRect", uint, hdcMem, uint, ballX, uint, ballY, uint, ballX + ballWidth, uint, ballY + ballHeight, uint, ballWidth, uint, ballHeight)
		if (ballX <= 0) or (ballX >= boardwidth - ballWidth)
			balldirection := _ChangeBallDirection_Wall(balldirection)
		DllCall("OffsetRgn", "Int", ball, "Int", ballOffset, "Int", ballOffset)
		
		;bricks
		DllCall("SelectObject", "Uint", hdcMem, "UInt", brickBrush)
		DllCall("SelectObject", "Uint", hdcMem, "UInt", brickPen)
		bx := 0
		by := 0
		brickNumber := 1
		Loop, %brickRows%
			{
				Loop, %bricksPerRow%
					{	
						If (sBrick%brickNumber%)
							DllCall("Rectangle", uint, hdcMem, uint, bx, uint, by, uint, brickWidth+bx, uint,BrickHeight+by)
						bx += brickWidth
						brickNumber++
					}
				by += brickheight
				bx := 0
			}

		;score
		scoreD := "score : " score
		livesD := "lives : " lives
		DllCall("TextOut", "uint", hdcMem, "uint", scoreX, "uint", scoreY, "uint", &scoreD, "uint", StrLen(scoreD))
		DllCall("TextOut", "uint", hdcMem, "uint", livesX, "uint", livesY, "uint", &livesD, "uint", StrLen(livesD))
		
		;update screen
		DllCall("BitBlt", "uint", hdcWin, "int", 0, "int", 0, "int", BoardWidth, "int", BoardHeight, "uint", hdcMem, "int", 0, "int", 0, "uint", 0xCC0020)
		
		;if ball hits paddle
		if ( DllCall("PtInRegion", "Int", paddleC, "uint", ballX, "uint", ballY+ballheight)) or ( DllCall("PtInRegion", "Int", paddleC, "uint", ballX + ballWidth, "uint", ballY+ballheight))
			{
				ballDirection := _ChangeBallDirection_paddleC(balldirection)
				ballSpeedX -= .01
				continue
			}
		
		if ( DllCall("PtInRegion", "Int", paddleL, "uint", ballX, "uint", ballY+ballheight)) or ( DllCall("PtInRegion", "Int", paddleL, "uint", ballX + ballWidth, "uint", ballY+ballheight))
			{
				ballDirection := "LeftUp"
				ballSpeedX += .1
				continue
			}
			
		if ( DllCall("PtInRegion", "Int", paddleR, "uint", ballX, "uint", ballY+ballheight)) or ( DllCall("PtInRegion", "Int", paddleR, "uint", ballX + ballWidth, "uint", ballY+ballheight))
			{
				ballDirection := "RightUp"
				ballSpeedX += .1
				continue
			}
		
		;ball lost
		If (ballY >= BoardHeight)
			{
				Lives -= 1
				If (Lives <= 0)
					{
					MsgBox Game Over!`r`nScore: %Score%
					Reload
					}
				else
					{
					MsgBox, Score : %Score%`r`nLives : %Lives%
					ballX := startBallX
					ballY := startBallY
					balldirection = down
					}
			}
			
		If (ballY <= 0)
			ballDirection := _ChangeBallDirection_Brick(balldirection)
		
		If (bally <= BottomBrickY)			;no need to check if ball is below bottom brick
			{
				Loop % brickRows * bricksPerRow
					{
							if (!sbrick%A_Index%)	; if brick is already broke, dont check to see if the fekin ball hit it
								continue
							
							if ( DllCall("PtInRegion", "Int", brick%A_Index%, "uint", ballX, "uint", ballY+ballheight)) or ( DllCall("PtInRegion", "Int", brick%A_Index%, "uint", ballX, "uint", ballY)) or ( DllCall("PtInRegion", "Int", brick%A_Index%, "uint", ballX+ballwidth, "uint", ballY)) or ( DllCall("PtInRegion", "Int", brick%A_Index%, "uint", ballX+ballwidth, "uint", ballY+ballheight))
							{
								sBrick%A_Index% := 0
								DllCall("DeleteObject", "UInt", Brick%A_Index%)
								ballDirection := _ChangeBallDirection_Brick(balldirection)
								Score += 10
								brickCount --
								if (brickcount <= 0)
									{
										sleep, 2000
										gosub, CreateBrickArray
										gosub, CreateBrickRegions
										ballX := startBallX
										ballY := startBallY
										ballDirection = Down
									}
							}
					}
			}
	}

CreateBrickArray:
brickCount := bricksPerRow * brickRows
Loop %brickCount%
	sbrick%A_Index% := 1
return

CreateBrickRegions:
bx := 0
by := 0
brickNumber := 1   
Loop, %brickRows%
	{
		Loop, %bricksPerRow%
			{	
				If (sBrick%brickNumber%)
					{
						DllCall("DeleteObject", "UInt", brick%brickNumber%)
						Brick%brickNumber% := DllCall("CreateRectRgn", uint, bx, uint, by, uint,  brickWidth+bx, uint, BrickHeight+by)
					}
						bx += brickWidth
						brickNumber++
			}
				by += brickheight
				bx := 0
	}
return

CreatePaddleRegions:
paddleC := DLLCall("CreateRectRgn", "Int", PaddleX + paddlewidth * .25, "Int", paddleY, "Int",  PaddleX + paddlewidth * .75, "Int", paddleY + paddleHeight)
paddleL := DLLCall("CreateRectRgn", "Int", PaddleX, "Int", paddleY, "Int",  PaddleX + paddlewidth * .25, "Int", paddleY + paddleHeight)
paddleR := DLLCall("CreateRectRgn", "Int", PaddleX + paddlewidth * .75, "Int", paddleY, "Int",  paddleX + paddleWidth, "Int", paddleY + paddleHeight)
return

CreateBallRegion:
Ball := DLLCall("CreateRoundRectRgn", "Int", ballX, "Int", ballY, "Int", ballX + ballWidth, "Int", ballY + ballHeight, "Int", ballWidth, "Int", ballHeight)
return


esc::
ExitApp

_ChangeBallDirection_Wall(direction){
	if direction = up
		direction = Down
	else if direction = down
		direction = up
	else if direction = leftup
		direction = rightup
	else if direction = rightup
		direction = leftup
	else if direction = leftdown
		direction = rightdown
	else if direction = rightdown
		direction = leftdown
	return, direction
}

_ChangeBallDirection_Brick(direction){
	if direction = up
		direction = Down
	else if direction = down
		direction = LeftUp
	else if direction = leftup
		direction = leftdown
	else if direction = rightup
		direction = rightdown
	else if direction = leftdown
		direction = leftup
	else if direction = rightdown
		direction = rightup
	return, direction
}

_ChangeBallDirection_paddleC(direction){
	if direction = down
		direction = up
	else if direction = leftdown
		direction = leftup
	else if direction = rightdown
		direction = rightup
	return, direction
}



nimda
  • Members
  • 4368 posts
  • Last active: Aug 09 2015 02:36 AM
  • Joined: 26 Dec 2010
Cooll 8)
one thing I noticed: your if syntax is wrong. It works, but not in the way you think
If (a) or if (b)
; should be
If (a) or (b)
; or even
If (a or b)
Your if statements like the first one I showed say "If the variable 'a' is true; or if the variable 'if' concactenated with the variable 'b' is true"
surely you don't intend to use "if" as a variable? Or do you... :shock: :p

ton80
  • Members
  • 74 posts
  • Last active: Oct 26 2017 12:20 AM
  • Joined: 18 Dec 2009

Cooll 8)
one thing I noticed: your if syntax is wrong. It works, but not in the way you think

If (a) or if (b)
; should be
If (a) or (b)
; or even
If (a or b)
Your if statements like the first one I showed say "If the variable 'a' is true; or if the variable 'if' concactenated with the variable 'b' is true"
surely you don't intend to use "if" as a variable? Or do you... :shock: :p



thanks nimda. changes made.

Zaelia
  • Members
  • 754 posts
  • Last active: Jan 17 2015 02:38 AM
  • Joined: 31 Oct 2008
Congratulations ! an other classic game with ahk
"You annoy me, therefore I exist."

sumon
  • Moderators
  • 1317 posts
  • Last active: Dec 05 2016 10:14 PM
  • Joined: 18 May 2010

Congratulations ! an other classic game with ahk


Indeed!

Fun, difficult. Score.

Learning one
  • Members
  • 1483 posts
  • Last active: Jan 02 2016 02:30 PM
  • Joined: 04 Apr 2009
Nice work ton80! :)

Elesar
  • Members
  • 696 posts
  • Last active: May 08 2015 09:51 PM
  • Joined: 28 Jun 2007
Well done!

My high score so far...550

nimda
  • Members
  • 4368 posts
  • Last active: Aug 09 2015 02:36 AM
  • Joined: 26 Dec 2010
I got well past 1000 before I realized that I have access to high quality first person shooters 8)

It's an excellent game :D

automaticman
  • Members
  • 658 posts
  • Last active: Nov 20 2012 06:10 PM
  • Joined: 27 Oct 2006
Another wow, great. Thanks!