搜索
bottom↓
回复: 6

linux局域网五子棋代码,电脑显卡和udp协议联系(适合新手)

[复制链接]

出0入0汤圆

发表于 2012-5-5 01:59:34 | 显示全部楼层 |阅读模式
下面是部分源代码(显示器是1024*768,可以更具自身显示器大小改动代码)
  1. #include <stdio.h>
  2. #include <fcntl.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <sys/types.h>
  6. #include "fb.h"
  7. #include "bmp.h"
  8. #include <netinet/in.h>
  9. #include <sys/select.h>
  10. #include <sys/ioctl.h>
  11. #include <unistd.h>
  12. #include <poll.h>

  13. #define PORT 1000
  14. #define SIZE 1024
  15. #define WIDTH 15
  16. #define HEIGHT 15
  17. #define pith 33
  18. #define MPATH "/dev/input/mouse0"

  19. #define PATH "map.txt"
  20. char buf1[SIZE];
  21. struct Game{
  22.         struct Framebuffer *fb;
  23.         struct Bmp *map, *cur, *white, *black;
  24.         struct Rect rect_map, rect_cur, rect_zi;
  25.         int mouse_fd;
  26.         char buf[3];
  27.         int flag;
  28.         int x0, y0, width, height;
  29.         struct pixel_32 gate;
  30. };

  31. void draw_map(struct Game *game, int map[][WIDTH])
  32. {
  33.         int  i, j;
  34.         struct Rect rect = game->rect_map;
  35.         show_bmpfile_full(game->map, game->fb, &game->rect_map, NULL, 1, 1);
  36.         for (i = 0; i < HEIGHT; i++)
  37.         {
  38.                 for (j = 0; j < WIDTH; j++)
  39.                 {
  40.                         if (map[i][j])
  41.                         {
  42.                                 rect.x = j * pith + 227 - 10 + 300;
  43.                                 rect.y = i * pith + 193 + 22;
  44.                                 if (map[i][j] == 1)
  45.                                         show_bmpfile(game->black, game->fb, &game->rect_zi, &rect, 1, 1);
  46.                                 else if (map[i][j] == 2)
  47.                                         show_bmpfile(game->white, game->fb, &game->rect_zi, &rect, 1, 1);
  48.                                
  49.                         }
  50.                 }
  51.         }
  52.                
  53. }
  54. void init_game(struct Game *game, int map[][WIDTH])
  55. {
  56.         struct Rect rect = {200, 200, 1024, 768};
  57.         game->fb = open_fb(1);
  58.         game->map = open_bmpfile("5.bmp");
  59.         game->cur = open_bmpfile("cur.bmp");
  60.         game->white = open_bmpfile("wz.bmp");
  61.         game->black = open_bmpfile("bz.bmp");
  62.         game->rect_map = game->rect_cur = game->rect_zi = rect;
  63.         game->rect_zi.x = 3;
  64.         game->rect_zi.y = 3;
  65.         game->rect_zi.height = 25;
  66.         game->rect_zi.width = 25;
  67.         game->gate.red = 255;
  68.         game->gate.green = 255;
  69.         game->gate.blue = 255;
  70.         game->x0 = 200;
  71.         game->y0 = 190;
  72.         game->width = game->map->width;
  73.         game->height = game->map->height;
  74.         game->mouse_fd = open(MPATH, O_RDWR);
  75.         draw_map(game, map);
  76.         show_bmpfile_full(game->cur, game->fb, &game->rect_cur, &game->gate, 1, 1);
  77.         fflush_fb(game->fb);
  78. }

  79. int is_win(int map[][WIDTH], int x, int y)
  80. {
  81.         int i;
  82.         int count[4] = {0}, flag[8] = {1, 1, 1, 1, 1, 1, 1, 1};
  83.         for (i = 1; i < 5; i++)
  84.         {
  85.                 if (x - i >= 0 && map[y][x - i] == map[y][x] && flag[0])
  86.                         count[0]++;
  87.                 else
  88.                         flag[0] = 0;
  89.                 if (x + i < WIDTH && map[y][x + i] == map[y][x] && flag[1])
  90.                         count[0]++;
  91.                 else
  92.                         flag[1] = 0;
  93.                 if (y - i >= 0 && map[y - i][x] == map[y][x] && flag[2])
  94.                         count[1]++;
  95.                 else
  96.                         flag[2] = 0;
  97.                 if (y + i < HEIGHT && map[y + i][x] == map[y][x] && flag[3])
  98.                         count[1]++;
  99.                 else
  100.                         flag[3] = 0;
  101.                 if (x - i >= 0 && y - i >= 0 && map[y - i][x - i] == map[y][x] && flag[4])
  102.                         count[2]++;
  103.                 else
  104.                         flag[4] = 0;
  105.                 if (x + i < WIDTH  && y + i < HEIGHT && map[y + i][x + i] == map[y][x] && flag[5])
  106.                         count[2]++;
  107.                 else
  108.                         flag[5] = 0;

  109.                 if (x + i < WIDTH && y - i >= 0 && map[y - i][x + i] == map[y][x] && flag[6])
  110.                         count[3]++;
  111.                 else
  112.                         flag[6] = 0;
  113.                 if (x - i >= 0 && y + i < HEIGHT && map[y + i][x - i] == map[y][x] && flag[7])
  114.                         count[3]++;
  115.                 else
  116.                         flag[7] = 0;
  117.         }
  118.         for (i = 0; i < 4; i++)
  119.                 if (count[i] >= 4)
  120.                         return map[y][x];
  121.         return 0;
  122. }
  123. int play_game(struct Game *game, int map[][WIDTH],int flag)
  124. {
  125.         struct Rect rect;

  126.         int x, y;
  127.         int x1, y1;
  128.         x = game->rect_cur.x - 227;
  129.         x1 = x / pith * pith;
  130.         if (x <= x1 + 16)
  131.                 x = x1;
  132.         else
  133.                 x = (x1 / pith + 1) * pith;
  134.         x1 = x / pith;
  135.         y = game->rect_cur.y - 193;
  136.         y1 = y / pith * pith;
  137.         if (y <= y1 + 16)
  138.                 y = y1 ;
  139.         else
  140.                 y = (y1 / pith + 1) * pith;
  141.         y1 = y / pith;
  142.         rect = game->rect_map;
  143.         rect.x = x + 227 - 10 + 300;
  144.         rect.y = y + 193 + 22;
  145.         rect.width = 1024;
  146.         rect.height = 768;
  147.         if (map[y1][x1] == 0)
  148.         {
  149.                 if (flag)
  150.                 {
  151.                       map[y1][x1] = 1;
  152.                       show_bmpfile(game->black, game->fb, &game->rect_zi, &rect, 1, 1);
  153.                       flag = 0;
  154.                 }
  155.                 else
  156.                 {
  157.                         map[y1][x1] = 2;
  158.                         show_bmpfile(game->white, game->fb, &game->rect_zi, &rect, 1, 1);
  159.                         flag = 1;
  160.                 }
  161.                 if (is_win(map, x1, y1))
  162.                         return map[y1][x1];
  163.         }
  164.         game->flag = flag;
  165.         return 0;
  166. }
  167. void game_over(struct Game *game, int ch)
  168. {
  169.         fflush_fb(game->fb);
  170.         close(game->mouse_fd);
  171.         printf("Game over!\n");
  172. }
  173. int main(void)
  174. {
  175.         int fd;
  176.         system("clear");
  177.         char buf1[SIZE];
  178.         static int flag = 1;
  179.         printf("请输入对方的ip:");
  180.         scanf("%s",buf1);
  181.         int bufz[4]={1,1,1,0};
  182.         fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
  183.         if (fd == -1)
  184.                 return -1;
  185.         struct sockaddr_in self_addr, con_addr;
  186.         bzero(&self_addr, sizeof(self_addr));
  187.         self_addr.sin_family = PF_INET;
  188.         self_addr.sin_port = htons(PORT);
  189.         self_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  190.         bzero(&con_addr, sizeof(con_addr));
  191.         con_addr.sin_family = PF_INET;
  192.         con_addr.sin_port = htons(PORT);
  193.         con_addr.sin_addr.s_addr =inet_addr(buf1);//("127.0.0.1");// htonl(INADDR_ANY);
  194.         bind(fd, (struct sockaddr *)&self_addr, sizeof(self_addr));
  195.         int len = sizeof(struct sockaddr);
  196.         int map[HEIGHT][WIDTH] = {0};
  197.         int ret;
  198.         int fok;
  199.         struct Game *game;
  200.         game = malloc(sizeof(struct Game));
  201.         init_game(game, map);
  202.         int win = 1;
  203.         struct pollfd polfd[2];
  204.         int fk;
  205.         polfd[0].fd = game->mouse_fd;
  206.         polfd[0].events = POLLIN;
  207.         polfd[1].fd = fd;
  208.         polfd[1].events = POLLIN;
  209.         int xian = 0;
  210.         while(win)
  211.         {
  212.                 poll(polfd, 2, -1);
  213.                 polfd[0].fd = game->mouse_fd;
  214.                 polfd[0].events = POLLIN;
  215.                 polfd[1].fd = fd;
  216.                 polfd[1].events = POLLIN;
  217.                 if(POLLIN == polfd[0].revents)
  218.                 {
  219.                         draw_map(game, map);

  220.                         read(game->mouse_fd, game->buf, 3);

  221.                         game->rect_cur.x += game->buf[1];
  222.                         if (game->rect_cur.x < game->x0)
  223.                                 game->rect_cur.x = game->x0;
  224.                         else if (game->rect_cur.x > game->x0 + game->width - 20)
  225.                                 game->rect_cur.x = game->x0 + game->width - 20;
  226.                         game->rect_cur.y += game->buf[2];       
  227.                         if (game->rect_cur.y < game->y0)
  228.                                 game->rect_cur.y = game->y0;
  229.                         else if (game->rect_cur.y > game->y0 + game->height - 30)
  230.                                 game->rect_cur.y = game->y0 + game->height - 30;
  231.                                 recvfrom(fd, bufz, sizeof(bufz),MSG_DONTWAIT, (struct sockaddr *)&con_addr, &len);
  232.                                 if(bufz[3] == 0)
  233.                                 xian == 1;
  234.                                 else if(bufz[3] == 1)
  235.                                 xian == 0;
  236.                         if ((game->buf[0] & 0x1 == 1) && (bufz[2] == (1 - xian)))
  237.                         {
  238.                                 ret = play_game(game, map,(1 - xian));
  239.                                 bufz[0]=game->rect_cur.x,bufz[1]=game->rect_cur.y,bufz[2]=game->flag,bufz[3] == (1 - xian);
  240.                                 sendto(fd, bufz, sizeof(bufz), MSG_DONTWAIT, (struct sockaddr *)&con_addr, len);
  241.                                 if (ret == -1)
  242.                                         win = 0;
  243.                                 if (ret > 0)
  244.                                         break;
  245.                         }
  246.                 }
  247.                 if(POLLIN == polfd[1].revents)
  248.                 {
  249.                         recvfrom(fd, bufz, sizeof(bufz), MSG_DONTWAIT, (struct sockaddr *)&con_addr, &len);
  250.                 //        if(bufz[2] == 1)
  251.                         {
  252.                                 if(bufz[3] == 0)
  253.                                 xian == 1;
  254.                                 else if(bufz[3] == 1)
  255.                                 xian == 0;
  256.                                 game->rect_cur.x = bufz[0],game->rect_cur.y = bufz[1],game->flag = bufz[2];
  257.                                 ret = play_game(game, map,xian);
  258.                                 if (ret == -1)
  259.                                         win = 0;
  260.                                 if (ret > 0)
  261.                                         break;
  262.                         }
  263.                 }
  264.                         show_bmpfile_full(game->cur, game->fb, &game->rect_cur, &game->gate, 1, 1);
  265.                         fflush_fb(game->fb);
  266.         }
  267.         close(fd);
  268.         game_over(game, ret);
  269.         return 0;       
  270. }
复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x

阿莫论坛20周年了!感谢大家的支持与爱护!!

你熬了10碗粥,别人一桶水倒进去,淘走90碗,剩下10碗给你,你看似没亏,其实你那10碗已经没有之前的裹腹了,人家的一桶水换90碗,继续卖。说白了,通货膨胀就是,你的钱是挣来的,他的钱是印来的,掺和在一起,你的钱就贬值了。

出0入0汤圆

发表于 2012-5-5 12:46:15 | 显示全部楼层
这么强大,可以试验一下

出0入0汤圆

发表于 2012-5-5 13:24:03 | 显示全部楼层
其实我一直想知道的是  lz上传的代码是什么格式的?

出0入0汤圆

 楼主| 发表于 2012-5-6 02:15:31 | 显示全部楼层
Garbage614 发表于 2012-5-5 13:24
其实我一直想知道的是  lz上传的代码是什么格式的?

用的是gcc编译器,在linux下面写的,.h和.c文件

出0入0汤圆

发表于 2012-5-6 02:42:45 | 显示全部楼层
快乐的小娃 发表于 2012-5-6 02:15
用的是gcc编译器,在linux下面写的,.h和.c文件

可能我没有说清楚!这种可以显示行号,有复制代码,这种是怎么实现的呢?

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x

出0入0汤圆

发表于 2012-5-6 10:05:16 | 显示全部楼层
学习了。。。谢谢分享!

出0入0汤圆

发表于 2012-5-7 01:45:46 来自手机 | 显示全部楼层
Garbage614 发表于 2012-5-6 02:42  可能我没有说清楚!这种可以显示行号,有复制代码,这种是怎么实现的呢? ...

使用[code]标签
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片。注意:要连续压缩2次才能满足要求!!】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-10-3 00:37

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表