<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
採用程式導向的遍程思想,通過acllib圖形庫來實現。
acllib下載地址:acllib
tom,jerry,dog,heart以及開始圖片,結束圖片這些可以自己找,圖片的格式必須為BMP、JPEG、GIF這三種。
通過visual studio(這裡用的visual studio 2022)建立win32專案以及新增我們所需要的資原始碼檔案。
1、選擇Windows桌面嚮導
2、修改專案名稱
3、選擇空專案
4、將我們所需要的圖片以及acllib.h,acllib.c複製到此資料夾中
5、新增新建項,即.cpp程式
單擊新增即可。
6、根據第五步新增現有項,即把資原始檔夾裡的acllib.c,acllib.h以及各種圖片新增到程式中來。新增後如下圖
至此win32專案建立完成。
#include"acllib.h" #include<time.h> #include<malloc.h> #include<stdio.h> const int winWidth = 960, winHeight = 660; ACL_Image tomimg; ACL_Image jerryimg; ACL_Sound sound; ACL_Image dogimg; ACL_Image heartimg; ACL_Image beginimg; ACL_Image endimg; const int maxNum = 2000; const int maxJerryNum = 20; const int maxDogNum = 20; const int maxHeartNum = 20; int jerryNum = 0; int dogNum = 0; int heartNum = 0; int preJerryNum = 0; int preDogNum = 0; int preHeartNum = 0; int flag = 0; void timerEvent(int id);//定時器 void keyEvent(int key, int e);//鍵盤 void initgame();//初始化函數 int moveid = 0, movetimerInterval = 30; int createid = 1, createtimerInterval = 1000; int dogid1 = 2, dogtimerInterval1 = 5000; int dogid2 = 3, dogtimerInterval2 = 30; int heartid1 = 4, hearttimerInterval1 = 30; int heartid2 = 5, hearttimerInterval2 = 8000; int score1 = 0, score2 = 8; void beginpaint();//開始介面 void Paint();//遊戲介面 void endpaint();//結束介面 struct sprite//精靈 { int x, y; int width, height; int distx,disty; }; typedef struct rect { int x, y; int w, h; }rect; int collision(rect r1, rect r2);//檢測碰撞 void change(int score);//改變Tom貓的大小 struct sprite tomsprite, *jerrysprite[maxNum]={NULL},*dogsprite[maxNum]={NULL},*heartsprite[maxNum]={NULL}; int Setup() { initWindow("My first game", DEFAULT, DEFAULT, winWidth, winHeight); loadImage("tom.bmp", &tomimg); loadImage("jerry.bmp", &jerryimg); loadSound("tada.wav", &sound); loadImage("dog.bmp", &dogimg); loadImage("heart.bmp", &heartimg); loadImage("begin.bmp", &beginimg); loadImage("end.bmp", &endimg); srand((unsigned)time(NULL)); initgame(); registerTimerEvent(timerEvent); registerKeyboardEvent(keyEvent); beginpaint(); return 0; } void initgame() { memset(jerrysprite, NULL, sizeof(jerrysprite)); memset(dogsprite, NULL, sizeof(dogsprite)); memset(heartsprite, NULL, sizeof(heartsprite)); score1 = 0; score2 = 8; jerryNum = 0; dogNum = 0; heartNum = 0; preJerryNum = 0; preDogNum = 0; preHeartNum = 0; jerrysprite[jerryNum] = (struct sprite*)malloc(sizeof(struct sprite)); jerrysprite[jerryNum]->distx = jerrysprite[jerryNum]->disty = 3; jerrysprite[jerryNum]->height = 50; jerrysprite[jerryNum]->width = 50; jerrysprite[jerryNum]->x = rand() % (winWidth - jerrysprite[jerryNum]->width + 1); jerrysprite[jerryNum]->y = rand() % (winHeight - jerrysprite[jerryNum]->height + 1); tomsprite.width = 100; tomsprite.height = 100; tomsprite.distx = tomsprite.disty = 5; tomsprite.x = rand() % (winWidth - tomsprite.width + 1); tomsprite.y = rand() % (winHeight - tomsprite.height + 1); } void keyEvent(int key, int e) { if (e != KEY_DOWN)return; if (key == 0x52) { initgame(); flag = 1; Paint(); startTimer(moveid, movetimerInterval); startTimer(createid, createtimerInterval); startTimer(dogid1, dogtimerInterval1); startTimer(dogid2, dogtimerInterval2); startTimer(heartid1, hearttimerInterval1); startTimer(heartid2, hearttimerInterval2); } else if (flag == 1) { switch (key) { case VK_UP: tomsprite.y -= tomsprite.disty; if (tomsprite.y <= 0)tomsprite.y = 0; break; case VK_DOWN: tomsprite.y += tomsprite.disty; if (tomsprite.y >= winHeight - tomsprite.height)tomsprite.y = winHeight - tomsprite.height; break; case VK_RIGHT: tomsprite.x += tomsprite.distx; if (tomsprite.x >= winWidth - tomsprite.width)tomsprite.x = winWidth - tomsprite.width; break; case VK_LEFT: tomsprite.x -= tomsprite.distx; if (tomsprite.x <= 0)tomsprite.x = 0; break; default:break; } rect r1, r2, r3, r4; r1.x = tomsprite.x; r1.y = tomsprite.y; r1.w = tomsprite.width; r1.h = tomsprite.height; for (int i = 0; i <= jerryNum; ++i) { if (jerrysprite[i]) { r2.x = jerrysprite[i]->x; r2.y = jerrysprite[i]->y; r2.w = jerrysprite[i]->width; r2.h = jerrysprite[i]->height; int c = collision(r1, r2); if (c) { playSound(sound, 0); score1++; change(score1); if (score1 >= 10) { flag = 0; endpaint(); cancelTimer(moveid); cancelTimer(createid); cancelTimer(dogid1); cancelTimer(dogid2); cancelTimer(heartid1); cancelTimer(heartid2); return; } preJerryNum--; delete(jerrysprite[i]); jerrysprite[i] = NULL; } } } for (int i = 1; i <= dogNum; ++i) { if (dogsprite[i]) { r3.x = dogsprite[i]->x; r3.y = dogsprite[i]->y; r3.w = dogsprite[i]->width; r3.h = dogsprite[i]->height; int c = collision(r1, r3); if (c) { playSound(sound, 0); score2--; if (score1 >= 3) score1 -= 3; else score1 = 0; change(score1); if (score2 <= 0) { flag = 0; endpaint(); cancelTimer(moveid); cancelTimer(createid); cancelTimer(dogid1); cancelTimer(dogid2); cancelTimer(heartid1); cancelTimer(heartid2); return; } preDogNum--; delete(dogsprite[i]); dogsprite[i] = NULL; } } } for (int i = 1; i <= heartNum; ++i) { if (heartsprite[i]) { r4.x = heartsprite[i]->x; r4.y = heartsprite[i]->y; r4.w = heartsprite[i]->width; r4.h = heartsprite[i]->height; int c = collision(r1, r4); if (c) { playSound(sound, 0); score2++; change(score1); preHeartNum--; delete(heartsprite[i]); heartsprite[i] = NULL; } } } Paint(); } } void timerEvent(int id) { int a; if (id == 0) { for (a = 0; a <= jerryNum; a++) { if (jerrysprite[a]) { if (jerrysprite[a]->x >= winWidth - jerrysprite[a]->width || jerrysprite[a]->x <= 0) { jerrysprite[a]->distx *= -1; } if (jerrysprite[a]->y >= winHeight - jerrysprite[a]->height || jerrysprite[a]->y <= 0) { jerrysprite[a]->disty *= -1; } jerrysprite[a]->x += jerrysprite[a]->distx; jerrysprite[a]->y += jerrysprite[a]->disty; } } //Paint(); } if (id == 1) { if (preJerryNum < maxJerryNum) { jerryNum++; preJerryNum++; } if (preJerryNum < maxJerryNum && jerrysprite[jerryNum] == NULL) { jerrysprite[jerryNum] = (struct sprite*)malloc(sizeof(struct sprite)); jerrysprite[jerryNum]->width = 50; jerrysprite[jerryNum]->height = 50; jerrysprite[jerryNum]->distx = jerrysprite[jerryNum]->disty = 3; jerrysprite[jerryNum]->x = rand() % (winWidth - jerrysprite[jerryNum]->width+1); jerrysprite[jerryNum]->y = rand() % (winHeight - jerrysprite[jerryNum]->height+1); } } if (id == 2) { if (preDogNum < maxDogNum) { dogNum++; preDogNum++; } if (preDogNum < maxDogNum && dogsprite[dogNum] == NULL) { dogsprite[dogNum] = (struct sprite*)malloc(sizeof(struct sprite)); dogsprite[dogNum]->width = 100; dogsprite[dogNum]->height = 100; dogsprite[dogNum]->distx = dogsprite[dogNum]->disty = 2; dogsprite[dogNum]->x = rand() % (winWidth - dogsprite[dogNum]->width+1); dogsprite[dogNum]->y = rand() % (winHeight - dogsprite[dogNum]->height+1); } } if (id == 3) { for (a = 1; a <= dogNum; a++) { if (dogsprite[a]) { if (dogsprite[a]->x >= winWidth - dogsprite[a]->width || dogsprite[a]->x <= 0) { dogsprite[a]->distx *= -1; } if (dogsprite[a]->y >= winHeight - dogsprite[a]->height || dogsprite[a]->y <= 0) { dogsprite[a]->disty *= -1; } dogsprite[a]->x += dogsprite[a]->distx; dogsprite[a]->y += dogsprite[a]->disty; } } //Paint(); } if (id == 4) { for (a = 1; a <= heartNum; a++) { if (heartsprite[a]) { if (heartsprite[a]->x >= winWidth - heartsprite[a]->width || heartsprite[a]->x <= 0) { heartsprite[a]->distx *= -1; } if (heartsprite[a]->y >= winHeight - heartsprite[a]->height || heartsprite[a]->y <= 0) { heartsprite[a]->disty *= -1; } heartsprite[a]->x += heartsprite[a]->distx; heartsprite[a]->y += heartsprite[a]->disty; } } //Paint(); } if (id == 5) { if (preHeartNum < maxHeartNum) { heartNum++; preHeartNum++; } if (preHeartNum < maxNum && heartsprite[heartNum] == NULL) { heartsprite[heartNum] = (struct sprite*)malloc(sizeof(struct sprite)); heartsprite[heartNum]->width = 50; heartsprite[heartNum]->height = 50; heartsprite[heartNum]->distx = heartsprite[heartNum]->disty = 1; heartsprite[heartNum]->x = rand() % (winWidth - heartsprite[heartNum]->width + 1); heartsprite[heartNum]->y = rand() % (winHeight - heartsprite[heartNum]->height + 1); } } rect r1, r2,r3,r4; r1.x = tomsprite.x; r1.y = tomsprite.y; r1.w = tomsprite.width; r1.h = tomsprite.height; for (int i = 0; i <= jerryNum; ++i) { if (jerrysprite[i]) { r2.x = jerrysprite[i]->x; r2.y = jerrysprite[i]->y; r2.w = jerrysprite[i]->width; r2.h = jerrysprite[i]->height; int c = collision(r1, r2); if (c) { playSound(sound, 0); score1++; change(score1); if (score1 >= 10) { flag = 0; endpaint(); cancelTimer(moveid); cancelTimer(createid); cancelTimer(dogid1); cancelTimer(dogid2); cancelTimer(heartid1); cancelTimer(heartid2); return; } preJerryNum--; delete(jerrysprite[i]); jerrysprite[i] = NULL; } } } for (int i = 1; i <= dogNum; ++i) { if (dogsprite[i]) { r3.x = dogsprite[i]->x; r3.y = dogsprite[i]->y; r3.w = dogsprite[i]->width; r3.h = dogsprite[i]->height; int c = collision(r1, r3); if (c) { playSound(sound, 0); score2--; if (score1 >= 3) score1 -= 3; else score1 = 0; change(score1); if (score2 <= 0) { flag = 0; endpaint(); cancelTimer(moveid); cancelTimer(createid); cancelTimer(dogid1); cancelTimer(dogid2); cancelTimer(heartid1); cancelTimer(heartid2); return; } preDogNum--; delete(dogsprite[i]); dogsprite[i] = NULL; } } } for (int i = 1; i <= heartNum; ++i) { if (heartsprite[i]) { r4.x = heartsprite[i]->x; r4.y = heartsprite[i]->y; r4.w = heartsprite[i]->width; r4.h = heartsprite[i]->height; int c = collision(r1, r4); if (c) { playSound(sound, 0); score2++; change(score1); preHeartNum--; delete(heartsprite[i]); heartsprite[i] = NULL; } } } Paint(); } void beginpaint() { beginPaint(); clearDevice(); putImageScale(&beginimg, 0, 0, winWidth, winHeight); setTextSize(60); setTextColor(BLUE); setTextBkColor(RGB(64,224,208)); setTextFont("楷體"); paintText(300, 10, "按R開始遊戲"); endPaint(); } void Paint() { beginPaint(); //putImage(&img, 10, 10); clearDevice(); putImageTransparent(&tomimg, tomsprite.x, tomsprite.y, tomsprite.width, tomsprite.height,ACL_Color WHITE); //putImageScale(&jerryimg, jerry_x, jerry_y, jerryWidth, jerryHeight); for (int a = 0; a <= jerryNum; a++) { if (jerrysprite[a]) { putImageTransparent(&jerryimg, jerrysprite[a]->x, jerrysprite[a]->y, jerrysprite[a]->width, jerrysprite[a]->height,ACL_Color WHITE); } } for (int a = 1; a <= dogNum; a++) { if (dogsprite[a]) { putImageTransparent(&dogimg, dogsprite[a]->x, dogsprite[a]->y, dogsprite[a]->width, dogsprite[a]->height,ACL_Color WHITE); } } for (int a = 1; a <= heartNum; a++) { if (heartsprite[a]) { putImageTransparent(&heartimg, heartsprite[a]->x, heartsprite[a]->y, heartsprite[a]->width, heartsprite[a]->height,ACL_Color WHITE); } } char str1[50],str2[50]; sprintf_s(str1, "score:%d", score1); sprintf_s(str2, "HP:%d", score2); setTextSize(30); setTextBkColor(RGB(255, 255, 255)); paintText(10, 10, str1); paintText(200, 10, str2); endPaint(); } void endpaint() { beginPaint(); clearDevice(); putImageScale(&endimg, 0, 0, winWidth, winHeight); setTextBkColor(RGB(255, 255, 255)); paintText(360, 10, "按R重新開始遊戲"); endPaint(); } int collision(rect r1, rect r2) { int c = 1; if (r1.x < r2.x && r1.x + r1.w >r2.x) { if (r1.y > r2.y && r1.y < r2.y + r2.h)return c; if (r1.y <r2.y && r1.y + r1.h >r2.y)return c; } else { if (r1.x > r2.x && r2.x + r2.w > r1.x) { if (r1.y > r2.y && r1.y < r2.y + r2.h)return c; if (r1.y <r2.y && r1.y + r1.h >r2.y)return c; } } c = 0; return c; } void change(int score) { int d = score / 8; tomsprite.height = 100 + d * 20; tomsprite.width = 100 + d * 20; }
以上就是C/C++實現精靈遊戲的範例程式碼的詳細內容,更多關於C++精靈遊戲的資料請關注it145.com其它相關文章!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45