-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyChezzMin.py
677 lines (582 loc) · 30.6 KB
/
pyChezzMin.py
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
"""
Vector Figures: https://commons.wikimedia.org/wiki/Category:SVG_chess_pieces
"""
import sys
import os
import json
import getpass
from time import gmtime, strftime
from random import random
from PyQt4 import QtGui, QtCore, QtSvg, uic
from PyQt4.QtCore import QPropertyAnimation
scriptPath = os.path.dirname(__file__)
uifile = scriptPath + "/ui/pyChezzMinUI.ui"
form_class = uic.loadUiType(uifile)[0]
class QPointFEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, QtCore.QPointF):
return [obj.x(), obj.y()]
# Let the base class default method raise the TypeError
return json.JSONEncoder.default(self, obj)
# https://stackoverflow.com/questions/2597278/python-load-variables-in-a-dict-into-namespace
class Bunch(object):
'''convert dict to attribute
darkFigures = Bunch(self.darkFigures)
darkFigures.darkKing
'''
def __init__(self, adict):
self.__dict__.update(adict)
# Snapping - http://www.walletfox.com/course/qgraphicsitemsnaptogrid.php
class myGraphicsSvgItem(QtSvg.QGraphicsSvgItem):
def __init__(self,
parent=None,
limitRect=QtCore.QRectF(0,0,1000,1000),
cursorShape=QtCore.Qt.OpenHandCursor,
name=None,
*args, **kwargs):
super(myGraphicsSvgItem, self).__init__(parent, *args,**kwargs)
self.anim_slide = None
self.anim_tilt = None
self.anim_fade = None
self.center = self.boundingRect().center()
self.cursorShape = cursorShape
self.limitRect = QtCore.QRectF(limitRect)
self.table_widget = self.getWidget('interactiveTable')
self.fields_widget = self.getWidget('grdFields')
# self.fields_widget = self.parentWidget()
self.setCursor(cursorShape)
self.setObjectName(name)
self.updateCenter()
def itemChange(self, change, value):
if change == QtGui.QGraphicsItem.ItemPositionChange:
new_value = value.toPointF()
if not self.limitRect.contains(new_value):
new_value.setX(min(self.limitRect.right(), max(new_value.x(), self.limitRect.left())))
new_value.setY(min(self.limitRect.bottom(), max(new_value.y(), self.limitRect.top())))
# print new_value, ' <---- NOT Contains'
value = QtCore.QVariant(new_value)
return QtGui.QGraphicsItem.itemChange(self, change, value)
if change == QtGui.QGraphicsItem.ItemPositionHasChanged:
pass
return QtGui.QGraphicsItem.itemChange(self, change, value)
def mousePressEvent(self, event):
print "-----------------"
self.setCursor(QtCore.Qt.ClosedHandCursor)
pos, item = self.getItemAtMousePos_byMouseEvent(event)
print "Press", pos, item.objectName() if item else None
self.doAanim_tilt(False)
super(myGraphicsSvgItem, self).mousePressEvent(event)
def mouseReleaseEvent(self, event):
print "-----"
self.setCursor(self.cursorShape)
pos, item = self.getItemAtMousePos_byMouseEvent(event)
print "Release", pos, item.objectName() if item else None
self.doAnim_slideTiltFade(item, doTilting=True)
# self.anim_do_slide(item)
# self.anim_do_tilt(False)
super(myGraphicsSvgItem, self).mouseReleaseEvent(event)
def getWidget(self, name):
wdg = pyChezzApp.findChildren((QtGui.QWidget, QtGui.QFrame, QtGui.QSpacerItem), QtCore.QString(name))[0]
return wdg
def getItemAtMousePos_byMouseEvent(self, event):
posGlobal = self.table_widget.mapToGlobal(self.pos().toPoint())
posLocal = self.fields_widget.mapFromGlobal(posGlobal)
widget = self.fields_widget.childAt(posLocal)
return posLocal, widget
def getWidgetAtTablePos(self, pos):
posGlobal = self.table_widget.mapToGlobal(pos)
posLocal = self.fields_widget.mapFromGlobal(posGlobal)
widget = self.fields_widget.childAt(posLocal)
return widget
def move_to_pos(self, pos, duration=200):
wdg = self.getWidgetAtTablePos(pos.toPoint())
self.doAnim_slide(wdg, duration)
def doAnim_slideTiltFade(self, to_widget, doTilting=False, isBecomeVisible=False, duration=400):
self.anim_group = QtCore.QParallelAnimationGroup()
self.doAnim_slide(to_widget, duration=duration, doStart=False)
self.anim_group.addAnimation(self.anim_slide)
if doTilting:
isTilted = abs(self.rotation())>1.0
self.doAanim_tilt(isTilted, duration=duration, doStart=False)
self.anim_group.addAnimation(self.anim_tilt)
if isBecomeVisible:
self.doAanim_fade(isBecomeVisible, duration=duration, doStart=False)
self.anim_group.addAnimation(self.anim_fade)
self.anim_group.start()
def doAnim_slide(self, to_widget, duration=200, doStart=True):
if to_widget is None:
self.anim_slide = None
return
posGlobal = self.fields_widget.mapToGlobal(to_widget.geometry().center())
posLocal = self.table_widget.mapFromGlobal(posGlobal)
self.anim_slide = QPropertyAnimation(self, "pos")
self.anim_slide.setDuration(duration)
self.anim_slide.setStartValue(self.pos())
self.anim_slide.setEndValue(posLocal)
self.anim_slide.setEasingCurve(QtCore.QEasingCurve.InOutQuint)
if doStart:
self.anim_slide.start()
return self.anim_slide
def doAanim_tilt(self, isTilted, duration=400, doStart=True):
self.anim_tilt = QPropertyAnimation(self, "rotation")
self.anim_tilt.setDuration(duration)
self.anim_tilt.setStartValue(-30 if isTilted else 0)
self.anim_tilt.setEndValue(0 if isTilted else -30)
self.anim_tilt.setEasingCurve(QtCore.QEasingCurve.OutBack)
if doStart:
self.anim_tilt.start()
return self.anim_tilt
def doAanim_fade(self, isBecomeVisible, duration=400, doStart=True):
self.anim_fade = QPropertyAnimation(self, "opacity")
self.anim_fade.setDuration(duration)
self.anim_fade.setStartValue(0.0 if isBecomeVisible else 1.0)
self.anim_fade.setEndValue(1.0 if isBecomeVisible else 0.0)
self.anim_fade.setEasingCurve(QtCore.QEasingCurve.InCirc)
if doStart:
self.anim_fade.start()
return self.anim_fade
def updateCenter(self):
self.center = self.boundingRect().center()
self.setTransformOriginPoint(self.center)
print '________\n' \
'Name: {},\n' \
'Pos: {},\n' \
'Bound: {},\n' \
'Center: {}\n' \
'Origin: {}'.format(self.objectName(),
self.pos(),
self.boundingRect(),
self.boundingRect().center(),
self.transformOriginPoint())
class ChessFigure(myGraphicsSvgItem):
def __init__(self, *args, **kwargs):
name = kwargs.get('name', None)
parent = kwargs.get('parent', None)
startField = kwargs.pop('startField', None)
limitRect = kwargs.get('limitRect', QtCore.QRectF(0, 0, 1000, 1000))
cursorShape = kwargs.get('cursorShape', QtCore.Qt.OpenHandCursor)
super(ChessFigure, self).__init__(*args, **kwargs)
self.name = name
self.isLight = False
self.isDark = False
self.imageFile = ''
self.startField = startField
self.currentField = ''
self.currentPos = QtCore.QPointF()
class pyChezzWin(QtGui.QWidget, form_class):
""" GUI class for chess using minimal number of widgets, no AI, on intuitive surface """
__widgetInst__ = None
__build__ = 56
def __init__(self, app = None):
if not self.__widgetInst__:
__widgetInst__ = app
super(pyChezzWin, self).__init__()
self.setupUi(self)
self.interactiveBoardView = QtGui.QGraphicsView(self)
self.scene = QtGui.QGraphicsScene(self.interactiveBoardView)
self.baseViewRect = QtCore.QRect()
# figures, icons and fields
self.fields = {}
self.snapPoints = []
self.icons = {}
self.figures = {}
# Connections
QtCore.QObject.connect(self.verticalSlider, QtCore.SIGNAL("valueChanged(int)"), self.valueHandlerSlider)
QtCore.QObject.connect(self.btnOpen, QtCore.SIGNAL("clicked()"), self.btnOpen_Clicked)
QtCore.QObject.connect(self.btnSave, QtCore.SIGNAL("clicked()"), self.btnSave_Clicked)
QtCore.QObject.connect(self.scene, QtCore.SIGNAL("clicked()"), self.btnOpen_Clicked)
# self.resizeEvent = self.alignTable
# game
self.myLogFile = ''
self.myFigColor = 'light'
# TODO: place common data-variables into the gameData dict! Replace references in the code!
# self.gameStarted = False
self.gameData = {}
self.opponentName = 'OPPONENT'
self.whoAmI = ''
self.isGameStarted = False
self.gameData['users'] = {'OPPONENT': 'dark'}
def show(self):
super(pyChezzWin, self).show()
self.setFixedSize(self.size())
self.frmIndicatorYOU.hide()
self.initializeBoard()
self.alignTable(self)
self.drawFigures()
self.drawIcons()
self.baseViewRect = self.interactiveBoardView.geometry()
self.collectSnapPoints()
self.enableFigures(self.darkFigures, state=False)
self.enableFigures(self.lightFigures, state=False)
# self.interactiveBoardView.setCursor(QtCore.Qt.OpenHandCursor)
print "Shown!!!"
# print "view + scene ---> ", self.interactiveBoardView.rect(), self.interactiveBoardView.sceneRect()
self.setAnimations()
@property
def whoAmI(self):
return self.gameData['whoAmI']
@whoAmI.setter
def whoAmI(self, name=None):
self.gameData['whoAmI'] = name
print "I\'am: ", name
@property
def opponentName(self):
return self.gameData['opponentName']
@opponentName.setter
def opponentName(self, name):
self.gameData['opponentName'] = name
@property
def isGameStarted(self):
return self.gameData['gameStarted']
@isGameStarted.setter
def isGameStarted(self, state):
self.gameData['gameStarted'] = state
def btnOpen_Clicked(self):
self.openDataFile()
def btnSave_Clicked(self):
self.saveNewDataFile()
self.isGameStarted = True
def valueHandlerSlider(self, value):
# FIXME: users data recognition must be fixed !!!!
if self.gameData['users'].keys().count(getpass.getuser()):
if value:
# opponent turn
#self.interactiveBoardView.setEnabled(False)
self.enableFigures(self.darkFigures, state=False)
self.enableFigures(self.lightFigures, state=False)
self.frmIndicatorME.hide()
self.frmIndicatorYOU.show()
self.saveChezzMinData()
else:
# my turn
# self.interactiveBoardView.setEnabled(True)
self.enableFigures(self.darkFigures)
self.enableFigures(self.lightFigures)
self.frmIndicatorME.show()
self.frmIndicatorYOU.hide()
self.loadChezzMinData()
def saveNewDataFile(self):
# openfile = QtGui.QFileDialog.getOpenFileName(self, filter='*.czz')
openfile = QtGui.QFileDialog.getSaveFileName(self, filter='*.czz')
# dialog success
if openfile != '':
# existing file
if os.path.isfile(openfile):
msgBox = QtGui.QMessageBox()
msgBox.setText("Over write is forbiden!");
msgBox.exec_();
return self.saveNewDataFile()
# new file will be created
else:
open(openfile, 'w')
self.myLogFile = openfile
self.saveChezzMinData()
file_info = QtCore.QFileInfo(openfile)
self.btnOpen.setText(file_info.fileName())
return True
else:
return False
def openDataFile(self):
# openfile = QtGui.QFileDialog.getOpenFileName(self, filter='*.czz')
openfile = QtGui.QFileDialog.getOpenFileName(self, filter='*.czz')
# dialog success
if os.path.isfile(openfile):
f = open(openfile, 'r')
self.myLogFile = openfile
file_info = QtCore.QFileInfo(openfile)
self.btnOpen.setText(file_info.fileName())
self.initGame()
self.loadChezzMinData()
self.isGameStarted = True
return True
else:
return False
def alignTable(self, null):
if self.baseViewRect.width() > 0:
targetRect = self.interactiveBoardView.geometry()
xForm = QtGui.QMatrix()
width_ratio = float(targetRect.width()) / self.baseViewRect.width()
height_ratio = float(targetRect.height()) / self.baseViewRect.height()
xForm.scale(width_ratio, height_ratio)
self.interactiveBoardView.setMatrix(xForm)
# self.interactiveBoardView.fitInView(QtCore.QRectF(0, 0, targetRect.width(), targetRect.height()))
def initializeBoard(self):
self.interactiveBoardView.setRenderHints(QtGui.QPainter.Antialiasing | QtGui.QPainter.SmoothPixmapTransform)
self.interactiveBoardView.setStyleSheet("background-color: transparent;")
self.interactiveBoardView.setFrameShape(QtGui.QFrame.NoFrame)
self.interactiveBoardView.setObjectName("interactiveTable")
self.interactiveBoardView.setScene(self.scene)
self.interactiveBoardView.setInteractive(True)
self.interactiveBoardView.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.interactiveBoardView.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.verticalSlider.setEnabled(False)
self.pebleOppLight.setVisible(False)
self.pebleMyDark.setVisible(False)
self.whoAmI = getpass.getuser()
self.gameData['users'][self.whoAmI] = 'light'
print 'Users: ', self.gameData['users']
# fit sceneRect
targetRect = self.frmOpponentPark.geometry()
targetRect.setBottom(self.frmMyPark.geometry().bottom())
self.interactiveBoardView.setGeometry(targetRect)
self.interactiveBoardView.setSceneRect(0, 0, targetRect.width(), targetRect.height())
# collecting field widgets from UI file
for row in range(1, 9):
for col in 'ABCDEFGH':
fld = eval('self.field%s%s' % (col, row))
self.fields['{}{}'.format(col, row)] = fld # self.fields['A1'] = self.fieldA1
def startNewGame(self):
if self.saveChezzMinData():
self.initGame()
def initGame(self, state=True):
self.isGameStarted = state
self.icons['rotateIcon'].setVisible(False)
self.icons['goIcon'].setVisible(False)
self.enableFigures(self.darkFigures)
self.enableFigures(self.lightFigures)
self.btnSave.setVisible(False)
self.verticalSlider.setEnabled(True)
# whois next !!
print 'Game Started!!!'
def setAnimations(self):
self.anim_rotateIcon = QPropertyAnimation(self.icons["rotateIcon"], "rotation")
self.anim_rotateIcon.setDuration(300)
self.anim_rotateIcon.setStartValue(0)
self.anim_rotateIcon.setEndValue(360)
# self.anim_rotateBoard = QPropertyAnimation(self.interactiveBoardView, "rotate")
# self.anim_rotateBoard.setDuration(300)
# self.anim_rotateBoard.setStartValue(0)
# self.anim_rotateBoard.setEndValue(180)
# self.anim_unRotateBoard = QPropertyAnimation(self.interactiveBoardView, "rotate")
# self.anim_unRotateBoard.setDuration(300)
# self.anim_unRotateBoard.setStartValue(180)
# self.anim_unRotateBoard.setEndValue(0)
def rotateBoard(self):
self.anim_rotateIcon.start()
boardRect = self.interactiveBoardView.frameRect()
goIconRect = self.icons["goIcon"].boundingRect()
if self.myFigColor.find('light') == 0:
self.interactiveBoardView.rotate(180)
# self.grdFields.rotate(180)
# self.anim_rotateBoard.start()
pos = QtCore.QPointF(boardRect.width()/2 + goIconRect.width()/2, boardRect.height()/2 - 60)
self.icons["goIcon"].setPos(pos)
self.icons["goIcon"].rotate(180)
for key, fig in self.figures.iteritems():
fig.rotate(180)
fig.moveBy(1.5, 2)
self.pebleOppLight.setVisible(True)
self.pebleOppDark.setVisible(False)
self.pebleMyLight.setVisible(False)
self.pebleMyDark.setVisible(True)
self.myFigColor = 'dark'
else:
self.interactiveBoardView.rotate(-180)
pos = QtCore.QPointF(boardRect.width()/2 - goIconRect.width()/2, boardRect.height()/2+60)
self.icons["goIcon"].setPos(pos)
self.icons["goIcon"].rotate(180)
for key, fig in self.figures.iteritems():
fig.rotate(180)
fig.moveBy(-1.5, -2)
self.pebleOppLight.setVisible(False)
self.pebleOppDark.setVisible(True)
self.pebleMyLight.setVisible(True)
self.pebleMyDark.setVisible(False)
self.myFigColor = 'light'
# self.users['dark'], self.users['light'] = (self.users['light'], self.users['dark'])
# self.users[self.users.keys()[0]], self.users[self.users.keys()[1]] = (self.users.values()[1], self.users.values()[0])
self.gameData['users'][self.gameData['users'].keys()[0]], self.gameData['users'][self.gameData['users'].keys()[1]] =\
(self.gameData['users'].values()[1], self.gameData['users'].values()[0])
print 'Users: ', self.gameData['users']
def getFiguresByAttribute(self, attr='name', value='light'):
return {k:v for k, v in self.figures.iteritems() if color in eval('v.{}'.format(name))}
def drawFigures(self):
imagesPath = os.path.dirname(__file__) + "/images/"
limitRect = self.interactiveBoardView.frameRect()
self.darkFigures = {"darkKing": ChessFigure(imagesPath + "kdt.svg", limitRect=limitRect, name='darkKing', startField='E8'),
"darkQueen": ChessFigure(imagesPath + "qdt.svg", limitRect=limitRect, name='darkQueen', startField='D8'),
"darkBishop1": ChessFigure(imagesPath + "bdt.svg", limitRect=limitRect, name='darkBishop1', startField='C8'),
"darkBishop2": ChessFigure(imagesPath + "bdt.svg", limitRect=limitRect, name='darkBishop2', startField='F8'),
"darkKnight1": ChessFigure(imagesPath + "ndt.svg", limitRect=limitRect, name='darkKnight1', startField='B8'),
"darkKnight2": ChessFigure(imagesPath + "ndt.svg", limitRect=limitRect, name='darkKnight2', startField='G8'),
"darkRook1": ChessFigure(imagesPath + "rdt.svg", limitRect=limitRect, name='darkRook1', startField='A8'),
"darkRook2": ChessFigure(imagesPath + "rdt.svg", limitRect=limitRect, name='darkRook2', startField='H8'),
"darkPawn1": ChessFigure(imagesPath + "pdt.svg", limitRect=limitRect, name='darkPawn1', startField='A7'),
"darkPawn2": ChessFigure(imagesPath + "pdt.svg", limitRect=limitRect, name='darkPawn2', startField='B7'),
"darkPawn3": ChessFigure(imagesPath + "pdt.svg", limitRect=limitRect, name='darkPawn3', startField='C7'),
"darkPawn4": ChessFigure(imagesPath + "pdt.svg", limitRect=limitRect, name='darkPawn4', startField='D7'),
"darkPawn5": ChessFigure(imagesPath + "pdt.svg", limitRect=limitRect, name='darkPawn5', startField='E7'),
"darkPawn6": ChessFigure(imagesPath + "pdt.svg", limitRect=limitRect, name='darkPawn6', startField='F7'),
"darkPawn7": ChessFigure(imagesPath + "pdt.svg", limitRect=limitRect, name='darkPawn7', startField='G7'),
"darkPawn8": ChessFigure(imagesPath + "pdt.svg", limitRect=limitRect, name='darkPawn8', startField='H7')}
self.lightFigures = {"lightKing": ChessFigure(imagesPath + "klt.svg", limitRect=limitRect, name='lightKing', startField='E1'),
"lightQueen": ChessFigure(imagesPath + "qlt.svg", limitRect=limitRect, name='lightQueen', startField='D1'),
"lightBishop1": ChessFigure(imagesPath + "blt.svg", limitRect=limitRect, name='lightBishop1', startField='C1'),
"lightBishop2": ChessFigure(imagesPath + "blt.svg", limitRect=limitRect, name='lightBishop2', startField='F1'),
"lightKnight1": ChessFigure(imagesPath + "nlt.svg", limitRect=limitRect, name='lightKnight1', startField='B1'),
"lightKnight2": ChessFigure(imagesPath + "nlt.svg", limitRect=limitRect, name='lightKnight2', startField='G1'),
"lightRook1": ChessFigure(imagesPath + "rlt.svg", limitRect=limitRect, name='lightRook1', startField='A1'),
"lightRook2": ChessFigure(imagesPath + "rlt.svg", limitRect=limitRect, name='lightRook2', startField='H1'),
"lightPawn1": ChessFigure(imagesPath + "plt.svg", limitRect=limitRect, name='lightPawn1', startField='A2'),
"lightPawn2": ChessFigure(imagesPath + "plt.svg", limitRect=limitRect, name='lightPawn2', startField='B2'),
"lightPawn3": ChessFigure(imagesPath + "plt.svg", limitRect=limitRect, name='lightPawn3', startField='C2'),
"lightPawn4": ChessFigure(imagesPath + "plt.svg", limitRect=limitRect, name='lightPawn4', startField='D2'),
"lightPawn5": ChessFigure(imagesPath + "plt.svg", limitRect=limitRect, name='lightPawn5', startField='E2'),
"lightPawn6": ChessFigure(imagesPath + "plt.svg", limitRect=limitRect, name='lightPawn6', startField='F2'),
"lightPawn7": ChessFigure(imagesPath + "plt.svg", limitRect=limitRect, name='lightPawn7', startField='G2'),
"lightPawn8": ChessFigure(imagesPath + "plt.svg", limitRect=limitRect, name='lightPawn8', startField='H2')}
self.figures = {}
self.figures.update(self.darkFigures)
self.figures.update(self.lightFigures)
for key, fig in self.figures.iteritems():
fig.setFlag(QtGui.QGraphicsItem.ItemIsMovable, True)
fig.setFlag(QtGui.QGraphicsItem.ItemIsSelectable, True)
fig.setFlag(QtGui.QGraphicsItem.ItemSendsGeometryChanges, True)
fig.setTransformOriginPoint(-fig.boundingRect().width()-4, -fig.boundingRect().height()-4)
# fig.updateCenter()
fig.setScale(0.7)
fig.setObjectName(key)
self.scene.addItem(fig)
# place figures to initial pos
for fig, figInf in self.darkFigures.iteritems():
print fig, figInf.startField
self.moveFigureToField(fig, figInf.startField)
figInf.currentField = figInf.startField
for fig, figInf in self.lightFigures.iteritems():
self.moveFigureToField(fig, figInf.startField)
figInf.currentField = figInf.startField
# print "%s - %s" % (fig, figInf['startField'])
def drawIcons(self):
imagesPath = os.path.dirname(__file__) + "/images/"
limitRect = self.interactiveBoardView.frameRect()
# rotateIcon
self.icons["rotateIcon"] = myGraphicsSvgItem(imagesPath + "rotate_square.svg",
cursorShape=QtCore.Qt.PointingHandCursor,
name="rotateIcon")
self.scene.addItem(self.icons["rotateIcon"])
iconRect = self.icons["rotateIcon"].boundingRect()
self.icons["rotateIcon"].setPos(QtCore.QPointF(limitRect.bottomRight()/2) - iconRect.bottomRight()/2)
# goIcon
self.icons["goIcon"] = myGraphicsSvgItem(imagesPath + "GO.svg",
cursorShape=QtCore.Qt.PointingHandCursor,
name="goIcon")
self.scene.addItem(self.icons["goIcon"])
iconRect = self.icons["goIcon"].boundingRect()
self.icons["goIcon"].setPos(QtCore.QPointF(limitRect.width()/2 - iconRect.width()/2, limitRect.height()/2+60))
def getItemAtMousePos_byMEvent(self, e):
posGlobal = self.mapToGlobal(e.pos())
posLocal = self.interactiveBoardView.mapFromGlobal(posGlobal)
item = self.interactiveBoardView.itemAt(posLocal)
return item
def mousePressEvent(self, QMouseEvent):
item = self.getItemAtMousePos_byMEvent(QMouseEvent)
if item is not None:
if str(item.objectName()).find('rotateIcon') == 0:
self.rotateBoard()
if str(item.objectName()).find('goIcon') == 0:
self.startNewGame()
print "Pressed: ", item.objectName()
def enableFigures(self, figures, state=True):
for fig in figures:
self.figures[fig].setFlag(QtGui.QGraphicsItem.ItemIsSelectable, state)
self.figures[fig].setFlag(QtGui.QGraphicsItem.ItemIsMovable, state)
def placeIcon(self, iconName, iconPos):
pass
def removeIcon(self, iconName):
pass
def moveFigureToField(self, figName, fldName, rndSpeed=400):
fig = self.figures[figName]
fld = self.fields[fldName]
rnd = random() * rndSpeed
if fig.pos()==QtCore.QPoint(0,0):
board_geo = self.interactiveBoardView.geometry()
rnd_x = random()*(board_geo.width()-10) + 5
rnd_y = board_geo.height() / 2.0
fig.setPos(rnd_x, rnd_y)
fig.doAnim_slideTiltFade(fld, isBecomeVisible=True, duration=2000+rnd)
else:
fig.doAnim_slideTiltFade(fld, duration=400+rnd)
def moveFigureToPos(self, figName, pos, rndSpeed=600):
fig = self.figures[figName]
rnd = random() * rndSpeed
fig.move_to_pos(pos, duration=1200+rnd)
def getFieldCenter(self, fldName):
fld = self.fields[fldName]
fldCenterOffset = QtCore.QPoint(fld.geometry().center()) - fld.pos()
return fld.mapToGlobal(fldCenterOffset)
def collectSnapPoints(self):
self.snapPoints = []
for key, fld in self.fields.iteritems():
fldCoordGlobal = self.getFieldCenter(key)
fldCoordLocal = self.interactiveBoardView.mapFromGlobal(fldCoordGlobal)
self.snapPoints.append(fldCoordLocal)
rectOpp = self.frmOpponentPark.geometry()
rectSpace = QtCore.QPoint(rectOpp.width()/9, rectOpp.height()/3)
for i in range(rectSpace.x(), 8*rectSpace.x(), rectSpace.x()):
for j in range(rectSpace.y(), rectSpace.y()*3, rectSpace.y()):
boardCoordGlob = self.frmOpponentPark.mapToGlobal(QtCore.QPoint(i, j))
fldCoordLocal = self.interactiveBoardView.mapFromGlobal(boardCoordGlob)
self.snapPoints.append(fldCoordLocal)
rectMy = self.frmMyPark.geometry()
# TODO: Complete collecting parking points for snap
# print self.snapPoints
def saveChezzMinData(self):
if self.myLogFile:
self.gameDataexport_dict = {}
figPlaces = {}
for key, item in self.figures.iteritems():
figPlaces[str(item.objectName())] = (item.pos().x(), item.pos().y())
time = strftime("%Y-%m-%d %H:%M:%S", gmtime())
self.gameData['fileDataType'] = ('pyChezzMin', 'v0.1.{}'.format(self.__build__), 'zhollosy@gmail.com')
self.gameData['gameStarted'] = time if 'gameStarted' not in self.gameData else self.gameData['gameStarted']
self.gameData['figPlaces'] = figPlaces
self.gameData['whoIsNext'] = (self.gameData['whoAmI'], self.gameData['opponentName'])[self.verticalSlider.value()]
json_data = json.dumps(self.gameData, sort_keys=True, indent=4, separators=(',', ': '))
try:
with open(self.myLogFile, "w") as log:
log.write(json_data)
finally:
print "Saved!"
return True
else:
return self.saveNewDataFile()
def loadChezzMinData(self):
json_data = {}
# if log file written and has data
if self.myLogFile and int(os.stat(self.myLogFile).st_size):
try:
with open(self.myLogFile, "r") as log:
json_data = json.loads(log.read())
except:
print "Cannot read data file!"
else:
# loaded and check if the user is part of the game
if self.whoAmI in json_data['users'].keys():
self.gameData = json_data
mySavedColor = [col for per, col in json_data['users'].iteritems() if per == self.whoAmI][0]
if mySavedColor != self.myFigColor: # flip the board if necessary
self.rotateBoard()
print 'rotated on load'
self.myFigColor = mySavedColor
# self.setGameStarted()
# turnIndex = ('ME', 'OPPONENT').index(json_data['whoIsNext'])
turnIndex = (self.whoAmI, self.opponentName).index(json_data['whoIsNext'])
# self.verticalSlider.setValue(turnIndex)
for key, value in json_data['figPlaces'].iteritems():
self.moveFigureToPos(key, QtCore.QPointF(value[0], value[1]))
print "Loaded!"
else:
print "You are not parted in this game!"
print 'Loaded: ', json_data
def main():
global pyChezzApp
app = QtGui.QApplication(sys.argv)
if pyChezzWin.__widgetInst__ is None:
pyChezzApp = pyChezzWin(app=app)
pyChezzApp.setWindowIcon(QtGui.QIcon(os.path.dirname(__file__) + "/images/pyChezz.svg"))
else:
pyChezzApp = pyChezzWin.__widgetInst__
pyChezzApp.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()