TGC Codebase Backup



SPRITE MOUSE MOVE. by Lourg

1st Jun 2007 10:21
Summary

Program to move a sprite to the location of the mouse and to select other sprites and move them around the map. It uses collision detection to find which sprite is currently under



Description



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    `SPRITE MOUSE MOVE.
`Program to move a sprite to the location of the mouse and to select other sprites and move them around the map
`Created by: Jeff Beougher
`All I ask for is credit in your program if you use my code.




sync on
sync rate 30
set window on
set display mode 800,600,32
`Hides the mouse make sure to load and image for the mouse curor
hide mouse

`The sprite number that is being moved around the screen used with collision detection to tell which tile to move
global SelectedMapTile as integer
`Variable used to tell if a tile is currently being moved 
global ChangeTileLocation as integer
`The currect rotation of the tile
global SpriteRotated as integer
`Counter used to create a new tile
global ClonedTile = 4
`Used to tell the difference between a mouse click and a mouse hold.  The former is used the latter is bad.
global MouseHold = 0

`Image tile to be moved and rotated around the screen
load image "1000.bmp",1
`Image for your Mouse Cursor
load image "fly3.bmp",2
`Image for your New Tile Button
load image "NewTileButton.bmp",3
`Used if more then one memblock needs to be created not need for this program but future use.
memblock = 1
`Create a memblock the size of the image file
make memblock memblock, file size("1000.bmp")
`Make a memblock from the image
MAKE MEMBLOCK FROM image memblock, 1
`Delete the image since it is no longer needed
delete image 1
`used for the main program loop
gameover = 1
`Create an image from the memblock
MAKE image FROM MEMBLOCK 1, memblock

`Create sprite 1and place it in the center of the screen and use image 1 as its image
sprite 1, 400  , 300 ,1 
`Change the offset of the sprite by 100 for height and width
offset sprite 1, 100, 100
`Load the mouse cursor image and set it to sprite 2
sprite 2, 100, 100, 2
`Change the size of the tile to 200 width and 200 height
size sprite 1, 200,200
`Sprite used as a button to create a new tile
sprite 100, 0 + 75, 500 + 35, 3


`Set the mouse cursor to be drawn last
SET SPRITE PRIORITY 2,1
`Main program loop while gameover = 1
while gameover = 1
    `Sprite 1's X position
    center text 10, 10, str$(SPRITE X(1))
    `Sprite 1's Y position
    center text 10, 20, str$(SPRITE Y(1))
    `Current sprite number that the mouse sprite is touching
    center text 10, 30, str$(SelectedMapTile)
    `Currnet Tile that has been created with the new tile button
    center text 10, 40, str$(ClonedTile)
    `if the mouse has been released
    center text 10, 50, str$(MouseHold)
    `Current direction the tile is facing
    center text 10, 60, str$(SpriteRotated) 
    `Run the mouse control function
    MouseControl()
    
    `If a tile is currently being moved around the screen is not skip this function.
    if ChangeTileLocation = 1 then MoveTile()
       
    sync
    
endwhile
`Delete the memblock on exit
DELETE MEMBLOCK memblock
`end the program
end

Function MouseControl()
    `Set the mouse sprite the the location of the mouse X and Y position
    sprite 2, mouseX(), Mousey(), 2
    `if Mouse button 1 is held down skip the loop
    if MouseHold <> 1
        `if Mouse button 1 is pressed and a tile is being moved continue the if statement
        if MouseClick() = 1 and ChangeTileLocation <> 0 
            `Make sure the sprite under the mouse sprite is a tile
            if SelectedMapTile < 100 
                `Increase the value of Sprite rotated by 90 degrees
                SpriteRotated = SpriteRotated + 90
                `Slight pause between rotations
                sleep 150
                `if sprited rotated is greater then 360 set the value to 90    
                if SpriteRotated > 360 then SpriteRotated = 90
                `rotate the current map tile sprite 90 degrees
                Rotate sprite selectedMapTile, SpriteRotated
            endif
            `Change the value of MouseHold to one to prevent the mouse being held down and changes to effect the sprite
            MouseHold = 1    
        endif
    endif
    
    `if Mouse button 1 is pressed and a tile is being moved continue the if statement
    if MouseHold <> 1 
        `if a map tile sprite is being moved skip the rest of the if statements
         if ChangeTileLocation <> 1    
            `if the mouse button 1 is pressed contine the if statment
            if MOUSECLICK() = 1
                `Clear the SelectMapTile Variable to 0
                selectedMapTile = 0
                `Check which sprite is currently under the mouse sprite and change the value of selectMapTile to that sprite number
                SelectedMapTile = SPRITE COLLISION(2, 0)
                `if selectMapTile is less then 100 which is a map tile button sprite is set to 100.                 
                if SelectedMapTile < 100 
                    `0 is not a sprite number do not change the ChangeTileLocation Variable since this will cause an error 
                    `when the program run the MoveTile Function
                    if SelectedMapTile > 0 then ChangeTileLocation = 1
                else
                   `if SelectedMapTile is greater then 100 then the New Tile button Sprite was pressed run the DrawNewTile function
                    DrawNewTile()
                
                endif
                    `Change the value of MouseHold to one to prevent the mouse being held down and changes to effect the sprite
                    mousehold = 1
            endif
        endif
        
    endif
    
    
    
    `if the mouse button 1 has been released then change the value of mousehold to 0
    if MouseClick() = 0 then mousehold = 0
    
    
        `if mouse button 2 is pressed set the value of ChangeTileLocation to 0 the current Map Tile sprite will be place on the
        `Screen at the mouse X and Y position and will no longer have is X and Y value you changed by the mouse
         if MOUSECLICK() = 2
            ChangeTileLocation = 0   
         endif
   
EndFunction

function MoveTile()
     `Move the current selected Map Tile sprite to the Mouse X and Y postion    
     sprite SelectedMapTile, MouseX(), Mousey(), 1   
     
endfunction

function DrawNewTile()
    `increase the value of CloneTile by 1 a max of 98 tile can be create with this function before and error will happen
    inc ClonedTile 
    `Create a new sprite and set its sprite number to ClonedTile number and place it in the center of the screen us the image 1 as the sprites image.    
    sprite ClonedTile, 400, 300, 1
    `offeset the new sprites X and Y value to the center of the sprite
    offset sprite ClonedTile, 100, 100
    `Change the size of the sprite to to 200 width and 200 height
    size sprite ClonedTile, 200,200 
endFunction