刚学,不够专业请勿喷,有不对的地方还请指出,我渴望进步!现在贴出这个游戏我原创的所有代码。希望可以帮到有需要的人。
游戏界面:
因为后面添加了.wav的音频文件,所以要在.pro页面添加下面内容。(提醒:使用QSound只可以播放.wav后缀的音频文件)
.h文件里面的内容
初始化:
画图事件paintEvent:
Q_UNUSED(event);
QPainter painter(this);
QPen pen;
pen.setColor(Qt::gray);
pen.setWidth(2);
painter.setPen(pen);
QBrush brush;
brush.setColor(Qt::white);
brush.setStyle(Qt::SolidPattern);
painter.setBrush(brush);
//棋子落在十字线上
//画行
for(int i=0;i<=game_row;i++){
painter.drawLine(0+15,i*small_box_l+15,game_col*small_box_l+15,i*small_box_l+15);
}
//画列
for(int j=0;j<=game_col;j++){
painter.drawLine(j*small_box_l+15,0+15,j*small_box_l+15,game_row*small_box_l+15);
}
//棋子落在格子里
// //画行
// for(int i=0;i<=game_row;i++){
// painter.drawLine(0,i*small_box_l,game_col*small_box_l,i*small_box_l);
// }
// //画列
// for(int j=0;j<=game_col;j++){
// painter.drawLine(j*small_box_l,0,j*small_box_l,game_row*small_box_l);
// }
if(isBegain == 1){//点击了开始游戏按钮
ui->label->setText("白棋先手");
for(int i=0;i<20;i++){
for(int j=0;j<20;j++){
if(QiZi[i][j].flag==1){
if( QiZi[i][j].color==1){//画白棋
painter.setBrush(Qt::white);
painter.drawEllipse(QiZi[i][j].x,QiZi[i][j].y,30,30);
}
if( QiZi[i][j].color==2){//画黑棋
//painter.setBrush(brush2);
painter.setBrush(Qt::black);
painter.drawEllipse(QiZi[i][j].x,QiZi[i][j].y,30,30);
}
}
}
}
}
鼠标按压事件mousePressEvent:
Q_UNUSED(event);
if(isBegain == 1){
find_mouse_area(event->pos().x(),event->pos().y());
}
if(n != 0 && n%2 == 1){
ui->label_2->setText("黑棋下");
}else if(n != 0 && n%2 == 0){
ui->label_2->setText("白棋下");
}
void MainWindow::find_mouse_area(int x, int y)//判断鼠标点击的范围,给数组对应的位置赋值
{
for(int i=0;i
for(int j=0;j
if(x>=i*small_box_l && x<(i+1)*small_box_l){
if(y>=j*small_box_l && y<(j+1)*small_box_l){
if(QiZi[i][j].flag == 0 && QiZi[i][j].x==0 && QiZi[i][j].y==0 && QiZi[i][j].color==0){
music = new QSound(":/new/prefix1/music/luozi_music.wav");
music->play();
QiZi[i][j].x=i*30;
QiZi[i][j].y=j*30;
if(n%2==0){
QiZi[i][j].color=1;
}else{
QiZi[i][j].color=2;
}
QiZi[i][j].flag=1;
isORno_win();
n++;
}
break;
}
}
}
}
update();
}
白棋赢了void MainWindow::whiteDefault()
{
music = new QSound(":/new/prefix1/music/win_music.wav");
music->play();
QMessageBox::about(this,"提示信息","您赢了!");
}
黑棋赢了:
void MainWindow::blackDefault()
{
music = new QSound(":/new/prefix1/music/lost_music.wav");
music->play();
QMessageBox::about(this,"提示信息","您输了!");
}
判断是否连成5子:
void MainWindow::isORno_win()
{
int win_white_flag=0;//0为没赢
int win_black_flag=0;
//遍历棋盘里的每一个子
for(int i=0;i<20;i++){
for(int j=0;j<20;j++){
//判定白棋是否满5子
if(QiZi[i][j].color == 1){
//横着找五个子
int white_count=0;//连续的白棋个数
for(int a=i,b=j;b<20;b++){
if(QiZi[a][b].flag==1 && QiZi[a][b].color == 1){
white_count++;
}else{
break;
}
}
if(white_count >=5){
win_white_flag = 1;//白棋赢
}
//竖着找五个字
white_count=0;
for(int a=i,b=j;a<20;a++){
if(QiZi[a][b].flag==1 && QiZi[a][b].color == 1){
white_count++;
}else{
break;
}
}
if(white_count >=5){
win_white_flag = 1;//白棋赢
}
//斜着找
white_count=0;
for(int a=i,b=j;a<20,b<20;a++,b++){
if(QiZi[a][b].flag == 1 && QiZi[a][b].color == 1){
white_count++;
}else{
break;
}
}
if(white_count>=5){
win_white_flag = 1;
}
white_count=0;
for(int a=i,b=j;a>=0,b<20;a--,b++){
if(QiZi[a][b].flag == 1 && QiZi[a][b].color == 1){
white_count++;
}else{
break;
}
}
if(white_count>=5){
win_white_flag = 1;
}
}
//判定黑棋是否满5子
if(QiZi[i][j].color == 2){
//横着找五个子
int black_count=0;//连续的黑棋个数
for(int a=i,b=j;b<20;b++){
if(QiZi[a][b].flag==1 && QiZi[a][b].color == 2){
black_count++;
}else{
break;
}
}
if(black_count >=5){
win_black_flag = 1;//黑棋赢
}
//竖着找五个字
black_count=0;
for(int a=i,b=j;a<20;a++){
if(QiZi[a][b].flag==1 && QiZi[a][b].color == 2){
black_count++;
}else{
break;
}
}
if(black_count >=5){
win_black_flag = 1;//黑棋赢
}
//斜着找
black_count=0;
for(int a=i,b=j;a<20,b<20;a++,b++){
if(QiZi[a][b].flag == 1 && QiZi[a][b].color == 2){
black_count++;
}else{
break;
}
}
if(black_count>=5){
win_black_flag = 1;
}
black_count=0;
for(int a=i,b=j;a>=0,b<20;a--,b++){
if(QiZi[a][b].flag == 1 && QiZi[a][b].color == 2){
black_count++;
}else{
break;
}
}
if(black_count>=5){
win_black_flag = 1;
}
}
}
}
if(win_white_flag == 1){
whiteDefault();
}
if(win_black_flag == 1){
blackDefault();
}
}
开始游戏按钮被点击后:
void MainWindow::on_pushButton_clicked()
{
isBegain = 1;
ui->label->setText("白棋先手");
}
再来一局的按钮被点击后:
void MainWindow::on_pushButton_2_clicked()
{
//初始化地图数组
for(int i=0;i<20;i++){
for(int j=0;j<20;j++){
QiZi[i][j].flag=0;
QiZi[i][j].color=0;
QiZi[i][j].x=0;
QiZi[i][j].y=0;
}
}
n=0;
update();
ui->label_2->setText("白棋下");
}