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

Username:   Password: 
 RegisterRegister   
 Collision Detection - Help... Again
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
jolly50




PostPosted: Mon Nov 19, 2007 9:51 pm   Post subject: Collision Detection - Help... Again

Ok, so i'm restarting this thread because I have a new game and has a lot of upgrade since before...but I have problem with my health bar and collision can someone tell me how I can fix this...

and could you please you please use example bits of code to explain because I am very new to turing and only know basics and what have my peers have helped me with this game...

This game is not stolen from anyone! Just to be clear on that!...i wrote this code word for word with the help of my teacher and friends!...

code:


import GUI
 
setscreen ("graphics:1010;400")
View.Set ("offscreenonly")

var mario_x1, mario_y1, mario_x2, mario_y2: int
var luigi_x1, luigi_y1, luigi_x2, luigi_y2: int
var vx, vy :int
var chars :array char of boolean
var timeLast := Time.Elapsed
var proj : flexible array 1 .. 0 of
    record
        x, y, vx, vy : real
    end record
var finished: boolean:= false
var left: int


var goomba_x1, goomba_x2, goomba_y1, goomba_y2 :int
var koopa_x1, koopa_x2, koopa_y1, koopa_y2, koopa_vy: int
var bowser_x1, bowser_x2, bowser_y1, bowser_y2, bowser_vy, bowser_vx: int

var playerdirection: int:=0

var CurrentFrame:int:= Pic.FileNew ("Mario1.bmp")
var CurrentFrameLeft:int:=Pic.Mirror (CurrentFrame)
var RunningRight:int:= Pic.FileNew ("mario_run_right.bmp")
var RunningLeft:int:= Pic.Mirror (RunningRight)
var MarioJumpRight:int:=Pic.FileNew ("mario_jump.bmp")
var MarioJumpLeft:int:= Pic.Mirror (MarioJumpRight)
var mariocrouch:int:= Pic.FileNew ("mario_crouch.bmp")
var fireball:int:= Pic.FileNew ("fireball.bmp")

var goomba:int:= Pic.FileNew ("Goomba.bmp")
var Koopa:int:= Pic.FileNew ("Koopa.bmp")
var bowser:int:= Pic.FileNew ("Bowser.bmp")

var luigicurrentframe:int:=Pic.FileNew ("luigi_sprite.bmp")
var luigirunningright:int:=Pic.FileNew ("luigi_run.bmp")
var luigirunningleft:int:=Pic.Mirror (luigirunningright)
var luigijumpright:int:=Pic.FileNew ("luigi_jump.bmp")
var luigijumpleft:int:=Pic.Mirror (luigijumpright)
var luigicrouch:int:=Pic.FileNew ("luigi_crouch.bmp")
var luigifireball:int:=Pic.FileNew ("luigi_fireball.bmp")

mario_x1:=15
mario_y1:=25
%mario_x2:=mario_x1+Pic.Width(CurrentFrame)
%mario_y2:=mario_y1+Pic.Height(CurrentFrame)

luigi_x1:=15
luigi_y1:=25
luigi_x2:=luigi_x1+Pic.Width(luigicurrentframe)
luigi_y2:=luigi_y1+Pic.Height(luigicurrentframe)

left:=70


goomba_x1:= maxx
goomba_y1:= 31
goomba_x2:=goomba_x1+Pic.Width(goomba)
goomba_y2:=goomba_y1+Pic.Height(goomba)


koopa_x1:= maxx
koopa_y1:=31
koopa_vy:=20
koopa_x2:=koopa_x1+Pic.Width(Koopa)
koopa_y2:=koopa_y1+Pic.Height(Koopa)


bowser_x1:=maxx
bowser_y1:=31
bowser_vy:=15
bowser_vx:=10
bowser_x2:=bowser_x1+Pic.Width(bowser)
bowser_y2:=bowser_y1+Pic.Height(bowser)

var mariosprite: int
mariosprite:=Sprite.New(CurrentFrame)

var luigisprite: int
luigisprite:=Sprite.New(luigicurrentframe)

var goombasprite: int
goombasprite:=Sprite.New(goomba)

var koopasprite: int
koopasprite:=Sprite.New(Koopa)

var bowsersprite: int
bowsersprite:=Sprite.New(bowser)

var mariorunright:int
mariorunright:=Sprite.New(RunningRight)

var jumping: boolean:= false

const gravity:=-1


Sprite.SetPosition (luigisprite,luigi_x1,luigi_y1,false)

Sprite.SetPosition (mariosprite,mario_x1,mario_y1,false)

Sprite.Show (goombasprite)

Sprite.SetPosition (goombasprite,goomba_x1,goomba_y1,false)

Sprite.Show (koopasprite)

Sprite.SetPosition (koopasprite,koopa_x1,koopa_y1,false)

Sprite.Show (bowsersprite)

Sprite.SetPosition (bowsersprite,bowser_x1,bowser_y1,false)

function cooldown (timeDelay : int) : boolean
    if Time.Elapsed - timeLast >= timeDelay then
        timeLast := Time.Elapsed
        result true
    end if
    result false
end cooldown

process PlayBackgroundMusic (file : string)

    loop
        Music.PlayFile (file)
        exit when finished
    end loop
   
end PlayBackgroundMusic

fork PlayBackgroundMusic ("Mario_Theme.MP3")

process fireballsound (file:string)

    Music.PlayFile (file)
end fireballsound

process jumpingsound (file:string)

    Music.PlayFile (file)

end jumpingsound

procedure controls

 
    Input.KeyDown (chars)
   
        if chars (KEY_RIGHT_ARROW) then
            mario_x1+=4
            playerdirection:=0
        end if
       
        if chars (KEY_LEFT_ARROW) then
            mario_x1-=4
            playerdirection:=180
        end if
       
        if chars (KEY_DOWN_ARROW) then
            mario_y1-=2
            Sprite.ChangePic (mariosprite,mariocrouch)
        end if
       
        if chars (KEY_DOWN_ARROW) and jumping then
            Sprite.ChangePic (mariosprite,mariocrouch)
        end if
       
        if not chars(KEY_DOWN_ARROW) then
            Sprite.ChangePic(mariosprite,CurrentFrame)
        end if
       
        if chars (KEY_UP_ARROW) and not jumping and mario_y1=27 then
            jumping:=true
            vy:=15
            Sprite.ChangePic (mariosprite, MarioJumpRight)
            fork jumpingsound ("mario jump.wav")
        end if
       
        if chars (' ') then
            fork fireballsound ("fireball.wav")
        end if
       
        if chars (' ') then
        if cooldown (300) then
            new proj, upper (proj) + 1
            if playerdirection=0 then
                proj (upper (proj)).x:= mario_x1+50
            elsif playerdirection=180 then
                proj (upper (proj)).x:= mario_x1
            end if
           
            proj (upper (proj)).y:= mario_y1+50
           
            if playerdirection=0 then
                proj (upper (proj)).vx:=16
            elsif playerdirection=180 then
                proj (upper(proj)).vx:=-16
            end if
           
            proj (upper (proj)).vy := 8
            timeLast:= Time.Elapsed
         end if
     end if
     
        if jumping then
            mario_x1+=0
            mario_y1+=vy
            vy+=gravity
            Sprite.ChangePic (mariosprite, MarioJumpRight)
       
            if mario_y1<= 27 then
                jumping:=false
                mario_y1:=27
                Sprite.ChangePic (mariosprite,CurrentFrame)
            end if
        end if
       
        if chars (KEY_LEFT_ARROW) and playerdirection=0 then
            CurrentFrame:=RunningRight
        end if
       
        if mario_y1<27 then
            mario_y1:=27
        end if
       
        if mario_x1>1370then
            mario_x1:=1370
        end if
       
        if mario_x1<0 then
            mario_x1:=0
        end if
       
        if chars (KEY_LEFT_ARROW) and jumping then
            Sprite.ChangePic (mariosprite, MarioJumpLeft)
        end if
           
   
    Sprite.SetPosition (mariosprite,mario_x1,mario_y1,false)
   
end controls

procedure luigicontrols

 
    Input.KeyDown (chars)
   
        if chars (KEY_RIGHT_ARROW) then
            luigi_x1+=4
            playerdirection:=0
        end if
       
        if chars (KEY_LEFT_ARROW) then
            luigi_x1-=4
            playerdirection:=180
        end if
       
        if chars (KEY_DOWN_ARROW) then
            luigi_y1-=2
            Sprite.ChangePic (luigisprite,luigicrouch)
        end if
       
        if chars (KEY_DOWN_ARROW) and jumping then
            Sprite.ChangePic (luigisprite,luigicrouch)
        end if
       
        if not chars(KEY_DOWN_ARROW) then
            Sprite.ChangePic(luigisprite,luigicurrentframe)
        end if
       
        if chars (KEY_UP_ARROW) and not jumping and luigi_y1=27 then
            jumping:=true
            vy:=15
            Sprite.ChangePic (luigisprite, luigijumpright)
            fork jumpingsound ("mario jump.wav")
        end if
       
        if chars (' ') then
            fork fireballsound ("fireball.wav")
        end if
       
        if chars (' ') then
        if cooldown (300) then
            new proj, upper (proj) + 1
            if playerdirection=0 then
                proj (upper (proj)).x:= luigi_x1+50
            elsif playerdirection=180 then
                proj (upper (proj)).x:= luigi_x1
            end if
           
            proj (upper (proj)).y:= luigi_y1+50
           
            if playerdirection=0 then
                proj (upper (proj)).vx:=16
            elsif playerdirection=180 then
                proj (upper(proj)).vx:=-16
            end if
           
            proj (upper (proj)).vy := 8
            timeLast:= Time.Elapsed
         end if
     end if
     
        if jumping then
            luigi_x1+=0
            luigi_y1+=vy
            vy+=gravity
            Sprite.ChangePic (luigisprite, luigijumpright)
       
            if luigi_y1<= 27 then
                jumping:=false
                luigi_y1:=27
                Sprite.ChangePic (luigisprite,luigicurrentframe)
            end if
        end if
       
        if chars (KEY_LEFT_ARROW) and playerdirection=0 then
            luigicurrentframe:=luigirunningright
        end if
       
        if luigi_y1<27 then
            luigi_y1:=27
        end if
       
        if luigi_x1>1370then
            luigi_x1:=1370
        end if
       
        if luigi_x1<0 then
            luigi_x1:=0
        end if
       
        if chars (KEY_LEFT_ARROW) and jumping then
            Sprite.ChangePic (luigisprite, luigijumpleft)
        end if
           
   
    Sprite.SetPosition (luigisprite,luigi_x1,luigi_y1,false)
   
end luigicontrols

procedure Goomba
    goomba_x1:=goomba_x1-7
        if goomba_x1<0 then
            goomba_x1:=maxx
        end if
end Goomba

procedure EnemyKoopa
    koopa_x1:=koopa_x1-5
    koopa_y1+=koopa_vy
    koopa_vy+=gravity
        if koopa_x1<0 then
            koopa_x1:=maxx
        end if
        if koopa_y1<27 then
            koopa_y1:=27
        end if
        if koopa_y1<=27 then
            koopa_vy:=20
        end if
       
end EnemyKoopa

procedure Bowser
    if bowser_x1>= maxx then
        bowser_vx :=-10
    elsif bowser_x1<235 then
        bowser_vx:=10
    elsif bowser_x1>=675 then
        bowser_vx:=-10
    end if
    bowser_y1+=bowser_vy
    bowser_vy+=gravity
    bowser_x1+=bowser_vx
       
       if bowser_y1<27 then
            bowser_y1:=27
       end if
       if bowser_y1<=27 then
            bowser_vy:=15
       end if
       
end Bowser

procedure MarioRun
 
    if chars (KEY_RIGHT_ARROW)and not jumping then
        Sprite.ChangePic (mariosprite,RunningRight)
    end if
   
    if chars (KEY_LEFT_ARROW)and not jumping then
        Sprite.ChangePic (mariosprite,RunningLeft)
    end if
   
end MarioRun

procedure LuigiRun
 
    if chars (KEY_RIGHT_ARROW)and not jumping then
        Sprite.ChangePic (luigisprite,luigirunningright)
    end if
   
    if chars (KEY_LEFT_ARROW)and not jumping then
        Sprite.ChangePic (luigisprite,luigirunningleft)
    end if
   
end LuigiRun

procedure updateproj

for i : 1 .. upper (proj)
        proj (i).vy += gravity
        proj (i).x += proj (i).vx
        proj (i).y += proj (i).vy
    end for

    for i : 1 .. upper (proj)
        if proj (i).x > maxx or proj (i).y < 0 or proj (i).x < 0 then
            proj (i) := proj (upper (proj))
            new proj, upper (proj) - 1
            exit
        end if
    end for
 for i : 1 .. upper (proj)
        Pic.Draw (fireball,round (proj (i).x), round (proj (i).y),picCopy)
    end for

end updateproj

procedure luigiupdateproj

for i : 1 .. upper (proj)
        proj (i).vy += gravity
        proj (i).x += proj (i).vx
        proj (i).y += proj (i).vy
    end for

    for i : 1 .. upper (proj)
        if proj (i).x > maxx or proj (i).y < 0 or proj (i).x < 0 then
            proj (i) := proj (upper (proj))
            new proj, upper (proj) - 1
            exit
        end if
    end for
 for i : 1 .. upper (proj)
        Pic.Draw (luigifireball,round (proj (i).x), round (proj (i).y),picCopy)
    end for

end luigiupdateproj



procedure MarioGame

Sprite.Show (mariosprite)

loop   
    cls
    drawfillbox (0,0,maxx,30,green)
    % Health Bar and Collision Detection

    mario_x2:=mario_x1+Pic.Width(CurrentFrame)
    mario_y2:=mario_y1+Pic.Height(CurrentFrame)
    if mario_x2 > goomba_x1 and mario_x1 < goomba_x2 and mario_y2 > goomba_y1 and mario_y1 < goomba_y2 then
          drawfillbox (0,380,70,maxy,brightblue)
    end if
    Input.KeyDown (chars)
    controls
    MarioRun
    updateproj
    Goomba
    %EnemyKoopa
    %Bowser
    Sprite.SetPosition (mariosprite,mario_x1,mario_y1,false)
    Sprite.SetPosition (goombasprite,goomba_x1,goomba_y1,false)
    Sprite.SetPosition (koopasprite,koopa_x1,koopa_y1,false)
    Sprite.SetPosition (bowsersprite,bowser_x1,bowser_y1,false)
    View.Update
    delay (15)
end loop

end MarioGame

procedure LuigiGame

Sprite.Show (luigisprite)

loop
    cls
    drawfillbox (0,0,maxx,30,green)
    drawfillbox (0,380,70,maxy,brightblue)
    Input.KeyDown (chars)
    luigicontrols
    LuigiRun
    luigiupdateproj
    %EnemyKoopa
    Goomba
    Sprite.SetPosition (luigisprite,luigi_x1,luigi_y1,false)
    Sprite.SetPosition (goombasprite,goomba_x1,goomba_y1,false)
    Sprite.SetPosition (koopasprite,koopa_x1,koopa_y1,false)
    Sprite.SetPosition (bowsersprite,bowser_x1,bowser_y1,false)
    % Some collision detection
        if luigi_x2 > goomba_x1 and luigi_x1 < goomba_x2 and luigi_y2 > goomba_y1 and luigi_y1 < goomba_y2 then
           drawfillbox (70,maxy,60,380,white)
        end if
    View.Update
    delay (15)
end loop
end LuigiGame

procedure HelpMenu
cls
put "To 'Jump' Press the UP arrow key"
put " "
put "To 'Run' press the LEFT and RIGHT arrow keys"
put " "
put "To 'Crouch' press the DOWN arrow key"
put " "
put "To 'Shoot Fireballs' press the SPACEBAR"
put " "
put "To Play, Please click on one of the following buttons to play as either Mario or Luigi!"
put " "
put "Enjoy The Game :)"
var button1:int:=GUI.CreateButton (250,150,0, "Play Game as Mario", MarioGame)
var button2:int:=GUI.CreateButton (400,150,0, "Play Game as Luigi", LuigiGame)

end HelpMenu



var backgroundpicture:int:=Pic.FileNew ("Super_Mario_64_box_cover copy2.jpg")

Pic.Draw (backgroundpicture,225,0,0)

var button1:int:=GUI.CreateButton (250,5,0, "Play Game as Mario", MarioGame)
var button2:int:=GUI.CreateButton (400,5,0, "Play Game as Luigi", LuigiGame)
var button3:int:=GUI.CreateButton (550,5,0, "Instructions", HelpMenu)
loop
    exit when GUI.ProcessEvent
end loop



Two things....

1. How can I fix my health bar because right now all it does is draw a blue bar....I want it to have a blue bar already there then take bits off when mario touches an enemy!

2. How can I make it so that when i shoot fireballs they are in the collision detection so that they will end the goomba loop and change to the enemy koopa procedure and so on

Thanks for any help!
Sponsor
Sponsor
Sponsor
sponsor
HeavenAgain




PostPosted: Mon Nov 19, 2007 10:03 pm   Post subject: RE:Collision Detection - Help... Again

can you please take out all those code and put the pic and that file in a zip? plus pointing out where the problem might be, no body likes reading other people's code, especially they are long Embarassed
"helps us to help you" Smile
Zampano




PostPosted: Tue Nov 20, 2007 9:58 am   Post subject: Re: Collision Detection - Help... Again

Well, I didn't wade through the entire code but for the decreasing health bar, how about having a variable increase every time the character gets hit (you'll need one for each). Each time the character gets hit, the variable increases slightly and you drawfillbox a box int eh colour of the background of the health bar with the second x as the the end of the health bar and the first x be the end of the health bar - the variable.
Alternatively, you can have the variable actually be a manipulated form of your health variable, to save one.
So at the beginning of the game you would draw the health bart completely filled and later on it would look like:
code:

if mario got hit then
health := health - 5
drawfillbox ( the end of your health bar , arbitrary lower height, end of health bar - ( maxhealth - currenthealth ) , arbitrary higher height, 7) %7 is black which is the best background colour
Degensquared




PostPosted: Tue Nov 20, 2007 10:51 am   Post subject: Re: Collision Detection - Help... Again

For the collisions I'm not exactly sure what you want to do with it, but try getting familliar with the Math.Distance and Math.DistancePointLine functions.

Essentially, Math.Distance returns the distance in pixels between two points, and Math.DistancePointLine returns the distance in pixels between a point and a line.

Just do something like this to check if two circles are touching:
code:

if Math.Distance (circle1.x,circle1.y,circle2.x,circle2.y) <= circle1.radius + circle2.radius then
put "The balls collided!"
end if


This just checks how far they are away from each other, and then checks if that is less than or equal too their combined radii.

Degen.
jolly50




PostPosted: Tue Nov 20, 2007 9:22 pm   Post subject: Re: Collision Detection - Help... Again

Ok, so I have a list of stuff that I need to fix, but I don't know how to do it!

List:

Health Bar...Right now it just blinks when the goomba touches mario....
Collision Detection...if mario jumps over the goomba it still says that mario touched the goomba
Collision Detection...how do I make it so when the fireball touches a goomba it will change from the goomba procedure to the enemy koopa procedure and etc
Collision Detection...is there a way so that if mario is jumping and he lands on the goomba then it do the above (change procedure)

...that it for now....but there will be more! lol



Mario_Game.zip
 Description:

Download
 Filename:  Mario_Game.zip
 Filesize:  2.88 MB
 Downloaded:  533 Time(s)

jolly50




PostPosted: Tue Nov 20, 2007 9:26 pm   Post subject: RE:Collision Detection - Help... Again

oh...and there is a lot of lag when the firball shoots ... i think it is because of the sound file I put in
jolly50




PostPosted: Wed Nov 21, 2007 3:31 pm   Post subject: RE:Collision Detection - Help... Again

umm....nobody has responded yet so I thought I would show my updated changes...

For the Procedure MarioGame:

I added this for the collision detection

code:

goombaDamage:=70
...
...
...

mario_x2 := mario_x1 + Pic.Width (CurrentFrame)
        mario_y2 := mario_y1 + Pic.Height (CurrentFrame)
        drawfillbox (0, 380, goombaDamage, maxy, brightblue)
        if mario_y1<=goomba_y1+Pic.Height (goomba) and mario_x2 >= goomba_x1 and mario_x1 <= goomba_x2 and mario_y2 >= goomba_y1 and mario_y1 <= goomba_y2 then
            goombaDamage:=goombaDamage-1
        end if
             
        if goombaDamage=0 then
            Sprite.Hide (mariosprite)
            Sprite.Hide (goombasprite)
            cls
            locatexy (320,200)
                put "THE END"
            delay (1000)
            cls
            locatexy (320,200)
                put "Thanks You for Playing My Game!"
            delay (1000)
            cls
            locatexy (320,200)
                put "This Game is Built By Chris Jolliffe"
            delay (1000)
           
             
                fork endgamesound ("Game Over.wav")
           
            exit
         
        end if


But the only problem with the game is that after the goomba passes mario, mario still loses damage....
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  [ 7 Posts ]
Jump to:   


Style:  
Search: