Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
321 changes: 321 additions & 0 deletions Makefile

Large diffs are not rendered by default.

1,274 changes: 1,274 additions & 0 deletions Makefile.Debug

Large diffs are not rendered by default.

1,262 changes: 1,262 additions & 0 deletions Makefile.Release

Large diffs are not rendered by default.

34 changes: 32 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,32 @@
# DLLTWZ_GUI
This is a team project for the real time class.
# iceHocky

桌面冰球游戏
=



##功能介绍
----------
**桌面**:
: 桌面坐标系的原点设置在正中心,x轴方向向右,y轴方向向下,四周的Frame为边框,冰球碰撞后回反弹(碰撞后的速度关于碰撞面法向对称),上下两个hole为门洞。

**冰球**:
: 具有参数speed和angle,分别表示冰球运动的速度大小和方向(长度单位为像素,时间单位为秒)。

**显示部件**:
: 显示冰球、击球器的位置速度信息等。

**待添加功能**:
: 更改桌面、冰球等尺寸参数
添加开始、结束界面;
增加计分功能;
击球器与冰球碰撞后的速度计算

----------
动画示意如下:

![](http://images2015.cnblogs.com/blog/1070222/201612/1070222-20161222172931276-1285608522.gif)

(桌面录像gif动画由软件[screenToGif](http://pan.baidu.com/s/1c2LVOPi)制作)

----------
135 changes: 135 additions & 0 deletions ball.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>
#include <QPainter>
#include <QStyleOption>

#include "ball.h"
#include "graphwidget.h"

#include <QtCore/qmath.h>
#include <qDebug>

Ball::Ball(GraphWidget *graphWidget) : graph(graphWidget)
{
setFlag(ItemIsMovable);
setCacheMode(DeviceCoordinateCache);
setZValue(1);
//运动参数初始化
speed = 0;//运动速度为100像素每秒;
angle = -PI/6.0;
Reset();//重置球的位置和方向
ratio = 2;//图形比例
Radius = 20*ratio;
}
bool Ball::advance()
{
qreal xvel = 0;
qreal yvel = 0;

xvel =speed*1.0/FRECUENCY *qCos(angle);
yvel = speed*1.0/FRECUENCY *qSin(angle);

newPos = pos() + QPointF(xvel, yvel);

if (newPos == pos())
return false;

setPos(newPos);
return true;

}

bool Ball::collisionCalcualte(collisionType direction)//计算碰撞后的速度和方向
{
if(direction ==Up) //如果碰到上边的Frame
{
qDebug()<<"collision to Up Frame!"<<endl;
angle = 0-angle;
}
if(direction ==Down) //如果碰到右边的Frame
{
qDebug()<<"collision to Down frame!"<<endl;
angle = 0-angle;
}
if(direction ==Left) //如果碰到右边的Frame
{
qDebug()<<"collision to Left Frame!"<<endl;
angle = PI-angle;
}
if(direction ==Right) //如果碰到右边的Frame
{
qDebug()<<"collision to Right Frame!"<<endl;
angle = PI-angle;
}
if(direction ==Handle) //如果碰到Handle,【】重点:叠加上Handle额外导致的速度;
{
qDebug()<<"collision to Handle now !"<<endl;
QPointF point = graph->HandlePosition;
qDebug()<<point;
point-=graph->ballPosition;
qreal collisionAngle = qAtan2(point.y(),point.x() );
angle = 2.0*(collisionAngle+PI/2.0)-angle; //angle 是由ball指向Handle
// //将嵌入到Handle的 ball退出来
// angle = angle+ PI; //angle 是由Handle指向ball

}


return true;
}

QRectF Ball::boundingRect() const
{
qreal adjust = 1;
return QRectF(-10*ratio + adjust*ratio, -10*ratio + adjust*ratio,
20*ratio - 2*adjust*ratio, 20*ratio - 2*adjust*ratio);
}

QPainterPath Ball::shape() const
{
QPainterPath path;
path.addEllipse(-10*ratio, -10*ratio, 20*ratio, 20*ratio);
return path;

}

void Ball::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
{

painter->setPen(Qt::NoPen);
painter->setBrush(Qt::darkGray);
// painter->drawEllipse(-9*ratio, -9*ratio, 20*ratio, 20*ratio);//在后面造成阴影效果

QRadialGradient gradient(-3*ratio, -3*ratio, 10*ratio);
if (option->state & QStyle::State_Sunken) {
gradient.setCenter(3*ratio, 3*ratio);
gradient.setFocalPoint(3*ratio, 3*ratio);
gradient.setColorAt(1, QColor(Qt::white).light(120));
gradient.setColorAt(0, QColor(Qt::gray).light(120));
} else {
gradient.setColorAt(0, Qt::white);
gradient.setColorAt(1, Qt::gray);
}
painter->setBrush(gradient);
painter->setPen(QPen(Qt::white, 0));
painter->drawEllipse(-10*ratio, -10*ratio, 20*ratio, 20*ratio);

// painter->drawEllipse(-10*ratio*0.8, -10*ratio*0.8, 20*ratio*0.8, 20*ratio*0.8);//画圆
//painter->drawRect(QRectF());
}
void Ball::setSpeed(qreal s)
{
speed = s;

}
void Ball::setAngle(qreal a)
{
angle = a;
}
//重置球的位置和方向
void Ball::Reset()
{
setPos(0,-400);
int rand = qrand()%100;
angle =0.1*PI+ 0.8*PI*rand/100.0;
}
46 changes: 46 additions & 0 deletions ball.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#ifndef BALL_H
#define BALL_H

#include <QGraphicsItem>
#include <QtGlobal>
//声明类

class GraphWidget;

typedef enum{Up=1,Down,Left,Right,Handle} collisionType;//用于表示碰撞方向类型(是否可以在类里定义)
#define PI 3.141592653
#define FRECUENCY 100

class Ball:public QGraphicsItem
{

public:
Ball(GraphWidget *graphWidget);

QRectF boundingRect() const;
QPainterPath shape() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);

bool advance(); //计算一定时间周期后的位置并重新setpos
bool collisionCalcualte(collisionType);//计算碰撞后的速度和方向;
void setSpeed(qreal);
void setAngle(qreal);
void Reset();

protected:

public: //只有声明为public后 其他的类才能访问【】或者添加额外的接口函数实现
GraphWidget *graph;
qreal Radius;//需要被另外的类访问就要声明为public类型
qreal angle;//运动的速度和方向
qreal speed;
QPointF newPos; //新的位置
qreal ratio ;//图形的比例大小


// typedef enum{up,down,left,right} Type;//用于表示碰撞类型


};

#endif // BALL_H
30 changes: 30 additions & 0 deletions bingqiuTest.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#-------------------------------------------------
#
# Project created by QtCreator 2016-12-20T12:51:08
#
#-------------------------------------------------

QT += core widgets gui

QT -= gui

TARGET = bingqiuTest
CONFIG += console
CONFIG -= app_bundle

TEMPLATE = app


SOURCES += main.cpp \
ball.cpp \
graphwidget.cpp \
frame.cpp \
userhandle.cpp \
gamewidget.cpp

HEADERS += \
ball.h \
graphwidget.h \
frame.h \
userhandle.h \
gamewidget.h
Loading