个人此次任务是有关于游戏角色的移动操做,也就是能够让英雄经过键盘动起来。这个东西我一开始是真的一头雾水,不知从何作起,百度还百度不到详细的教程。我是经过百度关键词c++小游戏
找到一些门路的。本人仍是比较喜欢经过视频的学习,我以为这样能更好的学习。c++
本次任务中总的分为三个内容。第一:有关地图的简单描绘。说是简单描绘,其实啥都没有,主要是为了让英雄能够移动。第二个就是关于英雄移动的问题了,这个比较头疼。由于涉及到了未接触过的新领域。经过各路的博客学习,参考别的小游戏例如“贪吃蛇”,“俄罗斯方块” 等小游戏来模仿实现。学习
这是关于地图的初始化。设计
#include "Graphic.h" int Graphic::m_screen_width = SCREEN_WIDTH; int Graphic::m_screen_height = SCREEN_HEIGHT; void Graphic::Create() { initgraph(m_screen_width, m_screen_height); } void Graphic::Destroy() { closegraph(); } int Graphic::GetScreenWidth() { return m_screen_width; } int Graphic::GetScreenHeight() { return m_screen_height; }
下面是关于英雄的描画与移动(我想后面会用图片代替描画)code
#include "hero.h" void hero::SetDir(Dir dir) { m_dir = dir; } void hero::DrawTankBody(int style) { fillrectangle(m_x - 4, m_y - 4, m_x + 4, m_y + 4); if (style == 1) { fillrectangle(m_x - 8, m_y - 6, m_x - 6, m_y + 6); fillrectangle(m_x + 6, m_y - 6, m_x + 8, m_y + 6); } else { fillrectangle(m_x - 6, m_y - 8, m_x + 6, m_y - 6); fillrectangle(m_x - 6, m_y + 6, m_x + 6, m_y + 8); } } void hero::Display() { COLORREF color_save = getfillcolor(); setfillcolor(m_color); switch (m_dir) { case UP: DrawHeroBody(1); line(m_x, m_y, m_x, m_y - 10); break; case DOWN: DrawHeroBody(1); line(m_x, m_y, m_x, m_y + 10); break; case LEFT: DrawHeroBody(0); line(m_x, m_y, m_x - 10, m_y); break; case RIGHT: DrawHeroBody(0); line(m_x, m_y, m_x + 10, m_y); break; default: break; } setfillcolor(color_save); } void hero::Move() { switch (m_dir) { case UP: m_y -= m_step; if (m_y < 0) m_y = Graphic::GetScreenHeight() - 1; break; case DOWN: m_y += m_step; if (m_y > Graphic::GetScreenHeight()) m_y = 1; break; case LEFT: m_x -= m_step; if (m_x < 0) m_x = Graphic::GetScreenWidth() - 1; break; case RIGHT: m_x += m_step; if (m_x > Graphic::GetScreenWidth()) m_x = 1; break; default: break; } }