Game programming notes-start (3)-Snake Game implementation

Source: Internet
Author: User

Game programming notes-getting started


3. Snake Game implementation





Code download: http://download.csdn.net/detail/you_lan_hai/3738025

The core code implementation is as follows. For the algorithm description, see "Start (1 )":

# Include "app. H "# include <string. h> # include <stdlib. h> int g_map [100] [100]; // int g_nwidth = 610; // window width int g_nheight = 514; // window height int g_rows = 0; // map number of rows int g_cols = 0; // map number of columns int g_nsize = 30; // map square size struct point // two-dimensional point {int R; // line int C; // column}; point g_snake [1000]; // snake int g_nlength = 0; // snake length int g_nspeed; bool g_blive = true; // whether to survive int g_nsnakedir = 0; // the current direction of the snake. 0-left, 1-right, 2-up, 3-down const point g_direction [4] = {0,-1 }, {}}; // four Increment motion directions. Point g_prizepos; // prize subcoordinate bool g_bpause = true; // whether to suspend hbrush g_brushs [4]; // void setsnkedir (int dir) // set the direction of the snake. {If (DIR = 0 & g_nsnakedir = 1) | (DIR = 1 & g_nsnakedir = 0) | (DIR = 2 & g_nsnakedir = 3) | (DIR = 3 & g_nsnakedir = 2 )) // when the opposite direction is moved, it will suspend {g_bpause = true; return; // directly return and do nothing.} Else {g_bpause = false; g_nsnakedir = dir ;}} void resetlevel () // reset level {/* initialize the map. Initialize the four sides as obstacles, that is, they cannot pass. * 0-pass, 1-obstacle, 2-prize Child, 3-snake */memset (g_map, 0, sizeof (g_map); For (INT r = 0; r <g_rows; ++ R) {g_map [r] [0] = 1; g_map [r] [g_cols-1] = 1 ;}for (int c = 0; c <g_cols; ++ c) {g_map [0] [c] = 1; g_map [g_rows-1] [c] = 1;} g_nspeed = 500; g_bpause = true; // pause g_nsnakedir = 1; // to the right. G_blive = true; g_nlength = 3; // The Snake initially has three nodes g_snake [0]. R = 1; // snake header g_snake [0]. C = 3; g_snake [1]. R = 1; g_snake [1]. C = 2; g_snake [2]. R = 1; g_snake [2]. C = 1; // write the initial state to the map for (INT I = 0; I <g_nlength; ++ I) {g_map [g_snake [I]. r] [g_snake [I]. c] = 3;} // initialization prize child coordinate g_prizepos.r = 1; g_prizepos.c = g_cols-2;} void Init () // game initialization {g_brushs [0] = createsolidbrush (RGB (255,255,255); // white brush g_brushs [1] = createsolidbrush (RGB (255,); // paint obstacle paint brush g_brushs [2] = createsolidbrush (RGB (, 0 )); // draw prize image brush g_brushs [3] = createsolidbrush (RGB (0,255, 0); // draw snake image brush srand (gettickcount ()); // correction window height and width data rect RC; getclientrect (gethwnd (), & rc); g_nwidth = RC. right-RC. left; g_nheight = RC. bottom-RC. top; // calculate the row and column values g_rows = g_nheight/g_nsize; g_cols = g_nwidth/g_nsize; resetlevel ();} void Update () // logical update {// clock control static int oldtime = 0; Int curtime = gettickcount (); If (curtime-oldtime <g_nspeed) {return;} oldtime = curtime; //////////////////////////////////////// //// // static int lastocurtime = 0; // If (curtime-lastocurtime> 10000) of the time when the prize was last displayed // update the position every 10 s. {// Lastocurtime is not-1. If it is not eaten, the prize child from the original location is erased from the map. If (lastocurtime! =-1) {g_map [g_prizepos.r] [g_prizepos.c] = 0;} lastocurtime = curtime; // randomly generate prize subpositions. Bool flag = true; while (FLAG) {g_prizepos.r = rand () % (g_rows-2) + 1; g_prizepos.c = rand () % (g_cols-2) + 1; if (g_map [g_prizepos.r] [g_prizepos.c] = 0) // meets the condition {flag = false; g_map [g_prizepos.r] [g_prizepos.c] = 2 ;}} if (! G_blive | g_bpause) // death or pause {return;} // first, judge whether it is accessible, that is, whether it is killed. Point newhead; newhead. R = g_snake [0]. R + g_direction [g_nsnakedir]. r; newhead. C = g_snake [0]. c + g_direction [g_nsnakedir]. c; If (g_map [newhead. r] [newhead. c] = 1 | g_map [newhead. r] [newhead. c] = 3) {g_blive = false; If (idyes = MessageBox (gethwnd (), L "you are killed! Have you come back? ", L" Death prompt ", mb_yesno) {resetlevel () ;}else {postquitmessage (0) ;}return ;} else if (g_map [newhead. r] [newhead. c] = 2) // The winner {++ g_nlength; lastocurtime =-1; g_nspeed-= 10 ;} else {// undo the legacy g_map [g_snake [g_nLength-1] on the map. r] [g_snake [g_nLength-1]. c] = 0;} memmove (g_snake + 1, g_snake, sizeof (point) * (g_nLength-1); // move the snake's intermediate data g_snake [0]. R = newhead. r; g_snake [0]. C = newhead. c; // Add the snake header data g_map [G_s Nake [0]. r] [g_snake [0]. c] = 3; //////////////////////////////////////// /// //} void render () // screen rendering {HDC = getdc (gethwnd (); // obtain the HDC memdc of the system plotting device = createcompatibledc (0 ); // create an auxiliary drawing device hbitmap BMP back = createcompatiblebitmap (HDC, g_nwidth, g_nheight); // create a mask Bitmap (canvas) SelectObject (memdc, BMP back ); // paste the canvas to the drawing device Hpen penback = createpen (ps_solid, 1, RGB (255,); // create the paint brush SelectObject (Memdc, penback); // select the paint brush to the drawing device // erase the background rect rcclient; // getclientrect (gethwnd (), & rcclient ); // obtain the hbrush brushtemp = (hbrush) getstockobject (white_brush) in the customer region; // obtain the inventory object and the white image brush. Fillrect (memdc, & rcclient, brushtemp); // fill the customer region. //////////////////////////////////////// /// // Draw a 2-dimensional grid, rectangle painting. For (INT r = 0; r <g_rows; ++ R) {for (int c = 0; C <g_cols; ++ c) {SelectObject (memdc, g_brushs [g_map [r] [c]); rectangle (memdc, C * g_nsize, R * g_nsize, (C + 1) * g_nsize, (R + 1) * g_nsize );}} //////////////////////////////////////// /// // bitblt (HDC, 0, 0, g_nwidth, g_nheight, memdc, 0, 0, srccopy); // eobject (penback) is displayed on the system device; // release the paint brush resource deleteobject (BMP back ); // release the bitmap resource deletedc (memdc );// Release the secondary drawing device releasedc (gethwnd (), HDC); // return the system drawing device sleep (10);} void clear () // resource release {// release the image brush resource for (INT I = 0; I <4; ++ I) {If (g_brushs! = NULL) {deleteobject (g_brushs [I]); g_brushs [I] = NULL ;}}/* window process. If no message is processed, return 0; otherwise, return 1. */Lresult wndproc (hwnd, uint umsg, wparam, lparam) {Switch (umsg) {Case wm_keydown: {Switch (wparam) {Case vk_left: setsnkedir (0 ); break; // to the left case vk_right: setsnkedir (1); break; // to the right case vk_up: setsnkedir (2); break; // up case vk_down: setsnkedir (3 ); break; // downward case vk_escape: destroywindow (hwnd); break ;}} break; default: Return 0;} return 1 ;}// main function int winapi winmain (hi Nstance hinstance, hinstance, lpstr, INT) {If (! Initapp (hinstance, l "Snake Game", g_nwidth, g_nheight) {return 0 ;}// initialize game Init (); // game loop mainloop (); // release the resource clear (); Return 0 ;}


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.