使用qt 键盘上的方向键只能控制ui界面上的按钮选择,不能实现我设定的功能
我做的是一个贪吃蛇游戏,现在我想实现的功能是:按下键盘上的上下左右箭头的方向按键时,贪吃蛇的蛇头也可以随之发生相应的改变。但是,按下方向按键时,方向按键控制的是ui界面上的控件
[b].h头文件程序[/color]
#ifndef GAME_WIDGET_H
#define GAME_WIDGET_H
#include <QWidget>
#include <QPaintEvent>//QWidget类中的paintEvent()
#include <QTimer>//产生信号
#include <QKeyEvent>
#define HEAD_UP 0
#define HEAD_DOWN 1
#define HEAD_LEFT 2
#define HEAD_RIGHT 3
namespace Ui {
class Game_widget;
}
class Game_widget : public QWidget
{
Q_OBJECT
public:
explicit Game_widget(QWidget *parent = 0);
~Game_widget();
void paintEvent(QPaintEvent *event);
void keyPressEvent(QKeyEvent *event);
signals:
void pressAslot(void);
void pressDslot(void);
void pressWslot(void);
void pressSslot(void);
/*
void pressUPslot(void);//方向箭头
void pressDOWNslot(void);
void pressLEFTslot(void);
void pressRIGHTslot(void);
*/
private slots:
void on_btn_start_clicked();
void timeout_handler(void);//超时处理函数
void starT(void);
void dirJud(int dire1,int dire2);
void on_btn_go_clicked();
void on_btn_back_clicked();
void on_btn_left_clicked();
void on_btn_right_clicked();
void on_btn_suspend_clicked();
void on_btn_restart_clicked();
private:
Ui::Game_widget *ui;
int snake[200][2];//存储蛇头到蛇尾每一个部分的坐标值
int foodx,foody;//食物坐标
int direction;//蛇头方向
QTimer *tm;//时钟超时信号
};
#endif // GAME_WIDGET_H
[color=#FF0000].cpp文件程序
#include "game_widget.h"
#include "ui_game_widget.h"
#include <QDebug>
#include <QPainter>
#include <QPalette>//调色板
#include <QBrush>
#include <QPixmap>
#include <QTime>
#define ROW 18
#define COL 20
Game_widget::Game_widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Game_widget)
{
ui->setupUi(this);
QPalette palet;
palet.setBrush(QPalette::Background,\
QBrush(QPixmap(":/new/prefix1/images/green.jpg").scaled(580,370)));
this->setPalette(palet);
starT();
/*创建时钟对象*/
tm = new QTimer();
tm->setInterval(500);//500ms,每0.5s产生一次超时信号
//链接时钟的timeout()信号与蛇头移动的槽函数
connect(tm,SIGNAL(timeout()),this,SLOT(timeout_handler()));
connect(this,SIGNAL(pressWslot()),ui->btn_go,SLOT(click()));
connect(this,SIGNAL(pressSslot()),ui->btn_back,SLOT(click()));
connect(this,SIGNAL(pressAslot()),ui->btn_left,SLOT(click()));
connect(this,SIGNAL(pressDslot()),ui->btn_right,SLOT(click()));
/*
connect(this,SIGNAL(pressUPslot()),ui->btn_go,SLOT(click()));
connect(this,SIGNAL(pressDOWNslot()),ui->btn_back,SLOT(click()));
connect(this,SIGNAL(pressLEFTslot()),ui->btn_left,SLOT(click()));
connect(this,SIGNAL(pressRIGHTslot()),ui->btn_right,SLOT(click()));
*/
}
/*产生蛇头和食物的位置*/
void Game_widget::starT(void)
{
/*产生蛇头的位置*/
QTime curt = QTime::currentTime();
qsrand(curt.msec());//秒钟
snake[0][0] = rand()%COL;//出现在行的位置x
snake[0][1] = rand()%ROW;//出现在lie的位置y
direction = rand()%4;
/*产生食物的位置*/
while(1)
{
foodx = rand()%COL;
foody = rand()%ROW;
//蛇头和食物的位置不允许重叠
if(snake[0][0] != foodx || snake[0][1] != foody)
break;
}
}
/*蛇头移动函数*/
void Game_widget::timeout_handler(void)
{
switch (direction) {
case HEAD_UP:
snake[0][1] --;
break;
case HEAD_DOWN:
snake[0][1] ++;
break;
case HEAD_LEFT:
snake[0][0] --;
break;
case HEAD_RIGHT:
snake[0][0] ++;
break;
default:
break;
}
this->repaint();//调用paintEvent()
}
/*只要调用update/repaint方法,自动调用paintEvent()*/
void Game_widget::paintEvent(QPaintEvent *event)
{
char ima_path[100] = {};//蛇头图片的路径
QPainter painter(this); //画笔对象
int i,j;
/*画网络*/
for(i=0;i<ROW;i++)
{
for(j=0;j<COL;j++)
{
painter.drawRect(j*20,i*20,20,20);
}
}
/*画蛇头*/
//向数组中填充内容
snprintf(ima_path,100,":/new/prefix1/images/head%d",direction);
//x,y的位置及大小,蛇头图片路径.缩放为20*20(即网格大小)
painter.drawImage(snake[0][0]*20,snake[0][1]*20,\
QImage(ima_path).scaled(20,20));
/*画食物*/
painter.drawImage(foodx*20,foody*20,\
QImage(":/new/prefix1/images/apple.png").scaled(20,20));
}
Game_widget::~Game_widget()
{
delete ui;
}
void Game_widget::keyPressEvent(QKeyEvent *event)
{
int keyvakue = event->key();
switch (keyvakue) {
case Qt::Key_W: //上
emit pressWslot();break;
case Qt::Key_S://下
emit pressSslot();break;
case Qt::Key_A://左
emit pressAslot();break;
case Qt::Key_D://右
emit pressDslot();break;
case Qt::Key_Up:
dirJud(HEAD_DOWN,HEAD_UP);break;
case Qt::Key_Down:
dirJud(HEAD_UP,HEAD_DOWN);break;
case Qt::Key_Left:
dirJud(HEAD_RIGHT,HEAD_LEFT);break;
case Qt::Key_Right:
dirJud(HEAD_LEFT,HEAD_RIGHT);break;
default:
break;
}
}
/*UI界面的按钮*/
void Game_widget::on_btn_start_clicked()
{
tm->start();//开始移动
}
/*例:当原蛇头方向为向上,欲使原来的蛇头向下时,蛇头方向不做改变*/
void Game_widget::dirJud(int dire1,int dire2)
{
if(direction == dire1)
return;
direction = dire2;
}
void Game_widget::on_btn_go_clicked()//上
{
dirJud(HEAD_DOWN,HEAD_UP);
}
void Game_widget::on_btn_back_clicked()//下
{
dirJud(HEAD_UP,HEAD_DOWN);
}
void Game_widget::on_btn_left_clicked()//左
{
dirJud(HEAD_RIGHT,HEAD_LEFT);
}
void Game_widget::on_btn_right_clicked()//右
{
dirJud(HEAD_LEFT,HEAD_RIGHT);
}
/*暂停*/
void Game_widget::on_btn_suspend_clicked()
{
tm->stop();
}
/*从头再来*/
void Game_widget::on_btn_restart_clicked()
{
starT();
}