In need of example code of this matter

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Mr Mie
Prole
Posts: 4
Joined: Wed Dec 18, 2019 7:43 am

In need of example code of this matter

Post by Mr Mie »

Hello. This is my 1st week of playing with love2d. i recently learning making Pong game from cs50 course. i code my game 100% on my old samsung note 3 phone with help of love2d app from playstore and code editor from market.

since pong tutorial teach about keyboard press, so it's impossible to replicate it on android coz android using touch function right.. so i i google every source code related to love2d 2019 and finally figured that if i want things to happen in game, i need to invisibily create a touch region on screen, hard coded it that if i press that region, things happen ( for example, the paddle pong moves if this region x,y touched).

but i got this question in my mind, how to make things touchable? for example if i draw a "start button" on screen. i want the the game start after pressed that button. how to do it? i meant code for enable the startbutton.png is touched..

please guide me with example code. i really want to learn this
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

Re: In need of example code of this matter

Post by Ref »

Maybe something like this?

Code: Select all

PointInBox = function( point, box )
		local x, y, w, h	= unpack( box )
		local px, py = point[1], point[2]
		if px>=x and px<=x+w and py>=y and py<=y+h then 
			return true 
			end
		return false
		end
touch = function( box )
		local mx, my = love.mouse.getPosition( )
		if love.mouse.isDown( 1 ) and PointInBox({mx,my},box) then
			return true
			end
		return false
		end
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: In need of example code of this matter

Post by raidho36 »

Well since you're making a mobile game you might as well use mobile controls - touchscreen and gyroscope. It should be pretty easy to control paddle position directly using phone tilt or finger position on the screen.

To answer your question, you simply check if touch coordinates are within certain range, for example X > 100 and X < 200 and Y > 100 and Y < 200. If that resolves to true, then you activate the action that's meant to happen when you press that region of the screen. Draw a sprite in the same location and you got a button.

A side note - pong (and breakout) are meant to be played on an analog slider controller, on a PC you'd use a mouse and old consoles had special pong controllers.
Mr Mie
Prole
Posts: 4
Joined: Wed Dec 18, 2019 7:43 am

Re: In need of example code of this matter

Post by Mr Mie »

Ref wrote: Fri Dec 20, 2019 7:43 pm Maybe something like this?

Code: Select all

PointInBox = function( point, box )
		local x, y, w, h	= unpack( box )
		local px, py = point[1], point[2]
		if px>=x and px<=x+w and py>=y and py<=y+h then 
			return true 
			end
		return false
		end
touch = function( box )
		local mx, my = love.mouse.getPosition( )
		if love.mouse.isDown( 1 ) and PointInBox({mx,my},box) then
			return true
			end
		return false
		end
hello, sorry for really late replay as i only able to access office PC during weekdays. thank you so much for this code. not only it's a much better version than mine ( which is uncessesary long and complex), your code also made me realize i thinking too much just for a simple problem lol.. my version of code is bad i'd shame to share here xD

also i learn new knowledge about unpack which i can see myself using this a lot in future.

thanks for effort and time helping beginner like me
Mr Mie
Prole
Posts: 4
Joined: Wed Dec 18, 2019 7:43 am

Re: In need of example code of this matter

Post by Mr Mie »

raidho36 wrote: Fri Dec 20, 2019 9:27 pm Well since you're making a mobile game you might as well use mobile controls - touchscreen and gyroscope. It should be pretty easy to control paddle position directly using phone tilt or finger position on the screen.

To answer your question, you simply check if touch coordinates are within certain range, for example X > 100 and X < 200 and Y > 100 and Y < 200. If that resolves to true, then you activate the action that's meant to happen when you press that region of the screen. Draw a sprite in the same location and you got a button.

A side note - pong (and breakout) are meant to be played on an analog slider controller, on a PC you'd use a mouse and old consoles had special pong controllers.
yeah after studying the code shared by RF, i learn how to do it as short as possible without assigning to unnecessary global variable ( my version) . i planned to make paddle move by dragging the paddle with finger. will search more info regarding it . thanks for reply, the touch concept explained. i appreciate it!
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 9 guests