KiXforms Forum Index KiXforms
The Forum for the KiXforms Community
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
 Quick Links 
Site News
Downloads
Documentation
Donations
Script Archive
Tracking Systems

Asteroids: In space - no one can hear you bust rocks.

 
Post new topic   Reply to topic    KiXforms Forum Index -> Fun & Games
View previous topic :: View next topic  
Author Message
Shawn
KiXforms Developer
KiXforms Developer


Joined: 22 Feb 2003
Posts: 1983
Location: Canada

PostPosted: Sun Feb 01, 2004 2:03 am    Post subject: Asteroids: In space - no one can hear you bust rocks. Reply with quote

Manoeuvre your ship, destroy as many asteroids as you can while avoiding collision with them. This version of the classic game, based on Mike Halls java version, excersises two new features of Kixforms build 42: Expando properties and user-defined objects:

This entire game is basically one big user-defined object ($Asteroids) - the form and every single variable in it is an Expando property ... the Sprite() object encapsulates and controls everything you see on the screen.

To run this game you will need three things:

1) Kixtart Version 4.20 or better

2) Kixforms Build 42

3) A computer running about 1 MHZ and up.

Feedback on comments welcome:

Code:

break on

;
; ship.kix
; adapted from Mike Hall's Asteroids.java
;
;
;  Keyboard Controls:
;
; P      - Pause
; S      - Ship
; H      - Hyperspace
; Cursor Left   - Left   
; Cursor Right   - Right 
; Cursor Up   - Thrusters
; Cursor Down   - Retro Thrusters
; Spacebar   - Fire Photon
;

global $system, $math, $asteroids

$= kixver(4.22,1)
if @ERROR
 $= MessageBox("Requires Kixtart Version 4.22","Error")
 ;exit
endif

$system = createobject("kixtart.system")
if not $system
 $=MessageBox("Requires Kixforms Build 42","Error")
 exit
endif

$math = $system.math

global $PI

$PI = $math.pi

SRND(ABS(@TICKS))

$Asteroids = Asteroids()

Asteroids_Run($Asteroids)

exit 1

;
;************************************************************************************************
;  Asteroids application object
;************************************************************************************************/

function Asteroids()

 dim $this, $i

 $this = $System.object()

 ; Constants

 $this.FRAMES_PER_SECOND = 17   ; target frames per second
 $this.MAX_SHIPS = 3      ; Starting number of ships per game.
 $this.MAX_SHOTS = 5      ; maximum number of sprites for photons,
 $this.MAX_ROCKS = 5      ; asteroids and explosions.
 $this.MAX_SCRAP = 8      ; maximum amount of scrap

 $this.SCRAP_COUNT = 20      ; decay cycles
 $this.HYPER_COUNT = 40      ;
 $this.STORM_PAUSE = 30      ;
 $this.UFO_PASSES  = 3      ;

 $this.MIN_ROCK_SIDES =  4   ; Asteroid shape and size ranges.
 $this.MAX_ROCK_SIDES =  8   ;
 $this.MIN_ROCK_SIZE  = 20   ;
 $this.MAX_ROCK_SIZE  = 40   ;
 $this.MIN_ROCK_SPEED =  5   ;
 $this.MAX_ROCK_SPEED = 10   ;

 $this.BIG_POINTS    =  25   ; Points for shooting different objects.
 $this.SMALL_POINTS  =  50   ;
 $this.UFO_POINTS    = 250   ;
 $this.MISSLE_POINTS = 500   ;

 $this.NEW_SHIP_POINTS = 5000   ; Number of points needed to earn a new ship.
 $this.NEW_UFO_POINTS  = 2750   ; Number of points between flying saucers.

 $this.LARGE_ROCK_SHAPE = -40,-40,40,-40,40,40,-40,40,-40,-40
; $this.LARGE_ROCK_SHAPE = 0,40,40,0,20,-40,-20,-40,-40,0,0,40

 $this.MEDIUM_ROCK_SHAPE = -20,-20,20,-20,20,20,-20,20,-20,-20
 $this.SMALL_ROCK_SHAPE = -10,-10,10,-10,10,10,-10,10,-10,-10

 $this.PHOTON_SHAPE = 1,1,1,-1,-1,1,-1,-1
 $this.SCRAP_SHAPE = 1,1,1,-1

 $this.score = 0
 $this.highScore = 0
 $this.newShipScore = $this.NEW_SHIP_POINTS
 $this.newUfoScore = $this.NEW_UFO_POINTS
 $this.asteroidsSpeed = $this.MIN_ROCK_SPEED

 $this.loaded = 0
 $this.paused = 0
 $this.playing = 0
 $this.sound = 1
 $this.detail = 1
 $this.invincible = 1

 ; Key flags.

 $this.keyLeft  = 0
 $this.keyRight = 0
 $this.keyUp    = 0
 $this.keyDown  = 0

 $this.form = $System.form()
 $this.form.clientsize = $system.size(640,480)

 $this.form.fileMenu = $this.form.Menu.MenuItems.Add("File")
 $this.form.exitMenu = $this.form.FileMenu.MenuItems.Add("Exit","OnExitClick()")
 $this.form.optionsMenu = $this.form.Menu.MenuItems.Add("Options")
 $this.form.invincibleMenu = $this.form.optionsMenu.MenuItems.Add("Invincible","InvincibleClick($$this)")
 $this.form.invincibleMenu.checked = $this.invincible
 $this.form.helpMenu = $this.form.Menu.MenuItems.Add("Help")
 $this.form.helpMenu = $this.form.helpMenu.MenuItems.Add("Player Controls...","HelpClick()")

 $this.form.text = "Asteroids"
 $this.form.startPosition = $System.formStartPosition.centerParent
 $this.form.borderstyle = $system.formBorderStyle.fixedDialog
 $this.form.keyPreview = 1
 $this.form.onKeyDown = "Asteroids_OnKeyDown($$this)"
 $this.form.onKeyUp = "Asteroids_OnKeyUp($$this)"

 $this.graphics = $this.form.controls.pictureBox()
 $this.graphics.font = $system.font("helvetica",12,$system.fontStyle.bold)
 $this.graphics.location = 0,0
 $this.graphics.right = $this.form.clientWidth
 $this.graphics.bottom = $this.form.clientHeight
 $this.graphics.backColor = "Black"
 $this.graphics.foreColor = "White"
 $this.graphics.anchor = $System.anchorStyles.all
 $this.graphics.BorderStyle = 1

 $this.width = $this.graphics.clientWidth
 $this.height = $this.graphics.clientHeight

 ; framerate calculations ...

 $this.frameRate = 0
 $this.timer2 = $this.form.timer(1000)
 $this.timer2.onTimer = "Asteroids_OnTimer2($$this)"

 ; Sprite objects.

 ; Ships ...

 dim $ships[2]

 ; Normal ship

 $ships[0] = $system.object()
 $ships[0].shape = 0,-10, 7,10, -7,10, 0,-10
 $ships[0].limit = 1.0 * $this.MIN_ROCK_SIZE
 $ships[0].turningRatio = 16.0
 $ships[0].color = &00FFFF
 $ships[0].photonShape = -1,-1,1,-1,1,1,-1,1,-1,-1
 $ships[0].photonOffset = 4
 $ships[0].photonColor = $ships[0].color

 ; Fast ship ...

 $ships[1] = $system.object()
 $ships[1].shape = 0,-10, 7,10, 0,3, -7,10, 0,-10
 $ships[1].limit = 2.0 * $this.MIN_ROCK_SIZE
 $ships[1].turningRatio = 10.0
 $ships[1].color = &FF00FF
 $ships[1].photonShape = 1,0,0,0
 $ships[1].photonOffset = 1
 $ships[1].photonColor = &FFFFFF

 ; Torpedo ship ...

 $ships[2] = $system.object()
 $ships[2].shape = 3,-10, 10,10, -10,10, -3,-10, 3, -10
 $ships[2].limit = 0.50 * $this.MIN_ROCK_SIZE
 $ships[2].turningRatio = 24.0
 $ships[2].color = &FFCCCC
 $ships[2].photonShape = -2,-2,2,-2,2,2,-2,2,-2,-2
 $ships[2].photonOffset = 8
 $ships[2].photonColor = $ships[2].color

 ; star-burst
 ;$ships[2].photonShape = 2,2,2,-2,-2,2,-2,-2
 ; torpedo ship
 ;$ships[2].shape = 0,-10, 7,10, 0,15, -7,10, 0,-10

 $this.ships = $ships
 $this.shipsIndex = 0

 dim $ship

 $ship = sprite($ships[$this.shipsIndex].shape, $this.width, $this.height)
 $ship.photonShape = $ships[$this.shipsIndex].photonShape
 $ship.photonOffset = $ships[$this.shipsIndex].photonOffset
 $ship.photoncolor = $ships[$this.shipsIndex].photonColor
 $ship.limit = $ships[$this.shipsIndex].limit
 $ship.turningRatio = $ships[$this.shipsIndex].turningRatio
 $ship.color = $ships[$this.shipsIndex].color
 $ship.offset = 8
 $ship.active = 0

 $this.ship = $ship

 $this.hyperCounter = 0;

 ; Asteroids ...

 dim $rocks[$this.MAX_ROCKS-1]
 for $i = 0 to ubound($rocks)
  $rocks[$i] = sprite($this.LARGE_ROCK_SHAPE, $this.width, $this.height)
 next
 $this.asteroids = $rocks

 ; Photons ...

 dim $photons[$this.MAX_SHOTS-1]
 for $i = 0 to ubound($photons)
  $photons[$i] = sprite($ship.photonShape, $this.width, $this.height)
 next
 $this.photons = $photons
 $this.photonCounter = 0

 ; Scrap (from explosions) ...

 dim $scraps[$this.MAX_SCRAP]
 for $i = 0 to ubound($scraps)
  $scraps[$i] = sprite($this.SCRAP_SHAPE, $this.width, $this.height)
 next

 $this.scraps = $scraps
 $this.scrapIndex = -1

 $asteroids = $this

EndFunction

Function Asteroids_Run($this)

 dim $, $target, $delay, $form

 $form = $this.form

 Asteroids_InitGame($this)

 $delay = 1000 / $this.FRAMES_PER_SECOND

 $form.Show

 while $form.visible

  $=execute($form.doevents(1))

  $target = @TICKS + $delay

  if (not $this.paused)

   Asteroids_UpdateShip($this)
   Asteroids_UpdatePhotons($this)
   Asteroids_UpdateAsteroids($this)
   Asteroids_UpdateScrap($this)

  endif

  ; If all asteroids have been destroyed create a new batch.

  if ($this.asteroidsLeft = 0)
   $this.asteroidsCounter = $this.asteroidsCounter - 1
   if ($this.asteroidsCounter <= 0)
    asteroids_initAsteroids($this);
    $this.ship.hyperCounter = $this.HYPER_COUNT
   endif
  endif

  Asteroids_Paint($this)

  while @TICKS < $target
   sleep(0.001)
  loop

 loop

endfunction

function OnExitClick()

 quit()

endfunction

function Asteroids_OnTimer2($this)

 $this.form.text = "FPS: " + $this.frameRate
 $this.frameRate = 0

EndFunction

function Asteroids_InitGame($this)

 ; Initialize game data and sprites.

 $this.score = 0;
 $this.shipsLeft = $this.MAX_SHIPS;
 $this.asteroidsSpeed = $this.MIN_ROCK_SPEED;
 $this.newShipScore = $this.NEW_SHIP_POINTS;
 $this.newUfoScore = $this.NEW_UFO_POINTS;

 Asteroids_InitPhotons($this)
 Asteroids_InitShip($this);
 Asteroids_InitAsteroids($this);

 $this.ship.active = 0;
 $this.playing = 0;
 $this.paused = 0;

EndFunction

Function Asteroids_InitPhotons($this)

 dim $ship, $photons,$i

 $ship = $this.ship
 $photons = $this.photons

 for $i = 0 to ubound($photons)

  $photons[$i].shape = $ship.photonShape
  $photons[$i].sprite = $ship.photonShape
  $photons[$i].offset = $ship.photonOffset
  $photons[$i].active = 0
  $photons[$i].counter = 0
  $photons[$i].color = $ship.photonColor

 next

 $this.photonIndex = 0

EndFunction

Function Asteroids_UpdatePhotons($this)

 dim $photons,$photon,$i

 $photons = $this.photons

 ; Move any active photons. Stop it when its counter has expired.

 for $i = 0 to ubound($photons)

  $photon = $photons[$i]

  if $photon.active
   sprite_advance($photon)
   sprite_render($photon)
   $photon.counter = $photon.counter - 1
   if ($photon.counter < 0)
    $photon.active = 0
   endif
  endif
 next

EndFunction

Function Asteroids_InitShip($this)

 dim $ship

 $ship = $this.ship

 $ship.active = 1;
 $ship.angle = 0.0;
 $ship.deltaAngle = 0.0;
 $ship.x = 0.0;
 $ship.y = 0.0;
 $ship.deltaX = 0.0;
 $ship.deltaY = 0.0;
 $ship.hyperCounter = $this.HYPER_COUNT

 sprite_render($ship)

EndFunction

Function Asteroids_UpdateShip($this)

 dim $dx, $dy, $limit, $ship;

 if (not $this.playing)
  return
 endif

 $ship = $this.ship
 $limit = $ship.limit

 ; Rotate the ship if left or right cursor key is down.

 if ($this.keyLeft)
  $ship.angle = $ship.angle + $PI / $ship.turningRatio
  if ($ship.angle > 2 * $PI)
   $ship.angle = $ship.angle - 2 * $PI
  endif
 endif

 if ($this.keyRight)
  $ship.angle = $ship.angle - $PI / $ship.turningRatio
  if ($ship.angle < 0)
   $ship.angle = $ship.angle + 2 * $PI
  endif
 endif

 ; Fire thrusters if up or down cursor key is down.
 ; Don't let ship go past the speed limit.

 $dx = -$math.sin($ship.angle)
 $dy =  $math.cos($ship.angle)

 if ($this.keyUp)
  if ($ship.deltaX + $dx > -$limit and $ship.deltaX + $dx < $limit)
   $ship.deltaX = $this.ship.deltaX + $dx;
  endif
  if ($ship.deltaY + $dy > -$limit and $ship.deltaY + $dy < $limit)
    $ship.deltaY = $ship.deltaY + $dy;
  endif
 endif

 if ($this.keyDown)
  if ($ship.deltaX - $dx > -$limit and $ship.deltaX - $dx < $limit)
   $ship.deltaX = $ship.deltaX - $dx
  endif
  if ($ship.deltaY - $dy > -$limit and $ship.deltaY - $dy < $limit)
   $ship.deltaY = $ship.deltaY - $dy;
  endif
 endif

 ; Move the ship. If it is currently in hyperspace, advance the countdown.

 if ($ship.active)
  Sprite_Advance($ship)
  Sprite_Render($ship)
  if ($ship.hyperCounter > 0)
   $ship.hyperCounter = $ship.hyperCounter - 1
  endif
 else ; ship is destroyed
  $ship.counter = $ship.counter - 1
  if ($ship.counter <= 0)
   if $this.shipsLeft > 0
    asteroids_initShip($this)
   endif
  endif 

 endif

EndFunction

Function Asteroids_StopShip($this)

 dim $ship

 $ship = $this.ship
 $ship.active = 0
 $ship.counter = 40

EndFunction

Function Asteroids_InitAsteroids($this)

 dim $i,$j,$s,$t,$r,$x,$y,$shape
 dim $asteroids,$asteroid

 ; TODO: Create random shapes, positions and movements for each asteroid.

 $asteroids = $this.asteroids

 for $i = 0 to ubound($asteroids)

  $asteroid = sprite($this.LARGE_ROCK_SHAPE, $this.width, $this.height)
  $asteroid.active = 1
  $asteroid.offset = 35 ; bounding-box collision detect
  $asteroid.size = 2
  $asteroid.angle = 0.0
  $asteroid.deltaAngle = ((0.001*RND(1000)) - 0.5) / 10.0
  $asteroid.color = &00FF00

  ; Place the asteroid at the edge of the screen

  if (RND(1))

   $asteroid.x = CINT(0.001 * RND(1000) * $asteroid.width)

   if (RND(1))

    $asteroid.x = -$asteroid.x

   endif

   $asteroid.y = CINT(0.001*RND(1000) * $asteroid.height)

  else

   $asteroid.x = CINT(0.001 * RND(1000) * $asteroid.width)
   $asteroid.y = - CINT(0.001*RND(1000) * $asteroid.height)

   if (RND(1))
    $asteroid.y = -$asteroid.y
   endif

  endif

  ; Set a random motion for the asteroid.
 
  $asteroid.deltaX = CINT(0.001*RND(1000) * $this.asteroidsSpeed)
  if (RND(1))
   $asteroid.deltaX = -$asteroid.deltaX
  endif
  $asteroid.deltaY = CINT(0.001*RND(1000) * $this.asteroidsSpeed)
  if (RND(1))
   $asteroid.deltaY = -$asteroid.deltaY;
  endif

  sprite_render($asteroid)

  $asteroids[$i] = $asteroid
 
 next

 $this.asteroids = $asteroids
 $this.asteroidsLeft = ubound($asteroids)+1
 $this.asteroidsCounter = 30

EndFunction

function Asteroids_InitSmallAsteroids($this, $n, $size)

 ; Create one or two smaller asteroids from a larger one using inactive asteroids.
 ; The new asteroid will be placed in the same position as the old one but will
 ; have a new, smaller shape and new, randomly generated movements.

 dim $count,$i,$tx,$ty,$asteroids,$asteroid

 $asteroids = $this.asteroids

 $tx = $asteroids[$n].x
 $ty = $asteroids[$n].y

 $count = 0
 $i = 0

 while ($i <= ubound($asteroids) and $count < 2)
  if (not $asteroids[$i].active)
   if $size = 2 ; large
    $asteroid = sprite($this.MEDIUM_ROCK_SHAPE, $this.width, $this.height )
    $asteroid.size = 1 ; medium
    $asteroid.offset = 20 ;
   else ; medium
    $asteroid = sprite($this.SMALL_ROCK_SHAPE, $this.width, $this.height )
    $asteroid.size = 0 ; small
    $asteroid.offset = 10 ;
   endif
   $asteroid.active = 1
   $asteroid.deltaAngle = ((0.001*RND(1000)) - 0.5) / 10.0
   $asteroid.x = $tx
   $asteroid.y = $ty
   $asteroid.deltaX = CINT(0.001*RND(1000) * 2 * $this.asteroidsSpeed - $this.asteroidsSpeed)
   $asteroid.deltaY = CINT(0.001*RND(1000) * 2 * $this.asteroidsSpeed - $this.asteroidsSpeed)
   $asteroid.color = &00FF00
   sprite_render($asteroid)
   $this.asteroidsLeft = $this.asteroidsLeft + 1
   $count = $count + 1
   $asteroids[$i] = $asteroid
  endif
  $i=$i+1
 loop

 $this.asteroids = $asteroids

endfunction

function Asteroids_UpdateAsteroids($this)

 dim $asteroids, $asteroid, $i, $j
 dim $photons
 dim $ship

 $asteroids = $this.asteroids

 for $i = 0 to ubound($asteroids)
 
  $asteroid = $asteroids[$i]
 
  if $asteroid.active

   sprite_advance($asteroid)
   sprite_render($asteroid)

   ; If hit by a photon, kill asteroid and advance score ...

   $photons = $this.photons

   for $j = 0 to ubound($photons)

    if ($photons[$j].active and $asteroid.active)

     if sprite_colliding($asteroid,$photons[$j])

      $asteroid.active = 0
      $photons[$j].active = 0
      $this.asteroidsLeft = $this.asteroidsLeft - 1

      asteroids_explode($this, $asteroid)
     
      if ($asteroid.size > 0) ; not small

       $this.score = $this.score + $this.BIG_POINTS
       asteroids_initSmallAsteroids($this, $i, $asteroid.size)

      else

       $this.score = $this.score + $this.SMALL_POINTS

      endif

     endif

    endif

   next

   ; If the ship is not in hyperspace, see if it is hit.

   $ship = $this.ship

   if (not $this.invincible and $ship.active and $ship.hyperCounter <= 0)
    if sprite_colliding($asteroid,$ship)
     asteroids_stopShip($this)
     asteroids_explode($this, $ship)
    endif
   endif

  endif

 next

endfunction

Function Asteroids_Explode($this, $s)

 ; Create sprites for explosion animation. Each individual line segment
 ; of the given sprite is used to create a new sprite that will move
 ; outward from the sprites original position with a random rotation.

 dim $i, $j, $c, $sprite, $shape
 dim $scraps, $scrap

 sprite_render($s)

 $c = 2

 $shape = $s.shape
 $sprite = $s.sprite
 $scraps = $this.scraps

 for $i = 0 to ubound($shape) step 2

  $j = $i + 2;
  if $j >= ubound($shape)
   $j = 0
  endif

  $this.scrapIndex = $this.scrapIndex + 1
  if $this.scrapIndex > ubound($scraps)
   $this.scrapIndex = 0
  endif

  $scrap = $scraps[$this.scrapIndex]

  $scrap.active = 1
  $scrap.shape = $shape[$i],$shape[$i+1],$shape[$j],$shape[$j+1]
;  $scrap.sprite = 0,0,0,0
  $scrap.angle = $s.angle
  $scrap.deltaAngle = (0.001*RND(1000) * 2 * $PI - $PI) / 15
  $scrap.x = $s.x
  $scrap.y = $s.y
  ;$scrap.deltaX = 1.50 * $s.deltaX
  ;$scrap.deltaY = 1.50 * $s.deltaY
  $scrap.deltaX = -$shape[$i] / 6
  $scrap.deltaY = -$shape[$i+1] / 6
  $scrap.counter = $this.SCRAP_COUNT
  $scrap.color = $s.color

 next

EndFunction

Function Asteroids_UpdateScrap($this)

 ; Move any active scrap. Stop scrap when its counter has expired.

 dim $i, $scraps, $scrap

 $scraps = $this.scraps

 for $i = 0 to ubound($scraps)
  $scrap = $scraps[$i]
  if $scrap.active
   sprite_advance($scrap)
   sprite_render($scrap)
   $scrap.counter = $scrap.counter - 1
   if $scrap.counter < 0
    $scrap.active = 0
   endif
  endif
 next

EndFunction

Function Asteroids_Paint($this)

 dim $i, $j, $x, $y, $sprite
 dim $asteroids, $asteroid
 dim $photons, $photon
 dim $scraps, $scrap
 dim $g

 ; Draw the asteroids ...


 $g = $this.graphics
 $g.beginUpdate

 $this.graphics.backcolor = &0

 ; Draw photons ...

 $photons = $this.photons

 for $i = 0 to ubound($photons)

  $photon = $photons[$i]

  if $photon.active

   $sprite = $photon.sprite
   
   for $j = 0 To ubound($sprite) - 2 step 2
    $g.line($sprite[$j],$sprite[$j+1],$sprite[$j+2],$sprite[$j+3],$photon.color)
   next

  endif

 next

 ; Draw asteroids ...

 $asteroids = $this.asteroids

 for $i = 0 to ubound($asteroids)

  $asteroid = $asteroids[$i]

  if $asteroid.active

   $sprite = $asteroid.sprite

   for $j = 0 to ubound($sprite) - 2 step 2
    $g.line($sprite[$j],$sprite[$j+1],$sprite[$j+2],$sprite[$j+3],$asteroid.color)
   next

  endif

 next

 ; Draw the ship

 dim $ship

 $ship = $this.ship

 if ($ship.active)

  $sprite = $ship.sprite

  for $j = 0 to ubound($sprite) - 2 step 2
   $g.line($sprite[$j],$sprite[$j+1],$sprite[$j+2],$sprite[$j+3],$ship.color)
  next

 endif

 ; Draw scrap ...

 dim $, $c

 $scraps = $this.scraps

 for $i = 0 to ubound($scraps)

  $scrap = $scraps[$i]

  if $scrap.active

   $ = (255 / $this.SCRAP_COUNT) * $scrap.counter
   $c = $scrap.color
 
   $c = iif($c/65536>$, $, $c/65536) * 65536 +
   iif(($c & 65280) / 256>$, $, ($c & 65280) / 256) * 256 +
   iif($c & 255>$, $, $c & 255)

   $sprite = $scrap.sprite

   for $j = 0 to ubound($sprite) - 2 step 2
    $g.line($sprite[$j],$sprite[$j+1],$sprite[$j+2],$sprite[$j+3],$c)
   next

  endif

 next

 dim $s

 if (not $this.playing)
  $s = "A S T E R O I D S"
  $g.printxy(255,150,$s)
  $s = "Adapted by Shawn Tassie"
  $g.printxy(220,180,$s)
  $s = "Copyright 1998 by Mike Hall"
  $g.printxy(215,210,$s)
  $s = "Press 'S' to Start"
  $g.printxy(250,240,$s)
 endif

 $g.printxy(10,10,"Score: "+$this.score)
 $g.printxy(10,$g.height-40,"Ships: "+$this.shipsLeft)

; $g.printxy(10,30,FORMATNUMBER($this.score,0))

 $g.endupdate
 
 $this.frameRate = $this.frameRate + 1

EndFunction

Function Asteroids_OnKeyDown($this)

 dim $key,$i
 dim $photons,$ship

 $key = $this.form.keycode

 ; "s" = ship shape

 if ($key = 83)

  if not $this.playing
   $this.playing = 1
   return
  endif

  dim $ships
  $ships = $this.ships
  $ship = $this.ship

  $this.shipsIndex = $this.shipsIndex + 1
  if $this.shipsIndex > ubound($ships)
   $this.shipsIndex = 0
  endif

  $ship.shape = $ships[$this.shipsIndex].shape
  $ship.sprite = $ships[$this.shipsIndex].shape
  $ship.limit = $ships[$this.shipsIndex].limit
  $ship.turningRatio = $ships[$this.shipsIndex].turningRatio
  $ship.color = $ships[$this.shipsIndex].color
  $ship.photonShape = $ships[$this.shipsIndex].photonShape
  $ship.photonOffset = $ships[$this.shipsIndex].photonOffset
  $ship.photonColor = $ships[$this.shipsIndex].photonColor

  Asteroids_InitPhotons($this)

  sprite_render($ship)

  return;

 endif

 ; spacebar = fire a photon and start its counter...

 if ($key = 32 and $this.ship.active)

  $this.photonIndex = $this.photonIndex + 1

  if ($this.photonIndex = $this.MAX_SHOTS)

   $this.photonIndex = 0

  endif

  $i = $this.photonIndex
  $photons = $this.photons
  $ship = $this.ship

  $photons[$i].active = 1
  $photons[$i].x = $ship.x
  $photons[$i].y = $ship.y
  $photons[$i].deltaX = CDBL($this.MIN_ROCK_SIZE) * -$math.sin($ship.angle)
  $photons[$i].deltaY = CDBL($this.MIN_ROCK_SIZE) * $math.cos($ship.angle)
  $photons[$i].counter = $this.MIN_ROCK_SIZE

  return
 
 endif

 $this.keyLeft = 0
 $this.keyRight = 0
 $this.keyUp = 0
 $this.keyDown = 0

 if $Key = 37
  $this.keyLeft = 1
 endif

 if $Key = 39
  $this.keyRight = 1
 endif

 if $Key = 38
  $this.keyUp = 1
 endif

 if $key = 40
  $this.keyDown = 1
 endif

 if $key = 80 ; p
   $this.paused = not $this.paused
 endif

EndFunction

Function Asteroids_OnKeyUp($this)

 Dim $key

 $key = $this.form.keyCode

 if $key = 37 ; left
  $this.keyLeft = 0
  $this.keyRight = 0
 endif

 if $key = 39 ; right
  $this.keyRight = 0
  $this.keyLeft = 0
 endif

 if $key = 38 ; up
  $this.keyUp = 0
  $this.keyDown = 0
 endif

 if $key = 40 ; down
   $this.keyDown = 0
   $this.keyUp = 0
 endif

EndFunction

;
;******************************************************************************
; SPRITE: The Sprite class defines a game object, including it's shape,
; position, movement and rotation. It also can detemine if two objects collide.
;******************************************************************************

function sprite($shape, $width, $height)

 dim $this

 $this = $system.object

 $this.width = $width
 $this.height = $height
 $this.shape = $shape

 dim $s[ubound($shape)]
 $this.sprite = $s

 $this.active = 0
 $this.angle = 0.0
 $this.deltaAngle = 0.0
 $this.x = 0.0
 $this.y = 0.0
 $this.deltaX = 0.0
 $this.deltaY = 0.0
 $this.color = &FFFFFF ; white

 $sprite = $this

endfunction

; Methods

Function Sprite_Advance($this)

 ; Update the rotation and position of the sprite based on the delta values.
 ; If the sprite moves off the edge of the screen, it is wrapped around to
 ; the other side.

 dim $angle,$x,$y

 $angle = $this.angle + $this.deltaAngle

 if $angle < 0
  $angle = $angle + 2 * $PI;
 endif

 if $angle > 2 * $PI
  $angle = $angle - 2 * $PI;
 endif

 $x = $this.x + $this.deltaX

 if $x < -$this.width / 2
   $x = $x + $this.width
 endif

 if $x > $this.width / 2
  $x = $x - $this.width
 endif

 $y = $this.y - $this.deltaY

 if $y < -$this.height / 2
  $y = $y + $this.height
 endif

 if $y > $this.height / 2
  $y = $y - $this.height
 endif

 $this.angle = $angle
 $this.x = $x
 $this.y = $y

EndFunction

function sprite_render($this)

 dim $i,$sprite,$shape

 ; Render the sprite's shape and location by rotating it's base
 ; shape and moving it to it's proper screen position.

 $shape = $this.shape
 $sprite = $this.sprite

 for $i = 0 to ubound($shape) step 2

  ; have to use val() to cooerce into number because dealing with doubles

  $sprite[$i] = cint((CDBL($shape[$i]) * $math.cos($this.angle) + CDBL($shape[$i+1]) * $math.sin($this.angle))
                + cint(($this.x) + $this.width / 2))

  $sprite[$i+1] = cint((CDBL($shape[$i+1]) * $math.cos($this.angle) - CDBL($shape[$i]) * $math.sin($this.angle))
      + cint(($this.y) + $this.height / 2))

 next

 $this.sprite = $sprite

endfunction

Function sprite_colliding($this, $sprite)

 dim $l, $r, $t, $b
 dim $l2, $r2, $t2, $b2

 $l = ($this.x + $this.width / 2) - $this.offset
 $r = ($this.x + $this.width / 2) + $this.offset
 $t = ($this.y + $this.height / 2) - $this.offset
 $b = ($this.y + $this.height / 2) + $this.offset

 $l2 = ($sprite.x + $sprite.width / 2) - $sprite.offset
 $r2 = ($sprite.x + $sprite.width / 2) + $sprite.offset
 $t2 = ($sprite.y + $sprite.height / 2) - $sprite.offset
 $b2 = ($sprite.y + $sprite.height / 2) + $sprite.offset

 if ($b < $t2 ) return endif
 if ($t > $b2) return endif
 if ($r < $l2) return endif
 if ($l > $r2) return endif

 $sprite_colliding = 1

EndFunction

function FadeToBlack($c, $)
 $FadeToBlack =
   iif($c/65536>$, $, $c/65536) * 65536 +
   iif(($c & 65280) / 256>$, $, ($c & 65280) / 256) * 256 +
   iif($c & 255>$, $, $c & 255)
endfunction

function HelpClick()

 dim $s,$nl

 $nl = @CRLF

 $s =   "The following keyboard commands are supported:"+$nl+
   ""+$nl+
   "P = Pause"+$nl+
   "S = Ship"+$nl+
   ; H      - Hyperspace
   "Left = Rotate Left"+$nl+   
   "Right = Rotate Right"+$nl+
   "Up = Forward Thrust"+$nl+
   "Down = Reverse Thrust"+$nl+
   "SpaceBar = Fire Photon"+$nl

 $= $system.messagebox.show($s,"Controls",
   $system.messageboxbuttons.ok,
   $system.messageboxicon.information)

endFunction

function invincibleClick($this)

 $this.form.invincibleMenu.checked = not $this.form.invincibleMenu.checked

 $this.invincible = $this.form.invincibleMenu.checked

 $this.ship.hyperCounter = 40

endFunction

Function KixVer($Minimum, OPTIONAL $SoftErr, OPTIONAL $BRC)

  Dim $CVer, $VerOK, $MSG, $fPtr, $fX
  $fPtr = 0

  $CVer = Split(@KIX,' ')
  For $fX = 0 to UBound($CVer)
    If InStr('Kix200', Left($CVer[$fX],3))
      $fPtr = $fPtr + 1
    EndIf
  Next

  $VerOK = 1              ; assume good version

  If $CVer[$fPtr] < $Minimum
    ; Version running is less than minimum allowed
    $VerOK = 0
    $MSG = 'Cant run with Outdated Kix Version!'
  Else
    ; If any commentary exists, it isn't a production version. $BRC true allows it, though.
    If UBound($CVer) > 0 And $BRC = 0
      $VerOK = 0        ; current, but not production
      $MSG = "Can't run with Beta or Release Candidate!"
    EndIf
  EndIf

  ; Prepare to return the current kixtart version
  $KixVer = @KIX

  ; Minimum version was not found
  If $VerOK = 0
    If $SoftErr = 0
      ; complain and terminate script if version is not acceptable
      ; use hard print and not MSG function when terminating!
      ? 'Version Check! Need ' + $Minimum + ', running @KIX'
      ? $MSG ?
      Quit 120
    Else
      Exit 120
    EndIf
  EndIf

  Exit 0

EndFunction
Back to top
View user's profile Send private message
Rod
KiXforms Webmaster
KiXforms Webmaster


Joined: 22 Feb 2003
Posts: 202
Location: United Kingdom

PostPosted: Sun Feb 01, 2004 12:06 pm    Post subject: Reply with quote

This script is now also available for download from the Script Archive.

Enjoy,

Rod.

_________________
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Jochen
KiXforms Devotee
KiXforms Devotee


Joined: 05 Mar 2003
Posts: 1204
Location: Stuttgart, Germany

PostPosted: Mon Feb 02, 2004 8:24 am    Post subject: Reply with quote

A great game when I was young,
Thanx for the trip on memory lane.

German Jury gives this a

Mr. Green Mr. Green Mr. Green Mr. Green Mr. Green

_________________
Jochen

Tell me, and I will forget.
Show me, and I may remember.
Involve me, and I will understand.
Back to top
View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    KiXforms Forum Index -> Fun & Games All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group