Posted: 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 endrecord var speed :=1 var player :array1.. 4of player_type
var keys :arraycharofboolean
View.Set("graphics: max; max, offscreenonly, nobuttonbar")
player (1).x :=10
player (1).y :=maxydiv2 + 5
player (1).velX := speed
player (1).velY :=0
player (1).coloR :=brightred
player (1).state :=true
player (2).x :=maxxdiv2 + 5
player (2).y :=10
player (2).velX :=0
player (2).velY := speed
player (2).coloR :=brightgreen
player (2).state :=true
player (3).x :=maxxdiv2 - 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 :=maxydiv2 - 5
player (4).velX := -speed
player (4).velY :=0
player (4).coloR :=yellow
player (4).state :=true
function CheckCollision (playerNum :int):boolean if(whatdotcolor(player (playerNum).x, player (playerNum).y + 5) ~=blackand player (playerNum).velY = speed)or (whatdotcolor(player (playerNum).x, player (playerNum).y - 5) ~=blackand player (playerNum).velY = -speed)or (whatdotcolor(player (playerNum).x + 5, player (playerNum).y) ~=blackand player (playerNum).velX = speed)or (whatdotcolor(player (playerNum).x - 5, player (playerNum).y) ~=blackand player (playerNum).velX = -speed)then
resulttrue else resultfalse endif 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) ~=blackand player (playerNumber).velX = speed)or(whatdotcolor(player (playerNumber).x - 5, player (playerNumber).y) ~= blackand player (playerNumber).velX = -speed)then if Rand.Int (1, 2)=1then
TurnRight (playerNumber) else
TurnLeft (playerNumber) endif elsif(whatdotcolor(player (playerNumber).x, player (playerNumber).y + 5) ~=blackand player (playerNumber).velX = speed)or(whatdotcolor(player (playerNumber).x, player (playerNumber).y - 5)
~=blackand player (playerNumber).velX = -speed)then if Rand.Int (1, 2)=1then
TurnUp (playerNumber) else
TurnDown (playerNumber) endif endif end AI
loop for x :1.. 4 if CheckCollision (x)=truethen Draw.FillOval(player (x).x, player (x).y, 100, 100, black) exit endif
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) endfor for x :2.. 4
AI (x) endfor 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) endif View.Update delay(6) endloop
Sponsor Sponsor
saltpro15
Posted: Wed Mar 18, 2009 8:10 pm Post subject: RE:Tron
exit should just exit the loop, this is cool, nice work
Insectoid
Posted: 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
Posted: 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
and this is the internet, I don't really care who wastes their life giving bad karma on CS forums
copthesaint
Posted: 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) endif
instead have an array 4 keys per player
Turing:
var playerchars :array1..16ofchar:=init(Keys ofall 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
Posted: 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
Posted: 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
Posted: 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
BigBear
Posted: 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
Posted: 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
Posted: 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
Posted: Wed Mar 18, 2009 9:54 pm Post subject: RE:Tron
This is what I ment if it was going to be 1 player.
for i :1.. 4 if keys (chars (i))then if i =2or i =4then
player (1).velX :=0
player (1).velY := speed *(3 - i) elsif i =1or i =3then
player (1).velX := speed *(2 - i)
player (1).velY :=0 endif endif endfor
The_Bean
Posted: Wed Mar 18, 2009 10:09 pm Post subject: Re: Tron
for i :1.. 4 if keys (chars (i))then
player (1).velX := xDir (i)* speed
player (1).velY:=yDir (i)* speed
endif endfor
copthesaint
Posted: Thu Mar 19, 2009 8:59 am Post subject: RE:Tron
yea just things like this to reduce the amount of executed lines
Insectoid
Posted: 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 endrecord var speed :=1 var player :array1.. 4of player_type
var keys :arraycharofboolean var quitCount :int:=0
View.Set("graphics: max; max, offscreenonly, nobuttonbar")
player (1).x :=10
player (1).y :=maxydiv2
player (1).velX := speed
player (1).velY :=0
player (1).coloR :=brightred
player (1).state :=true
player (2).x :=maxxdiv2 + 5
player (2).y :=10
player (2).velX :=0
player (2).velY := speed
player (2).coloR :=brightgreen
player (2).state :=true
player (3).x :=maxxdiv2 - 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 :=maxydiv2 - 5
player (4).velX := -speed
player (4).velY :=0
player (4).coloR :=yellow
player (4).state :=true
function CheckCollision (playerNum :int):boolean if(whatdotcolor(player (playerNum).x, player (playerNum).y + 5) ~=blackand player (playerNum).velY = speed)or (whatdotcolor(player (playerNum).x, player (playerNum).y - 5) ~=blackand player (playerNum).velY = -speed)or (whatdotcolor(player (playerNum).x + 5, player (playerNum).y) ~=blackand player (playerNum).velX = speed)or (whatdotcolor(player (playerNum).x - 5, player (playerNum).y) ~=blackand player (playerNum).velX = -speed)then
resulttrue else resultfalse endif 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) ~=blackand player (playerNumber).velX = speed)or(whatdotcolor(player (playerNumber).x - 5, player (playerNumber).y) ~= blackand player (playerNumber).velX = -speed)then if Rand.Int (1, 2)=1then
TurnUp (playerNumber) else
TurnDown (playerNumber) endif elsif(whatdotcolor(player (playerNumber).x, player (playerNumber).y + 5) ~=blackand player (playerNumber).velY = speed)or(whatdotcolor(player (playerNumber).x, player (playerNumber).y - 5)
~=blackand player (playerNumber).velY = -speed)then if Rand.Int (1, 2)=1then
TurnLeft (playerNumber) else
TurnRight (playerNumber) endif endif end AI
loop
for x :1.. 4 if player (x).state =falsethen
quitCount +=1 endif if CheckCollision (x)=truethen Draw.FillOval(player (x).x, player (x).y, 100, 100, black)
player (x).state :=false endif if player (x).state =truethen
player (x).x += player (x).velX
player (x).y += player (x).velY
endif Draw.FillOval(player (x).x, player (x).y, 2, 2, player (x).coloR) endfor exitwhen quitCount >= 3
quitCount :=0 for x :2.. 4
AI (x) endfor 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) endif View.Update delay(6) endloop