Changelevel in a maze upon collision help.
Author |
Message |
MattehMatt
|
Posted: Sat Nov 01, 2008 6:55 pm Post subject: Changelevel in a maze upon collision help. |
|
|
Hey I'm coding a simple maze game right now but I cant get it so that when one square hits the other square the level ends and goes to the next level, I cant even seem to get collision to work either.
If anyone wouldn't mind coding in what I should have to make the code below terminate or clear the screen or something obvious so that I know it worked, (btw this isn't for school, just trying to figure it out!)
Turing: | procedure getKey
var ch : string (1)
locate (37, 75)
getch (ch )
end getKey
%%%%%%%%%%%%%%
%TITLE SCREEN%
%%%%%%%%%%%%%%
View.Set ("graphics:600;600")
var font : int
var font2 : int
var font3 : int
var font4 : int
font := Font.New ("SNIPER:60")
font2 := Font.New ("sidewalk:50")
font3 := Font.New ("Hobo STD:25")
font4 := Font.New ("Hobo STD:15")
colorback (7)
cls
Font.Draw ("T", 100, 500, font2, 0)
delay (200)
cls
Font.Draw ("Th", 100, 500, font2, 0)
delay (200)
cls
Font.Draw ("The", 100, 500, font2, 0)
delay (200)
cls
Font.Draw ("The M", 100, 500, font2, 0)
delay (200)
cls
Font.Draw ("The Ma", 100, 500, font2, 0)
delay (200)
cls
Font.Draw ("The Maz", 100, 500, font2, 0)
delay (200)
cls
Font.Draw ("The Maze", 100, 500, font2, 0)
delay (200)
cls
Font.Draw ("The Maze!", 100, 500, font2, 0)
Font.Draw ("Press any key to continue...", 50, 50, font3, 0)
getKey
delay (100)
cls
%%%%%%%%%%%%%%%
%INSTRUCTIONS!%
%%%%%%%%%%%%%%%
View.Set ("graphics:600;600")
colorback (7)
Font.Draw ("INSTRUCTIONS", 1, 550,font, brightgreen)
Font.Draw ("The objective of the game is to have the green square", 1, 500,font4, white)
Font.Draw ("reach the white one, good luck!", 1, 470,font4, white)
Font.Draw ("Use the arrow keys to move!", 1, 350,font4, white)
Font.Draw ("any key to continue...", 1, 1,font4, white)
getKey
%%%%%%%%%%
%THE GAME%
%%%%%%%%%%
View.Set ("graphics:100;100,offscreenonly")
var tiles : flexible array 0 .. 0 of int
var FileName1 : string := "level1.txt"
var FileNo : int
var totalX, totalY : int := 0
function Array (x, y : int) : int
result totalX * y + x
end Array
open : FileNo, FileName1, get
get : FileNo, totalX
get : FileNo, totalY
new tiles, totalX * totalY - 1
for decreasing y : totalY - 1 .. 0
for x : 0 .. totalX - 1
get : FileNo, tiles (Array (x, y ))
end for
end for
close (FileNo )
procedure reDraw
for x : 0 .. totalX - 1
for y : 0 .. totalY - 1
if tiles (Array (x, y )) = 1 then
Draw.FillBox (x * 10, y * 10, x * 10 + 10, y * 10 + 10, red)
else
Draw.FillBox (x * 10, y * 10, x * 10 + 10, y * 10 + 10, black)
end if
end for
end for
end reDraw
var keys : array char of boolean
var px, py : int := 1
var gx, gy : int
loop
reDraw
Input.KeyDown (keys )
if keys (KEY_LEFT_ARROW) and tiles (Array (px - 1, py )) = 0 then
px - = 1
end if
if keys (KEY_RIGHT_ARROW) and tiles (Array (px + 1, py )) = 0 then
px + = 1
end if
if keys (KEY_DOWN_ARROW) and tiles (Array (px, py - 1)) = 0 then
py - = 1
end if
if keys (KEY_UP_ARROW) and tiles (Array (px, py + 1)) = 0 then
py + = 1
end if
gx := 50
gy := 60
Draw.FillBox (gx, gy, gx - 10, gy - 10, 0)
Draw.FillBox (px * 10, py * 10, px * 10 + 10, py * 10 + 10, 48)
if whatdotcolor (55, 55) = white then
cls
delay (500)
end if
View.Update
delay (60)
end loop |
|
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
S_Grimm

|
Posted: Sat Nov 01, 2008 7:04 pm Post subject: RE:Changelevel in a maze upon collision help. |
|
|
There is a great tutorial on collision detection in the turing tutorials. i don't have a link right now, ill pm it to you l8r. anyway, to detect collision between two boxes, you have to determine if the edges hit. it goes something like this (assuming that the wall is at an x coordinate of 300 and there is a top wall at a y coordinate of 400)
Turing: |
var x1,y1,x2,y2 : int
x1 := 200
y1: = 200
x2 := 250
y2:= 250
loop
Draw.FillBox (x1,y1,x2,y2 )
%Code for movement here.
if x2 = 300 then
x2 := 300
x1 := 250
end if
if y2 = 400 then
y2 := 400
y1 := 350
end loop
|
|
|
|
|
|
 |
MattehMatt
|
Posted: Sat Nov 01, 2008 7:09 pm Post subject: Re: Changelevel in a maze upon collision help. |
|
|
Thanks for the tip, ill try that out.
About that collision tutorial, I read it, multiple times, but I couldnt seem to wrap my head around it ill try what you gave me and post again later if I can't get it |
|
|
|
|
 |
MattehMatt
|
Posted: Sat Nov 01, 2008 9:02 pm Post subject: Re: Changelevel in a maze upon collision help. |
|
|
Alright well it changes levels now, but I need a way to replace the goal dot. Any suggestions? Code so far :
Turing: |
procedure getKey
var ch : string (1)
locate (37, 75)
getch (ch )
end getKey
%%%%%%%%%%%%%%
%TITLE SCREEN%
%%%%%%%%%%%%%%
View.Set ("graphics:600;600")
var font : int
var font2 : int
var font3 : int
var font4 : int
font := Font.New ("SNIPER:60")
font2 := Font.New ("sidewalk:50")
font3 := Font.New ("Hobo STD:25")
font4 := Font.New ("Hobo STD:15")
colorback (7)
cls
Font.Draw ("T", 100, 500, font2, 0)
delay (200)
cls
Font.Draw ("Th", 100, 500, font2, 0)
delay (200)
cls
Font.Draw ("The", 100, 500, font2, 0)
delay (200)
cls
Font.Draw ("The M", 100, 500, font2, 0)
delay (200)
cls
Font.Draw ("The Ma", 100, 500, font2, 0)
delay (200)
cls
Font.Draw ("The Maz", 100, 500, font2, 0)
delay (200)
cls
Font.Draw ("The Maze", 100, 500, font2, 0)
delay (200)
cls
Font.Draw ("The Maze!", 100, 500, font2, 0)
Font.Draw ("Press any key to continue...", 50, 50, font3, 0)
getKey
delay (100)
cls
%%%%%%%%%%%%%%%
%INSTRUCTIONS!%
%%%%%%%%%%%%%%%
View.Set ("graphics:600;600")
colorback (7)
Font.Draw ("INSTRUCTIONS", 1, 550, font, brightgreen)
Font.Draw ("The objective of the game is to have the green square", 1, 500, font4, white)
Font.Draw ("reach the white one, good luck!", 1, 470, font4, white)
Font.Draw ("Use the arrow keys to move!", 1, 350, font4, white)
Font.Draw ("any key to continue...", 1, 1, font4, white)
getKey
%%%%%%%%%%
%THE GAME%
%%%%%%%%%%
View.Set ("graphics:100;100,offscreenonly")
var tiles : flexible array 0 .. 0 of int
var level1 : string := "level1.txt"
var level2 : string := "map.txt"
var FileNo : int
var totalX, totalY : int := 0
function Array (x, y : int) : int
result totalX * y + x
end Array
open : FileNo, level1, get
get : FileNo, totalX
get : FileNo, totalY
new tiles, totalX * totalY - 1
for decreasing y : totalY - 1 .. 0
for x : 0 .. totalX - 1
get : FileNo, tiles (Array (x, y ))
end for
end for
close (FileNo )
procedure reDraw
for x : 0 .. totalX - 1
for y : 0 .. totalY - 1
if tiles (Array (x, y )) = 1 then
Draw.FillBox (x * 10, y * 10, x * 10 + 10, y * 10 + 10, red)
else
Draw.FillBox (x * 10, y * 10, x * 10 + 10, y * 10 + 10, black)
end if
end for
end for
end reDraw
var keys : array char of boolean
var px, py : int := 1
var px1, py1, px2, py2 : int
var gx, gy, gx2, gy2 : int
var l2gx, l2gy, l2gx2, l2gy2 : int
var l2px, l2py, l2px2, l2py2 : int
loop
reDraw
Input.KeyDown (keys )
if keys (KEY_LEFT_ARROW) and tiles (Array (px - 1, py )) = 0 then
px - = 1
end if
if keys (KEY_RIGHT_ARROW) and tiles (Array (px + 1, py )) = 0 then
px + = 1
end if
if keys (KEY_DOWN_ARROW) and tiles (Array (px, py - 1)) = 0 then
py - = 1
end if
if keys (KEY_UP_ARROW) and tiles (Array (px, py + 1)) = 0 then
py + = 1
end if
%%%%%%%%%%%%%%%%%%%%
gx := 40
gy := 50
gx2 := gx + 10
gy2 := gy + 10
px1 := px * 10
py1 := py * 10
px2 := px * 10 + 10
py2 := py * 10 + 10
%%%%%%%%%%%%%%%%%%%%
Draw.FillBox (gx, gy, gx2, gy2, 0)
Draw.FillBox (px1, py1, px2, py2, 48)
View.Update
delay (60)
%%%%%%%%%%%%%%%%%%COLLISION%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if px1 = gx and py1 = gy and px2 = gx2 and py2 = gy2 then
cls
View.Set ("graphics:200;200,offscreenonly")
open : FileNo, level2, get
get : FileNo, totalX
get : FileNo, totalY
new tiles, totalX * totalY - 1
for decreasing y : totalY - 1 .. 0
for x : 0 .. totalX - 1
get : FileNo, tiles (Array (x, y ))
end for
end for
close (FileNo )
end if
end loop
%%%%%%%%%%%%%%%%%COLLISION END%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%GOAL VARIABLES%
l2gx := 170
l2gy := 170
l2gx2 := l2gx + 10
l2gy2 := l2gy + 10
%PLAYER VARIABLES%
l2px := px * 10
l2py := py * 10
l2px2 := l2px + 10
l2py2 := l2py + 10
%MAIN SEQUENCE
loop
Draw.FillBox (l2gx, l2gy, l2gx2, l2gy2, 0)
Draw.FillBox (l2px, l2py, l2px2, l2py2, 48)
end loop
| [/syntax] |
|
|
|
|
 |
CodeMonkey2000
|
Posted: Sat Nov 01, 2008 9:45 pm Post subject: Re: Changelevel in a maze upon collision help. |
|
|
MattehMatt @ Sat Nov 01, 2008 7:09 pm wrote: Thanks for the tip, ill try that out.
About that collision tutorial, I read it, multiple times, but I couldnt seem to wrap my head around it  ill try what you gave me and post again later if I can't get it
The basic idea is that you store the world in a 2D matrix (array). Each element in the matrix represent an nXn area of pixels of screen. So if each element represented 10x10 pixels, element at [2,2] represents the pixels from 20,20 to 30,30. Now if you are given a pixel coordinate: 25,26 for example, you can figure out what element you are in using some basic math. If each element represents an area of 10x10 pixels, then you floor divide (divide and round down) 25 and 26 to get 2,2 (25/10=2 and 26/10 =2). Intuitively you should see why this is true. It's very basic math, it has been used in many games from the snes and nes era. So the point 25,26 lies in element 2,2. This method is much faster since you no longer need to compare one object to all other object; you immediately know what is in the area. This method is also better since pathfinding for AI is MUCH easier since you are using a 2d matrix.
I plan to rewrite that tutorial one of these days... |
|
|
|
|
 |
|
|