Implement a Snake Game in VxWorks

Source: Internet
Author: User

/* <Br/> ********************************** * copyright (c) * ****************************** <br/> * <br/> * ----------------------------------------------------------------------------- <br/> * File Name: snake. c <br/> * file Description: Snake <br/> * Author: Wu Yulin, victorywyl@126.com <br/> * creation date: <br/> * compiling environment: <br/> * hardware environment: S3C2410A (32 bit ARM920T RISC core from Samsung) <br/> *-------------------------- Modified <br/> * ---------------------------------- modification record --------------------------------- <br/> * modifier: None <br/> * modification date: none <br/> * description: none <br/> * others <br/> ***************************** **************************************** * ********** <br/> */</P> <p>/* includ Es */</P> <p> # include <VxWorks. h> </P> <p> # include <tasklib. h> </P> <p> # include <wdlib. h> </P> <p> # include <syslib. h> </P> <p> # include <stdio. h> </P> <p> # include <stdlib. h> </P> <p> # include <msgqlib. h> </P> <p> # include <loglib. h> </P> <p> # include ".. /.. /headers/usrdatatype. H "</P> <p> # include ".. /.. /headers/keyboard. H "</P> <p> # include ".. /.. /headers/guibasicdrv. H "</P> <p>/* macros */<br/> /********************* ********* * ***** <Br/> * using flyleft take place left <br/> * using flyright take place right <br/> * using "Sen" take place up <br/> * using "copy" take place down <br/> * using "exit" take place ESC <br/> *************** * *******************/</P> <p> # define left 51 </P> <p> # define right 34 </P> <p> # define up 2 </P> <p> # define down 4 </P> <p> # define ESC 9 </P> <p> # define color_grass (31> 1) <6) </P> <p> # define color_black 0x0000 </P> <p> # define color_white 0 xfffe </P> <p> # define color_blue 0x003e </P> <p> # define N 200 </P> <p>/* global variables */</P> <p> int I, j, K, key = 0; </P> <p> int score = 0;/* score */</P> <p> int task1, task2; </P> <p>/* structure of food */</P> <p> struct food <br/> {<br/> int X; /* horizontal coordinates of food */</P> <p> int y;/* Y coordinates of food */</P> <p> int yes; /* variable used to determine whether food is expected */</P> <p>} food; </P> <p>/* snake struct */</P> <p> struct snake <br/> {<br/> int X [N]; </P> <p> int y [N]; </P> <p> int node;/* Number of snake nodes */</P> <p> int direction; /* snake movement direction */</P> <p> int life;/* snake life, 0 active, 1 dead */</P> <p>} snake; </P> <p>/* functiond forward declarations function declaration */</P> <p> void Init (void ); /* initialize the keyboard drive and display of the target machine */</P> <p> void close (void ); /* disable display */</P> <p> void drawk (void);/* game Start Screen */</P> <p> void gameover (void ); /* game end */</P> <p> void gameplay (void);/* Specific game process */</P> <p> void move (void ); </P> <p> void prscore (void);/* print Home score */</P> <p> void keyhandle (void);/* direction in the direction of snake movement */</P> <p> void generatefood (void ); /* whether food is generated */</P> <p> extern void taskbeep (void); </P> <p> extern void BEEP (INT freq, int ticks ); </P> <p> extern void gui_init (void); </P> <p> extern void gui_putbutton (INT x0, int y0, int wide, int High, int8u * Name); </P> <p> extern void gui_botton (INT x0, int y0, int wide, int high, int8u * Name, int16u color ); </P> <p> extern void LCD _ Putpixel (int x, int y, int16u color ); </P> <p>/* entry of the whole code program entry point */</P> <p> void entry_snake (void) </P> <p >{</P> <p> Init (); </P> <p> drawk (); </P> <p> taskspawn ("tbeep", 90, 0,500, (funcptr) taskbeep ); </P> <p> task1 = taskspawn ("maintask", 95, 0, 20000, (funcptr) gameplay ); </P> <p> task2 = taskspawn ("keytask", 95, 0, 20000, (funcptr) Move, 0 ); </P> <P >}</P> <p>/* Other functions */</P> <p> void Init (void) <br/>{< br/> If (keyinit () = Error) </P> <p> logmsg ("keyboard initialization failed... ", 0, 0, 0, 0, 0); </P> <p> gui_init (); </P> <p> LCD _turnon (); </P> <p> BEEP (2000, sysclkrateget () * 2); </P> <p >}</P> <p> void drawk (void) <br/>{</P> <p> LCD _fillcolor (color_black);/* set the background color */</P> <p> for (I = 100; I <700; I + = 10)/* draw the background wall */</P> <p> {Gu I _putbutton (I, 60,10, 10, ""); </P> <p> gui_putbutton (I, 500,10, 10 ,""); </P> <p >}</P> <p> for (I = 70; I <500; I + = 10) </P> <p> {gui_putbutton (100, I, 10, 10, ""); </P> <p> gui_putbutton (690, I, 10, 10 ,""); </P> <p >}</P> <p> food. yes = 1;/* 1 indicates that new food needs to appear, and 0 indicates that a food already exists */</P> <p> snake. life = 0;/* live */</P> <p> snake. direction = down;/* direction to right */</P> <p> snake. X [0] = 200; snake. Y [0] = 100;/* snake header */</P> <p> snake. X [1] = 190; snake. Y [1] = 100; </P> <p> snake. node = 2; </P> <p> generatefood (); </P> <p >}</P> <p> void generatefood (void) <br/>{</P> <p> If (food. yes = 1) </P> <p >{< br/> food. X = rand () % 400 + 100; </P> <p> food. y = rand () % 350 + 80; </P> <p> while (food. X % 10! = 0)/* after a random occurrence of a food, the food must be in the whole grid so that the snake can eat */</P> <p> food. X ++; </P> <p> while (food. Y % 10! = 0) </P> <p> food. Y ++; </P> <p> for (j = food. x; j <(food. x) + 10; j ++)/* draw food */</P> <p> for (k = food. y; k <(food. y) + 10; k ++) </P> <p> {LCD _putpixel (j, k, color_blue) ;}</P> <p> food. yes = 0; /* food on the screen */</P> <p >}</P> <p> void close (void) <br/>{</P> <p> LCD _turnoff (); </P> <p >}</P> <p> void gameover (void) <br/>{</P> <p> puts ("game over... "); </P> <p >}</P> <p> void gameplay (void) <br/>{</P> <p> while (1) <br/>{</P> <p> while (Key = = 0)/* the snake moves its own body without a button */<br/> {</P> <p> generatefood (); </P> <p> for (I = snake. node-1; I> 0; I --)/* each step of the snake moves forward, that is, the key algorithm of the snake */<br/> {<br/> snake. X [I] = snake. X [I-1]; </P> <p> snake. Y [I] = snake. Y [I-1]; <br/>}</P> <p> switch (snake. direction) <br/>{< br/> case right: Snake. X [0] + = 10; break; </P> <p> case left: Snake. X [0]-= 10; break; </P> <p> case up: Snake. Y [0]-= 10; break; </P> <p> case down: Snake. Y [0] + = 10; break; </P> <p>} </P> <p>/* collision detection */</P> <p> for (I = 3; I <snake. node; I ++)/* judge whether the snake hits itself from the fourth section of the snake, because the snake header is two sections, section 3 cannot be transferred */<br/> {<br/> If (snake. X [I] = snake. X [0] & snake. Y [I] = snake. Y [0]) <br/>{< br/> gameover ();/* display failed */</P> <p> snake. life = 1; </P> <p> break; <br/>}</P> <p> If (snake. X [0] <110 | snake. X [0]> 690 | snake. Y [0] <70 | snake. Y [0]> 490)/* Whether the snake hits the wall */<br/> {<br/> gameover (); /* this game ends */</P> <p> snake. life = 1;/* snake dead */</P> <p >}</P> <P> If (snake. life = 1)/* after the above two judgments, if the snake dies, it will jump out of the inner loop and start again */</P> <p> break; </P> <p> If (snake. X [0] = food. X & snake. Y [0] = food. y)/* after eating food */</P> <p >{< br/> snake. X [Snake. node] =-20; snake. Y [Snake. node] =-20;/* the new section is placed in an invisible position, and the position of the previous section is obtained in the next loop */</P> <p> snake. node ++;/* the length of the snake's body */</P> <p> food. yes = 1;/* new food needs to appear on the screen */</P> <p> score + = 10; </P> <p> prscore (); /* output new score */<br/>}</P> <p> for (I = 0; I <snake. node; I ++)/* draw a snake */</P> <p> {gui_putbutt On (snake. X [I], Snake. Y [I], 10, 10, ""); </P> <p> taskdelay (sysclkrateget ()/10 ); </P> <p >}</P> <p> for (j = snake. X [Snake. node-1]; j <(snake. X [Snake. node-1] + 10); j ++) </P> <p> for (k = snake. Y [Snake. node-1]; k <snake. Y [Snake. node-1] + 10; k ++) </P> <p> {LCD _putpixel (j, k, color_black) ;}</P> <p> key = 0; </P> <p >}/ * endwhile (Key = 0) */</P> <p> If (snake. life = 1)/* jump out of the loop if the snake dies */</P> <p> break; </P> <p >}/ * endwhile (1) */</P> <p >}</P> <p> Void move (void) <br/>{< br/> while (1) </P> <p> {key = keygetvalue (); </P> <p> tasksuspend (task1); </P> <p> keyhandle (); </P> <p> taskresume (task1 ); <br/>}</P> <p> void keyhandle (void) </P> <p >{</P> <p> If (Key = up & snake. direction! = Down)/* determine whether to move in the opposite direction */</P> <p> snake. direction = up; </P> <p> else </P> <p> If (Key = Right & snake. direction! = Left) </P> <p> snake. Direction = right; </P> <p> else <br/> If (Key = left & snake. direction! = Right) </P> <p> snake. direction = left; </P> <p> else </P> <p> If (Key = down & snake. direction! = Up) </P> <p> snake. direction = down; </P> <p >}</P> <p> void prscore (void) <br/>{</P> <p> logmsg ("your current score is % d/N", score, 0 ); </P> <p >}</P> <p>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.