16,224
社区成员
发帖
与我相关
我的任务
分享
#include <qpainter.h>
#include <qbrush.h>
#include <qevent.h>
#include <qmainwindow.h>
#include <qmessagebox.h>
#include <qstring.h>
#include <qlabel.h>
#include <qwidget.h>
#include <qpushbutton.h>
#include <QMenu>
#include <QAction>
#include <stdio.h>
#include <QMessageBox>
#include "chess.h"
int mychess::timecount = 0;
int gametime_count = 0;
int mousecheck_count = 0;
int mousecheck_x = 0;
int mousecheck_y = 0;
int flag =1;
int gameoverFlag = 0;
int gamewinFlag =0;//
QString game_str;
struct _playFlag
{
int usedFlag;// 1 means pressed.
int pressFlag;//0 nothing,1 white,2 black
//int test;
}a[600][600];
void initplay();
mychess::mychess(QWidget *parent)//构造函数
:QWidget(parent)
{
initplay();
mytimer = new QTimer(this);
connect(mytimer,SIGNAL(timeout()),this,SLOT(myredraw()));
connect(this,SIGNAL(mystop()),this,SLOT(killtime()));
QAction *newgame_action = new QAction(tr("新游戏"),this);
connect(newgame_action,SIGNAL(triggered()),this,SLOT(new_game()));
QAction *exit_action = new QAction(tr("退出"),this);
connect(exit_action,SIGNAL(triggered()),this,SLOT(close()));
QAction *help_action = new QAction(tr("帮助"),this);
connect(help_action,SIGNAL(triggered()),this,SLOT(help_function()));
gamemenu_btn = new QPushButton(tr("游戏"),this);
QMenu *menu_game = new QMenu();
gamemenu_btn->setMenu(menu_game);
menu_game->addAction(newgame_action);
menu_game->addAction(exit_action);
gamemenu_btn->setGeometry(525,150,60,25);
helpmenu_btn = new QPushButton(tr("帮助"),this);
QMenu *menu_help = new QMenu();
helpmenu_btn->setMenu(menu_help);
menu_help->addAction(help_action);
background_image = new QPixmap();
background_image->load("./image/background.jpg");///
resize(620,520);
setMaximumSize(620,520);
setMinimumSize(620,520);
time_label = new QLabel(this);//时间条
mytimer->start(100);
}
void mychess::new_game()//新游戏
{
mytimer->start(100);
initplay();
mousecheck_count=0;
gametime_count = 0;//
mousecheck_x = 0;
mousecheck_y = 0;
gameoverFlag = 0;
gamewinFlag = 0;
game_str = tr("");
this->update();//key
//printf("new game.\n");
}
void mychess::help_function()
{
QMessageBox::about(this,tr("关于"),tr("Chess Desktop V2.0 \nRelease Build(1108261718)\n\t---- 广ヨ〡ロ"));
}
void initplay()//结构体初始化
{
int i;
for(i=0;i<=500;(i++))
{
for(int j=0;j<=500;(j++))
{
//a[i][j] = 0;
a[i][j].pressFlag = 0;
a[i][j].usedFlag = 0;
//a.test[i][j] = 0;
}
}
}
mychess::~mychess()
{
}
void mychess::killtime()
{
mytimer->stop();
}
void mychess::myredraw()//重绘函数
{
if(mytimer->isActive())
{
if(timecount =100)//
{
this->update();
}
else
{
emit mystop();
}
gametime_count++;
}
}
void mychess::paintEvent(QPaintEvent *)//绘图事件
{
time_label->setGeometry(530,230,150,50);
QString Time = tr("时间=")+QString::number(gametime_count/10);
time_label->setText(Time);
time_label->show();
QPainter *pain_line = new QPainter(this);
pain_line->setPen(Qt::black);
pain_line->drawPixmap(0,0,*background_image);
for(int line_count=0;line_count<25;line_count++)//绘制网格
{
pain_line->drawLine(20,20+(line_count*20),20*25,20+(line_count*20));
pain_line->drawLine(20+(line_count*20),20,20+(line_count*20),20*25);
}
pain_line->setBrush(Qt::black);
{
pain_line->drawEllipse(80-5,80-5,10,10);
pain_line->drawEllipse(440-5,80-5,10,10);
pain_line->drawEllipse(80-5,440-5,10,10);
pain_line->drawEllipse(440-5,440-5,10,10);
pain_line->drawEllipse(260-5,240-5,10,10);//中间的定位黑点
}
draw_chessman();
judge_line();
}
void mychess::mousePressEvent(QMouseEvent *e)//鼠标按下判断事件
{
QString str_x;
QString str_y;
str_x = QString::number(e->x());//鼠标左键按下时x坐标
str_y = QString::number(e->y());//鼠标左键按下时y坐标
mousecheck_x = str_x.toInt();
mousecheck_y = str_y.toInt();
if(e->button() == Qt::LeftButton && mousecheck_x>=20&&mousecheck_x<=510
&& mousecheck_y>=20 && mousecheck_y<=510
&& a[(mousecheck_x/20)-1][(mousecheck_y/20)-1].usedFlag != 1)
{
mousecheck_count++;
if(a[(mousecheck_x/20)-1][(mousecheck_y/20)-1].usedFlag==0)
{
if(mousecheck_count%2==0)
{
a[(mousecheck_x/20)-1][(mousecheck_y/20)-1].pressFlag = 2;
a[(mousecheck_x/20)-1][(mousecheck_y/20)-1].usedFlag = 1;
printf("b_a[%d][%d]=%d\n",(mousecheck_x/20)*20,(mousecheck_y/20)*20,
a[(mousecheck_x/20)*20][(mousecheck_y/20)*20].pressFlag);
}
if(mousecheck_count%2==1)
{
a[(mousecheck_x/20)-1][(mousecheck_y/20)-1].pressFlag = 1;
a[(mousecheck_x/20)-1][(mousecheck_y/20)-1].usedFlag = 1;
//printf("w_a[%d][%d]=%d\n",(mousecheck_x/20)*20,(mousecheck_y/20)*20,
//a[(mousecheck_x/20)*20][(mousecheck_y/20)*20].pressFlag);
}
}
}
draw_chessman();
judge_line();
}