Mouse going off window

Blitz3D Forums/Blitz3D Beginners Area/Mouse going off window

Yahfree(Posted 2007) [#1]
Hey, i'm trying to make this little paddle mouse controled, and my problem is, the mouse goes off the window to easy, is there any way how i can lock the mouse in the center of the window and still use it to move this paddle around?

heres the code it should work with no media:

;Breakout by Yahfree
;2007-04-4 -> ????-??-(?)?
AppTitle "Breakout by Yahfree","Are you sure?"

HidePointer

;sets the graphics mode, and the setbuffer
Graphics 800,600,32,2
SetBuffer BackBuffer()

;Globalized stuff
Global paddle, paddlex=380, paddley=560
Global ball,ballx=380, bally=580, ballxdir#=0, ballydir=-1, move=1
;Global blope=LoadSound("Beep.wav")

;Constant numbers that never change..
Const ESC=1, RIGHTARR=205, LEFTARR=203, SPACE=57, MOUSE1=1


;Mid handles all of the images and creates a image called paddle.
AutoMidHandle True
   paddle=CreateImage(70,15)
   ball=CreateImage(10,10)

;draws on the empty image container..
SetBuffer ImageBuffer(paddle)
   Rect 0,0,70,15,1
SetBuffer ImageBuffer(ball)
   Oval 0,0,10,10,1
SetBuffer BackBuffer()

;!@!@!@!@!@!@!@!@!@!@!@!@!@!@
;!@!@!@!@ MAIN LOOP !@!@!@!!@
;!@!@!@!@!@!!@!@!@!@!@!@!@!@!
While Not KeyHit(1)
Cls


addplayercontrols()

DrawImage paddle,paddlex,paddley
DrawImage ball,ballx,bally

addcollisions()


;small delay in the loop for less slow down.
;flips, ends the loop and termanates the program..
Delay 5
  Flip
Wend
End

;Include "Functions.bb"

;Player controls
Function addplayercontrols()

;Balls spot if move=1
If move=1 ballx=MouseX()-35
If move=1 And ballx<35 ballx=35
If move=1 bally=paddley-15
If move=1 ballydir=-1 ballxdir=0

;Move=2 if the spacebar is pressed
If KeyHit(SPACE) Or MouseHit(MOUSE1)
   move=2
End If

;Paddle controls
paddlex=MouseX()-35
If paddlex<35 paddlex=35

;calls the moveball function if move=2
If move=2
   moveball()
End If

End Function

;Moving the ball
Function moveball()

bally=bally+ballydir*6
ballx=ballx+ballxdir*6

If bally<0 ballydir=1 ;PlaySound(blope)
If bally>600 move=1 ballydir=-1
If ballx<0 ballxdir=1 ;PlaySound(blope)
If ballx>800 ballxdir=-1 ;PlaySound(blope)

End Function

;All the games collisions..
Function addcollisions()

If ImagesCollide(paddle,paddlex,paddley,0,ball,ballx,bally,0)
ballxdir=(ballx - paddlex)*.025
ballydir=-1
;PlaySound(blope)
End If
End Function


as you can see the mouse can easly run off the screen leavin the player useless untill he gets it back on there, how can i lock it into a place?


big10p(Posted 2007) [#2]
MoveMouse to constantly position it in the screen centre?


Yahfree(Posted 2007) [#3]
but then i cant move the paddle, the paddle is based on the x of the mouse (mouse controled)

edit, right idea though, i want the mouse to be locked in like that but i want to still be able to control the paddle with the mouse


Sledge(Posted 2007) [#4]
Yeah, you place it at the centre then see how far it has moved each frame -- you're after a relative value. I'll post an example if nobody else beats me to it.


b32(Posted 2007) [#5]
Yes, first use x = x + MouseXSpeed() to move the paddle, then use MoveMouse 400, 300 to center the cursor.


Sledge(Posted 2007) [#6]
Graphics 800,600,0,2
SetBuffer BackBuffer()
HidePointer
MoveMouse GraphicsWidth()/2,GraphicsHeight()/2
playerX=GraphicsWidth()/2

While Not KeyHit(1)
	playerX=playerX+MouseXSpeed()
	If playerX>(GraphicsWidth()-50) Then playerX=GraphicsWidth()-50
	If playerX<10 Then playerX=10
	MoveMouse GraphicsWidth()/2,GraphicsHeight()/2
	Text playerX,500,"X---X"
	Flip : Cls
Wend



Yahfree(Posted 2007) [#7]
this doesnt work:
;Player controls
Function addplayercontrols()
MoveMouse 380,280

;Balls spot if move=1
If move=1 ballx=ballx+MouseXSpeed()
If move=1 And ballx<35 ballx=35
If move=1 bally=paddley-15
If move=1 ballydir=-1 ballxdir=0

;Move=2 if the spacebar is pressed
If KeyHit(SPACE) Or MouseHit(MOUSE1)
   move=2
End If

;Paddle controls
paddlex=paddlex+MouseXSpeed()
If paddlex<35 paddlex=35

;calls the moveball function if move=2
If move=2
   moveball()
End If

End Function


notice: paddlex=paddlex+MouseXSpeed()
and: If move=1 ballx=ballx+MouseXSpeed()
and: MoveMouse 380,280


Sledge(Posted 2007) [#8]
Try:



Yahfree(Posted 2007) [#9]
Ok, i see what i did wrong, thanks, one more thing, how do i get it so it doesnt keep going right, it doesnt stop..

edit, also the ball looks delayed on its position, it laggs behind when moving with the paddle


Yahfree(Posted 2007) [#10]
ok i fixed the first problem, still cant figure out why its delaying...


Sledge(Posted 2007) [#11]
It's delaying because you are updating the ball's position before updating the paddle's position, making it lag a frame behind.




Yahfree(Posted 2007) [#12]
Thanks, it works perfectly now... thanks for the quick responce..

edit: last question, how do you make the code box on the forums scroll?


Mattizzle(Posted 2007) [#13]
use: codebox /codebox instead of code /code


Yahfree(Posted 2007) [#14]
Hmmmm... everything works great but there is a small problem more like a inconvinice, even if the program is minimized or your not playing it, it likes to lock the mouse to the center of the screen.. any way to make it so it only locks the mouse down if the program is in use?


Yahfree(Posted 2007) [#15]
Ok, i found a quick fix for that problem, now i'm stumped, why is the mouse image lagging behind the mouse? on the "Imagescollide" example it follows the image perfectly.. this is my code for a main menu:

Function Mainmenu()
Graphics 800,600,32,2

selection=1

mainimage=LoadImage("mainmenu.png")
continue=LoadImage("Playbutton.png")
mouse=LoadImage("mouse.png")
MaskImage mouse,255,0,255



While selection=1
mouse_x=MouseX()
mouse_y=MouseY()
Cls


DrawImage mainimage,0,0
DrawImage continue,100,400
DrawImage mouse,mouse_x,mouse_y

If ImageRectCollide(continue,100,400,0,MouseX(),MouseY(),5,5) And MouseHit(1)
selection=2
End If

If KeyHit(1)
End
End If

Delay 5
Wend


EDIT: and yes i did try to take out the delay it doesnt change it