⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ball_game.c

📁 在Vxworks下UGL工具编写的弹球游戏
💻 C
📖 第 1 页 / 共 2 页
字号:
#include <vxWorks.h>
#include <sysLib.h>
#include <stdio.h>
#include <ugl/ugl.h>
#include <ugl/uglos.h>
#include <ugl/uglMsg.h>
#include <ugl/uglfont.h>
#include <ugl/uglinput.h>
#include <ugl/uglucode.h>
#include "wdLib.h"
#include <semLib.h>
#include <taskLib.h>
#include <ugl/ugldib.h>
#include <stdlib.h>

UGL_LOCAL int displayHeight, displayWidth;
UGL_LOCAL UGL_GC_ID gc;
UGL_LOCAL UGL_INPUT_SERVICE_ID inputServiceId;
UGL_LOCAL UGL_FONT_ID fontEcho;
UGL_LOCAL UGL_FONT_ID fontBanner;
UGL_DEVICE_ID devId;
UGL_FONT_DRIVER_ID fontDrvId;         
UGL_FONT_DEF fontDef;
UGL_ORD textOrigin;
UGL_MODE_INFO modeInfo;
UGL_MDIB patternDib;
UGL_MDDB_ID patternBitmap;
UGL_PAGE_ID page[2];
UGL_FB_INFO fbInfo;

UGL_LOCAL int pauseDemo(void);
UGL_LOCAL int ready(void);
UGL_LOCAL int ballmoving(void);
void Quit(void);
void kanmen(void);
void timer(void);
void pause(void);
void keeping(void);

WDOG_ID wdTimer;
SEM_ID ASemId;
int retVal,quity,i,l,g,replay,pausy,keep,life,start;/*l用于ballmoving,i用于Quit ,g为挡板起点横坐标*/
int xinc , yinc ,ginc;
int a=30;
int pageIndex;
char *text1 = "Sorry, you fail!!";
char *text2 = "WELCOME  TO  BALL PLAYING  !!";
char *text3 = "press 'c' to continue .";
char *text4 = "press 'q' to quit .";
char *text5 = "press 'l' to continue .";
char *text8 = "ARE  YOU  READY  ?";
char *text6 = "THANK  YOU  FOR  PLAYING !!";
char *text7 = "the window will be closed soon...";
char *text9 = "     press 'p' to get a pause .  ";
char *text10 = "press 'o' to continue playing.";
char *text11 = "Sorry, the game is over !!";
char *text12 = "Do you want to play again(Y/N?)";
char *text13 = "LIFE: 2 ";
char *text14 = "LIFE: 1 ";
char *text15 = "LIFE: 0 ";
char *text16 = "press 'j' to turn to left.";
char *text17 = "press 'k' to turn to right.";


UGL_LOCAL struct _colorStruct
    {
    UGL_ARGB rgbColor;
    UGL_COLOR uglColor;
    }
colorTable[] =
    {
    { UGL_MAKE_ARGB(0xff, 0, 0, 0), 0},    /* The color table uses ARGB's */
    { UGL_MAKE_ARGB(0xff, 0, 0, 168), 0},  /* (see uglColorAlloc).        */
    { UGL_MAKE_ARGB(0xff, 0, 168, 0), 0},  /* Initialize alpha to 255 for */
    { UGL_MAKE_ARGB(0xff, 0, 168, 168), 0},/* now (opaque).               */

    { UGL_MAKE_RGB(168, 0, 0), 0},         /* UGL_MAKE_RGB takes care of  */
    { UGL_MAKE_RGB(168, 0, 168), 0},       /* the alpha for us.           */
    { UGL_MAKE_RGB(168, 84, 0), 0},
    { UGL_MAKE_RGB(168, 168, 168), 0},

    { UGL_MAKE_RGB(84, 84, 84), 0},
    { UGL_MAKE_RGB(84, 84, 255), 0},
    { UGL_MAKE_RGB(84, 255, 84), 0},
    { UGL_MAKE_RGB(84, 255, 255), 0},

    { UGL_MAKE_RGB(255, 84, 84), 0},
    { UGL_MAKE_RGB(255, 84, 255), 0},
    { UGL_MAKE_RGB(255, 255, 84), 0},
    { UGL_MAKE_RGB(255, 255, 255), 0}
    };

#define BLACK			(0)
#define BLUE			(1)
#define GREEN			(2)
#define CYAN			(3)
#define RED		    	(4)
#define MAGENTA			(5)
#define BROWN			(6)
#define LIGHTGRAY		(7)
#define DARKGRAY		(8)
#define LIGHTBLUE		(9)
#define LIGHTGREEN		(10)
#define LIGHTCYAN		(11)
#define LIGHTRED		(12)
#define LIGHTMAGENTA		(13)
#define YELLOW			(14)
#define WHITE			(15)


void main (int mode)
    {
	ASemId=semBCreate(SEM_Q_FIFO,SEM_EMPTY);
	sysClkRateSet(1000); 
	uglInitialize();              /*ugl初始化*/
  
	devId = (UGL_DEVICE_ID)uglRegistryFind (UGL_DISPLAY_TYPE,  0, 0,0)->id;
	inputServiceId = (UGL_INPUT_SERVICE_ID)uglRegistryFind (UGL_INPUT_SERVICE_TYPE,  0, 0,0)->id;
	fontDrvId =  (UGL_FONT_DRIVER_ID)uglRegistryFind (UGL_FONT_ENGINE_TYPE,  0, 0,0)->id;
    
	uglInfo(devId, UGL_MODE_INFO_REQ, &modeInfo);
	displayWidth = modeInfo.width;
	displayHeight = modeInfo.height;
    
	gc = uglGcCreate(devId);
         
        uglColorAlloc (devId, &colorTable[BLACK].rgbColor, UGL_NULL, 
                   &colorTable[BLACK].uglColor, 1);
	uglColorAlloc(devId, &colorTable[BLUE].rgbColor, UGL_NULL,
                  &colorTable[BLUE].uglColor, 1);
    	uglColorAlloc(devId, &colorTable[GREEN].rgbColor, UGL_NULL,
                  &colorTable[GREEN].uglColor, 1);
    	uglColorAlloc(devId, &colorTable[CYAN].rgbColor, UGL_NULL,
                  &colorTable[CYAN].uglColor, 1);
    	uglColorAlloc(devId, &colorTable[RED].rgbColor, UGL_NULL,
                  &colorTable[RED].uglColor, 1);
    	uglColorAlloc(devId, &colorTable[MAGENTA].rgbColor, UGL_NULL,
                  &colorTable[MAGENTA].uglColor, 1);
   	uglColorAlloc(devId, &colorTable[BROWN].rgbColor, UGL_NULL,
                  &colorTable[BROWN].uglColor, 1);
    	uglColorAlloc(devId, &colorTable[LIGHTGRAY].rgbColor, UGL_NULL,
                  &colorTable[LIGHTGRAY].uglColor, 1);
    	uglColorAlloc(devId, &colorTable[DARKGRAY].rgbColor, UGL_NULL,
                  &colorTable[DARKGRAY].uglColor, 1);
	uglColorAlloc(devId, &colorTable[LIGHTBLUE].rgbColor, UGL_NULL,
                  &colorTable[LIGHTBLUE].uglColor, 1);
    	uglColorAlloc(devId, &colorTable[LIGHTGREEN].rgbColor, UGL_NULL,
                  &colorTable[LIGHTGREEN].uglColor, 1);
    	uglColorAlloc(devId, &colorTable[LIGHTCYAN].rgbColor, UGL_NULL,
                  &colorTable[LIGHTCYAN].uglColor, 1);
	uglColorAlloc(devId, &colorTable[LIGHTRED].rgbColor, UGL_NULL,
                  &colorTable[LIGHTRED].uglColor, 1);
    	uglColorAlloc(devId, &colorTable[LIGHTMAGENTA].rgbColor, UGL_NULL,
                  &colorTable[LIGHTMAGENTA].uglColor, 1);
    	uglColorAlloc(devId, &colorTable[YELLOW].rgbColor,  UGL_NULL,
                  &colorTable[YELLOW].uglColor, 1);
    	uglColorAlloc(devId, &colorTable[WHITE].rgbColor,  UGL_NULL,
                  &colorTable[WHITE].uglColor, 1);
        	
        uglOSTaskCreate("Demo", (UGL_FPTR)pauseDemo, 115, 
		    0, 10240,0,0,0,0,0);   
	uglOSTaskCreate("ready", (UGL_FPTR)ready, 110, 
		    0, 10240,0,0,0,0,0);		    	
                                                
	taskSpawn ("Time", 120, 0, 2000, (FUNCPTR)kanmen, 0,0,0,0,0,0,0,0,0,0);
	    
    }

void kanmen(void)
    {
    	wdTimer = wdCreate();
	wdStart(wdTimer,1, (FUNCPTR)timer, NULL);
    }
    
void timer(void)
   {  
   	semGive(ASemId);
   	wdStart(wdTimer, 1, (FUNCPTR)timer, NULL);
   }
   
UGL_LOCAL int pauseDemo(void)
    {
    UGL_MSG msg;
    UGL_STATUS status;


	UGL_FOREVER
	   {
		status = uglInputMsgGet (inputServiceId, &msg, UGL_WAIT_FOREVER);
	    		
		if (msg.type == MSG_KEYBOARD)
		{
			if (msg.data.keyboard.modifiers & UGL_KBD_KEYDOWN) 
			{
				
	                   	if (msg.data.keyboard.key == 'q') 
	                        {
	                        	quity = 1;
	                        	
	                   	}
	                   	else if (msg.data.keyboard.key == 'c') 
	                        {
	                        	quity = 2;
	                        	
	                   	}
	                   	else if (msg.data.keyboard.key == 'a')            
				{
					retVal = 1;
					
				}
				else if (msg.data.keyboard.key == 'd')         
				{
	                   		retVal = 2;   
	                                                 
	                        }
	                        else if (msg.data.keyboard.key == 'y') 
	                        {
	                        	replay = 1;
	                        	
	                   	}
	                   	else if (msg.data.keyboard.key == 'n') 
	                        {
	                        	replay= 2;
	                        	
	                   	}
	                   	else if (msg.data.keyboard.key == 'l') 
	                        {
	                        	keep = 1; 
	                        	
	                   	}
	                   	else if (msg.data.keyboard.key == 'p')        /*暂停*/
	                     	{
	                         	pausy = 1;   
	                        	
	                   	}
	                   	else if (msg.data.keyboard.key == 'o')        /*恢复游戏*/
	                     	{
	                         	pausy = 2;   
	                        }
	                         else if (msg.data.keyboard.key == 'j')       
	                     	{
	                         	start = 1;   
	                        }
	                         else if (msg.data.keyboard.key == 'k')       
	                     	{
	                         	start = 2;   
	                        }
                    	}
                }
	   }
   }
    
int ready(void)
    {
        retVal=100;
        
        quity=100;
    	replay=100;
    	life=2;
    	ClearScreen(gc);                     
	
	textOrigin = UGL_FONT_TEXT_UPPER_LEFT;
    	uglFontDriverInfo(fontDrvId, UGL_FONT_TEXT_ORIGIN, &textOrigin);
    	uglFontFindString(fontDrvId, "familyName=Helvetica; pixelSize=24", &fontDef);
        
        if ((fontBanner = uglFontCreate(fontDrvId, &fontDef)) == UGL_NULL)
	   {
        	printf("Font not found. Exiting.\n");
        	return(0);        
           }
      
    	uglForegroundColorSet(gc, colorTable[GREEN].uglColor);
    	uglBackgroundColorSet(gc, colorTable[BLUE].uglColor);
     
    	uglFontSet(gc, fontBanner);
    
    	uglTextDraw(gc, 200, 200, -1, text2);
    	uglTextDraw(gc, 270, 260, -1, text8);
    	uglTextDraw(gc, 280, 320, -1, text3);
    	uglTextDraw(gc, 280, 350, -1, text4);
    	if (pageIndex == 0)
		    {
		    uglPageVisibleSet(devId, page[1]);
		    uglPageDrawSet(devId, page[0]);
		    pageIndex = 1;
		    }
		else
		    {
		    uglPageVisibleSet(devId, page[0]);
		    uglPageDrawSet(devId, page[1]);
		    pageIndex = 0;
		    }	   
    	    	
    	while(1)
    	{	
    		semTake(ASemId,WAIT_FOREVER);
    		
    		if(quity==1)
    		{	
    			Quit();
    			break;
            	}
        
    		else if(quity==2)
   		{	
   			uglOSTaskCreate("ballmoving", (UGL_FPTR)ballmoving, 110, 
		    		0, 10240,0,0,0,0,0);
		        break;
        	}  
 	}
 	
    }  

void ClearScreen(UGL_GC_ID gc)
   {
    	uglBackgroundColorSet(gc, colorTable [BLUE].uglColor);   /*初始界面背景设置*/
    	uglForegroundColorSet(gc, colorTable [BLUE].uglColor);
    	uglLineStyleSet(gc, UGL_LINE_STYLE_SOLID);
    	uglLineWidthSet(gc, 1);
    	uglRectangle(gc, 0, 0, displayWidth - 1, displayHeight - 1);
   }
   
void pause(void)
    {
    	textOrigin = UGL_FONT_TEXT_UPPER_LEFT;
    	uglFontDriverInfo(fontDrvId, UGL_FONT_TEXT_ORIGIN, &textOrigin);
    	uglFontFindString(fontDrvId, "familyName=Helvetica; pixelSize=24", &fontDef);
        
        if ((fontBanner = uglFontCreate(fontDrvId, &fontDef)) == UGL_NULL)
	   {
        	printf("Font not found. Exiting.\n");
                
           }
      
    	uglForegroundColorSet(gc, colorTable[GREEN].uglColor);
    	uglBackgroundColorSet(gc, colorTable[BLUE].uglColor);
     
    	uglFontSet(gc, fontBanner);
    
    	uglTextDraw(gc, 480, 480, -1, text10);
    	if (pageIndex == 0)
		    {
		    uglPageVisibleSet(devId, page[1]);
		    uglPageDrawSet(devId, page[0]);
		    pageIndex = 1;
		    }
		else
		    {
		    uglPageVisibleSet(devId, page[0]);
		    uglPageDrawSet(devId, page[1]);
		    pageIndex = 0;
		    } 
    	while(1)
           {
           	semTake(ASemId,WAIT_FOREVER);
           	if(pausy==2)
           	break;                     /*恢复游戏*/
           }		
    	}    
    	
void Quit(void)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -