|
下面是部分源代码(显示器是1024*768,可以更具自身显示器大小改动代码)- #include <stdio.h>
- #include <fcntl.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/types.h>
- #include "fb.h"
- #include "bmp.h"
- #include <netinet/in.h>
- #include <sys/select.h>
- #include <sys/ioctl.h>
- #include <unistd.h>
- #include <poll.h>
- #define PORT 1000
- #define SIZE 1024
- #define WIDTH 15
- #define HEIGHT 15
- #define pith 33
- #define MPATH "/dev/input/mouse0"
- #define PATH "map.txt"
- char buf1[SIZE];
- struct Game{
- struct Framebuffer *fb;
- struct Bmp *map, *cur, *white, *black;
- struct Rect rect_map, rect_cur, rect_zi;
- int mouse_fd;
- char buf[3];
- int flag;
- int x0, y0, width, height;
- struct pixel_32 gate;
- };
- void draw_map(struct Game *game, int map[][WIDTH])
- {
- int i, j;
- struct Rect rect = game->rect_map;
- show_bmpfile_full(game->map, game->fb, &game->rect_map, NULL, 1, 1);
- for (i = 0; i < HEIGHT; i++)
- {
- for (j = 0; j < WIDTH; j++)
- {
- if (map[i][j])
- {
- rect.x = j * pith + 227 - 10 + 300;
- rect.y = i * pith + 193 + 22;
- if (map[i][j] == 1)
- show_bmpfile(game->black, game->fb, &game->rect_zi, &rect, 1, 1);
- else if (map[i][j] == 2)
- show_bmpfile(game->white, game->fb, &game->rect_zi, &rect, 1, 1);
-
- }
- }
- }
-
- }
- void init_game(struct Game *game, int map[][WIDTH])
- {
- struct Rect rect = {200, 200, 1024, 768};
- game->fb = open_fb(1);
- game->map = open_bmpfile("5.bmp");
- game->cur = open_bmpfile("cur.bmp");
- game->white = open_bmpfile("wz.bmp");
- game->black = open_bmpfile("bz.bmp");
- game->rect_map = game->rect_cur = game->rect_zi = rect;
- game->rect_zi.x = 3;
- game->rect_zi.y = 3;
- game->rect_zi.height = 25;
- game->rect_zi.width = 25;
- game->gate.red = 255;
- game->gate.green = 255;
- game->gate.blue = 255;
- game->x0 = 200;
- game->y0 = 190;
- game->width = game->map->width;
- game->height = game->map->height;
- game->mouse_fd = open(MPATH, O_RDWR);
- draw_map(game, map);
- show_bmpfile_full(game->cur, game->fb, &game->rect_cur, &game->gate, 1, 1);
- fflush_fb(game->fb);
- }
- int is_win(int map[][WIDTH], int x, int y)
- {
- int i;
- int count[4] = {0}, flag[8] = {1, 1, 1, 1, 1, 1, 1, 1};
- for (i = 1; i < 5; i++)
- {
- if (x - i >= 0 && map[y][x - i] == map[y][x] && flag[0])
- count[0]++;
- else
- flag[0] = 0;
- if (x + i < WIDTH && map[y][x + i] == map[y][x] && flag[1])
- count[0]++;
- else
- flag[1] = 0;
- if (y - i >= 0 && map[y - i][x] == map[y][x] && flag[2])
- count[1]++;
- else
- flag[2] = 0;
- if (y + i < HEIGHT && map[y + i][x] == map[y][x] && flag[3])
- count[1]++;
- else
- flag[3] = 0;
- if (x - i >= 0 && y - i >= 0 && map[y - i][x - i] == map[y][x] && flag[4])
- count[2]++;
- else
- flag[4] = 0;
- if (x + i < WIDTH && y + i < HEIGHT && map[y + i][x + i] == map[y][x] && flag[5])
- count[2]++;
- else
- flag[5] = 0;
- if (x + i < WIDTH && y - i >= 0 && map[y - i][x + i] == map[y][x] && flag[6])
- count[3]++;
- else
- flag[6] = 0;
- if (x - i >= 0 && y + i < HEIGHT && map[y + i][x - i] == map[y][x] && flag[7])
- count[3]++;
- else
- flag[7] = 0;
- }
- for (i = 0; i < 4; i++)
- if (count[i] >= 4)
- return map[y][x];
- return 0;
- }
- int play_game(struct Game *game, int map[][WIDTH],int flag)
- {
- struct Rect rect;
- int x, y;
- int x1, y1;
- x = game->rect_cur.x - 227;
- x1 = x / pith * pith;
- if (x <= x1 + 16)
- x = x1;
- else
- x = (x1 / pith + 1) * pith;
- x1 = x / pith;
- y = game->rect_cur.y - 193;
- y1 = y / pith * pith;
- if (y <= y1 + 16)
- y = y1 ;
- else
- y = (y1 / pith + 1) * pith;
- y1 = y / pith;
- rect = game->rect_map;
- rect.x = x + 227 - 10 + 300;
- rect.y = y + 193 + 22;
- rect.width = 1024;
- rect.height = 768;
- if (map[y1][x1] == 0)
- {
- if (flag)
- {
- map[y1][x1] = 1;
- show_bmpfile(game->black, game->fb, &game->rect_zi, &rect, 1, 1);
- flag = 0;
- }
- else
- {
- map[y1][x1] = 2;
- show_bmpfile(game->white, game->fb, &game->rect_zi, &rect, 1, 1);
- flag = 1;
- }
- if (is_win(map, x1, y1))
- return map[y1][x1];
- }
- game->flag = flag;
- return 0;
- }
- void game_over(struct Game *game, int ch)
- {
- fflush_fb(game->fb);
- close(game->mouse_fd);
- printf("Game over!\n");
- }
- int main(void)
- {
- int fd;
- system("clear");
- char buf1[SIZE];
- static int flag = 1;
- printf("请输入对方的ip:");
- scanf("%s",buf1);
- int bufz[4]={1,1,1,0};
- fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
- if (fd == -1)
- return -1;
- struct sockaddr_in self_addr, con_addr;
- bzero(&self_addr, sizeof(self_addr));
- self_addr.sin_family = PF_INET;
- self_addr.sin_port = htons(PORT);
- self_addr.sin_addr.s_addr = htonl(INADDR_ANY);
- bzero(&con_addr, sizeof(con_addr));
- con_addr.sin_family = PF_INET;
- con_addr.sin_port = htons(PORT);
- con_addr.sin_addr.s_addr =inet_addr(buf1);//("127.0.0.1");// htonl(INADDR_ANY);
- bind(fd, (struct sockaddr *)&self_addr, sizeof(self_addr));
- int len = sizeof(struct sockaddr);
- int map[HEIGHT][WIDTH] = {0};
- int ret;
- int fok;
- struct Game *game;
- game = malloc(sizeof(struct Game));
- init_game(game, map);
- int win = 1;
- struct pollfd polfd[2];
- int fk;
- polfd[0].fd = game->mouse_fd;
- polfd[0].events = POLLIN;
- polfd[1].fd = fd;
- polfd[1].events = POLLIN;
- int xian = 0;
- while(win)
- {
- poll(polfd, 2, -1);
- polfd[0].fd = game->mouse_fd;
- polfd[0].events = POLLIN;
- polfd[1].fd = fd;
- polfd[1].events = POLLIN;
- if(POLLIN == polfd[0].revents)
- {
- draw_map(game, map);
- read(game->mouse_fd, game->buf, 3);
- game->rect_cur.x += game->buf[1];
- if (game->rect_cur.x < game->x0)
- game->rect_cur.x = game->x0;
- else if (game->rect_cur.x > game->x0 + game->width - 20)
- game->rect_cur.x = game->x0 + game->width - 20;
- game->rect_cur.y += game->buf[2];
- if (game->rect_cur.y < game->y0)
- game->rect_cur.y = game->y0;
- else if (game->rect_cur.y > game->y0 + game->height - 30)
- game->rect_cur.y = game->y0 + game->height - 30;
- recvfrom(fd, bufz, sizeof(bufz),MSG_DONTWAIT, (struct sockaddr *)&con_addr, &len);
- if(bufz[3] == 0)
- xian == 1;
- else if(bufz[3] == 1)
- xian == 0;
- if ((game->buf[0] & 0x1 == 1) && (bufz[2] == (1 - xian)))
- {
- ret = play_game(game, map,(1 - xian));
- bufz[0]=game->rect_cur.x,bufz[1]=game->rect_cur.y,bufz[2]=game->flag,bufz[3] == (1 - xian);
- sendto(fd, bufz, sizeof(bufz), MSG_DONTWAIT, (struct sockaddr *)&con_addr, len);
- if (ret == -1)
- win = 0;
- if (ret > 0)
- break;
- }
- }
- if(POLLIN == polfd[1].revents)
- {
- recvfrom(fd, bufz, sizeof(bufz), MSG_DONTWAIT, (struct sockaddr *)&con_addr, &len);
- // if(bufz[2] == 1)
- {
- if(bufz[3] == 0)
- xian == 1;
- else if(bufz[3] == 1)
- xian == 0;
- game->rect_cur.x = bufz[0],game->rect_cur.y = bufz[1],game->flag = bufz[2];
- ret = play_game(game, map,xian);
- if (ret == -1)
- win = 0;
- if (ret > 0)
- break;
- }
- }
- show_bmpfile_full(game->cur, game->fb, &game->rect_cur, &game->gate, 1, 1);
- fflush_fb(game->fb);
- }
- close(fd);
- game_over(game, ret);
- return 0;
- }
复制代码 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
阿莫论坛20周年了!感谢大家的支持与爱护!!
你熬了10碗粥,别人一桶水倒进去,淘走90碗,剩下10碗给你,你看似没亏,其实你那10碗已经没有之前的裹腹了,人家的一桶水换90碗,继续卖。说白了,通货膨胀就是,你的钱是挣来的,他的钱是印来的,掺和在一起,你的钱就贬值了。
|