-
Notifications
You must be signed in to change notification settings - Fork 20
Mouse Controls
In this part I want to implement mouse controls for the game.
We need to make the platform follow the mouse coursor and launch the ball on mouse keypress.
To make platform follow the mouse we can read the coursor position and then move the platform in it's direction. The first problem is that mouse controls confilct with keyboard. We can't move platform to the coursor and make it react on arrow keys simultaneously. It could be possible to retain keyboard controls if the coursor is outside the game window. Unfortunately, love doesn't provide an easy way to check it. Keyboard controls have to be dropped:
function platform.update( dt )
platform.follow_mouse( dt )
end
function platform.follow_mouse( dt )
local x, y = love.mouse.getPosition()
local left_wall_plus_half_platform = 34 + platform.width / 2
local right_wall_minus_half_platform = 576 - platform.width / 2
if ( x > left_wall_plus_half_platform and
x < right_wall_minus_half_platform ) then
platform.position.x = x - platform.width / 2
elseif x < left_wall_plus_half_platform then
platform.position.x =
left_wall_plus_half_platform - platform.width / 2
elseif x > right_wall_minus_half_platform then
platform.position.x =
right_wall_minus_half_platform - platform.width / 2
end
end
Second: the platform moves to the coursor immediately. It allows cheating: pause the game, position the coursor, continue. But I just ignore it.
Let's also add appropriate callback to launch the ball on mouse press.
function game.mousereleased( x, y, button, istouch )
if button == 'l' or button == 1 then
ball.launch_from_platform()
elseif button == 'r' or button == 2 then
music:pause()
gamestates.set_state(
"gamepaused",
{ ball, platform, bricks, walls, lives_display } )
end
end
function love.mousereleased( x, y, button, istouch )
gamestates.state_event( "mousereleased", x, y, button, istouch )
end
Transition between the gamestates is also controlled from the mouse:
function gamepaused.mousereleased( x, y, button, istouch )
if button == 'l' or button == 1 then
gamestates.set_state( "game" )
elseif button == 'r' or button == 2 then
love.event.quit()
end
end
Same for the "menu" and "gamefinished".
Feedback is crucial to improve the tutorial!
Let me know if you have any questions, critique, suggestions or just any other ideas.
Chapter 1: Prototype
- The Ball, The Brick, The Platform
- Game Objects as Lua Tables
- Bricks and Walls
- Detecting Collisions
- Resolving Collisions
- Levels
Appendix A: Storing Levels as Strings
Appendix B: Optimized Collision Detection (draft)
Chapter 2: General Code Structure
- Splitting Code into Several Files
- Loading Levels from Files
- Straightforward Gamestates
- Advanced Gamestates
- Basic Tiles
- Different Brick Types
- Basic Sound
- Game Over
Appendix C: Stricter Modules (draft)
Appendix D-1: Intro to Classes (draft)
Appendix D-2: Chapter 2 Using Classes.
Chapter 3 (deprecated): Details
- Improved Ball Rebounds
- Ball Launch From Platform (Two Objects Moving Together)
- Mouse Controls
- Spawning Bonuses
- Bonus Effects
- Glue Bonus
- Add New Ball Bonus
- Life and Next Level Bonuses
- Random Bonuses
- Menu Buttons
- Wall Tiles
- Side Panel
- Score
- Fonts
- More Sounds
- Final Screen
- Packaging
Appendix D: GUI Layouts
Appendix E: Love-release and Love.js
Beyond Programming: