| 1 | #include <stdlib.h>
|
| 2 | #include <stdio.h>
|
| 3 | #include <string.h>
|
| 4 | #include <time.h>
|
| 5 | #include <math.h>
|
| 6 | #include <stdint.h>
|
| 7 | #include "wgif.h"
|
| 8 |
|
| 9 | #define WIDTH 300
|
| 10 | #define HEIGHT 300
|
| 11 |
|
| 12 |
|
| 13 | int fwr(uint8_t *ptr, size_t bytes, void *wp) {
|
| 14 | return fwrite(ptr, 1, bytes, wp);
|
| 15 | }
|
| 16 |
|
| 17 | struct food {
|
| 18 | int x, y;
|
| 19 | };
|
| 20 |
|
| 21 | #define FC 10
|
| 22 | struct food food[FC];
|
| 23 |
|
| 24 | int dir=2;
|
| 25 | int maul=1;
|
| 26 | int cx, cy;
|
| 27 |
|
| 28 | void drawpac(uint8_t *buf) {
|
| 29 | #define L 33
|
| 30 | int i, j, l, k;
|
| 31 | int c;
|
| 32 |
|
| 33 | for (i=0;i<L;i++) {
|
| 34 | for(j=0;j<L;j++) {
|
| 35 | l=16-i;
|
| 36 | k=16-j;
|
| 37 | if (sqrt(l*l+k*k)<15) {
|
| 38 | c=1;
|
| 39 | } else {
|
| 40 | c=0;
|
| 41 | }
|
| 42 | buf[j+L*i]=c;
|
| 43 | }
|
| 44 | }
|
| 45 |
|
| 46 | if (maul) {
|
| 47 | switch (dir) {
|
| 48 | case 6: {
|
| 49 | for (i=0;i<L;i++) {
|
| 50 | for (j=0;j<L;j++) {
|
| 51 | if (j<15 && (i<L-j && i>j)) {
|
| 52 | buf[j+L*i]=0;
|
| 53 | }
|
| 54 | }
|
| 55 | }
|
| 56 | break;
|
| 57 | }
|
| 58 | case 2: {
|
| 59 | for (i=0;i<L;i++) {
|
| 60 | for (j=0;j<L;j++) {
|
| 61 | if (j>15 && (i>L-j && i<j)) {
|
| 62 | buf[j+L*i]=0;
|
| 63 | }
|
| 64 | }
|
| 65 | }
|
| 66 | break;
|
| 67 | }
|
| 68 | case 4: {
|
| 69 | for (i=0;i<L;i++) {
|
| 70 | for (j=0;j<L;j++) {
|
| 71 | if ((i>L-j && j<=15) || (i>j && j>15)) {
|
| 72 | buf[j+L*i]=0;
|
| 73 | }
|
| 74 | }
|
| 75 | }
|
| 76 | break;
|
| 77 | }
|
| 78 | case 0: {
|
| 79 | for (i=0;i<L;i++) {
|
| 80 | for (j=0;j<L;j++) {
|
| 81 | if ((i<j &&j<=15) || (j>15 && i<31-j)) {
|
| 82 | buf[j+L*i]=0;
|
| 83 | }
|
| 84 | }
|
| 85 | }
|
| 86 | break;
|
| 87 | }
|
| 88 | case 1: {
|
| 89 | for (i=0;i<L;i++) {
|
| 90 | for (j=0;j<L;j++) {
|
| 91 | if (j>15 && i<15) {
|
| 92 | buf[j+L*i]=3;
|
| 93 | }
|
| 94 | }
|
| 95 | }
|
| 96 | break;
|
| 97 | }
|
| 98 | case 7: {
|
| 99 | for (i=0;i<L;i++) {
|
| 100 | for (j=0;j<L;j++) {
|
| 101 | if (j<15 && i<15) {
|
| 102 | buf[j+L*i]=0;
|
| 103 | }
|
| 104 | }
|
| 105 | }
|
| 106 | break;
|
| 107 | }
|
| 108 | case 3: {
|
| 109 | for (i=0;i<L;i++) {
|
| 110 | for (j=0;j<L;j++) {
|
| 111 | if (j>15 && i>15) {
|
| 112 | buf[j+L*i]=0;
|
| 113 | }
|
| 114 | }
|
| 115 | }
|
| 116 | break;
|
| 117 | }
|
| 118 | case 5: {
|
| 119 | for (i=0;i<L;i++) {
|
| 120 | for (j=0;j<L;j++) {
|
| 121 | if (j<15 && i>15) {
|
| 122 | buf[j+L*i]=0;
|
| 123 | }
|
| 124 | }
|
| 125 | }
|
| 126 | break;
|
| 127 | }
|
| 128 | }
|
| 129 | }
|
| 130 |
|
| 131 | for (j=0;j<L;j++) {
|
| 132 | for (i=0;i<L;i++) {
|
| 133 | for (k=0;k<FC;k++) {
|
| 134 | if (food[k].x==-1||food[k].y==-1) {
|
| 135 | continue;
|
| 136 | }
|
| 137 | if (buf[i+j*L]!=1&&cx-16+i>=food[k].x && cx-16+i<food[k].x+5 && cy-16+j>=food[k].y && cy-16+j<food[k].y+5) {
|
| 138 | buf[i+j*L]=2;
|
| 139 | }
|
| 140 | }
|
| 141 | }
|
| 142 | }
|
| 143 | }
|
| 144 |
|
| 145 | void drawfood(uint8_t *b, int x, int y) {
|
| 146 | int i, j;
|
| 147 |
|
| 148 | for (i=0;i<5;i++) {
|
| 149 | for (j=0;j<5;j++) {
|
| 150 | b[x+j+(y+i)*WIDTH]=2;
|
| 151 | }
|
| 152 | }
|
| 153 | }
|
| 154 |
|
| 155 | int main() {
|
| 156 | uint32_t ct[4];
|
| 157 | struct gif_file gf;
|
| 158 | uint8_t ico[WIDTH*HEIGHT];
|
| 159 | int k;
|
| 160 | int cur_food;
|
| 161 | int foodcnt;
|
| 162 |
|
| 163 | srand(time(0));
|
| 164 |
|
| 165 | ct[0]=0x000000;
|
| 166 | ct[1]=0xffff00;
|
| 167 | ct[2]=0xbccccc;
|
| 168 | memset(ico, 0, sizeof(ico));
|
| 169 |
|
| 170 | gf.wr_fnct=fwr;
|
| 171 | gf.wparm=fopen("pacman.gif", "wb");
|
| 172 |
|
| 173 | gif_init(&gf, WIDTH, HEIGHT, ct, 3, 0);
|
| 174 | gif_ext_loop(&gf, 0);
|
| 175 | cx=WIDTH>>1;
|
| 176 | cy=HEIGHT>>1;
|
| 177 |
|
| 178 | for (k=0;k<FC;k++) {
|
| 179 | int maxx=WIDTH-22;
|
| 180 | int maxy=HEIGHT-22;
|
| 181 |
|
| 182 | food[k].x=rand()%maxx+17;
|
| 183 | food[k].y=rand()%maxy+17;
|
| 184 |
|
| 185 | drawfood(ico, food[k].x, food[k].y);
|
| 186 | }
|
| 187 |
|
| 188 | gif_image(&gf, 0, 0, WIDTH, HEIGHT, NULL, 0, ico);
|
| 189 |
|
| 190 | while (1) {
|
| 191 | cur_food=-1;
|
| 192 | foodcnt=0;
|
| 193 | for (k=0;k<FC;k++) {
|
| 194 | if (food[k].x!=-1 && food[k].y!=-1) {
|
| 195 | foodcnt++;
|
| 196 | }
|
| 197 | }
|
| 198 |
|
| 199 | for (k=0;k<FC;k++) {
|
| 200 | if (food[k].x!=-1 && food[k].y!=-1) {
|
| 201 | cur_food=k;
|
| 202 | break;
|
| 203 | }
|
| 204 | }
|
| 205 | if (cur_food==-1) {
|
| 206 | break;
|
| 207 | }
|
| 208 |
|
| 209 | for (k=0;k<FC;k++) {
|
| 210 | double cfl, kl;
|
| 211 |
|
| 212 | if (food[k].x==-1 || food[k].y==-1 || (k==cur_food&&foodcnt>1)) {
|
| 213 | continue;
|
| 214 | }
|
| 215 |
|
| 216 | cfl=sqrt((food[cur_food].x-cx)*(food[cur_food].x-cx) + (food[cur_food].y-cy)*(food[cur_food].y-cy));
|
| 217 | kl=sqrt((food[k].x-cx)*(food[k].x-cx) + (food[k].y-cy)*(food[k].y-cy));
|
| 218 | if (kl<cfl) {
|
| 219 | cur_food=k;
|
| 220 | }
|
| 221 |
|
| 222 | }
|
| 223 | while (abs(cx-food[cur_food].x)>2 || abs(cy-food[cur_food].y)>2) {
|
| 224 | if (abs(cx-food[cur_food].x)==1) {
|
| 225 | cx=food[cur_food].x;
|
| 226 | }
|
| 227 | if (abs(cy-food[cur_food].y)==1) {
|
| 228 | cy=food[cur_food].y;
|
| 229 | }
|
| 230 | if (cx<food[cur_food].x && cy<food[cur_food].y) {
|
| 231 | dir=3;
|
| 232 | cx+=2;
|
| 233 | cy+=2;
|
| 234 | } else if (cx<food[cur_food].x && cy==food[cur_food].y) {
|
| 235 | dir=2;
|
| 236 | cx+=2;
|
| 237 | } else if (cx==food[cur_food].x && cy>food[cur_food].y) {
|
| 238 | dir=0;
|
| 239 | cy-=2;
|
| 240 | } else if (cx<food[cur_food].x && cy>food[cur_food].y) {
|
| 241 | dir=1;
|
| 242 | cx+=2;
|
| 243 | cy-=2;
|
| 244 | } else if (cx>food[cur_food].x && cy>food[cur_food].y) {
|
| 245 | dir=7;
|
| 246 | cx-=2;
|
| 247 | cy-=2;
|
| 248 | } else if (cx>food[cur_food].x && cy==food[cur_food].y) {
|
| 249 | dir=6;
|
| 250 | cx-=2;
|
| 251 | } else if (cx>food[cur_food].x && cy<food[cur_food].y) {
|
| 252 | dir=5;
|
| 253 | cx-=2;
|
| 254 | cy+=2;
|
| 255 | } else if (cx==food[cur_food].x && cy<food[cur_food].y) {
|
| 256 | dir=4;
|
| 257 | cy+=2;
|
| 258 | }
|
| 259 |
|
| 260 | maul=(maul+1)&1;
|
| 261 | drawpac(ico);
|
| 262 | gif_graphicControlExtension(&gf, 8, -1);
|
| 263 | gif_image(&gf, cx-16, cy-16, 33, 33, NULL, 0, ico);
|
| 264 | }
|
| 265 |
|
| 266 | food[cur_food].x=-1;
|
| 267 | food[cur_food].y=-1;
|
| 268 | }
|
| 269 |
|
| 270 |
|
| 271 | gif_end(&gf);
|
| 272 | fclose(gf.wparm);
|
| 273 |
|
| 274 | return 0;
|
| 275 | } |