Masking a simple color and collisions?!

BlitzMax Forums/BlitzMax Beginners Area/Masking a simple color and collisions?!

Zacho(Posted 2011) [#1]
Hello everyone. I have been trying at program for it seems toooo toooo long. I used to code all wrong and so I am trying to start over. And so I am trying to make a pong remake.

I have two questions (if you can just tell me about them and not directly tell me the answer so I can figure it out on myself then this would be really helpful :)): the setmaskcolor function is only masking color from my ball image and I hear there is a special function that you have to use for collision detection if your image is transformed.

How does collision detection work? in my game? THanks in advance

 '
'PONG 2.1
'


'GAME CONSTANTS
'
Strict 'Must delcare variables before use
AppTitle = "PonG 2.0 - ZaChO" 'Title of the program
AutoMidHandle(True) 'Images are grabbed at their center
Graphics 800,600 'Graphics is 800 by 600
SeedRnd MilliSecs() 'Create random numbrs


'
'CONSTANTS
Const playerspeed:Int = 4 'The speed of the player
Const cpuspeed:Int = 3 'The speed of the computer
Const UPKEY:Int = 38 'The compiler's value of the up key
Const DOWNKEY:Int = 40 'The compiler's value of the down key
Const ESC:Int = 27 'The compiler's value of the esc key


'
'TYPES
Type paddle 'All of the variables needed for the player and computer
	Field y:Int 'The y value of the paddle
	Field x:Int 'The x value of the paddle
	Field score:Int 'The score
End Type

Type balltype 'All of the variables needed for the ball
	Field x:Int 'X value
	Field y:Int 'Y value
	Field xv:Int 'Xvelocity - value
	Field yv:Int 'Yvelocity - value
End Type 

'
'MASK IMAGEDS
SetMaskColor(255,255,255) 'Masks white

'
'IMAGES
Global playerimage:TImage=LoadImage("C:/Users/DellXPS/Documents/ZJWPics/player.png")
Global cpuimage:TImage=LoadImage("C:/Users/DellXPS/Documents/ZJWPics/cpu.png")
Global ballimage:TImage=LoadImage("C:/Users/DellXPS/Documents/ZJWPics/ball.png")



'
'CREATE TYPES
Global player:paddle = New paddle 'Creates a new player using the [paddle] type
Global cpu:paddle = New paddle 'Creates a new cpu using the [paddle] type
Global ball:balltype = New balltype 'Creates a new ball using the [balltype] type


'
'START THE GAME
DrawText "Ready...Set",325,300 'Starts the game by displaying 'Ready...Set" before the game actually starts
Flip 'Draws on the screen "Ready...Set"
Delay 1600 'Delay's the game 1.6 seconds

DrawText "Go!",350,340
Flip 'Draws on the screen "Go!" below "Ready...Set"
Delay 1000 'Delay the game 1 second


InitializeLevel() 'Start and load up the initial level


player.score = 0 'player score starts at 0 
cpu.score = 0 'computer score starts at 0


'
'LOOPS AND LOOPS [MAIN LOOP]
While Not KeyHit(ESC) 'While the ESC key isn't hit
Cls 'Clears the screen

Draw ()
TestKeyboard ()
TestAI ()
DrawScore ()

Flip 'Draw stuff on-screen
Wend 'End of while loop





'
'FUNCTIONS


Function InitializeLevel ()

'PLAYER
	player.y = 300
	player.x = 40
'CPU
	cpu.y = 300
	cpu.x = 708
'BALL
	ball.x = 300
	ball.y = 300
	ball.xv = Rand(1,3)
	ball.yv = Rand(1,4)

EndFunction


Function Draw ()

DrawImage(playerimage:TImage,player.x,player.y)
DrawImage(cpuimage:TImage,cpu.x,cpu.y)
DrawImage(ballimage:TImage,ball.x + ball.xv,ball.y + ball.yv)

End Function


Function DrawScore () 'Function to draw the score

DrawText "Player: " + player.score,700,0
DrawText "Cpu: " + cpu.score,700,25

	'DEBUG
		DrawText "Player Y: " + player.y,0,0
		DrawText "Cpu X: " + cpu.x,0,25
		DrawText "Ball X: " + ball.x,0,50
End Function


Function TestAI ()
	
'UPDATE BALL MOVEMENT	
	ball.x:+ ball.xv
	ball.y:+ ball.yv
	
		If ball.y <= 0 'If ball hits the top wall
			ball.yv = -ball.yv + Rand(-1,1)
			ball.xv = ball.xv + Rand(-1,1)
		EndIf
		If ball.y >= 584 'If ball hits the bottom wall
			ball.yv = -ball.yv + Rand(-1,1)
			ball.xv = ball.xv + Rand(-1,1)
		EndIf		
		If ImagesCollide(playerimage:TImage,player.x,player.y,0,ballimage:TImage,ball.x,ball.y,0)
			ball.xv = -ball.xv + Rand(-4,4)
			ball.yv = ball.yv + Rand(-2,2)
		EndIf 
		If ImagesCollide(cpuimage:TImage,player.x,player.y,0,ballimage:TImage,ball.x,ball.y,0)
			ball.xv = -ball.xv + Rand(-4,4)
			ball.yv = ball.yv + Rand(-2,2)
		EndIf
		If ball.x <= 15
			cpu.score = cpu.score + 1
			DrawText "Cpu scores!",300,400
			Flip
			Delay 2000
			InitializeLevel ()
		EndIf
		If ball.x > 785 'was 790
			player.score = player.score + 1
			DrawText "You score!",300,400
			Flip
			Delay 2000
			InitializeLevel ()
		EndIf 
		If ball.y > cpu.y
			cpu.y = cpu.y + cpuspeed
		EndIf
		If ball.y < cpu.y
			cpu.y = cpu.y - cpuspeed
		EndIf
		
End Function


Function TestKeyboard ()

'MOVING DOWNWARD
		If KeyDown(DOWNKEY) 'If the down key is pressed
			player.y = player.y + playerspeed 'Move the player down by 3 pixels
		EndIf
		
'MOVING UPWARD
		If KeyDown(UPKEY) 'If the up key is pressed
			player.y:- playerspeed 'Move the player up by 3 pixels
		EndIf 		
End Function 



YellBellzDotCom(Posted 2011) [#2]
I believe you need to use ImagesCollide2() for images that have been transformed or scaled. But I don't see anywhere in your code where you have scaled or rotated an image.

From the manual...
http://www.blitzbasic.com/bmdocs/command.php?name=ImagesCollide2&ref=2d_a-z

I've never used maskcolor though with a png image. If I wanted something to be transparent, I set that layer in the image transparent which more often than not would be the background. One of the beauties of using png images.

Again, I may be seeing something different as usual, hehehe.


Zacho(Posted 2011) [#3]
can you explain how to set the layer in the image transparent?


Zacho(Posted 2011) [#4]
Saved it as a *.bmp and it worked