Merge pull request #669 from aismann/dev

Improve Example: SimpleSnake
This commit is contained in:
一线灵 2022-06-25 00:06:06 +08:00 committed by GitHub
commit f858689dd0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 63 additions and 40 deletions

View File

@ -70,9 +70,9 @@ bool AppDelegate::applicationDidFinishLaunching()
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || \ #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || \
(CC_TARGET_PLATFORM == CC_PLATFORM_LINUX) (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX)
glview = GLViewImpl::createWithRect( glview = GLViewImpl::createWithRect(
"HelloCpp", cocos2d::Rect(0, 0, designResolutionSize.width, designResolutionSize.height)); "Example: Simple Snake", cocos2d::Rect(0, 0, designResolutionSize.width, designResolutionSize.height));
#else #else
glview = GLViewImpl::create("HelloCpp"); glview = GLViewImpl::create("Example: Simple Snake");
#endif #endif
director->setOpenGLView(glview); director->setOpenGLView(glview);
} }

View File

@ -90,10 +90,10 @@ bool SimpleSnake::init()
// add a label shows "Hello World" // add a label shows "Hello World"
// create and initialize a label // create and initialize a label
auto label = Label::createWithTTF("Example - Simple Snake", "fonts/Marker Felt.ttf", 24); auto label = Label::createWithTTF("Example: Simple Snake", "fonts/arial.ttf", 24);
if (label == nullptr) if (label == nullptr)
{ {
problemLoading("'fonts/Marker Felt.ttf'"); problemLoading("'fonts/arial.ttf'");
} }
else else
{ {
@ -105,11 +105,11 @@ bool SimpleSnake::init()
this->addChild(label, 1); this->addChild(label, 1);
} }
// add "HelloWorld" splash screen" // add "ADXE" splash screen"
auto sprite = Sprite::create("ADXE.png"sv); auto sprite = Sprite::create("ADXE_white.png"sv);
if (sprite == nullptr) if (sprite == nullptr)
{ {
problemLoading("'ADXE.png'"); problemLoading("'ADXE_white.png'");
} }
else else
{ {
@ -138,15 +138,15 @@ bool SimpleSnake::init()
char buffer[1024]; char buffer[1024];
for (int i = 0; i < snakeBodies; i++) for (int i = 0; i < snakeBodies+1; i++)
{ {
myScore[i] = 0.0; myScore[i] = 0.0;
sprintf(buffer, "Level %i Time : %f", i + 1, myScore[i]); sprintf(buffer, "Level %i: %f", i + 1, myScore[i]);
myScoreLabel[i] = Label::createWithTTF(std::string(buffer), "fonts/Marker Felt.ttf", 24); myScoreLabel[i] = Label::createWithTTF(std::string(buffer), "fonts/arial.ttf", 24);
myScoreLabel[i]->setPosition( myScoreLabel[i]->setPosition(Vec2(130, origin.y + visibleSize.height - label->getContentSize().height - i * 30));
Vec2(130, origin.y + visibleSize.height - label->getContentSize().height - i * 30));
this->addChild(myScoreLabel[i], 1); this->addChild(myScoreLabel[i], 1);
} }
myScoreLabel[snakeBodies]->setString("Eat as fast as you can!");
scheduleUpdate(); scheduleUpdate();
return true; return true;
@ -157,16 +157,20 @@ void SimpleSnake::onKeyPressed(EventKeyboard::KeyCode keyCode, Event* event)
switch (keyCode) switch (keyCode)
{ {
case cocos2d::EventKeyboard::KeyCode::KEY_LEFT_ARROW: case cocos2d::EventKeyboard::KeyCode::KEY_LEFT_ARROW:
if (dir != 2) dir = 1; if (dir != 2)
dir = 1;
break; break;
case cocos2d::EventKeyboard::KeyCode::KEY_RIGHT_ARROW: case cocos2d::EventKeyboard::KeyCode::KEY_RIGHT_ARROW:
if (dir != 1) dir = 2; if (dir != 1)
dir = 2;
break; break;
case cocos2d::EventKeyboard::KeyCode::KEY_UP_ARROW: case cocos2d::EventKeyboard::KeyCode::KEY_UP_ARROW:
if (dir != 3) dir = 0; if (dir != 3)
dir = 0;
break; break;
case cocos2d::EventKeyboard::KeyCode::KEY_DOWN_ARROW: case cocos2d::EventKeyboard::KeyCode::KEY_DOWN_ARROW:
if (dir != 0) dir = 3; if (dir != 0)
dir = 3;
break; break;
default: default:
break; break;
@ -200,19 +204,23 @@ void SimpleSnake::update(float delta)
{ {
case 0: case 0:
s[0].y++; s[0].y++;
if (s[0].y >= M) s[0].y = 0; if (s[0].y >= M)
s[0].y = 0;
break; break;
case 1: case 1:
s[0].x--; s[0].x--;
if (s[0].x < 0) s[0].x = N; if (s[0].x < 0)
s[0].x = N - 1;
break; break;
case 2: case 2:
s[0].x++; s[0].x++;
if (s[0].x >= N) s[0].x = 0; if (s[0].x >= N)
s[0].x = 0;
break; break;
case 3: case 3:
s[0].y--; s[0].y--;
if (s[0].y < 0) s[0].y = M; if (s[0].y < 0)
s[0].y = M - 1;
break; break;
default: default:
break; break;
@ -226,6 +234,7 @@ void SimpleSnake::update(float delta)
posOk = true; posOk = true;
f.x = rand() % N; f.x = rand() % N;
f.y = rand() % M; f.y = rand() % M;
for (int i = 0; i < num; i++) for (int i = 0; i < num; i++)
{ {
if (f.x == s[i].x && f.y == s[i].y) if (f.x == s[i].x && f.y == s[i].y)
@ -236,19 +245,14 @@ void SimpleSnake::update(float delta)
} }
} while (!posOk); } while (!posOk);
// score
char buffer[124];
myScore[num - StartBodies] = endLevelTime;
sprintf(buffer, "Level %i Time : %f", num - StartBodies + 1, myScore[num - StartBodies]);
myScoreLabel[num - StartBodies]->setString(buffer);
if (num >= (snakeBodies + StartBodies - 1))
{
myScoreLabel[num - StartBodies]->setString("FINISH");
finish = true;
}
// next level // next level
num++; num++;
if (num >= (snakeBodies + StartBodies - 1))
{
myScoreLabel[snakeBodies]->setString("FINISH");
finish = true;
drawAll(finish);
}
level -= 0.01; level -= 0.01;
startLevelTime = 0.0; startLevelTime = 0.0;
endLevelTime = startLevelTime; endLevelTime = startLevelTime;
@ -256,15 +260,33 @@ void SimpleSnake::update(float delta)
runTime = 0; runTime = 0;
} }
//////// draw /////// if (!finish)
{
drawAll(finish);
}
}
void SimpleSnake::drawAll(bool finish)
{
// score
char buffer[124];
myScore[num - StartBodies] = endLevelTime;
sprintf(buffer, "Level %i: %f", num - StartBodies + 1, myScore[num - StartBodies]);
myScoreLabel[num - StartBodies]->setString(buffer);
mydraw->clear(); mydraw->clear();
// draw snake body
// Draw food for (int i = 1; i < num; i++)
mydraw->drawDot(Vec2(size / 2 + f.x * size, size / 2 + f.y * size), size / 2, Color4B::GREEN);
// Draw snake
for (int i = 0; i < num; i++)
{ {
mydraw->drawDot(Vec2(size / 2 + s[i].x * size, size / 2 + s[i].y * size), size / 2, Color4B::BLUE); mydraw->drawDot(Vec2(size / 2 + s[i].x * size, size / 2 + s[i].y * size), size / 2, Color4B::BLUE);
} }
//draw snake head
mydraw->drawDot(Vec2(size / 2 + s[0].x * size, size / 2 + s[0].y * size), size / 2, Color4B::MAGENTA);
if (!finish)
{
// draw food
mydraw->drawDot(Vec2(size / 2 + f.x * size, size / 2 + f.y * size), size / 2, Color4B::GREEN);
}
} }

View File

@ -21,7 +21,7 @@
#include "cocos2d.h" #include "cocos2d.h"
const int snakeBodies = 10; const int snakeBodies = 9;
const int StartBodies = 4; const int StartBodies = 4;
class SimpleSnake : public cocos2d::Scene class SimpleSnake : public cocos2d::Scene
@ -32,6 +32,7 @@ public:
// a selector callback // a selector callback
void menuCloseCallback(Ref* sender); void menuCloseCallback(Ref* sender);
virtual void update(float delta) override; virtual void update(float delta) override;
void drawAll(bool finish);
void onKeyPressed(cocos2d::EventKeyboard::KeyCode keyCode, cocos2d::Event* event); void onKeyPressed(cocos2d::EventKeyboard::KeyCode keyCode, cocos2d::Event* event);
cocos2d::Vec2 offset; cocos2d::Vec2 offset;
@ -49,7 +50,7 @@ public:
float endLevelTime = 0.0; float endLevelTime = 0.0;
int dir, num = StartBodies; int dir, num = StartBodies;
cocos2d::Label* myScoreLabel[snakeBodies + StartBodies + 1]; cocos2d::Label* myScoreLabel[snakeBodies + 1];
}; };
#endif // __SIMPLE_SNAKE_SCENE_H__ #endif // __SIMPLE_SNAKE_SCENE_H__

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.0 KiB