Wednesday, 11 June 2014

Break out game in C++ using QT 5.2.1 - Stage 1 - Header files

In his stage we were required to implement a bouncing ball inside a box. ball needs to bounce of the box edges at constant speed. singleton design pattern used here. this is done in QT. we have header and implementation files for coordinate, ball, and dialog classes, in addition to main method.
here is the coordinate header file...coordinate.h...

#ifndef COORDINATE_H
#define COORDINATE_H
#include <stdlib.h>
class Coordinate
{
public:
Coordinate(unsigned
int yCoordinate,
unsigned int xCoordinate,
unsigned int frameHeight,
unsigned int frameWidth);
int getQtRenderingXCoordinate();
int getQtRenderingYCoordinate();
void changeInXCoordinate(int change);
void changeInYCoordinate(int change);
void setYCoordinateToZero(int offset);
void setXCoordinateToZero(int offset);
unsigned int getFrameHeight();
unsigned int getFrameWidth();
void setFrame(unsigned int height, unsigned int width);
private:
unsigned int m_xCoordinate;
unsigned int m_yCoordinate;
unsigned int m_frameHeight;
unsigned int m_frameWidth;
};
#endif // COORDINATE_H
view raw coordinate.h hosted with ❤ by GitHub
ball.h
#ifndef BALL_H
#define BALL_H
#include "coordinate.h"
#include <QPainter>
class Ball
{
public:
Ball(Coordinate coordinate);
Ball(Coordinate coordinate,
unsigned int radius);
Ball(Coordinate coordinate,
unsigned int radius,
double gravity,
double yVelocity,
double xVelocity);
~Ball(){}
void render(QPainter &painter, unsigned int time);
bool isCollision();
unsigned int getRadius();
Coordinate* getCoordinate();
private:
Ball();
Coordinate m_coordinate;
unsigned int m_radius;
double m_gravity;
double m_xVelocity;
double m_yVelocity;
};
#endif // BALL_H
view raw ball.h hosted with ❤ by GitHub
dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include "ball.h"
#include <QTimer>
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
static Dialog* Instance(Ball ball, bool bgChange, bool frameChange);
static Dialog* getInstance();
QList<QString> getColors();
bool getbgChange();
~Dialog();
public slots:
void nextFrame();
protected:
explicit Dialog(Ball &ball, bool bgChange, bool frameChange,QWidget *parent = 0);
void closeEvent(QCloseEvent *);
void resizeEvent(QResizeEvent *event);
void paintEvent(QPaintEvent *event);
private:
static Dialog* m_this;
Ui::Dialog *ui;
Ball m_ball;
int m_counter;
bool m_bgChange, m_frameChange;
QList<QString> m_colors;
};
#endif // DIALOG_H
view raw dialog.h hosted with ❤ by GitHub

sample config file looks like this

Frame= 500 450
Radius= 23
Velocity= 12 17
Position= -60 2873
bgChange= true
frameChange = true
view raw setup.config hosted with ❤ by GitHub

now some explanation. coordinate class is for translating coordinate in Qt in a way that we are comfortable of. it means Qt consider top left corner (0,0), and while you're moving to right or down it increases the x and y coordinate. personally, I think we don't need this..well...here it is... ball class has some functions needs proper implementation beside normal getter/setter methods. render and isCollision functions. isCollision function checks if the ball hit any edges of the box, and render function handle the ball movements. dialog class is the core of this to put them all together. we used Singleton Design Pattern here to make sure only one instance of dialog exists at any time. I hope I explained it good enough, as I'm still learning. I'll post the implementation files and main method in near future, meanwhile stay hungry, keep coding

Some other information:

  • this is not my code. mine was similar but it didn't changed colour when ball hits the box
  • again this is implemented in Qt
  • the actual website that have the tutorial on basic set up to start on this assignment can be found Here

2 comments:

  1. Hi may I ask that you still keep OO quiz last year?

    Thanks

    ReplyDelete
  2. Hi, unfortunately not. we couldn't take the quiz paper out. quiz is easy, just normal stuff. you should be worried about assignments and final exam.and since the lecturer is changed this semester...last year lecturer was real good..., good luck

    ReplyDelete