TGC Codebase Backup



360 Sprite Motion by Webber

10th Aug 2011 19:07
Summary

A sprite that rotates, and follows the mouse.



Description

This gets the positions of the mouse, and position of a sprite to calculates the angle, speed, and new position, using trig and stuff.

Basically the sprite points towards the mouse, and follows the mouse.

Could be a useful program if you need a 2D object to accurately follow a point on the screen.

Credit to The Game Guy would be appreciated, but is not required.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    `Project: 360 Sprite Motion
`Author: The Game Guy

sync on : sync rate 60

Rem loads the image of the space ship
load image "SpaceShip.bmp",1

PX# = 320 : PY# = 240 `positions of the space ship
Speed# = 10 `the speed of the space ship

Rem Creates the space ship
sprite 1,PX#,PY#,1
set sprite 1,0,1
offset sprite 1,30,26




Rem Main Loop
do
cls `clears the screen

MX = MouseX() : MY = MouseY() `mouse positions

PMX# = MX-PX# : PMY# = MY-PY# `mouse/player positions

Rem Get the players angle / angle of the space ship
PAngle# = Atan(PMX#/-PMY#)

Rem adjusts the angle of the space ship
if PMX# > 0 and PMY# > 0 then dec PAngle#,180
if PMX# < 0 and PMY# > 0 then inc PAngle#,180

Rem fixes more of the space ships angles
if PMX# > 0 and PMY# = 0 then PAngle# = 90
if PMX# = 0 and PMY# > 0 then PAngle# = 180
if PMX# < 0 and PMY# = 0 then PAngle# = 270

Rem Makes the space ship follow the mouse
if PMX# > Speed# or PMX# < -Speed# or PMY# > Speed# or PMY# < -Speed#
`if mouseclick() = 1
inc PX#,Sin(PAngle#)*Speed#
inc PY#,-Cos(PAngle#)*Speed#
`endif
endif

Rem Positions and rotates the space ship sprite
sprite 1,PX#,PY#,1 : rotate sprite 1,PAngle#




Rem makes the angles easier to read for testing
if PAngle# < 0 then inc PAngle#,360
if PAngle# > 360 then dec PAngle#,360

Rem Display text for testing the program
set cursor 0,0
print "Press any key to quit"
print "Player/Mouse X: ";PMX#
print "Player/Mouse Y: ";PMY#
print "X_Speed: ";Sin(PAngle#)*Speed#
print "Y_Speed: ";-Cos(PAngle#)*Speed#
print "Angle: ";PAngle#
print "FPS: ";screen fps()

if inkey$() <> "" then exit

sync
loop

end