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

Username:   Password: 
 RegisterRegister   
 Gravitational pull game
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
TheOneTrueGod




PostPosted: Tue May 02, 2006 8:50 pm   Post subject: Gravitational pull game

Allright, the idea for this is entirely plagerized (Though I guess its not plagerism when I give credit... oh well) from holtsoft. They programmed it originally, and I decided to practice using classes by making this game. hope you enjoy Very Happy

code:

class planet
    export DrawMe, Init, Dist, size, ModifyValues
    const MaxDist := max (maxx, maxy)
    const DistModifier := MaxDist / 1.3
    var x, y, size, clr : int
    var SizeModifier : real
    procedure Init (x_, y_, size_, clr_ : int)
        x := x_
        y := y_
        size := size_
        clr := clr_
        SizeModifier := size / 1.5
    end Init
    procedure DrawMe
        drawfilloval (x, y, size, size, clr)
    end DrawMe
    function Dist (x_, y_ : real) : real
        result sqrt ((x - x_) ** 2 + (y - y_) ** 2)
    end Dist
    function Angle (x2, y2 : real) : real
        var theta : real
        if x = x2 then
            if y2 > y then
                theta := 90
            else
                theta := 270
            end if
        else
            theta := arctand ((y - y2) / (x - x2))
            if x2 > x then
                if y2 > y then
                else
                    theta := 360 + theta
                end if
            else
                theta := 180 + theta
            end if
        end if
        result theta
    end Angle
    function PullX (x_, y_ : real) : real
        result cosd (Angle (x_, y_)) * max (MaxDist - Dist (x_, y_), 0) / DistModifier * size / SizeModifier
    end PullX
    function PullY (x_, y_ : real) : real
        result sind (Angle (x_, y_)) * max (MaxDist - Dist (x_, y_), 0) / DistModifier * size / SizeModifier
    end PullY
    procedure ModifyValues (var value1, value2 : real, x_, y_ : real)
        value1 -= PullX (x_, y_)
        value2 -= PullY (x_, y_)
    end ModifyValues
end planet

var P : flexible array 1 .. 0 of ^planet

const ShipSize := 7
var ShipX, ShipY, ShipXS, ShipYS : real := 0

procedure InitPlanets (NumPlanets : int)
    ShipX := Rand.Int (0, maxx)
    ShipY := Rand.Int (0, maxy)

    new P, 1
    new planet, P (1)
    planet (P (1)).Init (Rand.Int (0, maxx), Rand.Int (0, maxy), Rand.Int (30, 80), brightblue)
    %Separate because the first one is a special case.
    for i : 2 .. NumPlanets + 1
        new P, upper (P) + 1
        new planet, P (upper (P))
        planet (P (upper (P))).Init (Rand.Int (0, maxx), Rand.Int (0, maxy), Rand.Int (20, 40), brightred)
    end for
end InitPlanets

var Score, MaxScore : int := 0

procedure DrawCrap
    for i : 1 .. upper (P)
        planet (P (i)).DrawMe
    end for
    drawfilloval (round (ShipX), round (ShipY), ShipSize, ShipSize, brightgreen)
    locate (1, 1)
    put "Score: ", Score, "/", MaxScore
end DrawCrap

const MaxShipSpeed := 10
procedure GetSpeed
    const DrawConstant := 3
    var ch : string (1)
    loop
        DrawCrap
        drawfilloval (round (ShipX + ShipXS * DrawConstant), round (ShipY + ShipYS * DrawConstant), ShipSize div 2, ShipSize div 2, red)
        View.Update
        cls
        ch := getchar
        if ch = KEY_UP_ARROW then
            ShipYS += 1
        elsif ch = KEY_DOWN_ARROW then
            ShipYS -= 1
        end if
        if ch = KEY_RIGHT_ARROW then
            ShipXS += 1
        elsif ch = KEY_LEFT_ARROW then
            ShipXS -= 1
        end if
        ShipXS := max (min (ShipXS, MaxShipSpeed), -MaxShipSpeed)
        ShipYS := max (min (ShipYS, MaxShipSpeed), -MaxShipSpeed)
        exit when ch = KEY_ENTER
    end loop
end GetSpeed

procedure MoveShip
    const MaxSpeed := MaxShipSpeed * 2
    if ShipY + ShipYS > maxy or ShipY + ShipYS < 0 then
        ShipYS *= -1
    end if
    if ShipX + ShipXS > maxx or ShipX + ShipXS < 0 then
        ShipXS *= -1
    end if
    ShipX += ShipXS
    ShipY += ShipYS
    for i : 1 .. upper (P)
        planet (P (i)).ModifyValues (ShipXS, ShipYS, ShipX, ShipY)
        ShipXS := max (min (ShipXS, MaxSpeed), -MaxSpeed)
        ShipYS := max (min (ShipYS, MaxSpeed), -MaxSpeed)
    end for
end MoveShip

var exiting : boolean := false
View.Set ('offscreenonly')
View.Set ('graphics:max,max')
View.Set ('nobuttonbar')
View.Set ('nocursor')
loop
    InitPlanets (MaxScore)
    DrawCrap
    GetSpeed
    loop
        MoveShip
        DrawCrap
        View.Update
        Time.DelaySinceLast (30)
        cls
        for i : 1 .. upper (P)
            if planet (P (i)).Dist (ShipX, ShipY) < ShipSize + planet (P (i)).size then
                if i = 1 then
                    Score += 1
                end if
                MaxScore += 1
                exiting := true
            end if
        end for
        exit when exiting
    end loop
    exiting := false
    ShipXS := 0
    ShipYS := 0
    new P, 0
end loop
Sponsor
Sponsor
Sponsor
sponsor
upthescale




PostPosted: Fri May 05, 2006 10:56 am   Post subject: (No subject)

pretty amazing good job
War_Caymore




PostPosted: Fri May 05, 2006 12:41 pm   Post subject: (No subject)

I'm not an expert programmer, but great job. Although it become impossible after about 15 tries. i suggest lowering the gravitational pull on the red planets, and mabey slowing it down a little bit. But i have no clue how to do that... jsut a sugestion.
BenLi




PostPosted: Thu May 11, 2006 3:39 pm   Post subject: (No subject)

pretty cool, you should make the gravity puul proportional to the size of the planet,

also, in the later levels, the sheer number of planets overwhlem me, the ball just kinda gets caught and dances back and forth between the planets, mayb3e you can fin other ways to make it harder
Cervantes




PostPosted: Thu May 11, 2006 4:31 pm   Post subject: (No subject)

Good to see more OOP code. Smile

A few minor notes:

  • Class names should start with a capital letter, by convention.
  • Distance and Angle are not code that are specific to planets. They are general principles of Euclidian Geometry, and so they should live in a Math module. You could still have these functions in your Planet class, but they would operate based on Euclidian Geometry (ie. the code within the Math module).
  • a ModifyValues procedure doesn't sound like too good of an idea. It sounds like it's one procedure that allows you to mass change the data in your object. That's not a good thing. Now, I realize that's not how this proc behaves, but you could choose a better name for it. I also don't see why you're passing it value1 and value2. From what I can tell in the procedure, it looks to be a procedure that applies gravity. Here's how I would do such a thing:
    code:

        proc attract_to (other_body : ^CelestialBody, mutual : boolean)
            const f := mass * other_body -> mass / Math.Distance(x, y, other_body -> x, other_body -> y) ** 2
            const ang := Math.Angle (x, y, other_body -> x, other_body -> y)
            vx += cos (ang) * f
            vy += sin (ang) * f
            if mutual then
                other_body -> attract_to (self, false)
            end if
        end attract_to

    That's untested and maybe I've got the physics wrong too... Wink


More to come when I actually run this program. Smile
Clayton




PostPosted: Thu May 11, 2006 4:48 pm   Post subject: (No subject)

not bad, however, i experienced a couple of problems:
1) later on the ball would get hung up between planets (mentioned above)
2) target planet got covered up by red planets at one point
3) you can start on a red planet if you have bad luck Sad

otherwise its not bad, good job, also, as mentioned above, gravity should be proportionate to size
TheOneTrueGod




PostPosted: Thu May 11, 2006 9:33 pm   Post subject: (No subject)

Thaks for feedback. If you read the code, I do believe that the pull IS proportional to size, just very minutely, because otherwise the planets had too much of a pull on the ball. I didn't plan on people getting to like level 15, so I never actually tested that far Razz.

@Cervantes, I didn't realise that the self keyword existed, or that you could declare a procedure with a pointer to itself in it. That would have made my life so much easier in some of these other programs Razz

Is there a site that I can see these conventions, because i've never heard of a lot of em Razz (My teacher actually enocouraged things like capitalization of variables.)
Cervantes




PostPosted: Fri May 12, 2006 8:57 pm   Post subject: (No subject)

Well, the convention for Turing is basically what we, CompSci.ca, say. Wink

Really, Turing convention is pretty much just a meld of popular conventions. The pretty universal conventions (such as FirstLetterCaps for class names) are carried over to Turing. Java names variables likeThis, and many people use this naming scheme in Turing. I personally prefer naming variables with underscores, like_this. In this, "Turing convention" is lax.

So if you want to see some Turing convention guidelines, just look at conventions from other popular languages. Anything that is pretty universal should be carried over to Turing (reason being that not carrying them over just means you'll have to get used to a different convention upon switching to a new language). Things that are less universal you get to take your pick at.

Regarding self: Did I not talk about it in the classes tutorial? If not, I'll add it to Part II.
Sponsor
Sponsor
Sponsor
sponsor
zylum




PostPosted: Sat May 13, 2006 2:30 pm   Post subject: (No subject)

Cervantes wrote:
Well, the convention for Turing is basically what we, CompSci.ca, say. Wink

Really, Turing convention is pretty much just a meld of popular conventions. The pretty universal conventions (such as FirstLetterCaps for class names) are carried over to Turing. Java names variables likeThis, and many people use this naming scheme in Turing. I personally prefer naming variables with underscores, like_this. In this, "Turing convention" is lax.


its called CamelCase Wink

http://en.wikipedia.org/wiki/CamelCase
Rocket




PostPosted: Thu May 18, 2006 5:53 pm   Post subject: (No subject)

nice

but a few errors, not endingness, as well as the start starting on a red planet
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 10 Posts ]
Jump to:   


Style:  
Search: