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

Username:   Password: 
 RegisterRegister   
 Not Understanding Colission
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Pacman




PostPosted: Fri Nov 24, 2006 1:35 am   Post subject: Not Understanding Colission

I read tutorials etc and im still not understanding the whole colision/detection thing. A little help would be much appreciated, I need my Pacman (the one that chomps and move with arrows keys) to not just run over the dots and leave a black streak behind but to also detect he has and give 10 points or w/e for each, just a little explanation would be perfect. He is the code so far, i dont have all of the dots put in, just a few, i can make the rest later.

code:

setscreen ("graphics:490;540,nobuttonbar")
View.Update
colorback (black)
cls
color (white)
%Normal For Game/Maze
const backgrnd : int := black
const pacrad : int := 11
const wall : int := 30
const room : int := 4
var direction : int := 360
var x : int := 245
var y : int := 230
var x_temp, y_temp : int := 0
var xInc, yInc : int := 0
var mouthangl : int := 0
var move : string (1) := KEY_RIGHT_ARROW
const DEL : int := 15
%Controls Font
var font1 : int
font1 := Font.New ("arial:15")
%Created By Font
var font2 : int
font2 := Font.New ("arial:8")

%Copy, At Beginning ------------
const backgrnd2 : int := black
const pacrad2 : int := 11
var direction2 : int := 360
var x2 : int := 245
var y2 : int := 230
var x_temp2, y_temp2 : int := 0
var xInc2, yInc2 : int := 0
var mouthangl2 : int := 0



%GAME PROCEDURES----------------------------------------------
procedure walls
    colourback (backgrnd)
    cls
    %Draw Walls
    var box1x : array 1 .. 49 of int := init (0, 0, 0, 490, 0, 480, 50, 140, 190, 240, 290, 340, 0, 440, 50, 90, 140, 290, 390, 390, 0, 90, 0, 140, 190, 240, 340, 390, 390, 400, 0, 90, 0, 390, 390,
        390,
        140, 340, 140, 290, 50, 390, 190, 240, 50, 140, 290, 390, 240)
    var box1y : array 1 .. 49 of int := init (0, 0, 530, 260, 300, 300, 50, 50, 100, 100, 50, 60, 100, 100, 150, 160, 160, 160, 160, 160, 210, 260, 260, 260, 210, 210, 260, 260, 260, 200, 300, 360,
        350,
        350, 300, 300, 300, 300, 350, 350, 400, 400, 400, 410, 450, 450, 450, 450, 450)
    var box2x : array 1 .. 49 of int := init (10, 490, 490, 480, 10, 490, 200, 150, 300, 250, 440, 350, 50, 490, 100, 100, 200, 350, 400, 440, 100, 100, 90, 150, 300, 250, 350, 480, 400, 480, 100,
        100,
        100, 480, 480, 400, 150, 350, 200, 350, 100, 440, 300, 250, 100, 200, 350, 440, 250)
    var box2y : array 1 .. 49 of int := init (260, 10, 540, 0, 540, 540, 60, 110, 110, 50, 60, 110, 110, 110, 160, 100, 150, 150, 100, 150, 200, 200, 250, 200, 200, 150, 200, 250, 200, 210, 310, 310,
        360, 360, 310, 360, 410, 410, 360, 360, 410, 410, 410, 350, 490, 490, 490, 490, 540)

    for i : 1 .. 49
        drawfillbox (box1x (i), box1y (i), box2x (i), box2y (i), brightblue)
    end for
    for i : 45 .. 48
        drawfillbox (box1x (i) + 10, box1y (i) + 10, box2x (i) - 10, box2y (i) - 10, black)
    end for
end walls

%Mouth Opening and closing....

%Draw mouth close
procedure drawMouthClose (x : int, y : int)
    mouthangl := mouthangl mod 45 + 1
    drawfillarc (x, y, pacrad, pacrad, mouthangl + direction, -mouthangl + direction, 14)
end drawMouthClose



%Erase Pacman
procedure erasePacman (x : int, y : int)
    drawfillarc (x, y, pacrad, pacrad, mouthangl + direction, -mouthangl + direction, backgrnd)
end erasePacman

%Change Direction
procedure directionChange
    if hasch then
        getch (move)
        if ord (move) = ORD_DOWN_ARROW then
            direction := 270
            xInc := 0
            yInc := -1
        elsif ord (move) = ORD_UP_ARROW then
            direction := 90
            xInc := 0
            yInc := 1
        elsif ord (move) = ORD_LEFT_ARROW then
            direction := 180
            xInc := -1
            yInc := 0
        elsif ord (move) = ORD_RIGHT_ARROW then
            direction := 0
            xInc := 1
            yInc := 0

        end if
    end if
end directionChange
%-------------------------------------------------------------

% MUSIC/SOUNDS------------------------------------------------
procedure musicbegin
    Music.PlayFile ("GAMEBEGINNING.wav")
end musicbegin
procedure soundchomp
loop
    Music.PlayFile ("pacchomp.wav")
    end loop
end soundchomp
%-------------------------------------------------------------


%Mouth Opening And Closing For The Main Menu (not working)-----------------------------------
procedure drawMouthClose2 (x2 : int, y2 : int)
    mouthangl2 := mouthangl2 mod 45 + 1
    drawfillarc (x2, y2, pacrad2, pacrad2, mouthangl2 + direction2, -mouthangl2 + direction2, 14)
end drawMouthClose2

%Erase Pacman
procedure erasePacman2 (x : int, y : int)
    drawfillarc (x2, y2, pacrad2, pacrad2, mouthangl2 + direction2, -mouthangl2 + direction2, backgrnd2)
end erasePacman2

%Movement Mouth Speed
const DEL2 : int := 8

procedure paceat
    loop
        drawMouthClose2 (x2, y2)
        delay (DEL2)
        erasePacman2 (x2, y2)
        x_temp2 := x2 + xInc2
        y_temp2 := y2 + yInc2
    end loop
end paceat

%Start Menu
procedure gui12
    %Background -----------------------
    const picesizeb : int := 20

    var picb : int := Pic.FileNew ("the best pic ever.bmp")
    var newPicb : int
    var picWidthb, picHeightb : int
    picWidthb := Pic.Width (picb)
    picHeightb := Pic.Height (picb)
    newPicb := Pic.Scale (picb, picWidthb div 1, picHeightb div 1)
    Pic.Draw (newPicb, maxx div 2000 + 2, maxy div 1000 + 1, picCopy)
    Pic.Free (newPicb)

    % Start Button -------------------
    const picesize : int := 20

    var pic : int := Pic.FileNew ("startgame.bmp")
    var newPic : int
    var picWidth, picHeight : int
    picWidth := Pic.Width (pic)
    picHeight := Pic.Height (pic)
    newPic := Pic.Scale (pic, picWidth div .8, picHeight div .8)
    Pic.Draw (newPic, maxx div 2.6 + 10, maxy div 5 + 60, picCopy)
    Pic.Free (newPic)

    % Controls Button --------------
    const picesize2 : int := 20

    var pic2 : int := Pic.FileNew ("controls.bmp")
    var newPic2 : int
    var picWidth2, picHeight2 : int
    picWidth2 := Pic.Width (pic2)
    picHeight2 := Pic.Height (pic2)
    newPic2 := Pic.Scale (pic2, picWidth2 div .8, picHeight2 div .8)
    Pic.Draw (newPic2, maxx div 2.5 + 10, maxy div 5 + 30, picCopy)
    Pic.Free (newPic2)

    %Exit Button ----------------
    const picesize3 : int := 20

    var pic3 : int := Pic.FileNew ("exit2.bmp")
    var newPic3 : int
    var picWidth3, picHeight3 : int
    picWidth3 := Pic.Width (pic3)
    picHeight3 := Pic.Height (pic3)
    newPic3 := Pic.Scale (pic3, picWidth3 div .8, picHeight3 div .8)
    Pic.Draw (newPic3, maxx div 2.32 + 10, maxy div 5 + 1, picCopy)
    Pic.Free (newPic3)

    Font.Draw ("Created By pacmaN & Sinergy", 290, 5, font2, brightred)
end gui12

var c, d, button, button2 : int
gui12

%Mainline
loop
    mousewhere (c, d, button)
    if button = 1 and 200 <= c and c <= 295 and 185 >= d and d >= 168 then
        cls
        walls
        musicbegin
        walls
        %Draws Dots--------------------------------------------------------
        %Draws Point Dots
        for draw1 : 1 .. 15
            drawfilloval (30 * draw1, 30, 5, 5, yellow)
        end for
        drawfilloval (30, 55, 5, 5, yellow)
        drawfilloval (30, 80, 5, 5, yellow)
        drawfilloval (60, 80, 5, 5, yellow)
        drawfilloval (90, 80, 5, 5, yellow)
        for draw2 : 3 .. 17
            drawfilloval (120, 30 * draw2, 5, 5, yellow)
        end for
        drawfilloval (220, 55, 5, 5, yellow)
        drawfilloval (220, 80, 5, 5, yellow)
        drawfilloval (195, 80, 5, 5, yellow)
        drawfilloval (170, 80, 5, 5, yellow)
        drawfilloval (170, 105, 5, 5, yellow)
        for draw3 : 5 .. 12
            drawfilloval (30 * draw3, 130, 5, 5, yellow)
        end for

        %Draws Booster Dots
        drawfilloval (30, 130, 10, 10, brightgreen)
        drawfilloval (460, 130, 10, 10, brightgreen)
        drawfilloval (460, 470, 10, 10, brightgreen)
        drawfilloval (30, 470, 10, 10, brightgreen)
        %-----------------------------------------------------------------
        loop
            drawMouthClose (x, y)
            View.Update
            delay (DEL)
            erasePacman (x, y)
            x_temp := x + xInc
            y_temp := y + yInc
            directionChange
            if x_temp = 15 then
                x := 475
            elsif x_temp = 475 then
                x := 15
            end if
            if whatdotcolor (x_temp + pacrad, y_temp) ~= brightblue
                    and whatdotcolor (x_temp + pacrad + room, y_temp) ~= brightblue
                    and whatdotcolor (x_temp - pacrad - room, y_temp) ~= brightblue
                    and whatdotcolor (x_temp, y_temp + pacrad + room) ~= brightblue
                    and whatdotcolor (x_temp, y_temp - pacrad - room) ~= brightblue
                    and whatdotcolor (x_temp + pacrad + room, y_temp + pacrad + room) ~= brightblue
                    and whatdotcolor (x_temp - pacrad - room, y_temp - pacrad - room) ~= brightblue
                    and whatdotcolor (x_temp - pacrad - room, y_temp + pacrad + room) ~= brightblue
                    and whatdotcolor (x_temp + pacrad + room, y_temp - pacrad - room) ~= brightblue
                    and whatdotcolor (x_temp + pacrad, y_temp + pacrad + room) ~= brightblue
                    and whatdotcolor (x_temp - pacrad, y_temp - pacrad - room) ~= brightblue
                    and whatdotcolor (x_temp - pacrad, y_temp + pacrad + room) ~= brightblue
                    and whatdotcolor (x_temp + pacrad, y_temp - pacrad - room) ~= brightblue
                    and whatdotcolor (x_temp + pacrad + room, y_temp + pacrad) ~= brightblue
                    and whatdotcolor (x_temp - pacrad - room, y_temp - pacrad) ~= brightblue
                    and whatdotcolor (x_temp - pacrad - room, y_temp + pacrad) ~= brightblue
                    and whatdotcolor (x_temp + pacrad + room, y_temp - pacrad) ~= brightblue
                    then
                x := x + xInc
                y := y + yInc
            end if
        end loop
        x := x + xInc
        y := y + yInc

    elsif button = 1 and 200 <= c and c <= 295 and 155 >= d and d >= 138 then
        cls
        Font.Draw ("Use Your Arrow Keys, Up Down Left Right,", 5, 400, font1, yellow)
        Font.Draw ("Move Your Pacman To Eat All The Dots", 5, 370, font1, brightred)
        Font.Draw ("Collect All Of The Dots And Move To The Next Level!", 5, 340, font1, yellow)
        Font.Draw ("Collect Green Dots To Get A Booster Pac!", 5, 310, font1, brightred)
        Font.Draw ("Back To Main", 5, 100, font1, brightred)
        drawbox (1, 90, 140, 120, white)
    end if
    exit when button = 1 and 200 <= c and c <= 295 and 110 <= d and d <= 127
end loop 
Sponsor
Sponsor
Sponsor
sponsor
TokenHerbz




PostPosted: Fri Nov 24, 2006 5:34 am   Post subject: (No subject)

"picWidthb := Pic.Width (picb)" = error...

Im going to post a little program to which explains detection later.
TokenHerbz




PostPosted: Fri Nov 24, 2006 7:07 am   Post subject: (No subject)

here guess i was bored enough to write a crappy program to which you may not get :S but here it is anyways.

code:

setscreen ("Graphics: max;max, offscreenonly")

type circle :
    record
        x, y : int      %%position
        radius : int    %%side
        vx, vy : int    %%movement
    end record

var ball : flexible array 1 .. 4 of circle

for i : 1 .. upper (ball) %% for the balls
    ball (i).radius := 10 %%set size to 10
    ball (i).x := Rand.Int (ball (i).radius, maxx - ball (i).radius) %%randomizing placment
    ball (i).y := Rand.Int (ball (i).radius, maxy - ball (i).radius)
    ball (i).vx := Rand.Int (-3, 3)  %%randomizing direction
    ball (i).vy := Rand.Int (-3, 3)
end for

%%user settings:
var pacman : circle
pacman.radius := 30
pacman.x := 100
pacman.y := 100
pacman.vx := 0
pacman.vy := 0

var keys : array char of boolean
%%%to keep pacman moving.
var mr, ml, mu, md : boolean := false

proc move
    Input.KeyDown (keys)

    if keys (KEY_UP_ARROW) then
        mr := false
        ml := false
        mu := true
        md := false
    elsif keys (KEY_DOWN_ARROW) then
        mr := false
        ml := false
        mu := false
        md := true
    elsif keys (KEY_RIGHT_ARROW) then
        mr := true
        ml := false
        mu := false
        md := false
    elsif keys (KEY_LEFT_ARROW) then
        mr := false
        ml := true
        mu := false
        md := false
    end if

    %%will keep pacman traviling right direction.
    if mr = true then
        pacman.x += 5
    elsif ml = true then
        pacman.x += -5
    elsif mu = true then
        pacman.y += 5
    elsif md = true then
        pacman.y += -5
    end if

    for i : 1 .. upper (ball)
        ball (i).x += ball (i).vx
        ball (i).y += ball (i).vy
    end for
end move

proc boundries %%the boundries will make obsticles stay in the page, either teleporting it, or bouncing it off the sides.
    for i : 1 .. upper (ball)
        if ball (i).x - ball (i).radius <= 0 or ball (i).x + ball (i).radius >= maxx then
            ball (i).vx *= -1       %%reverse direction.  (u can teleport it, or do w/e else you want)
        end if
        if ball (i).y - ball (i).radius <= 0 or ball (i).y + ball (i).radius >= maxy then
            ball (i).vy *= -1
        end if
    end for

    %%didn't impiment pacman with boundries
end boundries

proc collition
    %%this will tell you if you collide with your ball

    %%here we go...
    for i : 1 .. upper (ball)
    %%for more acurate colltion, calculate in the speeds of both pacman and the ball.
    %%you could go further also, to calculate exactuly when the radius of both obsticle touch from any point on each of them.
   
        if ball (i).y - ball (i).radius <= pacman.y + pacman.radius and  %%ball bottom into pacman top and
                ball (i).y + ball (i).radius >= pacman.y - pacman.radius and %%ball top more then pacman bottom and
                ball (i).x + ball (i).radius <= pacman.x + pacman.radius and %%ball right less then pacman right and
                ball (i).x - ball (i).radius >= pacman.x - pacman.radius then %%ball left is more then pacman left  then

            %%we need to remove the ball
            ball (i) := ball (upper (ball))
            new ball, upper (ball) - 1
            exit
        end if
    end for
end collition

proc draw
    Draw.FillOval (pacman.x, pacman.y, pacman.radius, pacman.radius, yellow)
    for i : 1 .. upper (ball)
        Draw.FillOval (ball (i).x, ball (i).y, ball (i).radius, ball (i).radius, black)
    end for

    %%pac man points (shown in black)
    Draw.FillOval (pacman.x, pacman.y, 5, 5, black)     %%center of your pacman
    Draw.FillOval (pacman.x + pacman.radius, pacman.y, 1, 1, black) %%right of your pacman
    Draw.FillOval (pacman.x - pacman.radius, pacman.y, 1, 1, black) %%left of your pacman
    Draw.FillOval (pacman.x, pacman.y + pacman.radius, 1, 1, black) %%top
    Draw.FillOval (pacman.x, pacman.y - pacman.radius, 1, 1, black) %%bottom
end draw

loop
    Input.KeyDown (keys)
    cls

    move
    boundries
    collition
    draw

    View.Update
    delay (20)
end loop
TokenHerbz




PostPosted: Fri Nov 24, 2006 7:09 am   Post subject: (No subject)

oh, and may i suggest you drawing your map with .txt files...

Much easier IMO.
Pacman




PostPosted: Sun Nov 26, 2006 3:54 pm   Post subject: (No subject)

im breaking the program you showed down into parts to try and understand, and i sort of do but im having troubles implimenting it with my program, tried drawing my dots with for and proc statements but its a little confusing, and its getting me angry lmao
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: