关于算法:ELEX-4618面向对象设计

56次阅读

共计 4722 个字符,预计需要花费 12 分钟才能阅读完成。

ELEX 4618: Object Oriented Design Lab 6
Craig Hennessey 12/12/2021 1
Lab 6 – Asteroids
1 Introduction
In this lab, inheritance will be used to implement a simplified version of the game Asteroids.
2 Design Problem
2.1 Asteroids
The simplified game of Asteroids will have a number of game elements including the player ship, the
asteroids and missiles fired by the ship to destroy the asteroids. All objects will inherit from the same game
object and share many of the same characteristics. The ship position (X/Y) will be controlled by the
joystick, the same as the Etch-A-Sketch game. Asteroids will randomly appear at the top of the screen and
drift at a random angle and speed downwards. By pushing (and/or holding) a button on the controller, the
ship can fire missiles upwards towards the asteroids.
The player begins the game with 10 lives. If an asteroid collides with the ship, both the player and asteroid
lose a life (life = life – 1). If an asteroid or missile collides with the side of the display, they both lose a life.
If an asteroid and missile collide, both lose a life and the player score increases by 10 points.
The CAsteroidGame class will inherit from CBase4618 for the update/draw/run methods and the canvas
image. The class should contain a vector for asteroids and one for missiles as well as the other game related
member variables. In update, loop over the vector of asteroids and missiles to detect collisions and
determine remaining lives, then loop over again and remove any objects with lives <= 0. Finally, loop over
the remaining objects to draw.
2.1.1 CGameObject
All objects will be 2D circles, having a radius, position, and velocity. Missiles will be small and fast, the
ship will be a medium size and positioned according to the joystick, and asteroids will be a random size
with random speed and direction. A helpful technique for random sizes is to get a random number between
0 and 1 ((float)rand() / (float)RAND_MAX) which can then be scaled and offset to get any range of
numbers desired.
class CGameObject {
protected:
Point2f _position;
Point2f _velocity;
int _radius;
int _lives;
public:
void move();
bool collide(CGameObject &obj);
bool collide_wall(Size board);
void hit();
int get_lives() { return _lives;}
void set_lives(int lives) {_lives = lives;}
void set_pos(Point2f pos) {_position = pos;}
Point2f get_pos() { return _position;}
void draw(Mat &im);
};

ELEX 4618: Object Oriented Design Lab 6
Craig Hennessey 12/12/2021 2
2.1.2 CShip
The ship inherits from the CGameObject and require NO additional members other than the constructor.
The ships unique attributes, such as size (radius) and number of lives can be initialized in the constructor.
The position of the ship is controlled by the joystick (using the set_pos) method.
2.1.3 CAsteroid
The asteroids inherits from the CGameObject and require NO additional members other than the
constructor. As with the ship, the constructor can be used to initialize the random size, initial position at the
top of the screen and initial velocity.
2.1.4 CMissile
The missiles inherits from the CGameObject and require NO additional members other than the constructor.
The constructor can be used to initialize the size, while the position and velocity is set when the player
presses the fire button.
2.2 Additional Functionality (required)
Complete the following additional functionality enhancements after completing the tasks above.

  1. Add a digital input method connected to a push button to reset the game
  2. Pre Lab Work (8)
    Prior to coming to the lab, complete the following tasks:
    A) Setup the project with the CAsteroidGame game as well as the CGameObject, CAsteroid,
    CShip, CMissile and any the other classes needed for the game. Complete the class definition (H file) for
    all the classes, as well as all the member functions as stubs (empty functions).
    B) In the CAsteroidGame game update method, read two pushbutton inputs (debounced) for the missile
    fire and reset buttons, set a flag if pushed and print the event to the screen with cout.
    C) Also in the update method, read the joystick position as a percentage and set the ship position on the
    display accordingly and print the X/Y pixel position on the screen or draw the ship.
    D) In the game run function, randomly create an asteroid every few seconds. Initialize an asteroid object
    and push it into the asteroid vector as follows:
    CAsteroid astro();
    _asteroid_list.push_back(astro);
    E) Implement the move method of the CGameObject class. This is the exact same as the Pong ball.
    Measure the time since the last time the move method was called and set the pos = pos + ?t ? velocity.
    F) In the CAsteroidGame game draw method, draw the game objects; the ship, the vector of asteroids and
    the vector of missiles by calling the draw method for each game object. Print in text across the top of the
    screen the player score, number of lives of the ship, number of missiles active.
    G) Document the header files with Doxygen and generate the HTML documentation.
    F) Setup the Raspberry Pi with the latest version of Raspberry Pi OS. Copy Lab 2 to a folder on the Pi
    desktop. Setup a MAKEFILE to compile the .H and .CPP files. Show that the program runs on the Pi.
    ELEX 4618: Object Oriented Design Lab 6
    Craig Hennessey 12/12/2021 3
  3. Lab Completion Demo (6)
    Implement the Asteroids game and demonstrate to the lab instructor. Remaining work may include the
    collision detection of asteroids and walls, asteroids and ships, and asteroids and missiles. Deleting game
    objects on collisions, creation and movement of missiles, game scoring, and reset of the game.
    Copy your CPP and H files into the lab hand in folder on D2L.
正文完
 0