You are here: start » geeny » examples » games » asteroids

Asteroids

demo_asteroids.jpg

A simple demo.

shape_gen.gee

//////////////////////////////////////////////////////////////
BEGIN             object              SHAPE_GEN
//////////////////////////////////////////////////////////////
  VECTOR          Pos                 = | 0 0 |
  VECTOR          Vel                 = | 0 0 |
  FLOAT           Angle               = 0.0
  FLOAT           RotSpeed            = 0.0
  FLOAT           Grow                = 0.0
  VECTOR          Size                = | 1 1 |
  VECTOR          Plane               = | 1 0 0 1 |
  FLOATS          PtX                 =
  FLOATS          PtY                 = 
  INTEGERS        PtColor             = 
END
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
 
//////////////////////////////////////////////////////////////
branch xformedpts(FLOATS & x, FLOATS & y)
//////////////////////////////////////////////////////////////
  x             = PtX * Plane|x
  x            += PtY * Plane|z
  
  y             = PtX * Plane|y
  y            += PtY * Plane|w
end
 
//////////////////////////////////////////////////////////////
branch update()
//////////////////////////////////////////////////////////////
  FLOAT t       = frameTime()
  
  Angle         = degclp (Angle + (RotSpeed * t))
  Pos          += Vel * t
  Size         += Grow * t
 
  Plane|x       = (cos rad (Angle)) * Size|x
  Plane|y       = (sin rad (Angle)) * Size|x
 
  Plane|z       = (cos rad (Angle + 90)) * Size|y
  Plane|w       = (sin rad (Angle + 90)) * Size|y         
end
 
//////////////////////////////////////////////////////////////
branch renderAt(FLOAT posX, FLOAT posY)
//////////////////////////////////////////////////////////////
  FLOATS          ptx 
  FLOATS          pty 
  
  shape_gen     : xformedpts(ptx, pty)
    
  ptx          += posX
  pty          += posY
 
  INTEGERS  map = (count ptx) append (1 .. (count ptx - 1))
 
  rendLineFloat([ptx[map]], [pty[map]], [ptx], [pty], [PtColor])
end

AST_asteroid.gee

//////////////////////////////////////////////////////////////
BEGIN             object              AST_ASTEROID
//////////////////////////////////////////////////////////////
  OBJECT          Scripta             = AST_asteroid
  OBJECT          Parent              = SHAPE_GEN
  VECTOR          MaxSize             = | 55 35 |
  VECTOR          MinSize             = | 10 10 |
  FLOAT           Roughness           = 6
END
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
 
//////////////////////////////////////////////////////////////
branch init(REFERENCE parent = nul)
//////////////////////////////////////////////////////////////
  REFERENCE $.game = 'ASTEROIDS'
  INTEGER   $.Complexity = 10 + rand(10)
 
  PtColor    = rendColor(AST_COLORR, AST_COLORG, AST_COLORB, 0)
  
  if (parent == nul)
    Pos|x    = rand(game.GameResX)
    Pos|y    = rand(game.GameResY)
    Vel      = rand(| 0.2 0.2 |, | 2.4 2.4 | )
    Vel     *= rand({| -1 1 | | 1 -1 | | -1 -1 | | 1 1 | })
    Size     = rand(MinSize, MaxSize)
    RotSpeed = rand(RotSpeed)
  else
    Pos       = parent.Pos
    Vel       = rand(parent.Vel)
    Size      = rand(MinSize, parent.Size)
    RotSpeed  = rand(1.0, 1.5) * parent.RotSpeed
    Roughness = parent.Roughness
  endif
                      
  Vel *= 10.0
                        
  makeshape()
end
  
//////////////////////////////////////////////////////////////
branch makeshape()
//////////////////////////////////////////////////////////////
  VECTOR  v
 
  PtX   = rand(0.5, 1.0)
  PtY   = rand(-Roughness, Roughness)
 
  INTEGER i = 1
  while (i += 1) <= Complexity
    v|x = rand(0.5, 1.0) + PtX[i - 1]
    v|y = rand(-Roughness, Roughness)
 
    PtX = PtX append v|x
    PtY = PtY append v|y
  endwhile
    
  FLOAT m = PtX[Complexity]
  
  i = 0
  while (i += 1) <= Complexity
    v|x  = rad ((PtX[i] / m) * 360.0)
    v|y  = 1.0 + (PtY[i] / Size|x)
    
    PtX[i] = v|y * (cos v|x)
    PtY[i] = v|y * (sin v|x)
  endwhile   
end
 
//////////////////////////////////////////////////////////////
branch collision(VECTOR Origin, INTEGER & Flag)
//////////////////////////////////////////////////////////////
  VECTOR Distance = Pos - Origin
    
  if (Flag == 0) and ((abs (Distance|x)) < Size|x) and ((abs (Distance|y)) < Size|y)
    split()
    Flag = 1
  endif
end
 
//////////////////////////////////////////////////////////////
branch shipcollision()
//////////////////////////////////////////////////////////////
  REFERENCE ship = game.Ship
  
  VECTOR Distance = Pos - ship.Pos
  VECTOR MinSize = Size
  MinSize|x += ship.Size
  MinSize|y += ship.Size
 
  if ((abs (Distance|x)) < MinSize|x) and ((abs (Distance|y)) < MinSize|y)
    ship.explosion()
  endif 
end
 
//////////////////////////////////////////////////////////////
branch split()
//////////////////////////////////////////////////////////////
  Size      *= 0.5
 
  if (Size|x < MinSize|y) and (Size|y < MinSize|y)
    game.Asteroids = game.Asteroids ^- $
    return
  endif
  
  RotSpeed  *= 1 - 2 * rand(1)
 
  REFERENCES na = new (repeat('AST_ASTEROID', 1 + rand(1)))
 
  na.init($)
 
  game.Asteroids = game.Asteroids append na  
  
  makeshape()
end
 
//////////////////////////////////////////////////////////////
branch update()
//////////////////////////////////////////////////////////////
  shape_gen:update()
  
  if Pos|x > game.GameResX
    Pos|x -= game.GameResX
  endif
 
  if Pos|x < 0
    Pos|x += game.GameResX
  endif
 
  if Pos|y > game.GameResY
    Pos|y -= game.GameResY
  endif
 
  if Pos|y < 0
    Pos|y += game.GameResY
  endif
end
 
//////////////////////////////////////////////////////////////
branch render()
//////////////////////////////////////////////////////////////
  rendSetShape('linewidth', 24)
                 
  shape_gen:renderAt(Pos|x, Pos|y)
  
  VECTOR p = Pos
 
  INTEGER b = (Size|x max Size|y) + Roughness
 
  if Pos|x <= b
    p|x += game.GameResX
  endif
  
  if Pos|x >= (game.GameResX - b)
    p|x -= game.GameResX
  endif
 
  if Pos|y <= b
    p|y += game.GameResY
  endif
 
  if Pos|y >= (game.GameResY - b)
    p|y -= game.GameResY
  endif
  
  if p <> Pos  
    shape_gen:renderAt(p|x, p|y)
  endif
end

AST_starship.gee

//////////////////////////////////////////////////
BEGIN     object              AST_STARSHIP
//////////////////////////////////////////////////
  OBJECT  Scripta             = AST_starship
  VECTOR  Pos                 = | 0 0 |
  FLOAT   Speed               = 0.03
  VECTOR  Vel                 = | 0 0 |
  FLOAT   MaxSpeed            = 2.0
  FLOAT   Acc                 = 0
  FLOAT   Angle               = 0.0
  FLOAT   RotSpeed            = 0.0
  FLOAT   MaxRotSpeed         = 0.05
  FLOAT   dRotSpeed           = 0.005
  FLOAT   Size                = 20
  FLOAT   Width               = 15
  INTEGER Direction           = 0
  INTEGER Color               = 0
  INTEGER Timeout             = 0
  FLOAT   ProjectileSpeed     = 3.0
END
 
//////////////////////////////////////////////////
BEGIN     object              AST_PROJECTILE
//////////////////////////////////////////////////
  OBJECT  Scripta             = AST_starship
  VECTOR  Pos                 = | 0 0 |
  VECTOR  Vel                 = | 0.1 0.1 |
  VECTOR  Dir                 = | 0 0 |
  INTEGER Timeout             = 200
END
 
//////////////////////////////////////////////////
//////////////////////////////////////////////////
 
//////////////////////////////////////////////////
branch init()
//////////////////////////////////////////////////
  REFERENCE $.game = *
 
  Pos|x = game.GameResX / 2
  Pos|y = game.GameResY / 2
  Color = rendColor(100, 100, 100, 0)
  
  REFERENCES $.Projectiles = { }
end
 
//////////////////////////////////////////////////
branch update()
//////////////////////////////////////////////////
  if (Direction < 0) and (RotSpeed > -MaxRotSpeed)
    RotSpeed = (-MaxRotSpeed) max (RotSpeed - dRotSpeed)
  endif
  
  if (Direction > 0) and (RotSpeed < MaxRotSpeed)
    RotSpeed = MaxRotSpeed min (RotSpeed + dRotSpeed)
  endif
  
  if (Direction  == 0) and (RotSpeed <> 0)    
    if RotSpeed > 0
      RotSpeed -= dRotSpeed
      if RotSpeed < 0
        RotSpeed = 0
      endif
    else
      RotSpeed += dRotSpeed
      if RotSpeed > 0
        RotSpeed = 0
      endif
    endif
  endif
      
  Angle += RotSpeed
 
  if Acc <> 0
    Vel|x += (Acc * Speed) * (cos Angle)
    Vel|y += (Acc * Speed) * (sin Angle)
  endif
  
  if (len Vel) > MaxSpeed
    Vel *= MaxSpeed / (len Vel)
  endif
 
  Pos += Vel
 
  if Pos|x > game.GameResX
    Pos|x -= game.GameResX
  endif
 
  if Pos|x < 0
    Pos|x += game.GameResX
  endif
 
  if Pos|y > game.GameResY
    Pos|y -= game.GameResY
  endif
 
  if Pos|y < 0
    Pos|y += game.GameResY
  endif
end
 
//////////////////////////////////////////////////
branch render()
//////////////////////////////////////////////////
  rendSetShape('linewidth', 20)
 
  if Timeout == 0
      
    INTEGER x1 = Pos|x + (Size * cos Angle)
    INTEGER y1 = Pos|y + (Size * sin Angle)
    
    INTEGER x2 = Pos|x + (Size * (cos (Angle + Width)))
    INTEGER y2 = Pos|y + (Size * (sin (Angle + Width)))
  
    INTEGER x3 = Pos|x + (Size * (cos (Angle - Width)))
    INTEGER y3 = Pos|y + (Size * (sin (Angle - Width)))
    
    rendLineFloat(x1, y1, x2, y2, Color)
    rendLineFloat(x2, y2, x3, y3, Color)
    rendLineFloat(x3, y3, x1, y1, Color)
 
    game.Asteroids.shipcollision(pp)
  else
    Timeout -= 1
  endif
    
  INTEGER flag
 
  rendSetShape('linewidth', 24)
 
  INTEGER Color = rendColor(140, 140, 140)
  INTEGER i = 1
  
  while i <= count Projectiles
    Projectiles[i].Pos += Projectiles[i].Vel
 
    VECTOR pp = Projectiles[i].Pos
    VECTOR pv = Projectiles[i].Dir * 12.0
    
    rendLineFloat(pp|x, pp|y, (pp|x + pv|x), (pp|y + pv|y), Color)
    
    pp += pv
    
    flag = 0
 
    if Timeout == 0
      game.Asteroids.collision(pp, flag)
    endif
    
    if ((Projectiles[i]).Timeout -= 1) == 0
      flag = 1
    endif
 
    BOOL testf = (pp|y < 0) or (pp|y > game.GameResY)
    if (flag) or (pp|x < 0) or (pp|x > game.GameResX) or testf
      Projectiles = Projectiles ^- Projectiles[i]
 
      if (count (game.Asteroids)) == 0
        game.nextlevel()
      endif
 
    else
      i += 1
    endif
    
  endwhile
end
 
//////////////////////////////////////////////////
branch shoot()
////////////////////////////////////////////////// 
  REFERENCE Projectile = new 'AST_PROJECTILE'
  
  Projectile.Dir|x = cos Angle
  Projectile.Dir|y = sin Angle  
 
  Projectile.Vel  = Vel
  Projectile.Vel += Projectile.Dir * ProjectileSpeed
 
  Projectile.Pos|x = Pos|x + (Size * cos Angle)
  Projectile.Pos|y = Pos|y + (Size * sin Angle)
  
  Projectiles = Projectiles append Projectile
end
 
//////////////////////////////////////////////////
branch explosion()
//////////////////////////////////////////////////
  INTEGER i = 0
    
  while (i += 10) < 360
    Angle = rad i
    shoot()
  endwhile
  
  Timeout = 300
end

ASTEROIDS.gee

//////////////////////////////////////////////////
BEGIN         global              CONST
//////////////////////////////////////////////////
  INTEGER     AST_COLORR          = 100
  INTEGER     AST_COLORG          = 70
  INTEGER     AST_COLORB          = 30
  REFERENCE   GameTest            = GAME
END
 
//////////////////////////////////////////////////
BEGIN         object              ASTEROIDS
//////////////////////////////////////////////////
  OBJECT      Parent              = APPLET
  OBJECT      Scripta             = @ ASTEROIDS
  ATOM        Resolution          = R640
  ATOM        FSRenderer          = NORMAL
  INTEGER     Level               = 2
END
 
//////////////////////////////////////////////////
//////////////////////////////////////////////////
 
//////////////////////////////////////////////////
branch init()
//////////////////////////////////////////////////
  @.init()
 
  REFERENCE $.Ship            = new 'AST_STARSHIP'
  Ship.init()
   
  REFERENCES $.Asteroids      = {}
  nextlevel()
end
 
//////////////////////////////////////////////////
branch nextlevel()
//////////////////////////////////////////////////
  Asteroids  = new (repeat( 'AST_ASTEROID', (Level += 1)))
  Asteroids.init(25, 25)
end
 
//////////////////////////////////////////////////
branch FIRE(INTEGER player, INTEGER on)
//////////////////////////////////////////////////
  if on
    Ship.shoot()
  endif
end
 
//////////////////////////////////////////////////
branch update()
//////////////////////////////////////////////////
  Ship.Direction = Joystick[1]|x
  Ship.Acc = -Joystick[1]|y
  Ship.update()
  Asteroids.update()
end
 
//////////////////////////////////////////////////
branch render()
//////////////////////////////////////////////////
  rendSetColor(nul, rendColor(240,240,250,0))
  rendSetBlending('mul')
  rendFill(0, 0, GameResX, GameResY)
  rendSetBlending()
  
  rendSetShape('smoothing', 4)
  rendSetShape('linewidth', 16)
  rendSetShape('lastpixel', 1)
 
  Ship.render()  
  Asteroids.render()
end
 
geeny/examples/games/asteroids.txt · Last modified: 2015/11/18 07:37 by matija
Recent changes · Show pagesource · Login