How To Respond To Mouse Click And Position

Many game and program objectives require a user to respond via interaction with the game window. SwinGame accepts mouse inputs where the values can be used for many purposes.

Level

Expert

Details

SwinGame can respond to many mouse events, hide mouse, move mouse, and detect which button on the mouse is click along with many others which can be seen on the Input API Page.

To do all of these you need to:

  1. OPen a SwinGame window
  2. Call ProcessEvents to register user interaction
  3. Call ClearScreen
  4. Call HideMouse procedure when user presses the H key
  5. Call ShowMouse procedure when user presses S Key
  6. Draw text to screen when MouseShown is true
  7. Draw different text to screen when MouseShown is false.
  8. Call MouseX and MouseY to show the position of the mouse when user clicks the left mouse button
  9. Display "is down" in the game window if MouseDown is true
  10. Display "is up" in the game window if MouseUp is true
  11. Do the same for right button this time using the RightButton field with MouseDown and MouseUp
  12. Call MoveMouse to move the mouse position to a new coordinate
  13. Refresh Screen
  14. Released All Resources
  15. Run the program

On the right is a short video clip showing the program created from the code below.

Source Code Widget

  • program HowToRespondToMouseClickAndPosition;
    uses
      SwinGame, sgTypes, sysutils;  
      
    procedure Main();
    var
      str : String;  
      mPos : Point2D;  
    begin  
      OpenGraphicsWindow('Mouse Click And Position', 400, 220);
      LoadDefaultColors();  	
      
      repeat // The game loop...
        ProcessEvents();
        
        ClearScreen(ColorWhite);
        
        if KeyTyped(vk_h) then HideMouse();
        if KeyTyped(vk_s) then ShowMouse();
        if MouseShown () then DrawText('visible', ColorBlue, 'Arial', 14, 120, 90);
        if NOT MouseShown () then DrawText('not visible', ColorBlue, 'Arial', 14, 120, 90);
        if MouseClicked(LeftButton) then str := FloatToStr(MouseX()) + ',' + FloatToStr(MouseY());
        if MouseDown(LeftButton) then DrawText('is down', ColorBlue, 'Arial', 14, 200, 140);
        if MouseUp(LeftButton) then DrawText('is up', ColorBlue, 'Arial', 14, 200, 140);
        if MouseDown(RightButton) then DrawText('is down', ColorBlue, 'Arial', 14, 200, 165);
        if MouseUp(RightButton) then DrawText('is up', ColorBlue, 'Arial', 14, 200, 165);
        if KeyDown(vk_DOWN) then
        begin
          mPos := MousePosition();      
          mPos.y += 1;
          MoveMouse(mPos);    
        end;    
        if KeyDown(vk_UP) then 
        begin
          mPos := MousePosition();
          if mPos.y > 0 then
          begin
            mPos.y -= 1;
            MoveMouse(mPos);
          end;  
        end;
        if KeyDown(vk_LEFT) then 
        begin
          mPos := MousePosition();
          if mPos.x > 0 then
          begin
            mPos.x -= 1;
            MoveMouse(mPos);
          end;  
        end;
        if KeyDown(vk_RIGHT) then 
        begin
          mPos := MousePosition();
          mPos.x += 1;
          MoveMouse(mPos);    
        end;
        
        DrawText('Mouse Click And Position', ColorRed, 'Arial', 18, 115, 15);
        DrawText('Press H to hide the cursor', ColorBlue, 'Arial', 14, 20, 40);
        DrawText('Press S to show the cursor', ColorBlue, 'Arial', 14, 20, 65);
        DrawText('Cursor status :', ColorBlue, 'Arial', 14, 20, 90);
        DrawText('Mouse Position at: ' + FloatToStr(MousePosition.x) + ',' + FloatToStr(MouseY()), ColorGreen, 'Arial', 10, 0, 0);
        DrawText('Mouse last click at position : ' + str, ColorBlue, 'Arial', 14, 20, 115);
        DrawText('Mouse LeftButton status :', ColorBlue, 'Arial', 14, 20, 140);
        DrawText('Mouse RightButton status :', ColorBlue, 'Arial', 14, 20, 165);    
        DrawText('Use Up, Down, Left, Right to move the mouse cursor', ColorBlue, 'Arial', 14, 20, 190);
        
        RefreshScreen(60);    
      until WindowCloseRequested() OR KeyTyped(vk_ESCAPE) OR KeyTyped(VK_Q);  
      
      ReleaseAllResources();
    end;
    
    begin
      Main();
    end.
  • Source Code Coming Soon

  • Source Code Coming Soon

  • Source Code Coming Soon

  • Source Code Coming Soon


Possibilities

  • Build a simple game that moves an image or sprite across the screen from one side of the game window to the other using keyboard input.