C realizes tank movement

Source: Internet
Author: User

#include <Windows.h> #include <stdio.h> #include <stdlib.h> #include <conio.h> #include < time.h> #define USERTANKMAP 1#define enetankmap 2#define usertank "" #define TANK_DIR_UP 0#define tank_dir_dw 2#define TANK_DIR_LF 1#define tank_dir_rh 3//user tank typedef struct user//speed is 3{int x; int y; int life;//default is int aggressivity;//default to + int dir;} User, *puser;//global users myuser;//characters print void Printchar (int Wide, int high, Char*pszchar, WORD wattr) {//1. Set cursor Properties Console_ Cursor_info CCI; Cci.dwsize = 1; cci.bvisible = FALSE; HANDLE HOut = GetStdHandle (Std_output_handle); Setconsolecursorinfo (HOut, &cci); 2. Modify the coordinate position of the cursor in the console COORD Loc; Loc. X = Wide * 2; Loc. Y = high; 3. Set the coordinate position of the cursor in the console setconsolecursorposition (HOut, loc); Sets the foreground color background color of the print symbol Setconsoletextattribute (hOut, wattr); Output the printf you want to output ("%s", Pszchar);} void Tankchar (int Wide, int high, Char*pszchar, WORD wattr) {switch (myuser.dir) {case Tank_dir_lf:printchar (myuser.x, Myuser.y, Usertank, wattr); ///////////////Printchar (myuser.x, Myuser.y-1, Usertank, wattr); Printchar (Myuser.x-1, Myuser.y, Usertank, wattr); Printchar (myuser.x, Myuser.y + 1, Usertank, wattr); Printchar (myuser.x + 1, myuser.y-1, Usertank, wattr); Printchar (myuser.x + 1, myuser.y + 1, Usertank, wattr); Break Case Tank_dir_rh:printchar (myuser.x, Myuser.y, Usertank, wattr); Printchar (myuser.x + 1, myuser.y, Usertank, wattr); Printchar (myuser.x, Myuser.y-1, Usertank, wattr); Printchar (myuser.x, Myuser.y + 1, Usertank, wattr); Printchar (Myuser.x-1, Myuser.y-1, Usertank, wattr); Printchar (myuser.x-1, Myuser.y + 1, Usertank, wattr); Break Case Tank_dir_up:printchar (myuser.x, Myuser.y, Usertank, wattr); Printchar (myuser.x + 1, myuser.y-1, Usertank, wattr); Printchar (myuser.x, Myuser.y-1, Usertank, wattr); Printchar (myuser.x + 2, myuser.y, Usertank, WATTR); Printchar (myuser.x + 2, myuser.y-1, Usertank, wattr); Printchar (myuser.x + 1, myuser.y-2, Usertank, wattr); Break Case Tank_dir_dw:printchar (myuser.x, Myuser.y, Usertank, wattr); Printchar (Myuser.x-1, Myuser.y, Usertank, wattr); Printchar (Myuser.x-1, Myuser.y-1, Usertank, wattr); Printchar (myuser.x, Myuser.y + 1, Usertank, wattr); Printchar (myuser.x + 1, myuser.y, Usertank, wattr); Printchar (myuser.x + 1, myuser.y-1, Usertank, wattr); Break Default:break; }}void tankmove (int m_ndir) {if (Myuser.x > 3 | | myuser.x<37 | | myuser.y >3 | | myuser.y<37) {PRINTCHAR (myuser . x + 1, myuser.y, "", 0x00); System ("CLS"); Switch (m_ndir) {case tank_dir_up:myuser.y--, break, Case tank_dir_dw:myuser.y++, break, Case tank_dir_lf:myuser.x--; Break Case tank_dir_rh:myuser.x++; Break } Tankchar (Myuser.x, Myuser.y, Usertank, 0x44); }}bool Changedir (char cdir) {int nnewdir =-1; switch (cdir) {case ' W ': CAsE ' W ': Nnewdir = tank_dir_up; Break Case ' s ': Case ' s ': Nnewdir = tank_dir_dw; Break Case ' A ': Case ' a ': Nnewdir = TANK_DIR_LF; Break Case ' d ': Case ' d ': Nnewdir = TANK_DIR_RH; Break Case ' Q ': Case ' Q ': Exit (0); Break Case ' E ': Case ' E ': _getch (); Break Default:break; } Myuser.dir = Nnewdir; Tankmove (Nnewdir); return true;} void Main () {//////////////////simulates pressing the shit key////////////////keybd_event (vk_shift, 0, 0, 0); keybd_event (Vk_shift, 0, Keyeventf_keyup, 0); Myuser.x = 20; Myuser.y = 20; Myuser.dir = Tank_dir_dw; Printchar (myuser.x, Myuser.y, Usertank, 0x55); char ch = _getch (); if (ch = = ' W ' | | ch = = ' W ' | | ch = = ' A ' | | ch = = ' A ' | | ch = = ' s ' | | * = = ' s ' | | ch = = ' d ' | | ch = = ' d ') {//Set movement time interval for tank clock_t tbegin = Clock (), tEnd = 0; while (true) {if (_kbhit ()) {Char ch = _getch (); Changedir (CH); } tEnd = Clock (); if (Tend-tbegin > $) {tbegin = tEnd; Tankmove (Myuser.dir); } } }}

C implements tank movement

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.