-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTronWindow.py
112 lines (95 loc) · 4.34 KB
/
TronWindow.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
# Copyright 2013 Steph Kraemer.
# This file is part of TronBot.
# TronBot is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# TronBot is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with TronBot. If not, see <http://www.gnu.org/licenses/>.
from PyQt4 import QtGui
from PyQt4 import QtCore
from LightCycle import *
import Dot
import Direction
class TronWindow(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.size = (25,25)
self.resize(self.size[0]*Dot.PIXELS_PER_SQUARE,self.size[1]*Dot.PIXELS_PER_SQUARE)
self.startButton = QtGui.QPushButton('Start', self)
self.startButton.clicked.connect(self.startGame)
self.startButton.move(self.width()/2 - self.startButton.width()/2,
self.height()/2 - self.startButton.height()/2)
self.show()
def startGame(self):
self.startButton.hide()
self.grabKeyboard()
self.bots = [LightCycle('Blue', self, QtCore.Qt.blue,
Direction.add(self.size, (-1, -1)), Direction.Up)]
self.humanPlayer = LightCycle('Red', self, QtCore.Qt.red, (0,0), Direction.Down)
self.lightCycles = [self.humanPlayer] + self.bots
self.deadLightCycles = []
for lightCycle in self.bots:
lightCycle.computeNextDirection()
self.timer = QtCore.QTimer(self)
self.timer.timeout.connect(self.onTimer)
self.timer.start(200)
def onTimer(self):
numKilled = 0
for lightCycle in self.lightCycles:
lightCycle.move()
position = lightCycle.getHeadPosition()
if not self.checkBounds(position):
numKilled = numKilled + 1
self.deadLightCycles.append(lightCycle)
else:
collidingCycle = self.checkCollision(position)
if collidingCycle != None:
numKilled = numKilled + 1
self.deadLightCycles.append(lightCycle)
if lightCycle != collidingCycle and \
lightCycle.getHeadPosition() == collidingCycle.getHeadPosition():
numKilled = numKilled + 1
self.deadLightCycles.append(collidingCycle)
else:
lightCycle.occupiedSpaces[position[0]][position[1]] = True
for i in xrange(numKilled):
self.lightCycles.remove(self.deadLightCycles[-i-1])
if len(self.lightCycles) == 1:
self.timer.stop()
print 'Game over. Winner is ' + self.lightCycles[0].name
elif len(self.lightCycles) == 0:
self.timer.stop()
print 'Tie game: winners are:'
for i in xrange(numKilled):
print self.deadLightCycles[-i-1].name
else:
for lightCycle in self.bots:
lightCycle.computeNextDirection()
def keyPressEvent(self, keyEvent):
if self.humanPlayer != None and keyEvent.type() == QtCore.QEvent.KeyPress:
self.humanPlayer.setDirection( {
QtCore.Qt.Key_Up : Direction.Up,
QtCore.Qt.Key_Down : Direction.Down,
QtCore.Qt.Key_Left : Direction.Left,
QtCore.Qt.Key_Right : Direction.Right }.get(keyEvent.key(), None) )
def checkCollision(self, position):
for otherCycle in self.lightCycles + self.deadLightCycles:
if otherCycle.occupiedSpaces[position[0]][position[1]]:
return otherCycle
return None
def checkBounds(self, position):
if position[0] < 0 or position[1] < 0 or position[0] >= self.size[0] or \
position[1] >= self.size[1]:
return False
else:
return True
def checkPosition(self, position):
if self.checkBounds(position) and self.checkCollision(position) == None:
return True
else:
return False