locked
Problems with collisions in a shooter RRS feed

  • Question

  • I have recently been coding a space shooter that is played from birds-eye view. Very Asteroids in the playing style. I have made it so that the background is solid black, there is a score counter, small stars move by as white ellipse sprites in a straight, vertical line from the top of the screen to the bottom and i've made it so that when the player clicks the mouse it spawns a bullet that goes in a straight verticle line from the point of origin on the x-axis to the top of the screen and disappears. I had the entire game functioning at one point but i ran into a wall with collisions and resolved that the reason was that the enemy ships and bullets were sprites.

    Since, i have tried to create objects of both so i could record the coordinates of the objects but when i converted them to objects they no longer move across the screen in the way that i had perfected before-hand. I will be more than happy to post my code upon request but is there anyone who might be able to shed a sliver of insight as to how to make it so i can track the coordinates of the enemy fighter and the players bullets while having them still be able to move in the method i had originally intended.

     

    Additional Information: I was originally using the shapes.animate() method to move the bullets and the enemy ships as well as the shapes.move() method the place the ships at their random starting location on the x-axis. 

    Friday, January 14, 2011 6:33 AM

Answers

  • Nice graphic, it loads fine for me with your code.  Here is a simple moving test:

    'Graphics window dimensions
    gw = GraphicsWindow.Width
    gh = GraphicsWindow.Height
    'Load image and set it as a shape
    eFighter = ImageList.LoadImage("http://mattjessee.webs.com/enemyFighter.gif")
    enemies[0] = Shapes.AddImage(eFighter)
    'Width and height of image - make these the half width etc because that is what we need later
    enemiesWidth[0] = ImageList.GetWidthOfImage(eFighter)/2
    enemiesHeight[0] = ImageList.GetHeightOfImage(eFighter)/2
    'Initial Position
    enemiesX[0] = 300
    enemiesY[0] = 200
    'Initial velocities
    enemiesU[0] = 5 'The horizontal speed
    enemiesV[0] = 0 'The vertical speed

    While ("True")
      'Update the shape centre position based on velocity
      enemiesX[0] = enemiesX[0]+enemiesU[0]
      enemiesY[0] = enemiesY[0]+enemiesV[0]
      'Bounce the shape when it his a side wall - only handle the horizontal direction here since enemiesY is constant
      If (enemiesX[0] < enemiesWidth[0]) Then
        enemiesU[0] = -enemiesU[0]
        enemiesX[0] = enemiesWidth[0]
      EndIf
      If (enemiesX[0] > gw-enemiesWidth[0]) Then
        enemiesU[0] = -enemiesU[0]
        enemiesX[0] = gw-enemiesWidth[0]
      EndIf
      'The top left of the shapes are the coordinates requires - so the centre is at (enemiesX[0],enemiesY[0])
      Shapes.Move(enemies[0], enemiesX[0]-enemiesWidth[0], enemiesY[0]-enemiesHeight[0])
      Program.Delay(20) 'Small delay in the movement  - about 50 frames per sec
    EndWhile

    • Marked as answer by Kai Karden Saturday, January 15, 2011 7:15 AM
    Friday, January 14, 2011 7:44 PM

All replies

  • You can use arrays.

    EnemyFighter[1]["X"]=450

    EnemyFighter[1]["Y"]=500

     

    The arrays can change upon moving the enemy fighters, so you can get the coordinates of the enemy fighter and use only one variable. The same for the bullets.

    Does this help?


    ~~AirWaves!!~~
    Friday, January 14, 2011 6:50 AM
  • Kind of, That is one of the methods of keeping track of the coordinates that i've tried so far. is it possible to still use the shapes.animate() method when using arrays? If so then i'm probably using it wrong because it seems like the entire shapes library doesn't quite like me referencing arrays as the object that i need to move. If that isn't the proper way to move them then my problem arises of trying to figure out an alternative for the fluid motion that the shapes.animate() method provides for me.

    Edit: Hmm, re-reading your post it actually is a little bit different from what i did, I'll try it! Thanks man!

    Edit 2: Okay so I've figured out that the major flaw in my program thus far. Using arrays, i am assigning the first dimension the object itself, then the second dimension holds the coordinates. When i try to use the shapes class to do any sort of movement or animation to the ship, it has a problem with that. probably because it doesn't see the Array as an object which it can move. I don't know. Below is the code for a test spawner sub that i had thrown into my code to see if everything was working properly before i get to animation and it does not appear as such.

     

    Sub SpawnTest

      enemies[0] = Shapes.AddImage(eFighter)

      Shapes.Move(enemies[0], 300, 200)

    EndSub

    Friday, January 14, 2011 2:51 PM
  • Your code looks good, assuming eFighter is a string that contains the full path of the image you want, something like:

    eFighter = Program.Directory+"\eFighter.png"

    If eFighter is not set to a valid file you will not see anything.

    Friday, January 14, 2011 7:01 PM
  • Here is the exact code for the eFighter object:

    eFighter = ImageList.LoadImage("http://mattjessee.webs.com/enemyFighter.gif")

    Once i get everything all set to go i intend to convert it to a local file which i did without error earlier.

    Friday, January 14, 2011 7:24 PM
  • Nice graphic, it loads fine for me with your code.  Here is a simple moving test:

    'Graphics window dimensions
    gw = GraphicsWindow.Width
    gh = GraphicsWindow.Height
    'Load image and set it as a shape
    eFighter = ImageList.LoadImage("http://mattjessee.webs.com/enemyFighter.gif")
    enemies[0] = Shapes.AddImage(eFighter)
    'Width and height of image - make these the half width etc because that is what we need later
    enemiesWidth[0] = ImageList.GetWidthOfImage(eFighter)/2
    enemiesHeight[0] = ImageList.GetHeightOfImage(eFighter)/2
    'Initial Position
    enemiesX[0] = 300
    enemiesY[0] = 200
    'Initial velocities
    enemiesU[0] = 5 'The horizontal speed
    enemiesV[0] = 0 'The vertical speed

    While ("True")
      'Update the shape centre position based on velocity
      enemiesX[0] = enemiesX[0]+enemiesU[0]
      enemiesY[0] = enemiesY[0]+enemiesV[0]
      'Bounce the shape when it his a side wall - only handle the horizontal direction here since enemiesY is constant
      If (enemiesX[0] < enemiesWidth[0]) Then
        enemiesU[0] = -enemiesU[0]
        enemiesX[0] = enemiesWidth[0]
      EndIf
      If (enemiesX[0] > gw-enemiesWidth[0]) Then
        enemiesU[0] = -enemiesU[0]
        enemiesX[0] = gw-enemiesWidth[0]
      EndIf
      'The top left of the shapes are the coordinates requires - so the centre is at (enemiesX[0],enemiesY[0])
      Shapes.Move(enemies[0], enemiesX[0]-enemiesWidth[0], enemiesY[0]-enemiesHeight[0])
      Program.Delay(20) 'Small delay in the movement  - about 50 frames per sec
    EndWhile

    • Marked as answer by Kai Karden Saturday, January 15, 2011 7:15 AM
    Friday, January 14, 2011 7:44 PM
  • You, sir, are a god. Thank you very much, i've learned more about small basic from reading your code than i have from anything else. that is a very interesting idea of making it function. I'll have to keep that in mind for future reference. Greatly appreciated!
    Saturday, January 15, 2011 4:38 AM