C语言写炸弹人小游戏

陆续改了半个月,不断更改游戏目的,结合了老鼠走迷宫和贪吃蛇游戏,终于在昨天凌晨2点写好了我的炸弹超人游戏。基于Windows图形界面,用C语言编写。虽然并不复杂,但收获颇多。

文件1:winapp.cpp,指导老师所给框架及自己写的主函数:

#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 应用程序设计--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 应用程序设计--The Maze"), // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
1600, //CW_USEDEFAULT, // initial x size
900, //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); //上下
drawRectangle(36*WIDTH,5*WIDTH,41*WIDTH,6*WIDTH,WEAKWALL); //左右
drawRectangle(38*WIDTH,5*WIDTH,39*WIDTH,6*WIDTH,BOOM);

printText(800,400,"炸弹人!!!");
printText(800,450,"come on baby!!!");
printText(1100,355,"  へ    /|");
printText(1100,375,"  / \    ∠_/");
printText(1100,395,"  / │   / /");
printText(1100,415," │  _,< /   /`ヽ");
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,"你赢了 !!!");
return 0;
}
//char s[100];
switch (message)
{
case WM_KEYDOWN: //键盘消息
{
//键盘消息处理内容
switch ((int)wParam)
{
case VK_UP: //向上 ↑
up();
break;
case VK_DOWN: // 向下 ↓
down();
break;
case VK_LEFT: //向左 ←
left();
break;
case VK_RIGHT: // 向右 →
right();
break;
case VK_RETURN: //回车
initgame();
break;
case VK_SPACE: //空格放炸弹
backspace();
break;
case VK_ESCAPE: //esc
boomboom();
break;
}
}
break;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}

文件2:myfile.h,自己编写的程序:

#include<time.h>
#include<stdlib.h>
#define ROAD RGB(200,200,200)//浅灰
#define WEAKWALL RGB(255,174,200)//假墙
#define MOUSE RGB(255,100,168)//pink
#define BOOM RGB(0,0,0)//炸弹
#define WALL RGB(50,50,50)//深灰
#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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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); //上

//if(boomy1+WIDTH<=19)
drawRectangle(boomx1,boomy1+WIDTH,boomx2,boomy2+WIDTH,ROAD);//下
if(boomy1+2*WIDTH<=20)
drawRectangle(boomx1,boomy1+2*WIDTH,boomx2,boomy2+2*WIDTH,ROAD);//下

drawRectangle(boomx1-2*WIDTH,boomy1,boomx1,boomy2,ROAD);//左

//if(boomx1+WIDTH<=19) //右
drawRectangle(boomx1+WIDTH,boomy1,boomx2+WIDTH,boomy2,ROAD);
if(boomx1+2*WIDTH<=20) //右
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);

//外墙,死墙,自己,食物
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;
}

文件3:一些Windows图形界面下的函数,(老师给的框架):

#include <stdio.h>

HWND hwnd;
//设置定时器间隔
//speed为毫秒
void setSpeed(int movespeed)
{
SetTimer(hwnd,1, movespeed, NULL);
}
//暂停定时器
void stopTimer()
{
KillTimer(hwnd,1);
}
//指定位置输出字符串
void printText(int x,int y,char s[])
{
HDC hdc;
hdc=GetDC(hwnd);
TextOut(hdc,x,y,s,strlen(s));
ReleaseDC (hwnd, hdc) ;
}
//指定位置输出整型数
void printNumber(int x,int y, int z)
{
char str[100];
sprintf(str,"%d",z);
printText(x,y,str);
}
//指定位置输出实型数
void printNumber(int x,int y, double z)
{
char str[100];
sprintf(str,"%f",z);
printText(x,y,str);
}
//输出分数和等级
void printTitle(int score,int level)
{
char str[100];
sprintf(str,"当前分数 %5d",score);
printText(200,20,str);
sprintf(str,"  等级 %5d",level);
printText(200,50,str);
}
//画矩形,顶点为(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 (&rect,x1,y1,x2,y2);

hBrush = CreateSolidBrush (color) ;
FillRect (hdc, &rect, hBrush) ; //填充矩形
SelectObject (hdc, GetStockObject (BLACK_PEN)) ;
Polyline (hdc, a, 5) ; //画边框

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); // 问题:原图size尚未成功读取
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;
}
}

时间: 2024-12-23 00:58:37

C语言写炸弹人小游戏的相关文章

C语言写扫雷小游戏2

这是下午写的,这次的修改增加了鼠标的事件,需要的是windows的消息机制,WinAPI函数,以下是新添加的定义 struct { int num;//格子当前状态,1表示有雷,0表示无雷或已经显示数字 int roundnum;//统计格子周围的雷数 int flag;//右键按下显示红旗标志,0表示没有,1表示有 }Mine[10][10]; POINT Mouse;//定义鼠标事件 int MouseX, MouseY;//鼠标的x,y坐标 int mineNUM;//统计处理过的格子数

C语言写扫雷小游戏1

用的ide是vs 2013,自己学习都是参考网上的代码,由于vs不带tc的graphics.h,所以下载easyx库,将include和lib复制到vc的目录. easyx的graphics.h和tc的还是有区别的,百科上的示例 使用上,基本和 Turbo C没太大区别.启动Visual C++,创建一个控制台项目(Win32 Console Application),然后引用 graphics.h 头文件就可以了.看一个画圆的例子: #include <graphics.h> // 就是需要

C语言 三子棋 小游戏

//C语言 三子棋 小游戏 //棋盘为3x3大小,电脑棋子 X ,玩家 O : //用命令窗口模拟3子棋,玩家与电脑轮流下,先连成3子判赢:无路可走判平局 #include<stdio.h> #include<string.h> #include<windows.h> int judge_is_moved(char arr[][12], int place); void print_chessboard(char arr[][12])//输出棋盘 { int i = 0

实习2个月后,第一次自己写的小游戏来玩玩

这是小弟第一次写的小游戏,基于jQ写的,没用面向对象思维去写,可能写得不好,请见谅,亲测是能玩的!! 尾部我附上材料图片提供大家下载 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta charset="utf-8" /> <

用Python写一个小游戏

刚学Python时间不长,但也知道了一点,看别人的参考写了一个猜数字小游戏,也算是禹学于乐吧. #!/usr/bin/env   python #coding=utf-8 import random secret = random.randint(1,100) guess,tries = 0,0 print u"已经给出了一个1-99的数字" while guess != secret and tries < 5: print u"请给出你猜的数字:" pri

用Turbo C 2.0写五子棋小游戏

这辈子再也不用Turbo C写东西了_(:зゝ∠)_ 功能 有比较友好(大概友好吧:) )的界面. 采用贪心算法,能与计算机对弈 流程图 主函数 int main() { int gd = DETECT, gm = 0; int key; initgraph(&gd, &gm, "c:\\tc"); opencartoon(); /*开场动画*/ init(); /*界面和棋盘初始化*/ key = get_key(); while (!quit) { /*只要没有退出

用C语言写猜数字游戏

#include <stdio.h> #include <stdlib.h> #include <time.h> int main() {  int start=1;  while(start)  {   printf("请选择:\n");   printf("1:开始游戏\n");   printf("0:退出游戏\n");   scanf("%d",&start);   swit

C语言猜数字小游戏

#include <stdio.h> #include <time.h> #include <stdlib.h> int main() { int x,num,st,ed; char ch; srand((int)time(NULL)); while(1) { printf("请输入你想要猜的数的范围(用空格隔开):"); scanf("%d %d",&st,&ed); ed=ed-st+1; num=rand()

用原生js写2048小游戏

WEB前端交流群  172994155  回复 ww 验证  <!DOCTYPE html> <html> <head> <title> 2048-game </title> <meta charset="utf-8" /> <style media="screen"> #game { display: none; position: absolute; left: 0px; top