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

Username:   Password: 
 RegisterRegister   
 Whatdotcolor Collision help (script fixed)
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
sierius




PostPosted: Sun Nov 16, 2008 3:56 pm   Post subject: Whatdotcolor Collision help (script fixed)

code:

var win := Window.Open ("graphics:max;max;offscreenonly;nocursor")
const base := 60
const movement := 10
const jump_dis := 25
const gravity := 2
var keyslah : array char of boolean
var tcoins : nat := 0
var jumponly := true
% player position and velocity
var posx := 20
var velx := 0.0
var posy := base
var vely := 0.0

procedure drawbg
    drawline (0, base, maxx, base, 64)
    locate (2, 4)
    put "Coins collected : ", tcoins, "."
    locate (3, 4)
    put "Time elasped : "
end drawbg

procedure level_tut_1
    Draw.ThickLine (25, 180, 100, 180, 2, black)
    Draw.ThickLine(200, 220, 315, 220, 2, black)
    Draw.ThickLine(450, 240, 575, 240, 2, black)
    Draw.ThickLine(660, 260, 785, 260, 2, black)
end level_tut_1

loop
    cls
    Input.KeyDown (keyslah)
    if keyslah ('q') then
        exit
    end if
    % left and right keyt
    if keyslah (KEY_LEFT_ARROW) and posx - 10 > 0 then
        velx := -movement
    elsif keyslah (KEY_RIGHT_ARROW) and posx + 10 < maxx - 10 then
        velx := movement
    else
        velx := 0
    end if
    %% up key
    if keyslah (KEY_UP_ARROW) and jumponly = true then
        vely := jump_dis
        jumponly := false
    end if
    % subtract your gravity constant from the velocity
    if View.WhatDotColor (posx, posy - 1) not= black then
        vely -= gravity
        posx += round (velx)
        posy += round (vely)
    end if
    % on the ground ?
    if posy < base then
        posy := base
        vely := 0
        jumponly := true
    end if
    if View.WhatDotColor (posx, posy-1) = black then
        vely := 0
        vely += gravity
        jumponly := true
        delay (40)
    end if
    % change if colors if you can't jump
    if jumponly = true then
        drawfillbox (posx , posy, posx + 20, posy + 20, green)
    else
        drawfillbox (posx , posy, posx + 20, posy + 20, 79)
    end if
    %% just so I know where posx and posy are
    drawfilloval (posx, posy, 1, 1, 12)
    drawbg
    level_tut_1
    delay (25)
    View.Update
end loop

Window.Close (win)


Sorry about the previous post, I accidentally copied the wrong code then realized I could not edit.

The problem is I want the square to stay above the platforms (represented by the black lines) but by using whatdotcolor not only does it not stay above the platform it completely ignores the fact that its there.

Questions :

Can someone show me how to make square stay above the black platforms? Sorry if this is a nooby question but I've been only learning for a few months.

Do you suggest I use whatdotcolor to make my square stay above platforms or what other kind of codes?




Edit reason : note to readers
The variables coin and the the phrase "Time elasped :" are not finished because I was thinking of attempting to recreate a platformer game I found on this site once, they are fragmentations because I could now even get past the collision part, which I wanted to accomplish before moving into additional functions.
Edit2reason: forgot to code
Sponsor
Sponsor
Sponsor
sponsor
DanielG




PostPosted: Sun Nov 16, 2008 4:40 pm   Post subject: RE:Whatdotcolor Collision help (script fixed)

I didn't manage to fix your problem, but I found 2 things that could affect it. First, your gravity of 2 could cause you to miss them on the way down in your check. The second problem is that you checked the color before redrawing them. so you need to move drawbg and level_tut_1 up.
A.J




PostPosted: Sun Nov 16, 2008 6:16 pm   Post subject: Re: Whatdotcolor Collision help (script fixed)

I would say check for collision manually...

I have an example that might help you :

(The function you would want to check out in this example is the "checkIfOnLedge" function)

code:

type velocity :
    record
        x : real
        y : real
    end record
type ball_type :
    record
        x : real
        y : real
        r : int
        v : velocity
        c : int
    end record
type ledge :
    record
        x1 : int
        y1 : int
        x2 : int
        y2 : int
        c : int
    end record
const Gravity := 0.30
const Bounce_Factor := 1.25
var Key : array char of boolean
var inair := true
var balls : array 1 .. 1 of ball_type
var ledges : array 1 .. 3 of ledge
balls (1).x := maxx div 2
balls (1).y := maxy div 2
balls (1).r := 10
balls (1).v.y := 0
balls (1).c := brightmagenta

ledges (1).x1 := 100
ledges (1).y1 := 70
ledges (1).x2 := 200
ledges (1).y2 := 50
ledges (1).c := brightblue

ledges (2).x1 := 200
ledges (2).y1 := 170
ledges (2).x2 := 320
ledges (2).y2 := 150
ledges (2).c := brightred

ledges (3).x1 := 300
ledges (3).y1 := 270
ledges (3).x2 := 440
ledges (3).y2 := 250
ledges (3).c := brightgreen

proc drawball (var ball : ball_type)
    for i : 1 .. upper (ledges)
        drawfillbox (ledges (i).x1, ledges (i).y1, ledges (i).x2, ledges (i).y2, ledges (i).c)
    end for
    drawfilloval (floor (ball.x), floor (ball.y), ball.r, ball.r, ball.c)
end drawball

proc checkIfOnLedge (var ball : ball_type)
    for i : 1 .. upper (ledges)
        if ball.x >= ledges (i).x1 - ball.r and ball.x <= ledges (i).x2 + ball.r and ball.y < ledges (i).y1 + ball.r and ball.y > ledges (i).y2 + ball.r then
            if ball.v.y <= 0 then
                ball.y := ledges (i).y1 + ball.r
                ball.v.y := - (ball.v.y / Bounce_Factor)
                inair := false
            else
                ball.y := ledges (i).y2 - ball.r - 20
                ball.v.y := - (ball.v.y / Bounce_Factor)
            end if
        end if
    end for
end checkIfOnLedge

colorback (black)
View.Set ("offscreenonly")
loop
    cls
    Input.KeyDown (Key)
    if Key (KEY_RIGHT_ARROW) then
        balls (1).x += 1
    end if
    if Key (KEY_LEFT_ARROW) then
        balls (1).x -= 1
    end if
    if Key (KEY_UP_ARROW) and ~inair then
        balls (1).v.y := 8
        inair := true
    end if
    if balls (1).x < 10 then
        balls (1).x := 10
    elsif balls (1).x > maxx - balls (1).r then
        balls (1).x := maxx - balls (1).r
    end if
    balls (1).v.y -= Gravity
    balls (1).y += balls (1).v.y
    if balls (1).y < balls (1).r then
        balls (1).y := balls (1).r
        balls (1).v.y := - (balls (1).v.y / Bounce_Factor)
        inair := false
    end if
    checkIfOnLedge (balls (1))
    drawball (balls (1))
    View.Update
    delay (10)
end loop


sry if this doesn't help...

if u have any questions whatsoever, feel free to PM me Very Happy
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  [ 3 Posts ]
Jump to:   


Style:  
Search: