AppGameKit Classic

AppGameKit Classic

Not enough ratings
Creating a simple lasso to select sprites on screen.
By thescenecommander
In this guide, we look at creating some randomly moving sprites and creating a simple box lasso to select them. This is similar to the control method often seen in RTS (real time strategy) games and was inspired by a user request for some guides to cover the creation of those style of applications.
   
Award
Favorite
Favorited
Unfavorite
Creating a simple lasso to select sprites on screen.
Hi,

Welcome to another AppGameKit guide. For this one, we'll be looking at a code example inspired by a user request for some pointers into real time strategy game creation. The code also covers some of the other techniques we've used previously and expands or re-enforces on them.

As before, I've include a video of what you should see and documented the code clearly. You will need the image item0.png that we've used in previous guides, and this should be copied into the projects media folder. As a reminder this can be found in your AppGameKit projects folder, under the examples Sprite/SpriteAnim1.

All you need to do to use this code is to cut and paste it into your AppGameKit IDE. I encourage you to play around with the various settings and values.

To use, click anywhere on your screen, and hold down 'left' mouse or equivalent on your chosen device to drag the lasso. Any sprite covered (selected) by the box will fade until outside of the lasso.

If all has gone well, this is what you should see. At present this demonstration only allows selection from top left to bottom right and bottom right to top left.

I plan to expand on this later, to demonstrate some other, simple but useful RTS elements.



// Project: RTS lasso demo
// Created: 2014-12-05

// set window properties
SetWindowTitle( "RTS lasso demo" )
SetWindowSize( 1024, 768, 0 )

// set display properties
SetVirtualResolution( 1024, 768 )

// set some variable for the corners of the lasso box
global topleftx=0
global toplefty=0
global bottomrighttx=0
global bottomrighty=0

// create a type for the various elements of 'The units'
type sampleunittype
x as float
y as float
angle as integer
sprite as float
lasttime as float
endtype

// set up an array for the units.

dim units[10] as sampleunittype

//load in a sample image so we can see the sprite. This is the default balloon from SpriteAnim1 example that we've used in other demo's.

loadimage(1,"item0.png")

//Make our sprites

for makesprites=1 to 10
//create a sprite with image 1
createsprite(makesprites,1)
//randomly place them on screen
units[makesprites].x=random(30,990)
units[makesprites].y=random(30,710)
// we will be moving them around the screen at random angles to simulate movement
units[makesprites].angle=random(1,360)
//assign a sprite number
units[makesprites].sprite=makesprites
next makesprites

//

// main loop


do


// draw a box, left click and drag the box
if GetpointerState()=0
//if the pointer virtual left mouse click isn't down, move the top left corner to the mouse
topleftx=GetpointerX()
toplefty=GetPointery()
else
//if the pointer virtual left mouse click is down, drag the box.
bottomrightx=GetpointerX()
bottomrighty=GetPointery()
endif


//Make the colour of the box, in this case white
colour1=makecolor(255,255,255)
colour2=makecolor(255,255,255)
colour3=makecolor(255,255,255)
colour4=makecolor(255,255,255)

filled=0

//draw the box,

//topleftx - The X component of the top left corner of the box.
//toplefty - The Y component of the top left corner of the box.
//bottomrightx - The X component of the bottom right corner of the box.
//bottomrighty - The Y component of the bottom right corner of the box.
//color1 - The color to use in the top left corner.
//color2 - The color to use in the top right corner.
//color3 - The color to use in the bottom left corner.
//color4 - The color to use in the botom right corner.
//filled - 1 to draw a filled box, 0 to draw an empty box.

//I encourage you to play with all of the settings to see the different effects.


DrawBox(topleftx,toplefty,bottomrightx,bottomrighty,colour1,colour2,colour3,colour4,filled)

// display and move sprites. Use loop to check if sprite is in the box and colour if covered

for displaysprites=1 to 10

//Is the sprite in within the lasso box? For the purpose of this demo only dragging down to the right and up to the left are checked.
If GetSpriteinBox(units[displaysprites].sprite,topleftx,toplefty,bottomrightx,bottomrighty) or GetSpriteinBox(units[displaysprites].sprite,bottomrightx,bottomrighty,topleftx,toplefty)
SetSpriteColorAlpha(units[displaysprites].sprite,80)
else
SetSpriteColorAlpha(units[displaysprites].sprite,255)
endif
// move sprite by angle
units[displaysprites].x=units[displaysprites].x+sin(units[displaysprites].angle)
units[displaysprites].y=units[displaysprites].y+cos(units[displaysprites].angle)
// change direction every 3 seconds. This sample doesn't check to prevent sprites leaving the screen.
if timer()-units[displaysprites].lasttime>3
//reset the timer
units[displaysprites].lasttime=timer()
// set a new random angle
units[displaysprites].angle=random(1,360)
endif
// Set the sprites new position
SetSpritePosition(units[displaysprites].sprite,units[displaysprites].x,units[displaysprites].y)
next displaysprites

//update screen
sync()


loop

7 Comments
JESSIKA 07 2 Jul, 2020 @ 7:47am 
All the videos are being taken down!!! why? Those are so helpful!!!
ItDidn'tHappen88 4 Jun, 2019 @ 5:29pm 
I never got it to show anything. It's just a black screen. I tried putting it in my main.agk and using another file.agk with the #include " " format too. what gives?
HaggisBasher21 12 Aug, 2015 @ 3:26am 
will give it a try, thanks!
thescenecommander  [author] 7 Aug, 2015 @ 4:29am 
Set a variable, say, drawlasso=0 and only start drawing the lasso with that value is positive. (e.g. if drawlasso=1)
HaggisBasher21 4 Aug, 2015 @ 2:16pm 
Loving the guides, really helping me to get my head around it! Is there a way to make the lasso happen on a click rather than always be attached to the mouse? I can't seem to get it to work
thescenecommander  [author] 6 Jan, 2015 @ 12:27am 
I'm glad that you like it. Keep an eye out soon for some more tutorials now that Christmas is over.
JLansing 5 Jan, 2015 @ 5:22pm 
This is crazy! I understand it for the most part when I read it, but to be able to just sit down and write something like that is insane. Thank you for writing this... it's not making sense completely yet, but I'm sure if I keep reading these things I'll start to understand the logic.