#include "mainui.h"
#include "ui_mainui.h"
mainUi::mainUi(QWidget *parent) :
QWidget(parent)
{
// ui->setupUi(this);
this->setWindowTitle("行走的小蜜蜂");
this->setWindowIcon(QIcon(":/photo/green.jpg"));
resize(mapMax+10,mapMax+10);
setStyleSheet("QWidget{background:white}"); //设置窗口颜色
setWindowFlags(Qt::FramelessWindowHint);//去掉标题栏
this->setAttribute(Qt::WA_TranslucentBackground);//设置背景透明
srand((unsigned)time(NULL));//把时间作为随机数种子
timer = new QTimer;//创建一个定时器对象
timer->start(100);//开启定时器100ms
connect(timer,&QTimer::timeout,this,&mainUi::timefoult);
snake_x<<100<<100<<100<<100;//定义坐标
snake_y<<100<<110<<120<<130;
}
mainUi::~mainUi()
{
delete ui;
}
/*
遍历整个列表:同X坐标判断,每次只移动一次
*/
void mainUi::moveLeft()//移动
{
int snakexbrfore[2]={0},snakeybrfore[2]={0},signale=0;
if((snake_x[0]>snake_x[1]&&snake_y[0]==snake_y[1])||snake_x[0]<=mapMin){signale=1;}
for(int i=0;i<snake_y.length()&&signale==0;i++)
{
if(i==0){
snakexbrfore[0]=snake_x[i];snakeybrfore[0]=snake_y[i];
snake_x[i]-=snake_boy;
if(snake_x[i]==beanx&&snake_y[i]==beany){
snake_x.append(beanx);snake_y.append(beany);
beanx=-mapMax;beany=-mapMax;
}
continue;
}
movedll(snakexbrfore,snakeybrfore,i);
}
}
void mainUi::moveRight()
{
int snakexbrfore[2]={0},snakeybrfore[2]={0},signale=0;
if((snake_x[0]<snake_x[1]&&snake_y[0]==snake_y[1])||snake_x[0]>=mapMax){signale=1;}
for(int i=0;i<snake_y.length()&&signale==0;i++)
{
if(i==0){
snakexbrfore[0]=snake_x[i];snakeybrfore[0]=snake_y[i];
snake_x[i]+=snake_boy;
if(snake_x[i]==beanx&&snake_y[i]==beany){
snake_x.append(beanx);snake_y.append(beany);
beanx=-mapMax;beany=-mapMax;
}
continue;
}
movedll(snakexbrfore,snakeybrfore,i);
}
}
//除了第一个,之后每次的坐标都和前一次坐标相等
void mainUi::moveUp()//要注意移动的时候的坐标
{
int snakexbrfore[2]={0},snakeybrfore[2]={0},signale=0;
if((snake_y[0]>snake_y[1]&&snake_x[0]==snake_x[1])||snake_y[0]<=mapMin){signale=1;}//判断蛇头的有没有出界线
for(int i=0;i<snake_y.length()&&signale==0;i++)//如果没有就吃豆子
{
if(i==0&&snake_y[i]<=snake_y[i+1]){
snakexbrfore[0]=snake_x[i];snakeybrfore[0]=snake_y[i];
snake_y[i]-=snake_boy;
if(snake_x[i]==beanx&&snake_y[i]==beany){
snake_x.append(beanx);snake_y.append(beany);
beanx=-mapMax;beany=-mapMax;
}
continue;
}
movedll(snakexbrfore,snakeybrfore,i);
}
}
void mainUi::moveDown()
{
int snakexbrfore[2]={0},snakeybrfore[2]={0},signale=0;
if((snake_y[0]<snake_y[1]&&snake_x[0]==snake_x[1])||snake_y[0]>=mapMax){signale=1;}
for(int i=0;i<snake_y.length()&&signale==0;i++)
{
if(i==0){
snakexbrfore[0]=snake_x[i];snakeybrfore[0]=snake_y[i];
snake_y[i]+=snake_boy;
if(snake_x[i]==beanx&&snake_y[i]==beany){
snake_x.append(beanx);snake_y.append(beany);
beanx=-mapMax;beany=-mapMax;
}
continue;
}
movedll(snakexbrfore,snakeybrfore,i);
}
}
void mainUi::movedll(int *x,int *y,int i)//公共函数部分
{
*(x+1)=snake_x[i];*(y+1)=snake_y[i];
snake_x[i]=*x;snake_y[i]=*y;
*x=*(x+1);*y=*(y+1);
}
void mainUi::getaBean()//生成一个豆子,也就是随机生成一个整数坐标(位于500之内)
{
//用四舍五入法生成坐标
int x=40+(rand()%mapMax-40),y=rand()%mapMax;//X,Y的范围为40 ~ mapMax之间
if((x%10)!=0){x+=(10-(x%10));}
if((y%10)!=0){y+=(10-(y%10));}
beanx=x,beany=y;
}
void mainUi::timefoult()
{
current_time++;
if(moveFlag == 'D')
{
moveDown();
}
if(moveFlag == 'U')
{
moveUp();
}
if(moveFlag == 'L')
{
moveLeft();
}
if(moveFlag == 'R')
{
moveRight();
}
update();
}
void mainUi::keyPressEvent(QKeyEvent *event) //定义键盘事件
{
switch(event->key())
{
case Qt::Key_Up:
// moveUp();
if(moveFlag!= 'D')
{
moveFlag ='U';
}
break;
case Qt::Key_Down:
if(moveFlag !='U')
{
moveFlag ='D';
}
break;
case Qt::Key_Right:
if(moveFlag != 'L')
{
moveFlag ='R';
}
break;
case Qt::Key_Left:
if(moveFlag != 'R')
{
moveFlag='L';
}
break;
case Qt::Key_Space:
getaBean();
break;
case Qt::Key_Escape://退出游戏
this->close();
}
update();
}
void mainUi::paintEvent(QPaintEvent *event) //画家函数
{
QPainter painter(this);//定义一张画布
QPen pen;//定义一张画笔
painter.setPen(pen);//将画笔设置到画布中去
pen.setWidth(snake_boy);//笔刷宽度设置为蛇的宽度
QBrush brs(Qt::black);//设置笔刷为黑色
QFont font("黑体",12,QFont::ExtraLight,false);//计数栏设置
painter.setFont(font);//画布插入字体
painter.drawText(20,20,QString("时间过去:")+QString("%1").arg((int)current_time/10));
painter.drawText(20,40,QString("控制:")+QString("移动:上下左右——出豆子:空格——退出:ESC"));
brs.setColor(Qt::darkGray);//改变笔刷颜色
painter.setBrush(brs);
//画出矩形,蛇
for(int i=0;i<snake_x.length();i++)
{
painter.setBrush(brs);//设置画笔笔刷
painter.drawEllipse(snake_x.at(i),snake_y.at(i),snake_boy,snake_boy);//画出蛇的身体
}
//画豆子
if(beanx>0&&beany>0)
{
brs.setColor(Qt::darkMagenta);//设置豆子颜色
brs.setStyle(Qt::ConicalGradientPattern);//设置笔刷样式
painter.setBrush(brs);
painter.drawRect(beanx,beany,snake_boy,snake_boy);//画出豆子的身体
}
}
评论0