#include "gamemainwindow.h"
#include <QSqlQuery>
int gamemainwindow::m_setDiretion=4;
gamemainwindow::gamemainwindow(QWidget *parent) : QWidget(parent)
{
qsrand(time(NULL)); //随机数的食物
Score = 0; //设置分数
Difficulty = 1; //设置等级
resize(891,510);
setWindowTitle("贪吃蛇");
QPalette palette;
palette.setBrush(QPalette::Background,QBrush(QPixmap(":/resource/gamewindowback.jpg").scaled(this->size())));
this->setPalette(palette);
upPush =new QPushButton(this);
downPush=new QPushButton(this);
leftPush=new QPushButton(this);
rightPush=new QPushButton(this);
StartPush=new QPushButton(this);
ExitPush=new QPushButton(this);
buttonGroup=new QButtonGroup(this);
buttonGroup->addButton(upPush,1);
buttonGroup->addButton(downPush,2);
buttonGroup->addButton(leftPush,3);
buttonGroup->addButton(rightPush,4);
connect(buttonGroup,SIGNAL(buttonClicked(int)),this,SLOT(M_setDiretion(int)));//接收方向按键信号
upPush->setIconSize(QSize(45,90));
upPush->setGeometry(750,160,45,90);
upPush->setIcon(QIcon(":/resource/up.png"));
upPush->setFlat(1);
downPush->setIconSize(QSize(45,90));
downPush->setGeometry(750,260,45,90);
downPush->setIcon(QIcon(":/resource/down.png"));
downPush->setFlat(1);
leftPush->setIconSize(QSize(90,45));
leftPush->setGeometry(650,230,90,45);
leftPush->setIcon(QIcon(":/resource/left.png"));
leftPush->setFlat(1);
rightPush->setIconSize(QSize(90,45));
rightPush->setGeometry(800,230,90,45);
rightPush->setIcon(QIcon(":/resource/right.png"));
rightPush->setFlat(1);
StartPush->setIconSize(QSize(90,45));
StartPush->setGeometry(650,400,90,45);
StartPush->setFlat(1);
StartPush->setIcon(QIcon(":/resource/Start.png"));
ExitPush->setIconSize(QSize(90,45));
ExitPush->setGeometry(780,400,90,45);
ExitPush->setIcon(QIcon(":/resource/Exit.png"));
ExitPush->setFlat(1);
ScoreLabel=new QLabel(this);
LevelLabel=new QLabel(this);
ScoreLabel->setText("Score:");
ScoreLabel->setGeometry(690,10,120,90);
LevelLabel->setText("Level:");
LevelLabel->setGeometry(690,50,120,90);
ScoreLabelNumber=new QLabel(this);
LevelLabelNumber=new QLabel(this);
ScoreLabelNumber->setText(QString::number(Score)+"分");
ScoreLabelNumber->setGeometry(750,10,120,90);
LevelLabelNumber->setText(QString::number(Difficulty)+"级");
LevelLabelNumber->setGeometry(750,50,120,90);
Timer=new QTimer(this);
connect(Timer,SIGNAL(timeout()),this,SLOT(M_timeout()));
connect(StartPush,SIGNAL(clicked(bool)),this,SLOT(M_startPush()));
connect(ExitPush,SIGNAL(clicked(bool)),this,SLOT(M_exitPush()));
/* snake[0][0]=(rand()%18+1)*30+30;
snake[0][1]=(rand()%15+1)*30+30;
m_setDiretion=(rand()%4)+1;
foodx=(rand()%20)*30;
foody=(rand()%15)*30;
bodycount=1; //包含蛇的头和尾巴
Nobite=true;
*/
snake_Init();
this->repaint();
}
void gamemainwindow::keyPressEvent(QKeyEvent *e)//获取键盘按键并进行处理
{
switch(e->key())
{
case Qt::Key_Up:if(m_setDiretion!=2)m_setDiretion=1; break;
case Qt::Key_W:if(m_setDiretion!=2)m_setDiretion=1; break;
case Qt::Key_Down:if(m_setDiretion!=1)m_setDiretion=2; break;
case Qt::Key_S:if(m_setDiretion!=1)m_setDiretion=2; break;
case Qt::Key_Left:if(m_setDiretion!=4)m_setDiretion=3; break;
case Qt::Key_A:if(m_setDiretion!=4)m_setDiretion=3; break;
case Qt::Key_Right:if(m_setDiretion!=3)m_setDiretion=4; break;
case Qt::Key_D:if(m_setDiretion!=3)m_setDiretion=4; break;
default: break;
}
}
void gamemainwindow::paintEvent(QPaintEvent*)//绘图事件
{
QPainter painter(this);
if(0<=snake[0][0]&&snake[0][0]<=600&&0<=snake[0][1]&&snake[0][1]<=480&&Nobite)
{
// qDebug() << snake[0][0];
for(int i=bodycount;i>0;i--)
{
if(i==bodycount)//画出尾巴部分
{
if(snake[i][0]==snake[i-1][0]&&snake[i][1]<snake[i-1][1])
{
painter.drawImage(QRect(snake[i][0],snake[i][1],30,30),QImage(":/resource/taildown.png"));
}
else if(snake[i][0]==snake[i-1][0]&&snake[i][1]>snake[i-1][1])
{
painter.drawImage(QRect(snake[i][0],snake[i][1],30,30),QImage(":/resource/tailup.png"));
}
else if(snake[i][0]<snake[i-1][0]&&snake[i][1]==snake[i-1][1])
{
painter.drawImage(QRect(snake[i][0],snake[i][1],30,30),QImage(":/resource/tailright.png"));
}
else if(snake[i][0]>snake[i-1][0]&&snake[i][1]==snake[i-1][1])
{
painter.drawImage(QRect(snake[i][0],snake[i][1],30,30),QImage(":/resource/tailleft.png"));
}
}
else
{
if((snake[i][0]==snake[i-1][0]&& snake[i][1]<snake[i-1][1]&&snake[i+1][0]<snake[i][0]&&snake[i+1][1]==snake[i][1])||
((snake[i][0]>snake[i-1][0])&& snake[i][1]==snake[i-1][1]&&snake[i+1][0]==snake[i][0]&&snake[i+1][1]>snake[i][1]))
{
painter.drawImage(QRect(snake[i][0],snake[i][1],30,30),QImage(":/resource/leftdown.png"));
}
else if((snake[i][0]<snake[i-1][0]&& snake[i][1]==snake[i-1][1]&&snake[i+1][0]==snake[i][0]&&snake[i+1][1]>snake[i][1])||
(snake[i][0]==snake[i-1][0]&& snake[i][1]<snake[i-1][1]&&snake[i+1][0]>snake[i][0]&&snake[i+1][1]==snake[i][1]))
{
painter.drawImage(QRect(snake[i][0],snake[i][1],30,30),QImage(":/resource/rightdown.png"));
}
else if((snake[i][0]>snake[i-1][0]&& snake[i][1]==snake[i-1][1]&&snake[i+1][0]==snake[i][0]&&snake[i+1][1]<snake[i][1])||
(snake[i][0]==snake[i-1][0]&& snake[i][1]>snake[i-1][1]&&snake[i+1][0]<snake[i][0]&&snake[i+1][1]==snake[i][1]))
{
painter.drawImage(QRect(snake[i][0],snake[i][1],30,30),QImage(":/resource/leftup.png"));
}
else if(((snake[i][0]==snake[i-1][0])&& snake[i][1]>snake[i-1][1]&&snake[i+1][0]>snake[i][0]&&snake[i+1][1]==snake[i][1])||
(snake[i][0]<snake[i-1][0]&& snake[i][1]==snake[i-1][1]&&snake[i+1][0]==snake[i][0]&&snake[i+1][1]<snake[i][1]))
{
painter.drawImage(QRect(snake[i][0],snake[i][1],30,30),QImage(":/resource/rightup.png"));
}
else if((snake[i][0]==snake[i-1][0]&&snake[i][0]==snake[i+1][0])&&((snake[i][1]<snake[i-1][1]&&snake[i+1][1]<snake[i][1])||
(snake[i][1]>snake[i-1][1]&&snake[i+1][1]>snake[i][1])))
{
painter.drawImage(QRect(snake[i][0],snake[i][1],30,30),QImage(":/resource/downup.png"));
}
else if ((snake[i][1]==snake[i-1][1]&&snake[i][1]==snake[i+1][1])&&((snake[i][0]<snake[i-1][0]&&snake[i+1][0]<snake[i][0])||
(snake[i][0]>snake[i-1][0]&&snake[i+1][0]>snake[i][0])))
{
painter.drawImage(QRect(snake[i][0],snake[i][1],30,30),QImage(":/resource/leftright.png"));
}
}
}
}
painter.drawImage(QRect(foodx,foody,30,30),QImage(":/resource/gost.png"));
painter.setPen(Qt::green);
for(int i=0;i<=widthnumber;i++)
{
for(int j=0;j<=longnumber;j++)
{
painter.drawRect(QRect(30*j,30*i,30,30));//画出棋�