Computer Science Canada

Collision problems - Need Lasers to hit and destroy enemies

Author:  Tamaranian [ Wed Jun 12, 2013 8:00 pm ]
Post subject:  Collision problems - Need Lasers to hit and destroy enemies

What is it you are trying to achieve?
I'm trying to make lasers shoot out of the player's sprite by pressing the space bar (So far, I've got that right). Then, the lasers should collide with enemies, and make them disappear, while also adding 25 points to the score.

What is the problem you are having?
When the lasers hit the enemies, they don't disappear and nothing is added to the score. The coordinates of the two sprites appear to be correct, but I can't get anything to happen when they touch. Also, when the user clicks "main menu" to restart the game when they lose, collision doesn't work at all in the new game.


Describe what you have tried to solve this problem
I've tried changing collision coordinates and moving loops and code around, but I just don't understand how to fix it. I'm fairly new to this :/


Please specify what version of Turing you are using
Turing 4.1.1

Author:  Insectoid [ Wed Jun 12, 2013 8:38 pm ]
Post subject:  RE:Collision problems - Need Lasers to hit and destroy enemies

code:
if (LaserX + 60 > planeX - 60) and (LaserX - 60 < planeX + 60) and (LaserY + 35 > planeY - 40) and (LaserY - 35 < planeY + 40) then
                     score := score + 25
                    
                    
                     end if


Right now your laser isn't doing anything because you haven't told it to (other than to increase the score). Your code here looks fine to me, but since you didn't provide any images I can't test it. To confirm that it is actually working, add a put statement inside that if statement, and if it writes to the screen you know the collision is indeed detected.

So now you need to add the code to destroy an enemy. It looks like right now you only have one plane on the screen at a time, so I'm guessing that you want to spawn a new plane when the current one dies. This can be done simply by changing the plane's coordinates to some predefined spawning location (off-screen perhaps?) and drawing it there instead.

I found a few issues in your code other than collision.
code:
picGAME := Pic.FileNew ("NEW GAME BACKGROUND.JPG")
        picGAME := Pic.Scale (picGAME, maxx, maxy)


You have these lines several times in the same loop, even though they haven't changed. Why? This will slow your program down quite a bit. Just load it once at the beginning, scale it, and then use it in the loop.

Author:  Tamaranian [ Wed Jun 12, 2013 8:50 pm ]
Post subject:  Re: RE:Collision problems - Need Lasers to hit and destroy enemies

Insectoid @ Wed Jun 12, 2013 8:38 pm wrote:
code:
if (LaserX + 60 > planeX - 60) and (LaserX - 60 < planeX + 60) and (LaserY + 35 > planeY - 40) and (LaserY - 35 < planeY + 40) then
                     score := score + 25
                    
                    
                     end if


Right now your laser isn't doing anything because you haven't told it to (other than to increase the score). Your code here looks fine to me, but since you didn't provide any images I can't test it. To confirm that it is actually working, add a put statement inside that if statement, and if it writes to the screen you know the collision is indeed detected.

So now you need to add the code to destroy an enemy. It looks like right now you only have one plane on the screen at a time, so I'm guessing that you want to spawn a new plane when the current one dies. This can be done simply by changing the plane's coordinates to some predefined spawning location (off-screen perhaps?) and drawing it there instead.

I found a few issues in your code other than collision.
code:
picGAME := Pic.FileNew ("NEW GAME BACKGROUND.JPG")
        picGAME := Pic.Scale (picGAME, maxx, maxy)


You have these lines several times in the same loop, even though they haven't changed. Why? This will slow your program down quite a bit. Just load it once at the beginning, scale it, and then use it in the loop.


Sorry about the pictures, is there a way I can still post them? It looks like I can only attach things one at a time atm.

Author:  Insectoid [ Wed Jun 12, 2013 8:59 pm ]
Post subject:  RE:Collision problems - Need Lasers to hit and destroy enemies

Upload it as a zip file.

Author:  Tamaranian [ Wed Jun 12, 2013 8:59 pm ]
Post subject:  RE:Collision problems - Need Lasers to hit and destroy enemies

code:
if (LaserX + 60 > planeX - 60) and (LaserX - 60 < planeX + 60) and (LaserY + 35 > planeY - 40) and (LaserY - 35 < planeY + 40) then
                     score := score + 25
                     Sprite.SetPosition (Plane, 10000, 10000, true)
                   
                     end if


This doesn't seem to work either. Have I done this wrong?

Author:  Tamaranian [ Wed Jun 12, 2013 9:03 pm ]
Post subject:  Re: Collision problems - Need Lasers to hit and destroy enemies

Alright, here it is.

Author:  Insectoid [ Wed Jun 12, 2013 9:56 pm ]
Post subject:  Re: RE:Collision problems - Need Lasers to hit and destroy enemies

Tamaranian @ Wed Jun 12, 2013 8:59 pm wrote:
code:
if (LaserX + 60 > planeX - 60) and (LaserX - 60 < planeX + 60) and (LaserY + 35 > planeY - 40) and (LaserY - 35 < planeY + 40) then
                     score := score + 25
                     Sprite.SetPosition (Plane, 10000, 10000, true)
                   
                     end if


This doesn't seem to work either. Have I done this wrong?


You moved the sprite, but you didn't change it's X or Y variables, so the next time the loop iterates it's going to run Sprite.SetPosition (Plane, planeX, planeY, true) and move right back to where it was.
Also, this is going to move your plane to a ridiculous location (10000, 10000), which means you'll probably never see it again. Can you think of a better place to put it when it gets hit?

Also, I suggest putting the game over screen into its own loop. As it is, I can still move and shoot lasers in the game over screen even though the sprite is hidden. Your code should look like this:

code:

%set up variables

procedure mainMenu
    loop
    %draw buttons, detect clicks, etc
    if 'start game' is clicked then
        %run instruction procedure
        %run game procedure
        %run game over screen procedure (or win screen, as required)
    end if
    if 'quit' button is clicked then
        exit
    end if
    %continue for other buttons
    end loop
end mainMenu

procedure instructions
    loop
        %draw instructions
        %exit when start button is clicked
    end loop
end instructions

procedure gameOverScreen
    loop
        %draw the screen
        %exit when 'main menu' button is clicked
    end loop
end gameOverScreen

procedure game
    %initialize all X & Y values, score, etc. Variables that need to reset every time the game is restarted.
    loop
    cls
    %check for input, adjust x/y values accordingly
    %move AI characters
    %check for collisions and deal with them
    %draw everything
    View.Update
    %delay as necessary
    exit when life <= 0 %or whatever condition the game ends under
    end loop
end game

mainMenu


This way, your code is very easy to read and it's obvious what's going on. Everything is organized, and flows naturally. Starting from the main menu, if you click on 'start game', it will display the instructions, then quit back to the main menu where the main game procedure is immediately run. When that finishes, it quits back to the main menu before starting the game over screen. Note that the main menu is never displayed between these procedures, since it all happens inside the same if statement. You can easily re-start the game, since all of the required variables are reset to their initial values right at the top of the game procedure. You can now easily modify the menus, instructions, or game over screens without worrying about anything else breaking. You can change the functionality of the game in drastic ways without touching any other parts of the program. It will be a lot of work to move your program over to this format, but I guarantee it will be worth it, since it will solve a lot of your issues and make everything easier to tweak and edit later on (and your teacher will have a much easier time marking it and probably give you a way higher mark for it).

Author:  Tamaranian [ Wed Jun 12, 2013 10:02 pm ]
Post subject:  RE:Collision problems - Need Lasers to hit and destroy enemies

Thanks, I'll try rearranging it Smile


: