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

Username:   Password: 
 RegisterRegister   
 Tron
Index -> Programming, Turing -> Turing Submissions
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Insectoid




PostPosted: Wed Mar 18, 2009 7:42 pm   Post subject: Tron

So, I set about making Tron. After about 30 minutes of work, I had a semi-functional Tron game. The AI needs some work, and the collision detection is a bit odd (it does the explosion, but not the exit...odd). I'll post newer versions later.

Perhaps I'm using the wrong command. Will 'exit' just exit the loop, or the program? Anywho, the code:

Turing:


type player_type :
    record
        x : int
        y : int
        velX : int
        velY : int
        coloR : int
        state : boolean
    end record
var speed := 1
var player : array 1 .. 4 of player_type
var keys : array char of boolean


View.Set ("graphics: max; max, offscreenonly, nobuttonbar")
player (1).x := 10
player (1).y := maxy div 2 + 5
player (1).velX := speed
player (1).velY := 0
player (1).coloR := brightred
player (1).state := true

player (2).x := maxx div 2 + 5
player (2).y := 10
player (2).velX := 0
player (2).velY := speed
player (2).coloR := brightgreen
player (2).state := true

player (3).x := maxx div 2 - 5
player (3).y := maxy - 10
player (3).velX := 0
player (3).velY := -speed
player (3).coloR := brightblue
player (3).state := true

player (4).x := maxx - 10
player (4).y := maxy div 2 - 5
player (4).velX := -speed
player (4).velY := 0
player (4).coloR := yellow
player (4).state := true

colorback (black)
cls
Draw.Box (maxx, maxy, 0, 0, white)

function CheckCollision (playerNum : int) : boolean
    if (whatdotcolor (player (playerNum).x, player (playerNum).y + 5) ~= black and player (playerNum).velY = speed) or
            (whatdotcolor (player (playerNum).x, player (playerNum).y - 5) ~= black and player (playerNum).velY = -speed) or
            (whatdotcolor (player (playerNum).x + 5, player (playerNum).y) ~= black and player (playerNum).velX = speed) or
            (whatdotcolor (player (playerNum).x - 5, player (playerNum).y) ~= black and player (playerNum).velX = -speed) then
       
        result true
    else
        result false
    end if
end CheckCollision

proc TurnLeft (playerNumber : int)
    player (playerNumber).velY := 0
    player (playerNumber).velX := -speed
end TurnLeft

proc TurnRight (playerNumber : int)
    player (playerNumber).velY := 0
    player (playerNumber).velX := speed
end TurnRight

proc TurnUp (playerNumber : int)
    player (playerNumber).velY := speed
    player (playerNumber).velX := 0
end TurnUp

proc TurnDown (playerNumber : int)
    player (playerNumber).velY := -speed
    player (playerNumber).velX := 0
end TurnDown



proc AI (playerNumber : int)
    if (whatdotcolor (player (playerNumber).x + 5, player (playerNumber).y) ~= black and player (playerNumber).velX = speed) or (whatdotcolor (player (playerNumber).x - 5, player (playerNumber).y) ~=
            black and player (playerNumber).velX = -speed) then
        if Rand.Int (1, 2) = 1 then
            TurnRight (playerNumber)
        else
            TurnLeft (playerNumber)
        end if
    elsif (whatdotcolor (player (playerNumber).x, player (playerNumber).y + 5) ~= black and player (playerNumber).velX = speed) or (whatdotcolor (player (playerNumber).x, player (playerNumber).y - 5)
            ~= black and player (playerNumber).velX = -speed) then
        if Rand.Int (1, 2) = 1 then
            TurnUp (playerNumber)
        else
            TurnDown (playerNumber)
        end if
    end if
end AI


loop
    for x : 1 .. 4
        if CheckCollision (x) = true then
        Draw.FillOval (player (x).x, player (x).y, 100, 100, black)   
        exit
        end if
        player (x).x += player (x).velX
        player (x).y += player (x).velY
        Draw.FillOval (player (x).x, player (x).y, 2, 2, player (x).coloR)
    end for
    for x : 2 .. 4
        AI (x)
    end for
    Input.KeyDown (keys)
    if keys (KEY_UP_ARROW) and player (1).velY ~= -speed then
        TurnUp (1)
    elsif keys (KEY_DOWN_ARROW) and player (1).velY ~= speed then
        TurnDown (1)
    elsif keys (KEY_LEFT_ARROW) and player (1).velX ~= speed then
        TurnLeft (1)
    elsif keys (KEY_RIGHT_ARROW) and player (1).velX ~= -speed then
        TurnRight (1)
    end if
    View.Update
    delay (6)
end loop
Sponsor
Sponsor
Sponsor
sponsor
saltpro15




PostPosted: Wed Mar 18, 2009 8:10 pm   Post subject: RE:Tron

exit should just exit the loop, this is cool, nice work Very Happy
Insectoid




PostPosted: Wed Mar 18, 2009 8:15 pm   Post subject: RE:Tron

Some more bugs to post!

-there's a 'black hole' at the top centre (maxx div 2, maxy). I have yet to find out why, perhaps it's a perpetual collision. I suppose I'll post the controls too...and the objective.

Objective: Survive. Don't hit any walls.
Controls: You are the red line. Use the arrow keys to move around. You can only move at right angles.

lol, we have nice karma scores, don't we Saltpro?
saltpro15




PostPosted: Wed Mar 18, 2009 8:19 pm   Post subject: RE:Tron

well I'm pretty sure mine is due to the spite of corriep giving me negative karma every time he logs in, poor jealous kid Wink

and this is the internet, I don't really care who wastes their life giving bad karma on CS forums Very Happy
copthesaint




PostPosted: Wed Mar 18, 2009 8:24 pm   Post subject: Re: Tron

Why not make an array of chars instead of having
Turing:
if keys (KEY_UP_ARROW) and player (1).velY ~= -speed then
        TurnUp (1)
    elsif keys (KEY_DOWN_ARROW) and player (1).velY ~= speed then
        TurnDown (1)
    elsif keys (KEY_LEFT_ARROW) and player (1).velX ~= speed then
        TurnLeft (1)
    elsif keys (KEY_RIGHT_ARROW) and player (1).velX ~= -speed then
        TurnRight (1)
    end if

instead have an array 4 keys per player

Turing:
var playerchars : array 1..16 of char := init (Keys of all players)


If you don't understand how you would do this just ask But I'm sure you could figure it out.
Also even if you want this to be a 1 player game this would stll make your code shorter.
Insectoid




PostPosted: Wed Mar 18, 2009 8:28 pm   Post subject: RE:Tron

I see, but it's a 1-player game. 3 AI players. It's for me to try out AI. I'm going to add in that they will move spontaneously as well as thoughtfully. I get what you're doing, though I'd still have to have the 'if' statements there, to tell the program what to do if that key is pressed. So even if I do make it 4 player, I don't see why I'd do that.
BigBear




PostPosted: Wed Mar 18, 2009 8:39 pm   Post subject: RE:Tron

that exit statement exits the for loop but stays in the main loop. Try adding an exit when collision = true
Insectoid




PostPosted: Wed Mar 18, 2009 8:54 pm   Post subject: RE:Tron

I changed that in my more recent version. It now exits when 3 players crash. Still haven't figured out the black hole...

And I'm sure there was a command that completely terminates the program.
Sponsor
Sponsor
Sponsor
sponsor
BigBear




PostPosted: Wed Mar 18, 2009 9:21 pm   Post subject: RE:Tron

Well there is quit which crashes the prgram and which should not be used.
The_Bean




PostPosted: Wed Mar 18, 2009 9:31 pm   Post subject: Re: Tron

'return' could be used, although its more meant for procedures, but can be used for the main program.
BigBear




PostPosted: Wed Mar 18, 2009 9:51 pm   Post subject: RE:Tron

return will go back one step so it will act the same as exit.

or terminate the procedure
copthesaint




PostPosted: Wed Mar 18, 2009 9:54 pm   Post subject: RE:Tron

This is what I ment if it was going to be 1 player.
Turing:

var chars : array 1 .. 4 of char := init (KEY_RIGHT_ARROW, KEY_UP_ARROW, KEY_LEFT_ARROW, KEY_DOWN_ARROW)

for i : 1 .. 4
    if keys (chars (i)) then
        if i = 2 or i = 4 then
            player (1).velX := 0
            player (1).velY := speed * (3 - i)
        elsif i = 1 or i = 3 then
            player (1).velX := speed * (2 - i)
            player (1).velY := 0
        end if
    end if
end for
The_Bean




PostPosted: Wed Mar 18, 2009 10:09 pm   Post subject: Re: Tron

Or even:
Turing:

var chars : array 1 .. 4 of char := init (KEY_RIGHT_ARROW, KEY_UP_ARROW, KEY_LEFT_ARROW, KEY_DOWN_ARROW)
var xDir : array 1 .. 4 of int := init (1, 0, -1, 0)
var yDir : array 1 .. 4 of int := init (0, 1, 0, -1)

for i : 1 .. 4
    if keys (chars (i)) then
        player (1).velX := xDir (i) * speed
        player (1).velY:=yDir (i) * speed
    end if
end for
copthesaint




PostPosted: Thu Mar 19, 2009 8:59 am   Post subject: RE:Tron

yea just things like this to reduce the amount of executed lines
Insectoid




PostPosted: Thu Mar 19, 2009 9:58 am   Post subject: RE:Tron

at any rate, it is single-player, so I won't worry about that. I'll remember it though. Anywho, here's the newest version. Still buggy, but improved.


EDIT: *Smacks head on desk* Wow, I feel stupid now. I thought you meant that for easily making 4 player controls. After actually reading that and learning what that bit of code does, I...well, I know what it does. Woops. And the code:
Turing:


type player_type :
    record
        x : int
        y : int
        velX : int
        velY : int
        coloR : int
        state : boolean
    end record
var speed := 1
var player : array 1 .. 4 of player_type
var keys : array char of boolean
var quitCount : int := 0

View.Set ("graphics: max; max, offscreenonly, nobuttonbar")
player (1).x := 10
player (1).y := maxy div 2
player (1).velX := speed
player (1).velY := 0
player (1).coloR := brightred
player (1).state := true

player (2).x := maxx div 2 + 5
player (2).y := 10
player (2).velX := 0
player (2).velY := speed
player (2).coloR := brightgreen
player (2).state := true

player (3).x := maxx div 2 - 5
player (3).y := maxy - 10
player (3).velX := 0
player (3).velY := -speed
player (3).coloR := brightblue
player (3).state := true

player (4).x := maxx - 10
player (4).y := maxy div 2 - 5
player (4).velX := -speed
player (4).velY := 0
player (4).coloR := yellow
player (4).state := true

colorback (black)
cls
for x : 0 .. 5
    Draw.Box (maxx - x, maxy - x, x, x, white)
end for

function CheckCollision (playerNum : int) : boolean
    if (whatdotcolor (player (playerNum).x, player (playerNum).y + 5) ~= black and player (playerNum).velY = speed) or
            (whatdotcolor (player (playerNum).x, player (playerNum).y - 5) ~= black and player (playerNum).velY = -speed) or
            (whatdotcolor (player (playerNum).x + 5, player (playerNum).y) ~= black and player (playerNum).velX = speed) or
            (whatdotcolor (player (playerNum).x - 5, player (playerNum).y) ~= black and player (playerNum).velX = -speed) then

        result true
    else
        result false
    end if
end CheckCollision

proc TurnLeft (playerNumber : int)
    player (playerNumber).velY := 0
    player (playerNumber).velX := -speed
end TurnLeft

proc TurnRight (playerNumber : int)
    player (playerNumber).velY := 0
    player (playerNumber).velX := speed
end TurnRight

proc TurnUp (playerNumber : int)
    player (playerNumber).velY := speed
    player (playerNumber).velX := 0
end TurnUp

proc TurnDown (playerNumber : int)
    player (playerNumber).velY := -speed
    player (playerNumber).velX := 0
end TurnDown



proc AI (playerNumber : int)
    if (whatdotcolor (player (playerNumber).x + 5, player (playerNumber).y) ~= black and player (playerNumber).velX = speed) or (whatdotcolor (player (playerNumber).x - 5, player (playerNumber).y) ~=
            black and player (playerNumber).velX = -speed) then
        if Rand.Int (1, 2) = 1 then
            TurnUp (playerNumber)
        else
            TurnDown (playerNumber)
        end if
    elsif (whatdotcolor (player (playerNumber).x, player (playerNumber).y + 5) ~= black and player (playerNumber).velY = speed) or (whatdotcolor (player (playerNumber).x, player (playerNumber).y - 5)
            ~= black and player (playerNumber).velY = -speed) then
        if Rand.Int (1, 2) = 1 then
            TurnLeft (playerNumber)
        else
            TurnRight (playerNumber)
        end if
    end if
end AI


loop

    for x : 1 .. 4
        if player (x).state = false then
            quitCount += 1
        end if
        if CheckCollision (x) = true then
            Draw.FillOval (player (x).x, player (x).y, 100, 100, black)
            player (x).state := false
        end if
        if player (x).state = true then
            player (x).x += player (x).velX
            player (x).y += player (x).velY
        end if
        Draw.FillOval (player (x).x, player (x).y, 2, 2, player (x).coloR)
    end for
    exit when quitCount >= 3
    quitCount := 0
    for x : 2 .. 4
        AI (x)
    end for
    Input.KeyDown (keys)
    if keys (KEY_UP_ARROW) and player (1).velY ~= -speed then
        TurnUp (1)
    elsif keys (KEY_DOWN_ARROW) and player (1).velY ~= speed then
        TurnDown (1)
    elsif keys (KEY_LEFT_ARROW) and player (1).velX ~= speed then
        TurnLeft (1)
    elsif keys (KEY_RIGHT_ARROW) and player (1).velX ~= -speed then
        TurnRight (1)
    end if
    View.Update
    delay (6)
end loop
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 2  [ 28 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: