Bonjour,

Voici ma nouvelle contribution ,un programme qui permet (simplement) de déplacer un pion sur un échiquier ,grace aux touches directionnel du clavier...
La commande de sortie est la touche "s" du clavier...

Voilà en deux fichiers, le programme compilable :

deplacehobj.h
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#ifndef _DEPLACEHOBJ_H_
#define _DEPLACEHOBJ_H_
 
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QColor>
#include <QPolygonF>
#include <QPolygon>
#include <QObject>
 
class Nomduneclace : public QObject
{	Q_OBJECT
	public :
	void init()
	{	s_scene = new QGraphicsScene ;
		v_view = new QGraphicsView ;	
		igrec = 5; ikze = 20;
	}
 
	void adjuste(QGraphicsScene * mscene,QGraphicsView * mview)
	{ s_scene = mscene ; v_view = mview ; }
 
	void objdessin(QGraphicsScene * mscene,int icze = 0,int igrec = 0)
	{	QColor noirbrun(120,0,0) ;
		QColor noir(0,0,0) ;
		QPolygon mpolygon1 ;
	mpolygon1.setPoints(4,(15+icze),(15+igrec),(8+icze),(32+igrec),(32+icze),(32+igrec),(25+icze),(15+igrec) ) ;
		QPolygon mpolygon2 ;
	mpolygon2.setPoints(4,(8+icze),(40+igrec),(4+icze),(60+igrec),(36+icze),(60+igrec),(32+icze),(40+igrec) ) ;
		QPolygonF mpolygf1(mpolygon1) ;
		QPolygonF mpolygf2(mpolygon2) ;
			mscene->addEllipse((10+icze),igrec,20,16,noir,noirbrun) ;
			mscene->addPolygon(mpolygf1,noir,noirbrun) ;
			mscene->addRect((4+icze),(32+igrec),32,8,noir,noirbrun) ;
			mscene->addPolygon(mpolygf2,noir,noirbrun) ;
			mscene->addRect(icze,(60+igrec),40,10,noir,noirbrun) ;
	}
 
	void carreaux(QGraphicsScene * mscene)
	{	QColor noir(5,5,5) ;
		QColor jaune(250,250,0) ;
		int icze = 0 ;
//ligne 1 et 2
		for (icze = 0 ;icze <= 480 ; icze+= 160)
		{	mscene->addRect(icze,0,80,80,noir,noir) ;
			mscene->addRect((icze+80),0,80,80,jaune,jaune) ;
			mscene->addRect((icze+80),80,80,80,noir,noir) ;
			mscene->addRect(icze,80,80,80,jaune,jaune) ;
//ligne 3 et 4
			mscene->addRect(icze,160,80,80,noir,noir) ;
			mscene->addRect((icze+80),160,80,80,jaune,jaune) ;
			mscene->addRect((icze+80),240,80,80,noir,noir) ;
			mscene->addRect(icze,240,80,80,jaune,jaune) ;
//ligne 5 et 6
			mscene->addRect(icze,320,80,80,noir,noir) ;
			mscene->addRect((icze+80),320,80,80,jaune,jaune) ;
			mscene->addRect((icze+80),400,80,80,noir,noir) ;
			mscene->addRect(icze,400,80,80,jaune,jaune) ;
//ligne 7 et 8
			mscene->addRect(icze,480,80,80,noir,noir) ;
			mscene->addRect((icze+80),480,80,80,jaune,jaune) ;
			mscene->addRect((icze+80),560,80,80,noir,noir) ;
			mscene->addRect(icze,560,80,80,jaune,jaune) ;	
		}
 
	}
 
	public slots :
	void haut()
	{	if (igrec > 5) 
		{ igrec-= 80 ; }
		else 
		{ igrec = 5 ; }
 
		emit appuiet() ;
	}
	void bas()
	{	if(igrec < 565)
		{ igrec+= 80 ; }
		else
		{ igrec = 565; }	
		emit appuiet() ;
	}
	void droite()
	{	if(ikze < 580)
		{ ikze+= 80 ; }
		else
		{ ikze = 580 ; }
		emit appuiet() ;
	}
	void gauche()
	{	if(ikze > 20)
		{ ikze-= 80 ; }
		else
		{ ikze = 20 ; }
		emit appuiet() ;
	}	
 
	void deplace()
	{	s_scene->clear() ;
		carreaux(s_scene) ;
		objdessin(s_scene,ikze,igrec) ;	
	//	v_view->setScene(s_scene) ;
		v_view->update() ;
	}
 
	signals :
	void appuiet() ;
 
	private :
	QGraphicsScene * s_scene ;
	QGraphicsView * v_view ;	
	int igrec ;
	int ikze ;
};
 
#endif
deplacehobj.cpp
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
 
//un programme pour déplacer
//un objet dessin
#include "deplacehobj.h"
#include <QApplication>
#include <QShortcut>
 
//#include <QGraphicsScene>
//#include <QGraphicsView>
//#include <QColor>
//#include <QPolygonF>
//#include <QPolygon>
//#include <QObject>
int main(int argc,char ** argv)
{	QApplication app(argc,argv) ;
	QGraphicsScene * mscene = new QGraphicsScene(0,0,640,640) ;
	QGraphicsView * ecran = new QGraphicsView ;
	Nomduneclace objet1 ;
		objet1.init() ;
		objet1.carreaux(mscene) ;
		objet1.objdessin(mscene,20,5) ;
	QShortcut * hautbtn = new QShortcut(ecran) ;
	QShortcut * basbtn = new QShortcut(ecran) ;
	QShortcut * gauchebtn = new QShortcut(ecran) ;
	QShortcut * droitebtn = new QShortcut(ecran) ;
	QShortcut * objet2 = new QShortcut(ecran) ;
		hautbtn->setKey(Qt::Key_Up) ;
		basbtn->setKey(Qt::Key_Down) ;
		gauchebtn->setKey(Qt::Key_Left) ;
		droitebtn->setKey(Qt::Key_Right) ;
		objet2->setKey(Qt::Key_S) ;
	QObject::connect(hautbtn,SIGNAL(activated()),&objet1,SLOT(haut()) ) ;
	QObject::connect(basbtn,SIGNAL(activated()),&objet1,SLOT(bas()) ) ;
	QObject::connect(gauchebtn,SIGNAL(activated()),&objet1,SLOT(gauche()) ) ;
	QObject::connect(droitebtn,SIGNAL(activated()),&objet1,SLOT(droite()) ) ;
	QObject::connect(&objet1,SIGNAL(appuiet()),&objet1,SLOT(deplace()) ) ;
	QObject::connect(objet2,SIGNAL(activated()),&app,SLOT(quit()) ) ;
		ecran->setScene(mscene) ;
		ecran->setFixedSize(645,645) ;
		ecran->show() ;
		objet1.adjuste(mscene,ecran) ;
app.exec() ;
return 0 ;
}
Ce programme a été crée sous linux gnome 3 ubuntu 12.04 avec Qt 4.8.2

Bonne programmation .