none
Space Invaders help RRS feed

Answers

  • You want to use the Shapes.GetLeft() and Shapes.GetTop() functions to get the positions of objects on the screen.  And for collision detection, you want to see if your objects are overlapping each other, not just exactly on top of each other which will probably never happen.

    For instance something like this:

    if Math.Abs(Shapes.GetLeft(Tank) - Shapes.GetLeft(alienshot) + a) < b and Math.Abs(Shapes.GetTop(Tank) - Shapes.GetTop(alienshot) +c) < d then

       ' hit

    endif

    You have to figure out the a, b, c, and d values based on the size of the objects.  A and b are based on the difference in sizes, and b and d are from the sizes themselves.  I don't have to formulas for them off hand, maybe you can figure it out?  Usually I'm lazy and guess them until it works.

    There could be other problems, I didn't try to run it.  Good luck!  I hope to see it working, could be the first space invaders for smallbasic.

    Tuesday, November 16, 2010 9:31 PM
  • They are all related to the height/width of the alien and tank.  Try drawing both out on paper and put their dimensions on it.  See what makes sense to you on how you might see if two objects are overlapping each other.
    Friday, November 19, 2010 2:15 AM

All replies

  • its for small basic.
    Thursday, November 11, 2010 12:12 PM
  • Utterlystupid, copy and paste what code?
    ~~AirWaves!!~~
    Thursday, November 11, 2010 5:40 PM
  • nevermind the top question i got it working theres something wrong with my collsions i cant seem to find it.

    GraphicsWindow.BackgroundColor = "black"
    GraphicsWindow.Title = "Invaders of Space"
    GraphicsWindow.Width=1120
    GraphicsWindow.height=700
    GraphicsWindow.top=(Desktop.Height-GraphicsWindow.Height)/2
    GraphicsWindow.Left=(Desktop.Width-GraphicsWindow.width)/2
    GraphicsWindow.Show()

    DistanceDown = 50
    DistanceAcross = 80
    GraphicsWindow.KeyDown = keypressed
    'alien bullet pos
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    alienbullet = 350
    alienbullet2 = 600
    alienbullet3 = 100
    alienbullet4 = 800
    'tank bullets
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    bulletxpos = 520
    bulletypos = 200
    startgame()
    '1s
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    alienxpos = 100
    alienypos = 100

    'Tank move
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Sub keypressed
    If GraphicsWindow.LastKey = "Escape" Then
      Program.End()
    ElseIf GraphicsWindow.LastKey = "Left" and gunxpos > 10 then
      gunxpos=gunxpos -20
      Shapes.Move(Tank,gunxpos,585)
    Elseif GraphicsWindow.LastKey = "Right" and gunxpos < 990 then
      gunxpos=gunxpos +20
      Shapes.Move(Tank,gunxpos,585)
    EndIf
    endsub


    Sub startgame
      GraphicsWindow.PenColor = "Blue"
      GraphicsWindow.DrawLine(10,670,1110,670,)
     endsub
     'My Tank
     '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
      mytank = ImageList.LoadImage(Program.Directory + "\Tank.jpg")
      Tank = Shapes.AddImage(mytank)
      shapes.Move(Tank,520,585)


    'Aliens - alien size 80w x 50h
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    myalien = ImageList.LoadImage(Program.Directory + "\Space.jpg")
    GraphicsWindow.DrawImage(myalien,100,100)
    myalien = ImageList.LoadImage(Program.Directory + "\Space.jpg")
    GraphicsWindow.DrawImage(myalien,200,100)
    myalien = ImageList.LoadImage(Program.Directory + "\Space.jpg")
    GraphicsWindow.DrawImage(myalien,300,100)
    myalien = ImageList.LoadImage(Program.Directory + "\Space.jpg")
    GraphicsWindow.DrawImage(myalien,400,100)
    myalien = ImageList.LoadImage(Program.Directory + "\Space.jpg")
    GraphicsWindow.DrawImage(myalien,500,100)
    myalien = ImageList.LoadImage(Program.Directory + "\Space.jpg")
    GraphicsWindow.DrawImage(myalien,600,100)
    myalien = ImageList.LoadImage(Program.Directory + "\Space.jpg")
    GraphicsWindow.DrawImage(myalien,700,100)
    myalien = ImageList.LoadImage(Program.Directory + "\Space.jpg")
    GraphicsWindow.DrawImage(myalien,800,100)
    myalien = ImageList.LoadImage(Program.Directory + "\Space.jpg")
    GraphicsWindow.DrawImage(myalien,900,100)
    myalien = ImageList.LoadImage(Program.Directory + "\Space.jpg")
    GraphicsWindow.DrawImage(myalien,100,200)
    myalien = ImageList.LoadImage(Program.Directory + "\Space.jpg")
    GraphicsWindow.DrawImage(myalien,200,200)
    myalien = ImageList.LoadImage(Program.Directory + "\Space.jpg")
    GraphicsWindow.DrawImage(myalien,300,200)
    myalien = ImageList.LoadImage(Program.Directory + "\Space.jpg")
    GraphicsWindow.DrawImage(myalien,400,200)
    myalien = ImageList.LoadImage(Program.Directory + "\Space.jpg")
    GraphicsWindow.DrawImage(myalien,500,200)
    myalien = ImageList.LoadImage(Program.Directory + "\Space.jpg")
    GraphicsWindow.DrawImage(myalien,600,200)
    myalien = ImageList.LoadImage(Program.Directory + "\Space.jpg")
    GraphicsWindow.DrawImage(myalien,700,200)
    myalien = ImageList.LoadImage(Program.Directory + "\Space.jpg")
    GraphicsWindow.DrawImage(myalien,800,200)
    myalien = ImageList.LoadImage(Program.Directory + "\Space.jpg")
    GraphicsWindow.DrawImage(myalien,900,200)
    GraphicsWindow.DrawImage(myalien,100,300)
    myalien = ImageList.LoadImage(Program.Directory + "\Space.jpg")
    GraphicsWindow.DrawImage(myalien,200,300)
    myalien = ImageList.LoadImage(Program.Directory + "\Space.jpg")
    GraphicsWindow.DrawImage(myalien,300,300)
    myalien = ImageList.LoadImage(Program.Directory + "\Space.jpg")
    GraphicsWindow.DrawImage(myalien,400,300)
    myalien = ImageList.LoadImage(Program.Directory + "\Space.jpg")
    GraphicsWindow.DrawImage(myalien,500,300)
    myalien = ImageList.LoadImage(Program.Directory + "\Space.jpg")
    GraphicsWindow.DrawImage(myalien,600,300)
    myalien = ImageList.LoadImage(Program.Directory + "\Space.jpg")
    GraphicsWindow.DrawImage(myalien,700,300)
    myalien = ImageList.LoadImage(Program.Directory + "\Space.jpg")
    GraphicsWindow.DrawImage(myalien,800,300)
    myalien = ImageList.LoadImage(Program.Directory + "\Space.jpg")
    GraphicsWindow.DrawImage(myalien,900,300)


    'gun shooting
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    GraphicsWindow.MouseDown = Gunfire
     Sub GunMove
     gunxpos = Mouse.MouseX - 550
     gunypos = Mouse.MouseY
     Shapes.Move(Tank,gunxpos, 0)
    EndSub

    Sub Gunfire
    Bullet = Shapes.AddRectangle(8, 8)
    Shapes.Move(Bullet, gunxpos + 65, 642)
    Shapes.Animate(Bullet, gunxpos + 65, -100, 2000)
    Aliendead()
    endsub
    'alien dies
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    alienxpos = 100
    alienypos = 100
    sub Aliendead
    If Bullet = alienxpos+ alienypos Then
     GraphicsWindow.Clear()
      endif
    Endsub

    'Timer for alien bullets
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Timer.Interval = 2000
    Timer.Tick = alienshoot

    'Sub routine for Aliens shooting
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Sub alienshoot
      alienshot = Shapes.AddRectangle(8, 8)
    Shapes.Move(alienshot, alienbullet  + 65, 200)
    Shapes.Animate(alienshot, alienbullet + 65, 800, 4000)

    alienshot = Shapes.AddRectangle(8, 8)
    Shapes.Move(alienshot, alienbullet2  + 65, 200)
    Shapes.Animate(alienshot, alienbullet2 + 65, 800, 2000)

    alienshot = Shapes.AddRectangle(8, 8)
    Shapes.Move(alienshot, alienbullet3  + 65, 200)
    Shapes.Animate(alienshot, alienbullet3 + 65, 800, 3000)

    alienshot = Shapes.AddRectangle(8, 8)
    Shapes.Move(alienshot, alienbullet4  + 65, 200)
    Shapes.Animate(alienshot, alienbullet4 + 65, 800, 5000)
    Tankdead()
    endsub

    'Tank Dies
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    gunxpos = 520
    gunypos = 200
    Sub Tankdead
    If alienshot = gunxpos And gunypos then
      TextWindow.writeline("Game over")
    endif
    EndSub  
    Tuesday, November 16, 2010 4:13 PM
  • You want to use the Shapes.GetLeft() and Shapes.GetTop() functions to get the positions of objects on the screen.  And for collision detection, you want to see if your objects are overlapping each other, not just exactly on top of each other which will probably never happen.

    For instance something like this:

    if Math.Abs(Shapes.GetLeft(Tank) - Shapes.GetLeft(alienshot) + a) < b and Math.Abs(Shapes.GetTop(Tank) - Shapes.GetTop(alienshot) +c) < d then

       ' hit

    endif

    You have to figure out the a, b, c, and d values based on the size of the objects.  A and b are based on the difference in sizes, and b and d are from the sizes themselves.  I don't have to formulas for them off hand, maybe you can figure it out?  Usually I'm lazy and guess them until it works.

    There could be other problems, I didn't try to run it.  Good luck!  I hope to see it working, could be the first space invaders for smallbasic.

    Tuesday, November 16, 2010 9:31 PM
  • Thanks very helpful :)

    Wednesday, November 17, 2010 9:04 AM
  • Is A,B,C,D  the position of the alien or is it the hieght and width of the alien? btw my aliens wont be moving yet.
    Wednesday, November 17, 2010 9:23 AM
  • They are all related to the height/width of the alien and tank.  Try drawing both out on paper and put their dimensions on it.  See what makes sense to you on how you might see if two objects are overlapping each other.
    Friday, November 19, 2010 2:15 AM