基于GEC6818开发板图案渐变色代码

 样图如下: 

 通过Linux平台(ubuntu-18.04)用终端命令进行交叉编译(arm-linux-gcc)下列代码:

#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>

//屏幕宽长
#define ICTwide 480 
#define ICTlength 800 

//圆参数
#define r 150
#define x 400
#define y 240

//左鼻参数
#define rl 30
#define xl 350
#define yl 240

//右鼻参数
#define rr 30
#define xr 450
#define yr 240

//左耳参数
#define rel 50
#define xel 300
#define yel 100

//右耳参数
#define rer 50
#define xer 500
#define yer 100

//变色差
#define Coloe_Rate 8

//原始色
#define Original_Color 0x000000

//饱和色
#define Saturated_Color 0xffffff

//刷新率
#define Refresh_Coloe_Rate 1000000

颜色设置
//#define Ear_color 0xff0000			//耳朵
//#define Nose_color 0xee0000			//鼻子
//#define Orth_Color 0xcd0000			//矩形
//#define EarIn_color 0xffff00			//耳内
//#define Circle_color 0xffff00			//圆
//#define Bottom_color 0xff0000			//圆底
//#define Background_color 0xee4000		//背景

//矩形边框参数
#define L_wide 40 
#define W_wide 40 
#define W_Dvalue ICTwide-W_wide
#define L_Dvalue ICTlength-L_wide

//颜色设置
int Ear_color=0xff0000;			//耳朵
int Nose_color=0xee0000;			//鼻子
int Orth_Color=0xcd0000;		//矩形
int EarIn_color=0xffff00;		//耳内
int Circle_color=0xffff00;		//圆
int Bottom_color=0xff0000;		//圆底
int Background_color=0xee4000;	//背景

int main()
{
    //1.打开lcd屏幕
    int lcd_fd = open("/dev/fb0",O_RDWR);
    if (lcd_fd == -1)
    {
        perror("open lcd fail");
    }
    
    //2.打开映射
    int *plcd = mmap(NULL,ICTlength*ICTwide*4,PROT_READ|PROT_WRITE,
                    MAP_SHARED,lcd_fd,0);
                    
	while(1)
	{
	
	    //背景颜色设置
	    for(int i = 0;i < ICTwide;i++)
	    {
	        for(int j = 0;j < ICTlength;j++)
	        {
	            *(plcd + ICTlength*i + j) = Background_color;
	        }
	    }     


		//圆颜色设置
		for(int i = 0;i < ICTwide;i++)
		{
			for(int j = 0;j < ICTlength; j++)
			{

				//左耳朵
				if((j-xel)*(j-xel) +(i-yel)*(i-yel) <= rel*rel)
				{
					*(plcd + ICTlength*i + j) = Ear_color;
				}
				//右耳朵
				if((j-xer)*(j-xer) +(i-yer)*(i-yer) <= rer*rer)
				{
					*(plcd + ICTlength*i + j) = Ear_color;
				}

				//左内耳朵
				if((j-xel)*(j-xel) +(i-yel)*(i-yel) <= (rel-10)*(rel-10))
				{
					*(plcd + ICTlength*i + j) = EarIn_color;
				}
				//右内耳朵
				if((j-xer)*(j-xer) +(i-yer)*(i-yer) <= (rer-10)*(rer-10))
				{
					*(plcd + ICTlength*i + j) = EarIn_color;
				}
				
				//底圆
				if((j-x)*(j-x) +(i-y)*(i-y) <= (r+10)*(r+10))
				{
					*(plcd + ICTlength*i + j) = Bottom_color;
				}
				
				//上圆
				if((j-x)*(j-x) +(i-y)*(i-y) <= r*r)
				{
					*(plcd + ICTlength*i + j) = Circle_color;
				}
				
				//左眼
				if((j-xl)*(j-xl) +(i-yl)*(i-yl) <= rl*rl)
				{
					*(plcd + ICTlength*i + j) = Nose_color;
				}
				//右眼
				if((j-xr)*(j-xr) +(i-yr)*(i-yr) <= rr*rr)
				{
					*(plcd + ICTlength*i + j) = Nose_color;
				}

				
				
				
			}
		}

		//矩形边框设置
		for(int i = 0; i < ICTwide;i++)
		{
			for(int j = 0;j < ICTlength;j++)
			{
				//长
				if((i>=0&&i<W_wide)&&(j<ICTlength))
				{
					*(plcd + ICTlength*i + j) =Orth_Color;
				}
				if((i>=W_Dvalue&&i<ICTwide)&&(j<ICTlength))
				{
					*(plcd + ICTlength*i + j) =Orth_Color;
				}

				//宽
				if((i<ICTwide)&&(j>=0&&j<L_wide))
				{
					*(plcd + ICTlength*i + j) =Orth_Color;
				}
				if((i<ICTwide)&&(j>=L_Dvalue&&j<ICTlength))
				{
					*(plcd + ICTlength*i + j) =Orth_Color;
				}
			}
		}
		
		usleep(Refresh_Coloe_Rate);

		//颜色设置
		Ear_color+=Coloe_Rate;		//耳朵
		Nose_color+=Coloe_Rate;		//鼻子
		Orth_Color+=Coloe_Rate;		//矩形
		EarIn_color+=Coloe_Rate;		//耳内
		Circle_color+=Coloe_Rate;		//圆
		Bottom_color+=Coloe_Rate;		//圆底
		Background_color+=Coloe_Rate;	//背景
		if(Ear_color>Saturated_Color)
		{
			Ear_color=Original_Color;		
			
		}
		if(Nose_color>Saturated_Color)
		{
			Nose_color=Original_Color;			
			
		}
		if(Orth_Color>Saturated_Color)
		{
			Orth_Color=Original_Color;
			
		}
		if(EarIn_color>Saturated_Color)
		{
			EarIn_color=Original_Color;
			
		}
		if(Circle_color>Saturated_Color)
		{
			Circle_color=Original_Color;	
			
		}
		if(Bottom_color>Saturated_Color)
		{
			Bottom_color=Original_Color;			
		}
		if(Background_color>Saturated_Color)
		{
			Background_color=Original_Color;	
			
		}
		usleep(Refresh_Coloe_Rate);

	}



    //3.关闭lcd 解除映射
    munmap(plcd,ICTlength*ICTwide*4);
    close(lcd_fd);
    
    return 0;
}


 该代码采用映射实现图案,可随时间不断刷新图案颜色,可自定义刷新速率。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个简单的基于SDL库的C语言贪吃蛇游戏代码示例: ``` #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <time.h> #include "SDL2/SDL.h" #define SCREEN_WIDTH 640 #define SCREEN_HEIGHT 480 #define BLOCK_SIZE 20 int main(int argc, char* argv[]) { SDL_Window* window = NULL; SDL_Renderer* renderer = NULL; SDL_Event event; bool quit = false; int score = 0; int snake_length = 1; int snake_direction = 0; int snake_x[100]; int snake_y[100]; int food_x, food_y; bool food_exists = true; // 初始化SDL SDL_Init(SDL_INIT_VIDEO); // 创建窗口和渲染器 window = SDL_CreateWindow("Snake", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, 0); renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); // 随机生成食物位置 srand(time(NULL)); food_x = rand() % (SCREEN_WIDTH / BLOCK_SIZE) * BLOCK_SIZE; food_y = rand() % (SCREEN_HEIGHT / BLOCK_SIZE) * BLOCK_SIZE; // 初始化蛇的位置 snake_x[0] = SCREEN_WIDTH / 2 - BLOCK_SIZE / 2; snake_y[0] = SCREEN_HEIGHT / 2 - BLOCK_SIZE / 2; // 游戏循环 while (!quit) { // 处理事件 while (SDL_PollEvent(&event)) { if (event.type == SDL_QUIT) { quit = true; } else if (event.type == SDL_KEYDOWN) { switch (event.key.keysym.sym) { case SDLK_UP: snake_direction = 1; break; case SDLK_DOWN: snake_direction = 2; break; case SDLK_LEFT: snake_direction = 3; break; case SDLK_RIGHT: snake_direction = 4; break; } } } // 移动蛇的位置 for (int i = snake_length - 1; i > 0; i--) { snake_x[i] = snake_x[i - 1]; snake_y[i] = snake_y[i - 1]; } switch (snake_direction) { case 1: snake_y[0] -= BLOCK_SIZE; break; case 2: snake_y[0] += BLOCK_SIZE; break; case 3: snake_x[0] -= BLOCK_SIZE; break; case 4: snake_x[0] += BLOCK_SIZE; break; } // 判断是否吃到食物 if (snake_x[0] == food_x && snake_y[0] == food_y) { score++; snake_length++; food_exists = false; } // 生成新的食物 if (!food_exists) { food_x = rand() % (SCREEN_WIDTH / BLOCK_SIZE) * BLOCK_SIZE; food_y = rand() % (SCREEN_HEIGHT / BLOCK_SIZE) * BLOCK_SIZE; food_exists = true; } // 判断是否撞墙或撞到自己 if (snake_x[0] < 0 || snake_x[0] >= SCREEN_WIDTH || snake_y[0] < 0 || snake_y[0] >= SCREEN_HEIGHT) { printf("Game over! Score: %d\n", score); break; } for (int i = 1; i < snake_length; i++) { if (snake_x[i] == snake_x[0] && snake_y[i] == snake_y[0]) { printf("Game over! Score: %d\n", score); break; } } // 渲染游戏界面 SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); SDL_RenderClear(renderer); SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); SDL_Rect food_rect = { food_x, food_y, BLOCK_SIZE, BLOCK_SIZE }; SDL_RenderFillRect(renderer, &food_rect); SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255); for (int i = 0; i < snake_length; i++) { SDL_Rect snake_rect = { snake_x[i], snake_y[i], BLOCK_SIZE, BLOCK_SIZE }; SDL_RenderFillRect(renderer, &snake_rect); } SDL_RenderPresent(renderer); // 延时 SDL_Delay(100); } // 释放资源 SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); SDL_Quit(); return 0; } ``` 这段代码使用SDL库实现了一个简单的贪吃蛇游戏,可以在gec6818开发板上运行。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值