Skip to content

Mouse Controls

noooway edited this page Mar 24, 2017 · 26 revisions

In this part I want to implement mouse controls for the game. The platform should follow the mouse cursor, left click should launch the ball, and right click - pause the game.

In general, to make the platform follow the mouse it is necessary to read the cursor position and then move the platform toward it. The first problem of the mouse controls is that it conflicts with the keyboard: it is going to be confusing if the platform follows the cursor and reacts on arrow keys simultaneously. It could be possible to retain keyboard controls when the cursor is outside the game window. Unfortunately, LÖVE doesn't provide any easy ways 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".

    Home
    Acknowledgements
    Todo

Chapter 1: Prototype

  1. The Ball, The Brick, The Platform
  2. Game Objects as Lua Tables
  3. Bricks and Walls
  4. Detecting Collisions
  5. Resolving Collisions
  6. Levels

    Appendix A: Storing Levels as Strings
    Appendix B: Optimized Collision Detection (draft)

Chapter 2: General Code Structure

  1. Splitting Code into Several Files
  2. Loading Levels from Files
  3. Straightforward Gamestates
  4. Advanced Gamestates
  5. Basic Tiles
  6. Different Brick Types
  7. Basic Sound
  8. 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

  1. Improved Ball Rebounds
  2. Ball Launch From Platform (Two Objects Moving Together)
  3. Mouse Controls
  4. Spawning Bonuses
  5. Bonus Effects
  6. Glue Bonus
  7. Add New Ball Bonus
  8. Life and Next Level Bonuses
  9. Random Bonuses
  10. Menu Buttons
  11. Wall Tiles
  12. Side Panel
  13. Score
  14. Fonts
  15. More Sounds
  16. Final Screen
  17. Packaging

    Appendix D: GUI Layouts
    Appendix E: Love-release and Love.js

Beyond Programming:

  1. Game Design
  2. Minimal Marketing (draft)
  3. Finding a Team (draft)

Archive

Clone this wiki locally