Qbasicnews.com
February 25, 2021, 09:32:57 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Simple directional movement demo  (Read 1923 times)
steven_basic
Senior Member
**
Posts: 244


WWW
« on: February 18, 2005, 12:37:47 PM »

Finally got directional movement nailed down.
Here is a simple SCREEN with DRAW image demo.

Code:

'  directional movement test
'  using simple screen/draw graphics for demo

option explicit

'$include: "win/gdi32.bi"

#define SC_ESCAPE &h01
#define SC_SPACE  &h39
#define SC_UP     &h48
#define SC_LEFT   &h4B
#define SC_RIGHT  &h4D
#define SC_DOWN   &h50

#define PI  3.141592

sub sprite.moveforward(currentX as long, currentY as long, currentAngle as long, currentSpeed as long)
  dim radians as double
  '  calculate radians from current angle (direction object is facing)
  radians = (currentAngle - 90) * (pi / 180)
  '  calcualte new x/y positions dependant upon the current speed of movement
  currentX = currentX + currentSpeed * COS(radians)
  currentY = currentY + currentSpeed * SIN(radians)
end sub

type sprite
 xpos as long
 ypos as long
 length as long
 facing as long
 speed as long
end type

dim s1 as sprite
s1.xpos = 200
s1.ypos = 200
s1.length = 20
s1.facing = 0
s1.speed = 0

dim moveX as double
dim moveY as double
dim done as long

screen 12
done = FALSE
do
  '  DRAW sprite with color 0 to "erase"
  DRAW "S4"
  DRAW "BM" + str$(s1.xpos) + "," + str$(s1.ypos)
  DRAW "C0"
  DRAW "TA" + str$(-s1.facing) + "BD5L10E10F10L10BU5"
  '  process user commands to move sprite
  if multikey(SC_LEFT) then
    s1.facing = s1.facing - 1
  end if
  if multikey(SC_RIGHT) then
    s1.facing = s1.facing + 1
  end if
  if multikey(SC_UP) then
    s1.speed = 2
  end if
  if multikey(SC_DOWN) then
    s1.speed = -2
  end if
  '  adjust for sprite movement
  sprite.moveforward(s1.xpos, s1.ypos, s1.facing, s1.speed)
  '  keep image on screen
  if s1.xpos < 1 then s1.xpos = 639
  if s1.xpos > 639 then s1.xpos = 1
  if s1.ypos < 1 then s1.ypos = 479
  if s1.ypos > 470 then s1.ypos = 1
  '  draw image on screen
  DRAW "S4"
  DRAW "BM" + str$(s1.xpos) + "," + str$(s1.ypos)
  DRAW "C15"
  DRAW "TA" + str$(-s1.facing) + "BD5L10E10F10L10BU5"
  '  check for user quit
  if multikey(SC_ESCAPE) then
    done = TRUE
  end if
  sleep 1
  s1.speed = 0
loop until done
Logged

ature has its way of warning a person away from danger:  The distinct black and white coloration on a skunk, the chilling buzz of a rattlesanke, a redneck handing you his beer and saying "Watch this!"
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!