贪吃蛇_C/C++_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > C/C++ > 贪吃蛇

贪吃蛇

 2010/12/12 14:52:44  xiangjie88  http://xiangjie88.javaeye.com  我要评论(0)
  • 摘要:发一个自己写的贪吃蛇,很烂,就当给以后留下点回忆吧,靠它赢了顿必胜客,哈哈myt.h#include<QtGui/QWidget>#include"ui_myt.h"#include"qpainter.h"#include"QTimer"#include"QPainterPath"classMyT:publicQWidget{Q_OBJECTpublic:MyT(QWidget*parent=0,Qt::WFlagsflags=0);~MyT();enumDirection
  • 标签:贪吃蛇
发一个自己写的贪吃蛇,很烂,就当给以后留下点回忆吧,靠它赢了顿必胜客,哈哈
myt.h
#include <QtGui/QWidget>
#include "ui_myt.h"
#include "qpainter.h"
#include "QTimer"
#include "QPainterPath"

class MyT : public QWidget
{
	Q_OBJECT

public:
	MyT(QWidget *parent = 0, Qt::WFlags flags = 0);
	~MyT();

	enum Direction{
		mLeft=0,
		mRight,
		mUp,
		mDown
	};

	enum Level{
		mEasy=1,
		mMedium,
		mDifficult,
		mBT
	};

private slots:
	void move();
protected:
	void paintEvent(QPaintEvent* e);
	void keyPressEvent(QKeyEvent *e);
private:
	bool outOfBound();
	bool coincide();
	QString getTitle();
	void stop();
	void initWidget(Level=mEasy);
private:
	int mRectNum;
	double mRectWidth;
	double mRectHeight;
	QTimer* mTimer;
	Direction mDirection;
	Level mLevel;
	QRectF mMoveRect;
	QList<QRectF> mPathList;
	QList<QRectF> mRandRectList;
private:
	Ui::MyTClass ui;
};

#endif // MYT_H



myt.cpp
#include "myt.h"
#include "qpainter.h"
#include "QKeyEvent"
#include "QTimer"
#include "QMessageBox"
#include "time.h"

#define MoveRect 5
#define RandomRect 8
static int opportuncity = 3;//give 3 times to try
MyT::MyT(QWidget *parent, Qt::WFlags flags)
	: QWidget(parent, flags)
{
	ui.setupUi(this);
	initWidget(mEasy);
}

MyT::~MyT()	
{

}

void MyT::initWidget(Level level)
{
	mPathList.clear();
	mRandRectList.clear();
	mLevel = level;
	QSize size(300,300);
	this->setFixedSize(size);
	mRectNum = 30;
	mRectWidth = rect().width()/mRectNum;
	mRectHeight = rect().height()/mRectNum;
	int pathNum = 0;
	int randRectNum = 0;
	int interValTime = 500;
	if (level==mBT)
	{
		pathNum+=5;
		randRectNum+=8;
		interValTime=200;
	}

	//move path
	mMoveRect = QRectF(size.width()/10.,size.height()/10.,mRectWidth,mRectHeight);
	pathNum += MoveRect+mLevel;
	for (int i=0;i<pathNum;i++)
	{
		mMoveRect = QRectF(mMoveRect.x()+mRectWidth,mMoveRect.y(),mRectWidth,mRectHeight);
		mPathList.push_front(mMoveRect);
	}

	//random rect
	QRectF randRect;
	long randTimeNum = time(NULL);
	randRectNum += RandomRect+mLevel*2;
	for (int i=0;i<randRectNum;i++)
	{
		int randX = (qrand()+randTimeNum)%30;
		int randY = (qrand()+randTimeNum)%30;
		randRect = QRectF(randX*mRectWidth,randY*mRectHeight,mRectWidth,mRectHeight);
		if (mPathList.contains(randRect)||mRandRectList.contains(randRect))//the random rect can not be first move rect
		{
			i--;
		}else
		{
			mRandRectList.push_back(randRect);
		}
	}

	//connect time
	mTimer = new QTimer(this);
	connect(mTimer,SIGNAL(timeout()),this,SLOT(move()));
	mTimer->start(interValTime/level);//From then on, the move() slot is called every 0.3/level second.
	mDirection = mRight;//default move right
	QString title = getTitle();
	setWindowTitle(title);
	repaint();
}

/*!
	return the window title
*/
QString MyT::getTitle()
{
	QString title;
	switch(mLevel)
	{
	case mEasy:
		title = tr("Easy");
		break;
	case mMedium:
		title = tr("Medium");
		break;
	case mDifficult:
		title = tr("Difficult");
		break;
	case mBT:
		title = tr("BT");
		break;
	}
	QString times = QString(",%1 times to try!").arg(opportuncity);
	if (opportuncity>0)
	{
		title += times;
	}
	return title;
}

/*!
	draw the window
*/
void MyT::paintEvent(QPaintEvent *event)
{
	QPainter painter(this);
	for (int i=0;i<=mRectNum;i++)
	{
		painter.drawLine(mRectWidth*i,0,mRectWidth*i,rect().height());		
	}
	for (int j=0;j<=mRectNum;j++)
	{
		painter.drawLine(0,mRectHeight*j,rect().width(),mRectHeight*j);
	}	
	//draw path
	foreach(QRectF rect,mPathList)
	{
		painter.fillRect(rect,QBrush(Qt::SolidPattern));
	}
	//draw random rect
	foreach(QRectF rect,mRandRectList)
	{
		painter.fillRect(rect,QBrush(Qt::SolidPattern));
	}
}

void MyT::keyPressEvent(QKeyEvent *e)
{
	switch (e->key())
	{
	case Qt::Key_Up:
			if (mDirection!=mDown)
			{
				mDirection = mUp;
			}
			break;
	case Qt::Key_Down:
			if (mDirection!=mUp)
			{
				mDirection = mDown;
			}
			break;
	case Qt::Key_Left:
			if (mDirection!=mRight)
			{
				mDirection = mLeft;
			}
			break;
	case Qt::Key_Right:
			if (mDirection!=mLeft)
			{
				mDirection = mRight;
			}
			break;
	default:
			mDirection = mRight;
			break;
	}
	move();
}

/*!
	add new rect
*/
void MyT::move()
{
	switch(mDirection)
	{
	case mLeft:
		mMoveRect = QRectF(mMoveRect.x()-mRectWidth,mMoveRect.y(),mRectWidth,mRectHeight);
		break;
	case mRight:
		mMoveRect = QRectF(mMoveRect.x()+mRectWidth,mMoveRect.y(),mRectWidth,mRectHeight);
		break;
	case mUp:
		mMoveRect = QRectF(mMoveRect.x(),mMoveRect.y()-mRectHeight,mRectWidth,mRectHeight);
		break;
	case mDown:
		mMoveRect = QRectF(mMoveRect.x(),mMoveRect.y()+mRectHeight,mRectWidth,mRectHeight);
		break;
	default:
		mMoveRect = QRectF(mMoveRect.x()+mRectWidth,mMoveRect.y(),mRectWidth,mRectHeight);
		break;
	}
	//game over
	if (outOfBound()||coincide())
	{
		stop();
		QMessageBox::StandardButton button =QMessageBox::question(this,tr("loser"),tr("Game Over!Do you want to try it again?"),
			QMessageBox::Ok|QMessageBox::Close);
		if (button == QMessageBox::Close)
		{
			exit(0);
		}else
		{
			opportuncity--;
			if (opportuncity>0)
			{
				initWidget(mLevel);
			}else
			{
				opportuncity = 3;
				initWidget(mEasy);
			}			
			return;
		}
	}
	if (!mRandRectList.contains(mMoveRect))
	{
		mPathList.pop_back();
	}else
	{
		mRandRectList.removeOne(mMoveRect);
	}
	mPathList.push_front(QRectF(mMoveRect.x(),mMoveRect.y(),mRectWidth,mRectHeight));
	//win,next version
	if (mRandRectList.isEmpty())
	{
		stop();
		switch(mLevel)
		{
		case mEasy:
			mLevel = mMedium;
			break;
		case mMedium:
			mLevel = mDifficult;
			break;
		case mDifficult:
			mLevel = mBT;
			break;
		case mBT:
			//last winner
			QMessageBox::information(this,tr("winner"),
										   tr("Oh,you are so BT!"),
										   QMessageBox::Ok);
			exit(0);
			break;
		}
		QString title = getTitle();
		QMessageBox::StandardButton button = QMessageBox::question(this,tr("winner"),
																		tr("Nice,you have passed,would you want to try the %1").arg(title),
																		QMessageBox::Ok|QMessageBox::Close);
		if (button = QMessageBox::Ok)
		{
			initWidget(mLevel);
		}else
		{
			exit(0);
		}
	}
	repaint();
}

/*!
	lose or win to next version
*/
void MyT::stop()
{
	mTimer->stop();
	disconnect(mTimer,SIGNAL(timeout()),this,SLOT(move()));
}

/*!
	judge the rect the whether the head meet the body
*/
bool MyT::coincide()
{
	QRectF rect;
	for (int i=1;i<mPathList.size();i++)
	{
		rect = mPathList[i];
		if (rect==mMoveRect)
		{
			return true;
		}
	}
	return false;
}

/*!
	judge whether header is outer of bounder
*/
bool MyT::outOfBound()
{
	if(mMoveRect.x()>=rect().width()||mMoveRect.x()<0||mMoveRect.y()>=rect().height()||mMoveRect.y()<0)
	{
		return true;
	}
	return false;
}




main.cpp
#include <QtGui/QApplication>
#include "myt.h"

int main(int argc, char *argv[])
{
	QApplication a(argc, argv);
	MyT w;
	w.show();
	return a.exec();
}



ui是一个空的QWidget就不发了
  • 相关文章
发表评论
用户名: 匿名