A full game gets complicated and thick with scripts very fast so hopefully this gives beginners the pieces of the puzzle to work with. "Action Games" implies moving objects, as opposed to say a turn based strategy game, or RPG.

I am going to stick to 'graphics' objects as they keep us out of the file system and/or image data and they give good retro vibes.

Code posted here is just for the sake of following along if you are a beginner and need to know how it all comes together, the attached stack provides the code if you need to get into it.

**Step 1. Define the triangle "Ship"**

We might reuse this handler for many ships later. Probably with a different starting location

Note we delete any previous graphic of the same name as clones can result in unexpected behavior.

Code: Select all

```
on defineGraphicTriangle tName
if there is a graphic tName then delete graphic tName
set the style of the templateGraphic to "polygon"
set the points of the templategraphic to "0,0" &cr& "-120,60" &cr& "-120,-60" &cr& "0,0"
set the lineSize of the templateGraphic to 1
set the visible of the templateGraphic to true
set the opaque of the templateGraphic to false
set the name of the templateGraphic to tName
create graphic tName
set the location of graphic tName to width of this stack/2 , height of this stack /2
end defineGraphicTriangle
```

**Step 2. Make sure the graphic is created when we need it.**

For simplicity we do it here in the scrollbar, in a game you'll have some 'init' scripts to manage this.

Code: Select all

```
on mouseEnter
--- ensure that there is a graphic to manipulate. Assuming you copied this script or scrollbar to a new stack
if there is not a graphic "ship" then defineGraphicTriangle "ship"
end mouseEnter
```

**Step 3. Define the rotate triangle polygon function**

The '

*regular*' style polygon graphics used by the IDE can be rotated with the built-in '

**angle**' command, but '

*polygon*' style polygon graphics need the calculations like you see here in

**rotateTrianglePolygon**as they may not be of purely geometrically generated points. I hope I stated that clear enough.

Code: Select all

```
function rotateTrianglePolygon pPoints, pAngle
--- pPoints: A list of points (x1,y1,x2,y2,x3,y3) representing the triangle vertices.
--- pAngle: The angle in degrees by which to rotate the triangle.
put the number of items in pPoints into numPoints
if numPoints <> 6 then
answer "Please provide exactly three pairs of coordinates (x1,y1,x2,y2,x3,y3)."
exit to top
end if
--- Convert angle to radians
put pAngle into radians
divide radians by 180
multiply radians by pi
--- Extract individual coordinates
put item 1 of pPoints into x1
put item 2 of pPoints into y1
put item 3 of pPoints into x2
put item 4 of pPoints into y2
put item 5 of pPoints into x3
put item 6 of pPoints into y3
--- Rotate each point
put (x1 * cos(radians)) - (y1 * sin(radians)) into newX1
put (x1 * sin(radians)) + (y1 * cos(radians)) into newY1
put (x2 * cos(radians)) - (y2 * sin(radians)) into newX2
put (x2 * sin(radians)) + (y2 * cos(radians)) into newY2
put (x3 * cos(radians)) - (y3 * sin(radians)) into newX3
put (x3 * sin(radians)) + (y3 * cos(radians)) into newY3
-- Return the rotated coordinates
return newX1 & "," & newY1 & "," & newX2 & "," & newY2 & "," & newX3 & "," & newY3
end rotateTrianglePolygon
```

**Step 4. Rotate the angle of the graphic to the value of the thumb position of the scrollbar**

Code: Select all

```
on scrollBarDrag
put "0,0" & comma & "-120,60" & comma & "-120,-60" into tPoints
--- rotate the points of the graphic
put rotateTrianglePolygon(tPoints,the thumbposition of me) into tNewPoints
--- add first line of the points after last line to close the graphic
put cr & line 1 of tNewPoints after tNewPoints
--- store the original location of the ship
put the loc of graphic "ship" into tLoc
--- apply the new rotated points
set the points of graphic "ship" to tNewPoints
--- reset the ship to the original location
set the loc of graphic "ship" to tLoc
end scrollBarDrag
```