Problem with code. Please help.

Blitz3D Forums/Blitz3D Beginners Area/Problem with code. Please help.

RXArt(Posted 2004) [#1]
I'm trying to make a pong game from scratch, to get used to the blitz language. However, I'm stuck at the moment, because my bat would get stuck when it reaches the edge of the screen (I'm stopping the bat from going off-screen). Please help. Here's my full code:

Global screenx	= 640
Global screeny	= 480
Global upkey 	= 200
Global leftkey 	= 203
Global rightkey = 205
Global downkey 	= 208

Graphics screenx,screeny,16,2

;Set type for bat info
Type entityinfo
	Field x
	Field y
	Field width
	Field height
	Field xspeed#
	Field yspeed#
End Type

;Create Mybat instance
Mybat.entityinfo 	= New entityinfo
Mybat\x 			= 320
Mybat\y 			= 240
Mybat\width			= 100
Mybat\height		= 25

;Create Ball instance
Ball.entityinfo 	= New entityinfo
Ball\x				= 320
Ball\y				= 240
Ball\width			= 25
Ball\height			= 25

;Create bat and ball image
gfxBat 	= CreateImage(Mybat\width,Mybat\height)
gfxBall	= CreateImage(25,25)

;Draw bat and ball image
SetBuffer ImageBuffer(gfxBat)
	Color 114,108,219
	Rect 0,0,100,25,1
SetBuffer ImageBuffer(gfxBall)
	Color 114,108,219
	Oval 0,0,25,25,1

;Center the images
MidHandle gfxBat
MidHandle gfxBall

;Switch back to back buffer
SetBuffer BackBuffer()

;Game loops starts here
;================================
While Not KeyHit(1)
;================================
	;Clear screen
	Cls
	
	;Just to know x,y values of my bat
	Text 0,0,mybat\x
	Text 0,10,mybat\y
	
	;Draw my bat onto screen
	DrawImage gfxbat,mybat\x,mybat\y
	
	;Make sure bat is on screen
	If (Mybat\x > (Mybat\width)/2) And (Mybat\x < screenx-((Mybat\width)/2)) Then
		If leftkey()=1 Then
			Mybat\x = Mybat\x - 2
		EndIf
		If rightkey()=1 Then
			Mybat\x = Mybat\x + 2
		EndIf
	EndIf
	Flip	
Wend


;Just some functions
Function LeftKey()
	If KeyDown(leftkey) = 1 Then 
		Return 1
	Else
		Return 0
	EndIf
End Function

Function RightKey()
	If KeyDown(rightkey) = 1 Then 
		Return 1
	Else
		Return 0
	EndIf
End Function

Function UpKey()
	If KeyDown(upkey) = 1 Then 
		Return 1
	Else
		Return 0
	EndIf
End Function

Function DownKey()
	If KeyDown(Downkey) = 1 Then 
		Return 1
	Else
		Return 0
	EndIf
End Function




Bot Builder(Posted 2004) [#2]
how bout this?
;controls
	If leftkey() Then mybat\x=mybat\x-2	ElseIf rightkey() Then mybat\x=mybat\x+2	
	;Make sure bat is on screen
	If (Mybat\x > screenx-(Mybat\width)/2) Then mybat\x=screenx-(Mybat\width)/2 ElseIf (Mybat\x < Mybat\width/2) Then mybat\x=Mybat\width/2



xmlspy(Posted 2004) [#3]
.


Bot Builder(Posted 2004) [#4]
yeah that's the less 'slick'/efficient way :) no seriously, using elseif when possible is faster than just a bunch of ifs.


xmlspy(Posted 2004) [#5]
.


Bot Builder(Posted 2004) [#6]
that works just aswell :) infact I believe that select cases are converted to if...elseif....endif statements internally.


xmlspy(Posted 2004) [#7]
.


RXArt(Posted 2004) [#8]
thanks guys!! it worked. Now, I'm having some trouble making the ball bounce of the pad realisticly. Any suggestions, beside the "invert x and y speed" thingy? Thanks!


xmlspy(Posted 2004) [#9]
.


RXArt(Posted 2004) [#10]
Well, elastic type collision of course! But after thinking carefully, I realised that my bat is just a rect, and no matter how the ball collides on its surface, the ball will only be reflected at the same angle as it collides. I'd consider the ball colliding at the edge of the rect, but I'm not sure if I'm good enough to start using vectors and calculating normals.

Anyway, I'm still working this simple pong game. I'm sure I'll be hitting quite a lot of obstacle, so I'd really appreciate it if everyone would chip in and help me complete this simple game of mine. Cheers!


Ross C(Posted 2004) [#11]
You could take the balls position (mid point) when it hits the bat. Compare the two (make sure there the midpoints). Then divide the two( call X). Then multiple the angle of the ball by X. The ball should change direction, depending on where it's hit.

Only thing is, you'll need to use an angle variable :)

I'll post something when i get home :)