C++ 谷歌小恐龙#1 游戏制作实录
生活随笔
收集整理的這篇文章主要介紹了
C++ 谷歌小恐龙#1 游戏制作实录
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
版本號:1.0
嗨,我是一塊鈮金屬,新年快樂呀!
之前不是說啃了EGE庫嘛,所以我打算做一個項目練練手。
做什么呢……?谷歌小恐龍!
谷歌小恐龍就是一款谷歌瀏覽器內置的小游戲……多的我就不說了,在谷歌瀏覽器輸入chrome://dino/就可以玩啦!
在我的不懈努力下,終于把……小恐龍的動畫搞了出來。
(78行代碼啊!就把小恐龍的動畫搞了出來……)
接下來,就是要把地圖弄出來。
我們可以設置一個地圖類型,類型里有一個PIMAGE圖片,int類型的x坐標和不變的y坐標(極有可能變😅),滾動的效果就像這樣:
代碼如下:
#include <graphics.h> #include <bits/stdc++.h> #include <stdio.h>using namespace std;void zoomImage(PIMAGE& pimg, int width, int height) {if (pimg == NULL || (width == getwidth(pimg) && height == getheight(pimg))) {return;}PIMAGE temp = newimage(width, height);putimage(temp, 0, 0, width, height, pimg, 0, 0, getwidth(pimg), getheight(pimg));delimage(pimg);pimg = temp; }// use picture PIMAGE bird[2]; PIMAGE cactus[5]; PIMAGE dino[6]; PIMAGE ground[3]; // ground struct class rollGd {public:PIMAGE costunm = newimage();int x;int y = 325; // it is never change };char fileName[30]; rollGd grnd[2]; int speed = 10;void init() {// for (...) to get picture/*// birdfor (int i = 0; i < 2; i++) {}// cactusfor (int i = 0; i < 5; i++) {}*/// dino // for (int i = 0; i < 6; i++) { // // }for (int i = 0; i < 2; i++) {dino[i] = newimage();sprintf(fileName, "resource\\image\\dino%d.png", i + 1);getimage(dino[i], fileName);zoomImage(dino[i], 180, 180);}// groundfor (int i = 0; i < 3; i++) {ground[i] = newimage();sprintf(fileName, "resource\\image\\ground%d.png", i + 1);getimage(ground[i], fileName);zoomImage(ground[i], 810, 45);}// real groundfor (int i = 0; i < 2; i++) {grnd[i].costunm = ground[rand() % 3];if (i == 0) {grnd[i].x = 0;} else {grnd[i].x = 810;}} }void walk() {for (int i = 0; i < 2; i++) {for (int j = 0; j < 10; j++) {for (int k = 0; k < 2; k++) {grnd[k].x -= 7;if (grnd[k].x < -810) {grnd[k].costunm = ground[rand() % 3];grnd[k].x = 810;}putimage(grnd[k].x, grnd[k].y, grnd[k].costunm);}putimage_withalpha(NULL, dino[i], -35, 200); // drawSleep(2);bar(-35, 200, -35 + 180, 200 + 180); // cover}} }void release() {for (int i = 0; i < 2; i++) {delimage(dino[i]);}for (int i = 0; i < 3; i++) {delimage(ground[i]);} }int main() {// initsrand(time(0));initgraph(810, 480, 0); // screensetcaption("Google Dino War"); // captionsetbkcolor(EGERGB(0xFF, 0xFF, 0xFF)); // background colorsetfillcolor(EGERGB(0xFF, 0xFF, 0xFF)); // init fill colorinit(); // init picturewhile (true) {// quitif (GetKeyState('Q') < 0) {break;}walk(); // run animation}release(); // delete, drop memoryclosegraph(); }(第一次寫注釋,原理都在注釋里了)
至于怎么滾動地圖的原理嘛……自己去搜。
(要是看不懂我又可以水一篇文啦嘿嘿嘿)
滾動地圖我幾乎做了一天,所以你該做什么?點贊!關注!收藏!三聯!!!
現在的游戲僅僅是一個雛形,下期,我們將加入障礙和分數,讓整個游戲更……
好玩?
總結
以上是生活随笔為你收集整理的C++ 谷歌小恐龙#1 游戏制作实录的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 软件工程毕业设计课题(74)微信小程序毕
- 下一篇: h3cr4900g3安装系统_H3C案例