C language write bomb man games

Source: Internet
Author: User
Tags sprintf

Gradually changed for half a month, and constantly change the purpose of the game, combined with the mouse maze and snake game, finally in yesterday 2 o'clock in the morning wrote my bomb Superman game. Based on the Windows graphical interface, written in C language. It's not complicated, but it's very rewarding.

document 1:WINAPP.CPP, guide the teacher to give the framework and write their own main function:

#include <windows.h>
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
#include "Winapp.h"
#include "myfile.h"
int WINAPI WinMain (hinstance hinstance, hinstance hprevinstance,pstr szcmdline, int icmdshow)
{
Static TCHAR szappname[] = TEXT ("Windows application design--the Maze");
MSG msg;
Wndclass Wndclass;
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.lpszclassname = Szappname;
if (! RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows nt!"), Szappname, Mb_iconerror);
return 0;
}
hwnd = CreateWindow (szappname,//window class name
TEXT ("Windows application design--the Maze"),//Window caption
Ws_overlappedwindow,//Window style
Cw_usedefault,//initial X position
Cw_usedefault,//initial y position
//cw_usedefault,//initial x size
//cw_usedefault,//Initial y size
NULL,//parent window Handle
NULL,//Window menu handle
HINSTANCE,//program instance handle
NULL); Creation parameters
ShowWindow (hwnd, icmdshow);
UpdateWindow (HWND);

while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&MSG);
DispatchMessage (&MSG);
}
return msg.wparam;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM WPARAM, LPARAM LPARAM)
{


DrawRectangle (38*width,3*width,39*width,8*width,weakwall); Context
DrawRectangle (36*width,5*width,41*width,6*width,weakwall); Around
DrawRectangle (38*width,5*width,39*width,6*width,boom);

Printtext (800,400, "Bomb man!!!");
Printtext (800,450, "Come on baby!!!");
Printtext (1100,355, "へ/|");
Printtext (1100,375, "/\∠_/");
Printtext (1100,395, "/│//");
Printtext (1100,415, "│_,<//' 's finger licking");
Printtext (1100,435, "│ヽ/〉");
Printtext (1100,455, "\ \"//");
Printtext (1100,475, "| |〈/");
Printtext (1100,495, "() へ|\〈");
Printtext (1100,505, "> _ィ│//");
Printtext (1100,525, "/へ/<|\\");
Printtext (1100,545, "ヽ_| (_/│//");
Printtext (1100,565, "| |/");
Printtext (1100,585, ">―- ̄ ̄//―_");
if (yes==1)
{
Printtext (800,300, "~o (∩_∩) o~");
Printtext (800,350, "you won!!!");
return 0;
}
Char s[100];
Switch (message)
{
Case WM_KEYDOWN://Keyboard message
{
Keyboard Message processing content
switch ((int) wParam)
{
Case VK_UP://Up ↑
Up ();
Break
Case Vk_down://Down ↓
Down ();
Break
Case Vk_left://Left ←
Left ();
Break
Case Vk_right://Right →
Right ();
Break
Case Vk_return://Enter
Initgame ();
Break
Case Vk_space://Space put bombs
BACKSPACE ();
Break
Case Vk_escape://esc
Boomboom ();
Break
}
}
Break
}
Return DefWindowProc (HWND, message, WParam, LParam);
}

file 2:myfile.h, the program you wrote:

#include <time.h>
#include <stdlib.h>
#define ROAD RGB (200,200,200)//light gray
#define Weakwall RGB (255,174,200)//False wall
#define MOUSE RGB (255,100,168)//pink
#define BOOM RGB (0,0,0)//Bombs
#define WALL RGB (50,50,50)//dark grey
#define Fire RGB (225,0,0)//red
#define Yeah RGB (255,225,128)//yellow
#define Food RGB (50,50,225)//blue

#define UP 40
#define DOWN 41
#define LEFT 42
#define RIGHT 43
#define X0 0
#define Y0 0
#define WIDTH 30

int xnum=21;
int ynum=21;
int boomx1,boomy1,boomx2,boomy2;
int a[30][30]={
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,1},
{1,0,1,0,1,2,1,0,1,0,1,0,1,2,1,0,1,0,1,0,1},
{1,0,0,0,0,0,0,0,0,2,0,2,0,2,0,0,0,0,2,0,1},
{1,0,1,0,1,0,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1},
{1,0,0,0,0,2,0,2,2,0,0,2,0,0,2,2,0,2,0,0,1},
{1,0,1,2,1,0,1,0,1,2,1,2,1,2,1,0,1,2,1,2,1},
{1,0,2,0,0,0,0,2,0,0,2,2,0,2,0,0,2,0,0,2,1},
{1,0,1,2,1,2,1,2,1,0,1,0,1,0,1,0,1,0,1,2,1},
{1,0,2,0,2,2,0,2,0,0,2,2,0,2,0,0,0,0,0,0,1},
{1,2,1,0,1,2,1,2,1,0,1,2,1,0,1,0,1,0,1,0,1},
{1,2,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,2,1,2,1,0,1,0,1,0,1,2,1,0,1,0,1,0,1,2,1},
{1,0,0,2,0,0,0,0,0,2,0,2,0,0,0,0,2,0,0,0,1},
{1,0,1,0,1,0,1,0,1,0,1,0,1,2,1,2,1,2,1,0,1},
{1,2,0,0,0,0,2,2,0,0,2,0,2,0,0,0,2,0,0,0,1},
{1,0,1,0,1,0,1,2,1,0,1,0,1,0,1,0,1,2,1,0,1},
{1,0,0,0,2,0,0,0,2,2,2,2,0,0,2,2,2,0,2,0,1},
{1,0,1,0,1,0,1,2,1,2,1,0,1,0,1,0,1,0,1,0,1},
{1,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
};
int Mousex,mousey;
int foodx,foody;
int q,w,e,r;
int flag=0;
int yes=0;
int sum=0;
void Initgame ()
{
for (int x=0;x<xnum;x++)
for (int y=0;y<ynum;y++)
{
if (a[x][y]==1)
DrawRectangle (X*width,y*width, (x+1) *width, (y+1) *width,wall);
else if (a[x][y]==0)
DrawRectangle (X*width,y*width, (x+1) *width, (y+1) *width,road);
Else
DrawRectangle (X*width,y*width, (x+1) *width, (y+1) *width,weakwall);

}
Mousex=1,mousey=1;
DrawRectangle (x0+mousex*width,y0+mousey*width,x0+ (mousex+1) *width,y0+ (mousey+1) *width,mouse);

DrawRectangle (19*width,19*width,20*width,20*width,yeah);
}
void Right ()
{
if (a[mousex+1][mousey]==0)
{
if (flag==1)
DrawRectangle (Mousex*width,mousey*width, (mousex+1) *width, (mousey+1) *width,boom);
Else
DrawRectangle (Mousex*width,mousey*width, (mousex+1) *width, (mousey+1) *width,road);
mousex++;
DrawRectangle (x0+mousex*width,y0+mousey*width,x0+ (mousex+1) *width,y0+ (mousey+1) *width,mouse);
}
flag=0;
if (mousex==19&&mousey==19)
Yes=1;
}
void Left ()
{
if (a[mousex-1][mousey]==0)
{
if (flag==1)
DrawRectangle (Mousex*width,mousey*width, (mousex+1) *width, (mousey+1) *width,boom);
Else
DrawRectangle (Mousex*width,mousey*width, (mousex+1) *width, (mousey+1) *width,road);
mousex--;
DrawRectangle (x0+mousex*width,y0+mousey*width,x0+ (mousex+1) *width,y0+ (mousey+1) *width,mouse);
}
flag=0;
if (mousex==19&&mousey==19)
Yes=1;
}
void Up ()
{
if (a[mousex][mousey-1]==0)
{
if (flag==1)
DrawRectangle (Mousex*width,mousey*width, (mousex+1) *width, (mousey+1) *width,boom);
Else
DrawRectangle (Mousex*width,mousey*width, (mousex+1) *width, (mousey+1) *width,road);
mousey--;
DrawRectangle (x0+mousex*width,y0+mousey*width,x0+ (mousex+1) *width,y0+ (mousey+1) *width,mouse);
}
flag=0;
if (mousex==19&&mousey==19)
Yes=1;
}
void Down ()
{
if (a[mousex][mousey+1]==0)
{
if (flag==1)
DrawRectangle (Mousex*width,mousey*width, (mousex+1) *width, (mousey+1) *width,boom);
Else
DrawRectangle (Mousex*width,mousey*width, (mousex+1) *width, (mousey+1) *width,road);
mousey++;
DrawRectangle (x0+mousex*width,y0+mousey*width,x0+ (mousex+1) *width,y0+ (mousey+1) *width,mouse);
}
flag=0;
if (mousex==19&&mousey==19)
Yes=1;
}
void Backspace ()
{
Boomx1=mousex*width;
Boomy1=mousey*width;
Boomx2= (mousex+1) *width;
Boomy2= (mousey+1) *width;
q=mousex;w=mousey;e=mousex+1;r=mousey+1;
DrawRectangle (Boomx1,boomy1,boomx2,boomy2,boom);
flag=1;

}
void Boomboom ()
{
int i,j;
DrawRectangle (Boomx1,boomy1,boomx2,boomy2,road);
DrawRectangle (Boomx1,boomy1-2*width,boomx2,boomy1,road); On

if (boomy1+width<=19)
DrawRectangle (boomx1,boomy1+width,boomx2,boomy2+width,road);//Lower
if (boomy1+2*width<=20)
DrawRectangle (boomx1,boomy1+2*width,boomx2,boomy2+2*width,road);//Lower

DrawRectangle (boomx1-2*width,boomy1,boomx1,boomy2,road);//Left

if (boomx1+width<=19)//Right
DrawRectangle (Boomx1+width,boomy1,boomx2+width,boomy2,road);
if (boomx1+2*width<=20)//Right
DrawRectangle (Boomx1+2*width,boomy1,boomx2+2*width,boomy2,road);

a[(BOOMX1)/width][boomy1/width]=a[boomx1/width][(boomy1-2*width)/width]=a[boomx1/width][(Boomy1-WIDTH)/WIDTH ]=a[boomx1/width][(boomy1+width)/width]=a[boomx1/width][(boomy1+2*width)/width]=a[(Boomx1-WIDTH)/WIDTH][boomy1 /width]=a[(boomx1-2*width)/width][boomy1/width]=a[(boomx1+width)/width][boomy1/width]=a[(Boomx1+2*WIDTH)/WIDTH ][boomy1/width]=0;
for (i=mousex-2;i<=mousex+3;i++)
for (j=mousey-2;j<=mousey+3;j++)
/*if ((MouseX >= 2) && a[mousex-2][mousey]!=1)
a[mousex-2][mousey]=0;
Else
DrawRectangle ((mousex-2) *width, (Mousey) *width, (mousex-2+1) *width, (mousey+1) *width,wall);

if ((MouseX >= 1) && a[mousex-1][mousey]!=1)
a[mousex-1][mousey]=0;
Else
DrawRectangle ((mousex-1) *width, (Mousey) *width, (mousex-1+1) *width, (mousey+1) *width,wall);

if ((mousex+1<=20) && a[mousex+1][mousey]!=1)
a[mousex+1][mousey]=0;
Else
DrawRectangle ((mousex+1) *width, (Mousey) *width, (mousex+1+1) *width, (mousey+1) *width,wall);

if ((mousex+2<=20) &&a[mousex+2][mousey]!=1)
a[mousex+2][mousey]=0;
Else
DrawRectangle ((mousex+2) *width, (Mousey) *width, (mousex+2+1) *width, (mousey+1) *width,wall);

if ((Mousey >= 2) && a[mousex][mousey-2]!=1)
a[mousex][mousey-2]=0;
Else
DrawRectangle ((MouseX) *width, (mousey-2) *width, (mousex+1) *width, (mousey-2+1) *width,wall);

if ((Mousey >= 1) && a[mousex][mousey-1]!=1)
a[mousex][mousey-1]=0;
Else
DrawRectangle ((MouseX) *width, (mousey-1) *width, (mousex+1) *width, (mousey-1+1) *width,wall);

if ((mousey+1<=20) &&a[mousex][mousey+1]!=1)
a[mousex][mousey+1]=0;
Else
DrawRectangle ((MouseX) *width, (mousey+1) *width, (mousex+1) *width, (mousey+1+1) *width,wall);

if ((mousey+2<=20) &&a[mousex][mousey+2]!=1)
a[mousex][mousey+2]=0;
Else
DrawRectangle ((MouseX) *width, (mousey+2) *width, (mousex+1) *width, (mousey+2+1) *width,wall); * *
if ((q >= 2) && a[q-2][w]!=1)
a[q-2][w]=0;
Else
DrawRectangle ((q-2) *width, (W) *width, (q-2+1) *width, (w+1) *width,wall);

if ((q >= 1) && a[q-1][w]!=1)
a[q-1][w]=0;
Else
DrawRectangle ((q-1) *width, (W) *width, (q-1+1) *width, (w+1) *width,wall);

if (a[q+1][w]!=1)
a[q+1][w]=0;
Else
DrawRectangle ((q+1) *width, (W) *width, (q+1+1) *width, (w+1) *width,wall);

if (a[q+2][w]!=1)
a[q+2][w]=0;
Else
DrawRectangle ((q+2) *width, (W) *width, (q+2+1) *width, (w+1) *width,wall);

if ((w >= 2) && a[q][w-2]!=1)
a[q][w-2]=0;
Else
DrawRectangle ((q) *width, (w-2) *width, (q+1) *width, (w-2+1) *width,wall);

if ((w >= 1) && a[q][w-1]!=1)
a[q][w-1]=0;
Else
DrawRectangle ((q) *width, (w-1) *width, (q+1) *width, (w-1+1) *width,wall);

if (a[q][w+1]!=1)
a[q][w+1]=0;
Else
DrawRectangle ((q) *width, (w+1) *width, (q+1) *width, (w+1+1) *width,wall);

if (a[q][w+2]!=1)
a[q][w+2]=0;
Else
DrawRectangle ((q) *width, (w+2) *width, (q+1) *width, (w+2+1) *width,wall);


The outer wall, the dead wall, himself, the food
for (int x=0;x<xnum;x++)
for (int y=0;y<ynum;y++)
{
if (A[x][y]==1&&x!=mousex&&y!=mousey)
DrawRectangle (X*width,y*width, (x+1) *width, (y+1) *width,wall);
else if (A[x][y]==0&&x!=mousex&&y!=mousey)
DrawRectangle (X*width,y*width, (x+1) *width, (y+1) *width,road);
}
DrawRectangle (x0+19*width,y0+19*width,x0+ (19+1) *width,y0+ (19+1) *width,yeah);
Flag = 0;
}

file 3: Some functions under the Windows graphical interface (teacher-given framework):

#include <stdio.h>

HWND hwnd;
Set Timer interval
Speed is milliseconds
void setspeed (int movespeed)
{
SetTimer (hwnd,1, Movespeed, NULL);
}
Pause Timer
void Stoptimer ()
{
KillTimer (hwnd,1);
}
Specify location output string
void Printtext (int x,int Y,char s[])
{
HDC hdc;
HDC=GETDC (HWND);
TextOut (Hdc,x,y,s,strlen (s));
ReleaseDC (hwnd, HDC);
}
Specify the position output integer number
void Printnumber (int x,int y, int z)
{
Char str[100];
sprintf (str, "%d", z);
Printtext (X,Y,STR);
}
Specify position output real number
void Printnumber (int x,int y, double z)
{
Char str[100];
sprintf (str, "%f", z);
Printtext (X,Y,STR);
}
Output Fractions and ratings
void PrintTitle (int score,int level)
{
Char str[100];
sprintf (str, "Current score%5d", score);
Printtext (200,20,STR);
sprintf (str, "Rank%5d", level);
Printtext (200,50,STR);
}
Draw a rectangle with a vertex (x1,y1), (x2,y2), color
void DrawRectangle (int x1,int y1,int x2,int y2,int color)
{
HDC hdc;
HDC=GETDC (HWND);
Hbrush Hbrush;
Rect rect;
Point A[4];
A[0].x=x1,a[0].y=y1;
A[1].x=x2,a[1].y=y1;
A[2].x=x2,a[2].y=y2;
A[3].x=x1,a[3].y=y2;
A[4].x=x1,a[4].y=y1;
SetRect (&AMP;RECT,X1,Y1,X2,Y2);

Hbrush = CreateSolidBrush (color);
FillRect (hdc, &rect, hbrush); Fill Rectangle
SelectObject (hdc, Getstockobject (Black_pen));
Polyline (HDC, A, 5); Draw Border

ReleaseDC (hwnd, HDC);
DeleteObject (Hbrush);
}
void DrawImage (int x0, int y0, int x1, int y1, char *szimagefile)
{
int wid, HEI;
int i;

WID = x1-x0 + 1;
HEI = y1-y0 + 1;
HDC HDC =:: GetDC (HWND);

for (i = 0; i < 5; i++)
{
HANDLE h =:: LoadImage (0, Szimagefile, Image_bitmap, WID, Hei, lr_shared | Lr_loadfromfile);
Size size;
:: Getbitmapdimensionex ((HBITMAP) H, &size);//Issue: Original size not read successfully
HDC Hdcmem =:: CreateCompatibleDC (HDC);
:: SelectObject (Hdcmem, h);
:: BitBlt (hdc, x0, y0, wid, Hei, Hdcmem, 0, 0, srccopy);
WID/= 2;
Hei/= 2;
x0 + = wid * 2 + 10;
}
}

C language write bomb man games

Related Article

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.