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

Username:   Password: 
 RegisterRegister   
 so i was trying out the trig functions by making pong
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
limited_skillz




PostPosted: Mon Mar 15, 2004 12:17 am   Post subject: so i was trying out the trig functions by making pong

damn i have no idea what im doing, the ball flies everywhere and anywhere

my moving of the ball is in a case in a process called ball

dont comment about the game though, i just wanted to make a rough one, really fast without much work, i dont think i had to use processes or anything, but whatever, this is about trig

can you give me a few tips or something about using trig properly


code:
% Stuart Seupaul
% March 13, 2004
% pong.t

View.Set ("offscreenonly")
var chars : array char of boolean
const p1x : int := 75
const p2x : int := maxx - 75
const paddleSize : int := 20
var p1y, p2y : int
p1y := maxy div 2
p2y := maxy div 2
var ballx, bally : int
ballx := maxx div 2
bally := maxy div 2
const ballSize : int := 10
var processOK : boolean := true
var score : boolean := false
var p1sc, p2sc : int
p1sc := 0
p2sc := 0

process update
    loop
        View.Update
        processOK := true
        delay (3)
        processOK := false
        exit when score = true
    end loop
end update

process ball (direction : int)
    var balld : int
    balld := direction
    var rP1, rP2, trigAmount : int
    loop
        if bally + 5 > maxy - 36 then
            trigAmount := Rand.Int (25, 80)
            balld := Rand.Int (5, 6)
        end if
        if bally - 5 < 35 then
            trigAmount := Rand.Int (10, 80)
            balld := Rand.Int (7, 8)
        end if
        if ballx + 5 > p2x - 15 and bally <= p2y - 15 and bally >= p2y - 30 then
            balld := 2
        end if
        if ballx + 5 > p2x - 15 and ((bally <= p2y + 10 and bally >= p2y - 15) or (bally <= p2y - 30 and bally >= p2y - 60)) then
            trigAmount := Rand.Int (10, 80)
            loop
                balld := Rand.Int (1, 8)
                exit when balld = 7 or balld = 5
            end loop
        end if
        if ballx - 5 < p1x and (bally <= p1y - 15 and bally >= p1y - 30) then
            balld := 1
        end if
        if ballx - 5 < p1x and ((bally <= p1y + 10 and bally >= p1y - 15) or (bally <= p1y - 30 and bally >= p1y - 60)) then
            trigAmount := Rand.Int (10, 80)
            loop
                balld := Rand.Int (1, 8)
                exit when balld = 8 or balld = 6
            end loop
        end if
       
        % heres where to modify, just edit 5+, i put what each one does
       
        case (balld) of
            label 1 :
                ballx += 10 % right
            label 2 :
                ballx -= 10 % left
            label 3 :
                bally += 5 % up
            label 4 :
                bally -= 5 % down
            label 5 :
                ballx -= 5 * (1 + ceil (sin (trigAmount)))  % down left
                bally -= 5 * (1 + ceil (cos (trigAmount)))
            label 6 :
                ballx += 5 * (1 + ceil (sin (trigAmount))) % down right
                bally -= 5 * (1 + ceil (cos (trigAmount)))
            label 7 :
                ballx -= 5 * (1 + ceil (sin (trigAmount))) % up left
                bally += 5 * (1 + ceil (cos (trigAmount)))
            label 8 :
                ballx += 5 * (1 + ceil (sin (trigAmount))) % up right
                bally += 5 * (1 + ceil (cos (trigAmount)))
        end case
        if processOK = true then
            drawfilloval (ballx, bally, ballSize, ballSize, blue)
            delay (15)
            drawfilloval (ballx, bally, ballSize, ballSize, 0)
        end if
        if ballx > maxx - 45 or ballx < 40 then
            if ballx > maxx - 45 then
                p1sc += 1
            elsif ballx < 40 then
                p2sc += 1
            end if
            ballx := maxx div 2
            bally := maxy div 2
            score := true
            exit
        end if
    end loop
end ball

process enemyPaddle
    loop
        if bally + 20 > p2y and ballx > maxx div 2 then
            if p2y + 5 < maxy - 25 then
                p2y += 5
            end if
        end if
        if bally - 20 < p2y - 50 then
            if p2y - 5 > 75 then
                p2y -= 5
            end if
        end if
        if processOK = true then
            drawfillbox (p2x, p2y, p2x - 20, p2y - 50, red)
            delay (15)
            drawfillbox (p2x, p2y, p2x - 20, p2y - 50, 0)
        end if
        exit when score = true
    end loop
end enemyPaddle

% main
locate (1, 1)
put "Press any key to start."
loop
    exit when hasch
end loop
locate (1, 1)
put ""

for i : 1 .. 3
    drawbox (25, 25, maxx - 25, maxy - 25, 7)
    fork update
    fork enemyPaddle
    fork ball (Rand.Int (1, 4))
    loop
        Input.KeyDown (chars)
        if chars (KEY_UP_ARROW) then
            if p1y + 5 < maxy - 26 then
                p1y += 5
            end if
        end if
        if chars (KEY_DOWN_ARROW) then
            if p1y - 55 > 26 then
                p1y -= 5
            end if
        end if
        if processOK = true then
            drawfillbox (p1x, p1y, p1x - 20, p1y - 50, 7)
            delay (10)
            drawfillbox (p1x, p1y, p1x - 20, p1y - 50, 0)
        end if
        exit when score = true
    end loop
    color (54)
    locate (1, 1)
    put "Player 1: ", p1sc ..
    color (red)
    put " Player 2: ", p2sc
    loop
        exit when hasch
    end loop
    delay (100)
    score := false
end for
Sponsor
Sponsor
Sponsor
sponsor
jonos




PostPosted: Mon Mar 15, 2004 12:32 am   Post subject: (No subject)

thats som pretty cool stuff, but the ball kindof bounces at weird angles and atuff like that. i don't know trig, but you don't have to use trig for pong, i know shorthair made one in blitz you can check out, or zylum also made a really simple one that you can look at.
limited_skillz




PostPosted: Mon Mar 15, 2004 12:39 am   Post subject: (No subject)

thanks, i thought ppl would call me a n00b lol, if you change all the values in the case to 5 it works normal

i was just trying to experiment with trig stuff to make crazy (but not too crazy) angles

right now im checking some source code for other trig stuff, but its just not clicking, i need like a full in depth tutorial, ill check the net for one
jonos




PostPosted: Mon Mar 15, 2004 12:46 am   Post subject: (No subject)

you should learn trig in school, i have tried because we haven't gotten to that yet in math at school, but i just didn't understand that, but ive heard that once you do it, its really easy.
Cervantes




PostPosted: Mon Mar 15, 2004 9:35 am   Post subject: (No subject)

It is really easy. I don't really know it right now, but my friend taught me it on the bus going to school one day. Course, I forgot most of it because I didn't actually use it. But it is not complicated.

Using trig: You can either use x speed and y speed values, or you can use angles, velocities, and weights.

for x speed and y speed check out thoughtful's pool
for angles, velocities, and weights, check out Homer's Tutorial



Circle Pong.t
 Description:
Here's a simple Pong program I made that uses the xspeed and yspeed method: credit to throughtful for the collision data!

Download
 Filename:  Circle Pong.t
 Filesize:  4.13 KB
 Downloaded:  103 Time(s)

AsianSensation




PostPosted: Mon Mar 15, 2004 9:46 am   Post subject: (No subject)

well, for pong, isn't all you need is the angle of incidence = angle of reflection? The speed at which it bounces back depends on the material of the paddle etc, you can make up those values yourself. I don't see the incidence angle = reflectiona angle part, maybe thats why?
Mazer




PostPosted: Mon Mar 15, 2004 11:08 am   Post subject: (No subject)

In reality it would also depend on the direction in which the paddle is moving. If they collide while, say, the paddle is moving up while the ball is moving down, the vertical component of the ball's velocity wouldn't be as great as if the ball and paddle were moving in the same direction.
Then again, it's pong, so everything is in 45 degree angles as I remember.
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: