/[projet1]/users/barnsey123/grid/mainbollocks.c
Defence Force logotype

Contents of /users/barnsey123/grid/mainbollocks.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 451 - (show annotations)
Fri Apr 15 07:36:17 2011 UTC (9 years, 1 month ago) by barnsey123
File MIME type: text/plain
File size: 6107 byte(s)


1 // main.c by Barnsey123
2 // 22-03-2011 prog to draw a grid (for viking game)
3 // 23-03-2011 create new drawgrid function
4 // 01-04-2011 create new drawtile functions and use compact code (char instead of int etc)
5 // 02-04-2011 routine to read keyboard
6 // 06-04-2011 cursor drawing correct
7
8 #include <lib.h>
9 // DRAWBOX - draw a box at x,y length xbox
10 void drawbox(int x, int y, int z)
11 {
12 /*
13 x=xcoord for curset
14 y=ycoord for curset
15 z=size of line to draw
16 */
17 curset(x,y,1);
18 draw(z,0,1);
19 draw(0,z,1);
20 draw(-z,0,1);
21 draw(0,-z,1);
22 }
23 // DRAW CURSOR
24 void drawcursor( int x, int y, int z, int f)
25 {
26 pattern(170);
27 curset(x,y,f);
28 draw(z,0,f);
29 draw(0,z,f);
30 draw(-z,0,f);
31 draw(0,-z,f);
32 pattern(255);
33 }
34 // DRAW DEFENDERS TILE
35 void drawdefendtile(int x, int y)
36 {
37 // draws the defenders tile (a series of 2 concentric squares )
38 // ARGUMENTS starting x, y position for curset
39 int loop=0; // loop counter
40 int z=13; // line length
41 int a=x;
42 int b=y;
43 for (loop=0;loop<2;loop++)
44 {
45 drawbox(a,b,z); // drawbox at x,y position
46 z=z-4; // decrease size of line
47 a+=2; // increase x position
48 b+=2; // increase y position
49 }
50 }
51 // DRAW ATTACKERS TILE
52 void drawattacktile(int x, int y)
53 {
54 // draws the attackers tile ( a series of 3 concentric squares )
55 // ARGUMENTS starting x, y position for curset
56 int loop=0; // loop counter
57 int a=x;
58 int b=y;
59 int z=13; // line length
60 for (loop=0;loop<3;loop++)
61 {
62 drawbox(a,b,z); // drawbox at x,y position
63 z=z-4; // decrease size of line
64 a+=2; // increase x position
65 b+=2; // increase y position
66 }
67 }
68 // DRAW KINGS TILE
69 void drawkingtile( int x, int y)
70 {
71 /* draws the king tile ( a series of 5 sqares - one in each corner of the tile and one
72 square straddling all 4 ) nice, simple pattern.
73 ARGUMENTS starting x, y position for curset
74 */
75 drawbox(x,y,5); // box 1
76 drawbox(x+8,y,5); // box 2
77 drawbox(x,y+8,5); // box 3
78 drawbox(x+8,y+8,5); // box 4
79 drawbox(x+2,y+2,9); // box 5 (central, larger box)
80 }
81 // DRAW ALL THE TILES ON THE BOARD
82 void drawtiles(int x, int y, int z)
83 {
84 /* routine to draw the "background tiles"
85 Tile types:
86 0=blank
87 1=king square
88 2=attacker square
89 3=defender square
90 */
91 int tilex=x; // starting tile x position
92 int tiley=y; // starting tile y position
93 int row=0; // loop through tiles array by row
94 int col=0; // loop through tiles array by column
95 // Populate array with tile types
96 int tiles[11][11]={
97 {1,0,0,2,2,2,2,2,0,0,1},
98 {0,0,0,0,0,2,0,0,0,0,0},
99 {0,0,0,0,0,0,0,0,0,0,0},
100 {2,0,0,0,0,3,0,0,0,0,2},
101 {2,0,0,0,3,3,3,0,0,0,2},
102 {2,2,0,3,3,1,3,3,0,2,2},
103 {2,0,0,0,3,3,3,0,0,0,2},
104 {2,0,0,0,0,3,0,0,0,0,2},
105 {0,0,0,0,0,0,0,0,0,0,0},
106 {0,0,0,0,0,2,0,0,0,0,0},
107 {1,0,0,2,2,2,2,2,0,0,1}};
108 tilex+=2; // starting tile x position (offset by 2 to avoid grid lines)
109 tiley+=2; // starting tile y position (offset by 2 to avoid grid lines)
110 for (row=0;row<11;row++)
111 {
112 for (col=0;col<11;col++)
113 {
114 if ( tiles[row][col] == 1 ) drawkingtile(tilex,tiley); // draw king tile
115 if ( tiles[row][col] == 2 ) drawattacktile(tilex,tiley); // draw attackers tile
116 if ( tiles[row][col] == 3 ) drawdefendtile(tilex,tiley); // draw defenders tile
117 tilex+=z; // the x position is advanced to the next square "across" - including blank squares
118 }
119 tilex=x; // reset tilex
120 tilex+=2; // tilex offset to avoid gridlines
121 tiley+=z; // advance tiley position to next "row"
122 }
123 }
124 // DRAW THE MAIN 11x11 GRID
125 void drawgrid(unsigned char x, unsigned char y, unsigned char z)
126 {
127 unsigned char a=x; // copy of boardx (board offset x position)
128 unsigned char b=y; // copy of boardy (board offset y position)
129 unsigned char c=0; // loop counter
130 unsigned char length=z; // copy of boxsize
131 length*=11; // multiply length by 11 (determines length of line to draw)
132 for (c=0;c<=11;c++) // draw vertical lines
133 {
134 curset(a,b,1);
135 draw(0,length,1);
136 a+=z; // add boxsize to a
137 }
138 a=x; // reset a
139 for (c=0;c<=11;c++) // draw horizontal lines
140 {
141 curset(a,b,1);
142 draw(length,0,1);
143 b+=z; // add boxsize to b
144 }
145 }
146 // DRAW THE BOARD
147 /*
148 void drawboard(const int x, const int y, const int z)
149 {
150 ink(6); // color of board
151 drawgrid(x,y,z); // draw the 11x11 gridlines
152 drawtiles(x,y,z); // draw the background tiles
153 }
154 */
155 // playerturn
156 void playerturn(unsigned char x, unsigned char y, unsigned char z) // filter keyboard input
157 {
158 int key=0;
159 unsigned char cx=z;
160 unsigned char cy=z;
161 unsigned char cz=z;
162 unsigned char ns=5, ew=5; // north-south=5, east-west=5
163 cz=cz-2;
164 cx=cx*ew;
165 cy=cy*ns;
166 cx=cx+x;
167 cy=cy+y;
168 cx++;
169 cy++;
170 drawcursor(cx,cy,cz,1); // print dotted cursor at central square
171 while (key != 81) // repeat until 'Q' pressed
172 {
173 key=getchar(); // get code of pressed key
174 if ((key == 8 )&&(ew>0)) // cursor left
175 {
176 drawcursor(cx,cy,cz,0); // print blank cursor (effect=remove dots)
177 cx=cx-17;
178 drawcursor(cx,cy,cz,1); // print dotted cursor
179 ew--;
180 }
181 if ((key == 9)&&(ew<10)) // cursor right
182 {
183 drawcursor(cx,cy,cz,0); // print blank cursor (effect=remove dots)
184 cx=cx+17;
185 drawcursor(cx,cy,cz,1); // print dotted cursor
186 ew++;
187 }
188 if ((key == 10)&&(ns<10)) // cursor down
189 {
190 drawcursor(cx,cy,cz,0); // print blank cursor (effect=remove dots)
191 cy=cy+17;
192 drawcursor(cx,cy,cz,1); // print dotted cursor
193 ns++;
194 }
195 if ((key == 11)&&(ns>0)) // cursor up
196 {
197 drawcursor(cx,cy,cz,0); // print blank cursor (effect=remove dots)
198 cy=cy-17;
199 drawcursor(cx,cy,cz,1); // print dotted cursor
200 ns--;
201 }
202 }
203 }
204 main()
205 {
206 int boardx=12; // starting x co-ord of board
207 int boardy=0; // starting y co-ord of board
208 int boxsize=17; // set boxsize
209 hires();
210 ink(6);
211 //drawboard(boardx,boardy,boxsize); // draw the board
212
213 //drawgrid(boardx,boardy,boxsize);
214 drawtiles(boardx,boardy,boxsize);
215 //playerturn(boardx,boardy,boxsize);
216 }

  ViewVC Help
Powered by ViewVC 1.1.26