Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Help with Firing bullets and collision detection
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Teh_Pwnerer




PostPosted: Mon Jun 20, 2005 2:17 pm   Post subject: Help with Firing bullets and collision detection

Note: What I couldn't fit in the title is that both things I need help with are pictures. Anyway, here I am again, seeing as how my account got deleted, and posting this again. I finally got my ship to fire a bullet (yay!) but there's a problem. I have to hold space bar to move the bullet. I want the bullet to go when I press the space bar once, I don't want the bullet just to stay there if I don't hold it. Then there's the collision detection thing. I have three pictures: a ship, an enemy that appears at different y-co-ordinates, and a bullet. I need to make it so that when the bullet hits the enemy, it disappears and the score goes up by 10, the latter of which I can do. Then I need to make it so that if the ship collides with the enemy, the screen goes black and it says "Game over". The latter of which I can also do. Then after 3 enemies are defeated there will be another level, where the enemies appear in larger numbers and move faster. After that level the game's over unless I get alot of help with the levels and stuff tonight cause I can't work on it tomorrow as it's due. This is my code so far:
code:
%Francesco Lo Presti
%Mr. Paterson
%June 10th, 2005
%Zero Wing 2k5

var bullety : int
var bulletx : int
bulletx := 00
bullety := 200
var bullet : int %variable for bullet pic
var continue : string %variable for the option to continue playing
var chars : array char of boolean %variable for the command that tells program to see if key is pressed
var zig : int %variable for the Zig Fighter picture
var enemy : int %variable for the Enemy picture
var x, y, x1, y1 : int %co-ordinate variables - x, y : Zig Fighter; x1, y1 : Enemy
var Shooting : boolean := false
var score : int %variable for the score (goes up by 100 every time an enemy is defeated)
score := 0 %sets the score to zero
x := 00 %sets Zig Fighter x co-ordinate to zero
y := 200 %sets Zig Fighter y co-ordinate to 200
x1 := 700 %sets enemy's x co-ordinate to 700
y1 := Rand.Int (10, 365) %makes the enemy's y co-ordinate random
View.Set ("offscreenonly") %sets the view to offscreen only... outputs things to the offscreen buffer
var font : int %variable for a new font
font := Font.New ("serif:40") %creates a new font


loop
    locate (1, 3)
    put "Score: ", score %puts the score on the screen
    bullet := Pic.FileNew ("zigbullet.jpg") %tells the program that the enemy var is a picture and tells it which picture
    enemy := Pic.FileNew ("enemy1.jpg") %tells the program that the enemy variable is a picture and tells it which picture
    zig := Pic.FileNew ("Zig Fighter fire.jpg") %tells the program that the zig variable is a picture and tells it which picture
    Pic.Draw (bullet, bulletx, bullety, picCopy)
    Pic.Free (bullet)
    Pic.Draw (zig, x, y, picCopy) %draws the picture on the screen using the previously determined x and y values
    Pic.Free (zig) %frees the memory taken up by the picture
    Input.KeyDown (chars) %tells the program to see if a key is pressed

    if chars (KEY_UP_ARROW) or chars ('w') then %tells the program to see if the up arrow key or "w" key is pressed
        y := y + 5 %tells the program that if the up arrow key is pressed, to move the picture up 5 pixels
        bullety := bullety + 5
        if y > 365 then %tells the program to see if the y co-ordinate has a greater value than 380
            y := y - 5 %tells the program that if the y co-ordinate has a greater value than 380 to move it down 5 pixels
            bullety := bullety - 5
        end if
    end if
    if chars (KEY_DOWN_ARROW) or chars ('s') then %tells the program to see if the down arrow key or "s" key is pressed
        y := y - 5 %tells the program that if so, to move the picture down 5 pixels
        bullety := bullety - 5
        if y < 0 then %tells the program to see if the y co-ordinate is less than 0
            y := y + 5 %tells the program that if the above is true, to move the picture up 5 pixels
            bullety := bullety + 5
        end if
    end if
        if chars (' ') then
            Shooting := true
            bulletx := bulletx + 10
        end if
        if bulletx > 600 then
            Shooting := false
            bulletx := -20
        end if

    Pic.Draw (enemy, x1, y1, picCopy) %draws the enemy picture on the screen using the pre-determined x and y co-ordinates
    Pic.Free (enemy) %frees up the space taken by the picture
    delay (50) %delays the procedure by 1/20th of a second
    View.Update %tells the program to take everything from the offscreen buffer and place it in the run window
    cls %clears the screen
    x1 := x1 - 5 %tells the program to continuously move the enemy picture to the left by 5 pixels
    if x1 < 0 then %tells the program to see if the enemy's x co-ordinate is less than zero
        x1 := 650 %tells the program that if the above is true, to reset the x co-ordinate to the value shown
        y1 := Rand.Int (10, 380) %tells the program to simultaneously change the y co-ordinate to a randomly chosen number
        %between 10 and 380 pixels
    end if
end loop

Sponsor
Sponsor
Sponsor
sponsor
vagyb




PostPosted: Mon Jun 20, 2005 8:37 pm   Post subject: Re: Help with Firing bullets and collision detection

Quote:
I have to hold space bar to move the bullet. I want the bullet to go when I press the space bar once, I don't want the bullet just to stay there if I don't hold it. Then there's the collision detection thing. I have three pictures: a ship, an enemy that appears at different y-co-ordinates, and a bullet. I need to make it so that when the bullet hits the enemy, it disappears and the score goes up by 10, the latter of which I can do. Then I need to make it so that if the ship collides with the enemy, the screen goes black and it says "Game over". The latter of which I can also do. Then after 3 enemies are defeated there will be another level, where the enemies appear in larger numbers and move faster. After that level the game's over unless I get alot of help with the levels and stuff tonight cause I can't work on it tomorrow as it's due. This is my code so far:


okay for one u need to do collison detections , i would suggest use whatdotcolor, make the bullet red and if the bullet x and y + like 10 or something = black (make the outside of astroids a nice thin black color) then exit, and pic.draw the broken up astroid. and also a cool trick to makin the bullet disapear is after u write whatdotcolor bla bla then bulletx: = -40 or something and bullety: = -50, so it takes bullet off screen. and also make a variable var score int:=0 and when the whatdotcolor collison occurs put then score += 10,. and also make a secondary counter for each enemy u kill and a variable for speed of astroids, and multiply all speeds of astroids by the speed. (var speed: int:= 0) and whenever the counter for killing enemies is >3 then change the speed to to 2 or something this way everythin speeds up.[/quote]
Cervantes




PostPosted: Tue Jun 21, 2005 10:01 am   Post subject: (No subject)

First, learn to use arrays. Then, learn to use flexible arrays. In that flexible arrays tutorial, there's an example of how to do bullet firing. There's also a collision detection tutorial. Really, it's all in the walkthrough. Just go through that. Smile
Mr. T




PostPosted: Mon Jun 27, 2005 2:17 am   Post subject: Alex's Opinion

Kinda hard to learn that all in one night, seeing as how the project was due the next day. Rolling Eyes
MysticVegeta




PostPosted: Fri Jul 29, 2005 7:33 am   Post subject: (No subject)

why not start a week earlier then Smile
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: