scropioczn
本帖最后由

本帖最后由 scropioczn 于 2013-7-6 14:17 编辑

7.箭头

根据设想,四个箭头将环绕所选中的球,上下两个箭头负责切换球的颜色,左右则是切换所选中的球。最左,最右,最上,或最下都可以循环。

并且,为了美观:

1. 按键按下去后,相应的箭头将会改变颜色来显示激活。

2.四个球的位置将会按照箭头的位置进行动态调整。平时球与球之间的距离较小,当该球为所选中的球时,左右间隔将拉大以放置左右箭头。(将会在Input里面详细定义)

创建Arrow.h与Arrow.cpp

Arrow.h

[mw_shl_code=cpp,true]/*

* Arrow.h

*

* Created on: 2013-06-28

* Author: scropioczn

* Description: This is the header file of the Arrow class

* Version:

*/

#ifndef ARROW_H_

#define ARROW_H_

#include "SDL/SDL.h"

#include "SDL/SDL_image.h"

#include "SDL/SDL_ttf.h"

class Arrow {

private:

//Four inactive arrows

SDL_Surface *up_arrow;

SDL_Surface *down_arrow;

SDL_Surface *left_arrow;

SDL_Surface *right_arrow;

//Four active arrows

SDL_Surface *up_arrow_a;

SDL_Surface *down_arrow_a;

SDL_Surface *left_arrow_a;

SDL_Surface *right_arrow_a;

//activity

bool is_active_up;

bool is_active_down;

bool is_active_left;

bool is_active_right;

//arrow offsets

int arrow_width;

int arrow_height;

public:

//Constructor and destructor

Arrow();

~Arrow();

//----------Gets-----------//

bool isActiveUp();

bool isActiveDown();

bool isActiveLeft();

bool isActiveRight();

int getArrowWidth();

int getArrowHeight();

//----------Sets-----------//

void setActiveUp(bool set);

void setActiveDown(bool set);

void setActiveLeft(bool set);

void setActiveRight(bool set);

//render arrows

void render(int x, int y);

};

#endif /* ARROW_H_ */

[/mw_shl_code]

class中只定义了一个render(), 两个参数即为坐标。四个箭头将会作为一个整体进行显示。

Arrow.cpp

[mw_shl_code=cpp,true]/*

* Arrow.cpp

*

* Created on: 2013-06-28

* Author: scropioczn

* Description: This is the source file containing definitions of Arrow class.

* Version:

*/

//SDL libraries

#include "SDL/SDL.h"

#include "SDL/SDL_image.h"

#include "SDL/SDL_ttf.h"

//C/C++ libraries

#include *本站禁止HTML标签噢*

//Own header file

#include "Arrow.h"

//Linked header files

#include "Utility.h"

#include "Ball.h"

//external objects

extern Ball *ball;

//external variables

extern SDL_Surface *screen;

//Definitions

Arrow::Arrow() {

up_arrow = NULL;

down_arrow = NULL;

left_arrow = NULL;

right_arrow = NULL;

up_arrow_a = NULL;

down_arrow_a = NULL;

left_arrow_a = NULL;

right_arrow_a = NULL;

is_active_up = false;

is_active_down = false;

is_active_left = false;

is_active_right = false;

arrow_width = 0;

arrow_height = 0;

}

Arrow::~Arrow() {

SDL_FreeSurface(up_arrow);

SDL_FreeSurface(down_arrow);

SDL_FreeSurface(left_arrow);

SDL_FreeSurface(right_arrow);

SDL_FreeSurface(up_arrow_a);

SDL_FreeSurface(down_arrow_a);

SDL_FreeSurface(left_arrow_a);

SDL_FreeSurface(right_arrow_a);

}

int Arrow::loadArrow() {

up_arrow = loadImage("data/arrow/arrow_up.png");

down_arrow = loadImage("data/arrow/arrow_down.png");

left_arrow = loadImage("data/arrow/arrow_left.png");

right_arrow = loadImage("data/arrow/arrow_right.png");

up_arrow_a = loadImage("data/arrow/arrow_up_a.png");

down_arrow_a = loadImage("data/arrow/arrow_down_a.png");

left_arrow_a = loadImage("data/arrow/arrow_left_a.png");

right_arrow_a = loadImage("data/arrow/arrow_right_a.png");

if ((up_arrow == NULL) || (down_arrow == NULL) || (left_arrow == NULL)

|| (right_arrow == NULL)) {

return -1;

}

if ((up_arrow_a == NULL) || (down_arrow_a == NULL) || (left_arrow_a == NULL)

|| (right_arrow_a == NULL)) {

return -1;

}

setColorKey(up_arrow, 0xFF, 0xFF, 0xFF);

setColorKey(down_arrow, 0xFF, 0xFF, 0xFF);

setColorKey(left_arrow, 0xFF, 0xFF, 0xFF);

setColorKey(right_arrow, 0xFF, 0xFF, 0xFF);

setColorKey(up_arrow_a, 0xFF, 0xFF, 0xFF);

setColorKey(down_arrow_a, 0xFF, 0xFF, 0xFF);

setColorKey(left_arrow_a, 0xFF, 0xFF, 0xFF);

setColorKey(right_arrow_a, 0xFF, 0xFF, 0xFF);

arrow_width = up_arrow->w;

arrow_height = up_arrow->h;

return 0;

}

bool Arrow::isActiveUp() {

if (is_active_up == true) {

return true;

} else {

return false;

}

}

bool Arrow::isActiveDown() {

if (is_active_down == true) {

return true;

} else {

return false;

}

}

bool Arrow::isActiveLeft() {

if (is_active_left == true) {

return true;

} else {

return false;

}

}

bool Arrow::isActiveRight() {

if (is_active_right == true) {

return true;

} else {

return false;

}

}

int Arrow::getArrowHeight() {

return arrow_height;

}

int Arrow::getArrowWidth() {

return arrow_width;

}

void Arrow::setActiveUp(bool set) {

is_active_up = set;

}

void Arrow::setActiveDown(bool set) {

is_active_down = set;

}

void Arrow::setActiveLeft(bool set) {

is_active_left = set;

}

void Arrow::setActiveRight(bool set) {

is_active_right = set;

}

void Arrow::render(int x, int y) {

int up_x = 0, up_y = 0;

int down_x = 0, down_y = 0;

int left_x = 0, left_y = 0;

int right_x = 0, right_y = 0;

//up

up_x = x + ((ball->getBallWidth() / 2) - (arrow_width / 2));

up_y = y - arrow_height;

//down

down_x = x + ((ball->getBallWidth() / 2) - (arrow_width / 2));

down_y = y + ball->getBallHeight();

//left

left_x = x - arrow_width;

left_y = y + ((ball->getBallHeight() / 2) - (arrow_height / 2));

//right

right_x = x + ball->getBallWidth();

right_y = y + ((ball->getBallHeight() / 2) - (arrow_height / 2));

if (is_active_up == true) {

applySurface(up_x, up_y, up_arrow_a, screen, NULL);

} else if (is_active_up == false) {

applySurface(up_x, up_y, up_arrow, screen, NULL);

}

if (is_active_down == true) {

applySurface(down_x, down_y, down_arrow_a, screen, NULL);

} else if (is_active_down == false) {

applySurface(down_x, down_y, down_arrow, screen, NULL);

}

if (is_active_left == true) {

applySurface(left_x, left_y, left_arrow_a, screen, NULL);

} else if (is_active_left == false) {

applySurface(left_x, left_y, left_arrow, screen, NULL);

}

if (is_active_right == true) {

applySurface(right_x, right_y, right_arrow_a, screen, NULL);

} else if (is_active_right == false) {

applySurface(right_x, right_y, right_arrow, screen, NULL);

}

}[/mw_shl_code]

这里来个小Quiz:请问根据定义,render()里面的两个坐标参数所对应的是球的哪里?

答案在一发的最下,回复答案后可见。