Two dimential array map grid????
Author |
Message |
kirstenpratt
|
Posted: Wed Feb 28, 2007 3:57 pm Post subject: Two dimential array map grid???? |
|
|
Hello, im making a pacman game for a school project, i used a picture for the background, and was trying to use lines, and Math.DistancePointLine but a person told me to find out how to solve the problem better, well first off my problem is that my pacman eats the background, and i need to make a 2 dimentional array to say were pacman can go and cant go, i looked at the toutorials , but i still dont get how to make it so it will work. Please if u can post a solution to this probleme if you have had it before, or even explain 2 dimentional arrays and how to use them to make a grid, thank you please respond =D
here is my code;
code: |
%Kirsten Pratt
% PacMan game omg this is gun take so long
%%DECLARATION OF VARIABLES
var x, y : int % pacmans center location
var dx, dy : int %direction pacman moves
var eyeright, eyeup : int
var chars : array char of boolean
var mouth : int := 0 %size of mouth
var dir : int := 0
var exit1 : boolean := false
var picId : array 1 .. 10 of int
picId (1) := Pic.FileNew ("pacman map.bmp")
picId (2) := Pic.FileNew ("pacman fruit.bmp")
picId (3) := Pic.FileNew ("pacman get ready.bmp")
picId (4) := Pic.FileNew ("pacman ghost.bmp")
picId (5) := Pic.FileNew ("pacman life symbol.bmp")
picId (6) := Pic.FileNew ("pacman score.bmp")
x := 60
y := 40
eyeright := 3
eyeup := 9
dx := 1
dy := 0
%%% BORDER AND SCREEN SETUP
setscreen ("graphics:1000;650")
setscreen ("nocursour;noecho")
colorback (black)
cls
Pic.Draw (picId (1), 0, 0, 0)
Draw.ThickLine (0, 0, maxx, 0, 10, brightblue)
Draw.ThickLine (0, 0, 0, maxy, 10, brightblue)
Draw.ThickLine (0, maxy, maxx, maxy, 10, brightblue)
Draw.ThickLine (maxx, 0, maxx, maxy, 10, brightblue)
%%% PACMAN MOUTH loop
loop
Input.KeyDown (chars)
if chars (KEY_RIGHT_ARROW) then
dir := 360
dx := +1
dy := 0
eyeright := 3
eyeup := 9
elsif chars (KEY_LEFT_ARROW) then
dir := 180
dx := -1
dy := 0
eyeright := -3
eyeup := 9
elsif chars (KEY_UP_ARROW) then
dir := 90
dy := +1
dx := 0
eyeright := -10
eyeup := 3
elsif chars (KEY_DOWN_ARROW) then
dir := 270
dy := -1
dx := 0
eyeright := -10
eyeup := -3
elsif chars (KEY_ESC) then
exit1 := true
exit when exit1 = true
end if
if whatdotcolor (x + 20, y + 20) not= black then
dy := 0
dx := 0
y := y + 1
y := y - 1
x := x + 1
x := x - 1
end if
if Math.DistancePointLine (x, y, 0, 0, maxx, 0) < 27 then
dy := 0
dx := 0
y := y + 1
elsif Math.DistancePointLine (x, y, 0, 0, 0, maxy) < 27 then
dy := 0
dx := 0
x := x + 1
elsif Math.DistancePointLine (x, y, 0, maxy, maxx, maxy) < 27 then
dy := 0
dx := 0
y := y - 1
elsif Math.DistancePointLine (x, y, maxx, 0, maxx, maxy) < 27 then
dy := 0
dx := 0
x := x - 1
end if
y := y + dy
x := x + dx
mouth := mouth mod 35 + 1
Draw.FillArc (x, y, 15, 15, mouth + dir, -mouth + dir, yellow)
Draw.FillOval (x + eyeright, y + eyeup, 2, 2, black)
delay (5) %%%CHANGE PACMANS SPEED
Draw.FillArc (x, y, 15, 15, mouth + dir, -mouth + dir, black)
exit when exit1 = true
end loop
Window.Hide (-1)
|
Thank you =P |
|
|
|
|
|
Sponsor Sponsor
|
|
|
BenLi
|
Posted: Wed Feb 28, 2007 5:03 pm Post subject: RE:Two dimential array map grid???? |
|
|
okay, you need to understand one dimensional before you venture into multi dimensional arrays. So basically, an array is a mass of variables that are know by one name but referenced by numbers. I suggest doing the questions that the tutorial suggests you complete to acquaint yourself with the concept |
|
|
|
|
|
CodeMonkey2000
|
Posted: Wed Feb 28, 2007 6:50 pm Post subject: Re: Two dimential array map grid???? |
|
|
Here is some code using 2d mapping. I commented it as best as I could. Hope it helps. Turing: | setscreen ("offscreenonly,graphics:300;300")
/*
the tiles are going to be 10x10 pixels
and since there are 300 pixels on the x coordinate we can have 30 blocks on it
similarly since there are 300 pixels on the y coordinate we can have 30 bloacks on it
in total we have 90 blocks because 30x30=90
*/
var tile : array 0 .. 29, 0 .. 29 of int
%the variable for the player x,y is the real position cx,cy is the position on our grid
var player :
record
x, y, cx, cy : int
end record
%for input.keydown
var key : array char of boolean
%to get the tile types
var fileName : string := "map.txt"
var fileNo : int := 0
%gets the tile types in the data file
open : fileNo, fileName, get
for decreasing y : 29 .. 0
for x : 0 .. 29
get : fileNo, tile (x, y )
end for
end for
close : fileNo
%draws the map
proc redraw
for y : 0 .. 29
for x : 0 .. 29
if tile (x, y ) = 0 then
% this draws an open square
%the rest draw closed squares of different colours
drawfillbox (10 * x, y * 10, 10 + 10 * x, y * 10 + 10, black)
elsif tile (x, y ) = 1 then
drawfillbox (10 * x, y * 10, 10 + 10 * x, y * 10 + 10, red)
elsif tile (x, y ) = 2 then
drawfillbox (10 * x, y * 10, 10 + 10 * x, y * 10 + 10, blue)
elsif tile (x, y ) = 3 then
drawfillbox (10 * x, y * 10, 10 + 10 * x, y * 10 + 10, green)
end if
end for
end for
end redraw
%determins weather the square you are going to be in is open or not
function collide (x, y : int) : boolean
result tile (x, y ) = 0
end collide
player.x := 0
player.y := 0
%cx and cy keeps track of where you are in terms of our grid and x,y keeps track of where you are on the screen
player.cx := 0
player.cy := 0
%puts everything together
loop
Input.KeyDown (key )
redraw
if key ('w') and collide (player.cx, player.cy + 1) then
player.y + = 10
player.cy + = 1 %moved up 1 y unit in our grid
end if
if key ('s') and collide (player.cx, player.cy - 1) then
player.y - = 10
player.cy - = 1 %moved down 1 y unit in our grid
end if
if key ('d') and collide (player.cx + 1, player.cy ) then
player.x + = 10
player.cx + = 1 %moved up 1 x unit in our grid
end if
if key ('a') and collide (player.cx - 1, player.cy ) then
player.x - = 10
player.cx - = 1 %moved down 1 x unit in our grid
end if
drawfillbox (player.x, player.y, player.x + 10, player.y + 10, yellow)
View.Update
Time.DelaySinceLast (50)
end loop
|
Oh and here is a sample map I created. Just save it as map.txt
code: | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 0 0 0 0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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1
1 0 0 0 1 0 0 0 0 0 2 2 2 2 2 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1
1 0 0 0 1 1 2 2 0 0 0 0 0 0 2 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 2 2 2 2 2 2 2 1 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 |
|
|
|
|
|
|
kirstenpratt
|
Posted: Wed Feb 28, 2007 9:27 pm Post subject: Re: Two dimential array map grid???? |
|
|
ok thankyou your code is awesome but how can i fit that in with my pacman?i dont get it if you could can u help thank you so much |
|
|
|
|
|
CodeMonkey2000
|
|
|
|
|
|
|