快乐的小娃 发表于 2012-5-5 01:59:34

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

下面是部分源代码(显示器是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;
struct Game{
        struct Framebuffer *fb;
        struct Bmp *map, *cur, *white, *black;
        struct Rect rect_map, rect_cur, rect_zi;
        int mouse_fd;
        char buf;
        int flag;
        int x0, y0, width, height;
        struct pixel_32 gate;
};

void draw_map(struct Game *game, int map[])
{
        inti, 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)
                        {
                                rect.x = j * pith + 227 - 10 + 300;
                                rect.y = i * pith + 193 + 22;
                                if (map == 1)
                                        show_bmpfile(game->black, game->fb, &game->rect_zi, &rect, 1, 1);
                                else if (map == 2)
                                        show_bmpfile(game->white, game->fb, &game->rect_zi, &rect, 1, 1);
                               
                        }
                }
        }
               
}
void init_game(struct Game *game, int map[])
{
        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[], int x, int y)
{
        int i;
        int count = {0}, flag = {1, 1, 1, 1, 1, 1, 1, 1};
        for (i = 1; i < 5; i++)
        {
                if (x - i >= 0 && map == map && flag)
                        count++;
                else
                        flag = 0;
                if (x + i < WIDTH && map == map && flag)
                        count++;
                else
                        flag = 0;
                if (y - i >= 0 && map == map && flag)
                        count++;
                else
                        flag = 0;
                if (y + i < HEIGHT && map == map && flag)
                        count++;
                else
                        flag = 0;
                if (x - i >= 0 && y - i >= 0 && map == map && flag)
                        count++;
                else
                        flag = 0;
                if (x + i < WIDTH&& y + i < HEIGHT && map == map && flag)
                        count++;
                else
                        flag = 0;

                if (x + i < WIDTH && y - i >= 0 && map == map && flag)
                        count++;
                else
                        flag = 0;
                if (x - i >= 0 && y + i < HEIGHT && map == map && flag)
                        count++;
                else
                        flag = 0;
        }
        for (i = 0; i < 4; i++)
                if (count >= 4)
                        return map;
        return 0;
}
int play_game(struct Game *game, int map[],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 == 0)
        {
                if (flag)
                {
                      map = 1;
                      show_bmpfile(game->black, game->fb, &game->rect_zi, &rect, 1, 1);
                      flag = 0;
                }
                else
                {
                        map = 2;
                        show_bmpfile(game->white, game->fb, &game->rect_zi, &rect, 1, 1);
                        flag = 1;
                }
                if (is_win(map, x1, y1))
                        return map;
        }
        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;
        static int flag = 1;
        printf("请输入对方的ip:");
        scanf("%s",buf1);
        int bufz={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 = {0};
        int ret;
        int fok;
        struct Game *game;
        game = malloc(sizeof(struct Game));
        init_game(game, map);
        int win = 1;
        struct pollfd polfd;
        int fk;
        polfd.fd = game->mouse_fd;
        polfd.events = POLLIN;
        polfd.fd = fd;
        polfd.events = POLLIN;
        int xian = 0;
        while(win)
        {
                poll(polfd, 2, -1);
                polfd.fd = game->mouse_fd;
                polfd.events = POLLIN;
                polfd.fd = fd;
                polfd.events = POLLIN;
                if(POLLIN == polfd.revents)
                {
                        draw_map(game, map);

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

                        game->rect_cur.x += game->buf;
                        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;       
                        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 == 0)
                                xian == 1;
                                else if(bufz == 1)
                                xian == 0;
                        if ((game->buf & 0x1 == 1) && (bufz == (1 - xian)))
                        {
                                ret = play_game(game, map,(1 - xian));
                                bufz=game->rect_cur.x,bufz=game->rect_cur.y,bufz=game->flag,bufz == (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.revents)
                {
                        recvfrom(fd, bufz, sizeof(bufz), MSG_DONTWAIT, (struct sockaddr *)&con_addr, &len);
                //        if(bufz == 1)
                        {
                                if(bufz == 0)
                                xian == 1;
                                else if(bufz == 1)
                                xian == 0;
                                game->rect_cur.x = bufz,game->rect_cur.y = bufz,game->flag = bufz;
                                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;       
}

abnerle 发表于 2012-5-5 12:46:15

这么强大,可以试验一下

Garbage614 发表于 2012-5-5 13:24:03

其实我一直想知道的是lz上传的代码是什么格式的?

快乐的小娃 发表于 2012-5-6 02:15:31

Garbage614 发表于 2012-5-5 13:24 static/image/common/back.gif
其实我一直想知道的是lz上传的代码是什么格式的?

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

Garbage614 发表于 2012-5-6 02:42:45

快乐的小娃 发表于 2012-5-6 02:15 static/image/common/back.gif
用的是gcc编译器,在linux下面写的,.h和.c文件

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

zhuoxizx 发表于 2012-5-6 10:05:16

学习了。。。谢谢分享!

zhouwenjing 发表于 2012-5-7 01:45:46

Garbage614 发表于 2012-5-6 02:42可能我没有说清楚!这种可以显示行号,有复制代码,这种是怎么实现的呢? ...

使用标签
页: [1]
查看完整版本: linux局域网五子棋代码,电脑显卡和udp协议联系(适合新手)