Computer Science Canada

Tron

Author:  Insectoid [ 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

Author:  saltpro15 [ Wed Mar 18, 2009 8:10 pm ]
Post subject:  RE:Tron

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

Author:  Insectoid [ 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?

Author:  saltpro15 [ 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

Author:  copthesaint [ 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.

Author:  Insectoid [ 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.

Author:  BigBear [ 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

Author:  Insectoid [ 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.

Author:  BigBear [ 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.

Author:  The_Bean [ 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.

Author:  BigBear [ 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

Author:  copthesaint [ 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

Author:  The_Bean [ 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

Author:  copthesaint [ Thu Mar 19, 2009 8:59 am ]
Post subject:  RE:Tron

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

Author:  Insectoid [ 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

Author:  Tallguy [ Mon Mar 23, 2009 9:00 am ]
Post subject:  RE:Tron

this is gonna sound really dumb, but i never really played tron before, so wats the objective of this game?

Author:  A.J [ Mon Mar 23, 2009 9:38 am ]
Post subject:  RE:Tron

I haven't played Tron before, but I believe that the objective is to not collide with anything and to outlast the other lines. (i.e. last line standing Laughing)

Author:  Tallguy [ Mon Mar 23, 2009 11:56 am ]
Post subject:  RE:Tron

umm, i guess so,

Author:  SNIPERDUDE [ Tue Mar 24, 2009 12:23 pm ]
Post subject:  Re: Tron

Tron was an arcade game that was originally based off of a movie.

The movie:
http://en.wikipedia.org/wiki/Tron_(film)

The Game:
http://en.wikipedia.org/wiki/Tron_(arcade_game)

A screenshot of the original arcade game:
Posted Image, might have been reduced in size. Click Image to view fullscreen.

Author:  Gackt [ Wed Apr 15, 2009 9:21 am ]
Post subject:  RE:Tron

[mod removed]
[user banned]

Author:  Tallguy [ Wed Apr 15, 2009 9:59 am ]
Post subject:  RE:Tron

dude..NO

Author:  copthesaint [ Wed Apr 15, 2009 1:16 pm ]
Post subject:  Re: Tron

Insectiod, Why don't you destroy all of the tail of a player when they crash like in the real game?

Turing:
for i : 0..maxx
   for p : 0..maxy
      if whatdotcolor (i,p) = /*the player color*/ then
         drawdot (i,p, black)
      end if
   end for
end for

Author:  leafs23 [ Sat May 30, 2009 4:27 pm ]
Post subject:  RE:Tron

i like it

Author:  ecookman [ Sat May 30, 2009 6:49 pm ]
Post subject:  RE:Tron

good job...ai could use a little work tho.

bits!

Author:  leafs23 [ Sun May 31, 2009 1:59 pm ]
Post subject:  RE:Tron

yeah could u put AI in to the game

Author:  Turing_Gamer [ Fri Nov 06, 2009 3:33 pm ]
Post subject:  Re: Tron

Not bad, AI could use some work...
The collision pics need work...
It's a little slow...
Other than that, I give it 7/10

Author:  Kharybdis [ Fri Nov 06, 2009 7:41 pm ]
Post subject:  RE:Tron

why did gackt get banned?

Author:  Insectoid [ Fri Nov 06, 2009 9:20 pm ]
Post subject:  RE:Tron

Because, his post was nonconstructive and offensive to the original poster (me). Haven't looked at this in ages. Moved on to bigger and better things. Sorting algorithms at the moment.


: