Self-writing games-start my blog journey with retro snker

Source: Internet
Author: User

Hua Ge and Gao Xin told me. Write more blogs. Record your learning problems and feelings. In this way, we can better learn and communicate with you. You can also find more fun and extensive knowledge.

Yesterday. Xingge said that I have learned this for so long. I can also try to write some simple mini-programs and practice them first. In this way, you can learn from the practice and discover problems that cannot be found in the textbooks. For your reference. Yes, you are learning in a closed corner. The progress is really slow. So. I will write more blogs in the future. Ask more questions. I hope you can give me some advice. Thank you.

I tried to write a snake yesterday. Because I saw Gao Xin's source code two weeks ago. So the idea is also very clear.

However, many details are found in the writing process. Add the code first

// Snake
# Include <windows. h> # include <stdlib. h> # include <time. h> int num = 5; # define id_timer 1 int I, BTN = 4; lresult callback wndproc (hwnd, uint, wparam, lparam); struct snake {int left; int top; int right; int button;} s [500]; struct food {int X; int y;} f; int winapi winmain (hinstance, hinstance hprevinstance, pstr szcmdline, int icmdshow) {static tchar szappname [] = text ("retro snker"); hwnd; MSG; wndclass. style = cs_hredraw | cs_vredraw; wndclass. lpfnwndproc = wndproc; wndclass. cbclsextra = 0; wndclass. cbwndextra = 0; wndclass. hinstance = hinstance; wndclass. hicon = loadicon (null, idi_application); wndclass. hcursor = loadcursor (null, idc_arrow); wndclass. hbrbackground = (hbrush) getstockobject (white_brush); wndclass. lpszmenuname = NULL; wndclass. lpszclas Sname = szappname; If (! Registerclass (& wndclass) {MessageBox (null, text ("this program requires Windows NT! "), Szappname, mb_iconerror); Return 0;} hwnd = createwindow (szappname, // window class nametext (" retro snker _ crazw "), // window captionws_overlappedwindow, // window stylecw_usedefault, // initial X positioncw_usedefault, // initial y positioncw_usedefault, // initial X sizecw_usedefault, // initial y sizenull, // parent window handlenull, // Window Menu handlehinstance, // program instance Handlenull); // creation parametersshowwindow (hwnd, icmdshow); updatewindow (hwnd); While (getmessage (& MSG, null, 0, 0) {translatemessage (& MSG ); dispatchmessage (& MSG);} return MSG. wparam;} lresult callback wndproc (hwnd, uint message, wparam, lparam) {static HDC hdcscreen; HDC; paintstruct pS; rect; static int cxclient, cyclient; // int I, BTN = 3; // What is the definition of WSM? ???????????????????????? Switch (Message) {Case wm_create: settimer (hwnd, id_timer, 200, null); Return 0; Case wm_size: cxclient = loword (lparam); cyclient = hiword (lparam ); // Is there any difference between cxclient and cyclient getclientrect ????????? S [0]. left = 100; s [0]. top = 100; s [0]. right = 120; s [0]. button = 120; for (I = 1; I <num; I ++) {s [I]. left = s [I-1]. left + 20; S [I]. top = s [0]. top; s [I]. right = s [I-1]. right + 20; S [I]. button = s [0]. button;} srand (time (0); F. X = (RAND () % cxclient)/20*20; // make sure it is compatible with the snake head; this is what Gao Xin thinks. F. y = (RAND () % cyclient)/20*20; // invalidaterect (hwnd, null, true); Return 0; Case wm_timer: // response key if (BTN = 1) {for (I = 0; I <num-1; I ++) {s [I]. left = s [I + 1]. left; s [I]. top = s [I + 1]. top; s [I]. right = s [I + 1]. right; s [I]. button = s [I + 1]. button;} s [num-1]. top-= 20; S [num-1]. button-= 20;} else if (BTN = 2) {for (I = 0; I <num-1; I ++) {s [I]. left = s [I + 1]. left; s [I]. top = s [I + 1]. top; s [I]. right = S [I + 1]. right; s [I]. button = s [I + 1]. button;} s [num-1]. top + = 20; S [num-1]. button + = 20;} else if (BTN = 3) {for (I = 0; I <num-1; I ++) {s [I]. left = s [I + 1]. left; s [I]. top = s [I + 1]. top; s [I]. right = s [I + 1]. right; s [I]. button = s [I + 1]. button;} s [num-1]. right-= 20; S [num-1]. left-= 20;} else if (BTN = 4) {for (I = 0; I <num-1; I ++) {s [I]. left = s [I + 1]. left; s [I]. top = s [I + 1]. top; s [I]. right = S [I + 1]. right; s [I]. button = s [I + 1]. button;} s [num-1]. right + = 20; S [num-1]. left + = 20;} // how to die if (s [num-1]. right> cxclient | s [num-1]. left <0 | s [num-1]. top <0 | s [num-1]. button> cyclient + 20) // killtimer (hwnd, id_timer); MessageBox (hwnd, text ("Sorry, you lost! "), Text (" result "), mb_ OK | mb_iconerror); exit (0);} for (I = 0; I <num-1; I ++) {If (s [num-1]. left = s [I]. left & S [num-1]. right = s [I]. right & S [num-1]. top = s [I]. top & S [num-1]. button = s [I]. button) {killtimer (hwnd, id_timer); MessageBox (hwnd, text ("Sorry, you lost! "), Text (" result "), mb_ OK | mb_iconerror); exit (0) ;}// increase switch (BTN) {Case 1: if (s [num-1]. top = f. Y + 20 & S [num-1]. left = f. x) {num ++; s [num-1]. top = f. y; s [num-1]. left = f. x; s [num-1]. right = f. X + 20; S [num-1]. button = f. Y + 20; F. X = (RAND () % cxclient)/20*20; F. y = (RAND () % cyclient)/20*20; break;} Case 2: If (s [num-1]. top = f. Y & S [num-1]. left = f. x) {num ++; s [num-1]. top = f. y; s [num-1]. left = f. x; s [num-1]. right = f. X + 20; S [num-1]. button = f. Y + 20; F. X = (RAND () % cxclient)/20*20; F. y = (RAND () % cyclient)/20*20; break;} Case 3: If (s [num-1]. top = f. Y & S [num-1]. left = f. x) {num ++; s [num-1]. top = f. y; s [num-1]. left = f. x; s [num-1]. right = f. X + 20; S [num-1]. button = f. Y + 20; F. X = (RAND () % cxclient)/20*20; F. y = (RAND () % cyclient)/20*20; break;} case 4: If (s [num-1]. top = f. Y & S [num-1]. left = f. x) {num ++; s [num-1]. top = f. y; s [num-1]. left = f. x; s [num-1]. right = f. X + 20; S [num-1]. button = f. Y + 20; F. X = (RAND () % cxclient)/20*20; F. y = (RAND () % cyclient)/20*20; break;} default: break;} invalidaterect (hwnd, null, true); Return 0; Case wm_keydown: switch (wparam) {Case vk_up: BTN = 1; break; Case vk_down: BTN = 2; break; Case vk_left: BTN = 3; break; Case vk_right: BTN = 4; break;} return 0; Case wm_paint: HDC = beginpainting (hwnd, & PS); for (I = 0; I <num-1; I ++) {rectangle (HDC, s [I]. left, s [I]. top, s [I]. right, s [I]. button);} SelectObject (HDC, (hbrush) getstockobject (gray_brush); rectangle (HDC, s [num-1]. left, s [num-1]. top, s [num-1]. right, s [num-1]. button); ellipse (HDC, F. x, F. y, F. X + 20, F. Y + 20); endpaint (hwnd, & PS); Return 0; Case wm_destroy: killtimer (hwnd, id_timer); postquitmessage (0); Return 0;} return defwindowproc (hwnd, message, wparam, lparam );}

It has roughly achieved the basic functions of the snake. However, there are still many improvements. So. I hope you can give some suggestions and suggestions. Thank you very much.
Supplement: When the collision frame is processed. Gao Xin said it could be improved. You can initialize the window size and position when creating a window. Make global arrangements for the size and location of snakes and food.
I learned a little more.

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.