欢迎光临
我们一直在努力

基于Linux c framebuffer snake贪吃蛇 多线程(扫描按键)

 

 

#include<stdio.h>
#include<fcntl.h>
#include<sys/mman.h>
#include<unistd.h>
#include<math.h>
#include<ncurses.h>
#include<pthread.h>
#include<stdlib.h>
#define X 1376
#define Y 768
#define XN 1376 / 40
#define YN 768 / 40

/***********************************************************************************/
typedef unsigned int u32_t;
u32_t jbcolor[]={0xa000000,0x000000,0x000000,0x010000,0x000100,0x00000};
u32_t *p;
int OIL_X,OIL_Y;
int direction = 1;
int GAME_START = 0;
int ground[YN][XN] = {0};
struct SNAKE {
int x;
int y;
struct SNAKE *next;
struct SNAKE *father;
};
/***********************************************************************************/
char key;
void msys();
void check_buton();
void draw_box(int o_x,int o_y,int len,int wide,u32_t color,int jianbian);
void xiezi(int o_x,int o_y,u32_t color,int size,int ez,int zige[32]);
void draw_gezi();
void welcome();
u32_t * dataset();
void game_start();
void draw_oil();
void rand_oil();
void draw_body(struct SNAKE * head);
int check_oil(struct SNAKE *head);
int check_oil_yes(int x,int y);
struct SNAKE * malloc_body(int x, int y);
struct SNAKE * move_body(struct SNAKE *head);
/***********************************************************************************/
int main(void)
{
int c;
dataset();
initscr();
noecho();
cbreak();
keypad(stdscr,TRUE);
welcome();
while(1)
{
if(GAME_START != 3)
{
draw_gezi();
game_start();
}
}
endwin();
return 0;
}
/***********************************************************************************/
int check_oil(struct SNAKE *head)
{
struct SNAKE *p = head->next;
while(p != NULL)
{
if(head->x == p->x && head->y == p->y)
{
GAME_START = 3;
break;
}
p = p->next;
}
if(GAME_START == 1)
{
switch(direction)
{
case 1 : return (check_oil_yes(head->x + 1,head->y));break;
case 2 : return (check_oil_yes(head->x - 1,head->y));break;
case 3 : return (check_oil_yes(head->x,head->y + 1));break;
case 4 : return (check_oil_yes(head->x,head->y - 1));break;
}
}
GAME_START = 3;
return -1;

}
/***********************************************************************************/
int check_oil_yes(int x,int y)
{
if(x >= 0 && x < XN && y >= 0 && y < YN )
{
if(ground[y][x] == 1)
{
OIL_X = x;
OIL_Y = y;
}
return ground[y][x];
}
else
{
return -1;
}
}
/***********************************************************************************/
struct SNAKE * malloc_body(int x, int y)
{
struct SNAKE *head = NULL;
struct SNAKE *p = NULL;
if(head == NULL)
{
head = (struct SNAKE * )malloc(sizeof(struct SNAKE));
head->x = x;
head->y = y;
head->father = NULL;
head->next = NULL;
p = (struct SNAKE *)malloc(sizeof(struct SNAKE));
p->x = x - 1;
p->y = y;
p->father = head;
head->next = p;
p->next = NULL;
}
return head;
}
/***********************************************************************************/
void game_start()
{
struct SNAKE *head = NULL;
struct SNAKE * p = head;
GAME_START = 1;
while(GAME_START == 1)
{
rand_oil();
draw_oil();
head = malloc_body(6,6);
draw_body(head);
draw_gezi();

while(GAME_START == 1)
{
msys();
msys();
switch(check_oil(head))
{
case 0 : head = move_body(head);break;
case 1 : p = (struct SNAKE * )malloc(sizeof(struct SNAKE));
p->x = OIL_X;p->y = OIL_Y;p->next = head;head->father = p;head = p;
rand_oil();draw_oil();ground[OIL_Y][OIL_X] = 0;break;
case -1 : GAME_START = 3; break;

}
if(GAME_START != 3)
{
draw_body(head);
draw_gezi();
}
}
}
draw_box(0,0,768,1374,0x000000,1);
}
/***********************************************************************************/
void draw_body(struct SNAKE * head)
{
while(head != NULL)
{
draw_box(head->y * 40,head->x * 40,40,40,0xffffff,0);
head = head->next;
}
}
/***********************************************************************************/
struct SNAKE * move_body(struct SNAKE *head)
{
struct SNAKE * p = head;
while(p->next != NULL)
p = p->next;
draw_box(p->y * 40,p->x * 40,40,40,0x00fc00,0);
p = p->father;
p->next = NULL;
p = (struct SNAKE * )malloc(sizeof(struct SNAKE));
p->father = NULL;
p->next = head;
head->father = p;
head = p;
switch(direction)
{
case 1: head->x = head->next->x + 1;head->y = head->next->y;break;
case 2: head->x = head->next->x - 1;head->y = head->next->y;break;
case 3: head->y = head->next->y + 1;head->x = head->next->x;break;
case 4: head->y = head->next->y - 1;head->x = head->next->x;break;
}
return head;
}
/***********************************************************************************/
void rand_oil()
{
int x,y;
srand(time(NULL));
x = rand() % XN;
y = rand() % YN;
ground[y][x] = 1;
}
/***********************************************************************************/
void draw_oil()
{
int i,j;
for(i = 0; i < YN; i++)
{
for(j = 0; j < XN; j++)
{
if(ground[i][j] == 1)
draw_box(i * 40, j * 40, 40, 40, 0xff0000,0);
}
}
}
/***********************************************************************************/
void welcome()
{
int ret,i,j;
pthread_t id1;
for(i = 0; i < Y; i++)
for(j = 0; j < X; j++)
p[i * X + j] = 0x00fc00;
ret=pthread_create(&id1,NULL,(void *)check_buton,NULL);
if(ret!=0)
printf("error!\n");
}
/***********************************************************************************/
void check_buton()
{
char key;
while(1)
{
key = getchar();
if(key == 'd' && direction != 2)
{
direction = 1;
}
if(key == 'a' && direction != 1)
{
direction = 2;
}
if(key == 'w' && direction != 3)
{
direction = 4;
}
if(key == 's' && direction != 4)
{
direction = 3;
}
if(key == '\r')
{
;
}
}
}
/***********************************************************************************/
u32_t * dataset()
{
int fd;
fd=open("/dev/fb0",O_RDWR);
p=(u32_t *)mmap(NULL, X * Y * 4, PROT_READ | PROT_WRITE, MAP_SHARED,fd,0);
if(p == MAP_FAILED)
perror("mmap error");
}
/***********************************************************************************/
void draw_gezi()
{
int j;
for(j = 0; j < X; j++)
if(j % 40 == 0)
draw_box(0,j,Y,1,0x000000,0);
for(j = 0; j < Y; j++)
if(j % 40 == 0)
draw_box(j,0,1,X,0x000000,0);
}
/***********************************************************************************/

void draw_box(int o_x, int o_y, int len, int wide, u32_t color, int jianbian)
{
int i,j;
for(i = X * o_x; i < X * (o_x + len); i += X)
{
for(j = o_y; j < o_y + wide; j++)
p[i + j] = color;
if(jianbian == 1)
color += jbcolor[i % 5];
}
}
/***********************************************************************************/
void msys()
{
int i,j;
for(i = 0; i < 10000; i++)
for(j = 0; j < 10000; j++)
;
}
/***********************************************************************************/
void xiezi(int o_x,int o_y,u32_t color,int size,int ez,int zige[32])
{
int i,j,binary,x,y;
x = 0;
y = 0;
for(i = 0;i < ez; i++)
{
for(j = 0;j < 8; j++)
{
binary = ( zige[i] >> 7 - j ) & 0x01;
if(binary == 1)
draw_box(o_x + x * size, o_y + y * size, size, size, color, 0);
y++;
if(y == ez / 2)
{
x++;
y = 0;
}
}
}
}
/***********************************************************************************/

赞(1) 打赏
未经允许不得转载:huangea的博客 » 基于Linux c framebuffer snake贪吃蛇 多线程(扫描按键)

评论 3

  1. #1

    初来乍到,支持下

    银基网址导航10年前 (2013-12-18)回复
  2. #2

    来踩了,挺好的,加油

    adtask10年前 (2013-12-19)回复
  3. #3

    网站确实不错,支持一下。

    域名价格9年前 (2014-12-24)回复

有趣的网站

支持快讯、专题、百度收录推送、人机验证、多级分类筛选器,适用于垂直站点、科技博客、个人站,扁平化设计、简洁白色、超多功能配置、会员中心、直达链接、文章图片弹窗、自动缩略图等...

联系我们联系我们

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续给力更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

微信扫一扫打赏

登录

找回密码

注册