-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·290 lines (254 loc) · 8.93 KB
/
main.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
#!/usr/bin/python
import gtk
import gtk.gdk as gdk
import math
import random
import gobject
import sys
import signal
from Vector import Vec2d
import Sprites
import Robots
import Move
import WorldStates
import Collisions
import Server
from GameWorld import sprites, windowsize, windowloc, worldsize, addBots
import GameWorld
import cairo
if(len(sys.argv)>1):
bots = int(sys.argv[1])
addBots(bots)
servaddr = ('', 12345)
if(len(sys.argv)>2):
port = int(sys.argv[2])
servaddr = ('', int(port))
server = Server.MyServer(servaddr)
def drawScoreBoard(cr, mwidth=120):
cr.set_line_width(1)
cr.set_source_rgb(0.9,0.9,0.9)
cr.rectangle(0, 0, mwidth, mwidth)
cr.fill_preserve()
cr.set_source_rgb(0,0,0)
cr.stroke()
team_in_play = [False]*Robots.Teams.num
num_team_members = [0]*Robots.Teams.num
for sprite in sprites:
if isinstance(sprite, Robots.Robot):
team_in_play[sprite.colour] = True
num_team_members[sprite.colour] += 1
cr.set_font_size( cr.device_to_user_distance(18.0, 18.0)[0] )
teams_done = 0
for col in range(0, Robots.Teams.num):
if team_in_play[col]:
cr.move_to(mwidth/2-15, teams_done*20+20)
rgb = Robots.Teams.RGB[col]
cr.set_source_rgb(rgb[0], rgb[1], rgb[2])
t = str(num_team_members[col])
cr.show_text(t)
teams_done += 1
def drawMiniMap(cr, mwidth=120):
cr.set_line_width(1)
cr.set_source_rgb(0.9,0.9,0.9)
cr.rectangle(0, 0, mwidth, mwidth)
cr.fill_preserve()
cr.set_source_rgb(0,0,0)
cr.stroke()
scale = mwidth / float(worldsize[0]) # TODO non-square worlds
cr.scale(scale, scale)
drawSprites(cr, simple=True)
cr.transform( GameWorld.worldToWindowTransform() )
cr.set_line_width( cr.device_to_user_distance(1.0, 1.0)[0] )
cr.set_source_rgb(1,0,0)
cr.rectangle(0, 0, GameWorld.windowsize[0], GameWorld.windowsize[1])
cr.stroke()
cr.transform( GameWorld.windowToWorldTransform() )
cr.scale(1.0/scale, 1.0/scale)
def drawSprites(cr, simple=False):
for sprite in sprites:
cr.translate(sprite.position[0], sprite.position[1])
sprite.draw(cr, simple)
cr.translate(-sprite.position[0], -sprite.position[1])
# Draw the circles and update their positions.
def expose(*args):
cr = darea.window.cairo_create()
cr.set_source_rgb(.7,.7,.7)
cr.paint()
cr.transform(GameWorld.windowToWorldTransform())
cr.rectangle(0, 0, worldsize[0], worldsize[1])
cr.set_source_rgb(.85,.85,.85)
cr.fill_preserve()
cr.set_line_width( cr.device_to_user_distance(3.0, 3.0)[0] )
cr.set_source_rgb(.3,.3,.3)
cr.stroke()
drawSprites(cr, simple=(GameWorld.totalZoom()<0.2))
cr.transform(GameWorld.worldToWindowTransform())
mmwidth = 120
cr.translate(windowsize[0]-mmwidth-10, windowsize[1]-mmwidth-10)
drawMiniMap(cr, mmwidth)
cr.translate(-(windowsize[0]-mmwidth-10), -(windowsize[1]-mmwidth-10))
cr.translate(windowsize[0]-mmwidth-10, windowsize[1]-2*mmwidth-20)
drawScoreBoard(cr, mmwidth)
cr.translate(-(windowsize[0]-mmwidth-10), -(windowsize[1]-2*mmwidth-20))
newsprites = []
doneCollisions = set()
for sprite in sprites:
worldstate = WorldStates.WorldState();
for othersprite in sprites:
if othersprite==sprite:
continue
relpos = othersprite.position - sprite.position
#TODO see edge of object...
if(sprite.canSeeRelative(relpos)):
worldstate.addObjectInView( WorldStates.objectInView(othersprite, relpos) )
for othersprite in sprites:
if othersprite==sprite:
continue
collisionPair = frozenset([sprite, othersprite])
if(collisionPair in doneCollisions):
continue
doneCollisions.add(collisionPair)
relpos = othersprite.position - sprite.position
# Check for collisions
if(relpos.norm() < sprite.boundingradius + othersprite.boundingradius):
newsprite = Collisions.collide(sprite, othersprite)
# Maybe produced a new sprite
if(newsprite != None):
print "Collision of " + str(sprite) + " " + str(othersprite) + " produced " + str(newsprite)
newsprites.append(newsprite)
# Maybe this sprite got killed
breakloop = False
if(sprite.hitpoints<=0):
print str(sprite) + " was killed during its own collision step"
sprite.die()
sprites.remove(sprite)
breakloop = True
# Maybe other sprite got killed
if(othersprite.hitpoints<=0):
print str(othersprite) + " was killed during " + str(sprite) + "'s collision step"
othersprite.die()
sprites.remove(othersprite)
if(breakloop): break
move = sprite.getMove(worldstate);
if(move==Move.RESIGN):
# Don't call sprites.die() here. We assume the sprite has already killed itself.
sprites.remove(sprite)
continue
elif(move==Move.SPEED_UP):
if(sprite.speed + 1 <= sprite.maxspeed):
sprite.speed += 1
elif(move==Move.SLOW_DOWN):
if(sprite.speed - 1 >= 0):
sprite.speed -= 1
elif(move==Move.TURN_RIGHT):
sprite.direction.rotateDegrees(2)
elif(move==Move.TURN_LEFT):
sprite.direction.rotateDegrees(-2)
elif(move==Move.FIRE_MISSILE):
missile = Sprites.Missile(sprite.position + (sprite.boundingradius + Sprites.Missile.boundingradius + 10)*sprite.direction, 10, sprite.direction)
newsprites.append(missile)
elif(move==Move.FIRE_LASER):
# Speed penalty
#sprite.speed = 0
laser = Sprites.Laser(sprite.position + (sprite.boundingradius + Sprites.Missile.boundingradius + 25+1)*sprite.direction, 30, sprite.direction)
newsprites.append(laser)
elif(move>=Move.SIGNAL and move<Move.SIGNAL + Move.Signals.num):
sprite.signal = move - Move.SIGNAL
sprite.position += sprite.speed * sprite.direction
if sprite.position[0] > worldsize[0] or sprite.position[0] < 0:
sprite.direction[0] *= -1
if sprite.position[1] > worldsize[1] or sprite.position[1] < 0:
sprite.direction[1] *= -1
if(sprite.position[0] > worldsize[0]):
sprite.position[0] = worldsize[0]-1
if(sprite.position[0] < 0):
sprite.position[0] = 1
if(sprite.position[1] > worldsize[1]):
sprite.position[1] = worldsize[1]-1
if(sprite.position[1] < 0):
sprite.position[1] = 1
# Check if any dead
for sprite in sprites:
if(sprite.lifetime>=0):
sprite.age += 1
if(sprite.age > sprite.lifetime):
sprite.hitpoints = 0
if(sprite.hitpoints<=0):
sprite.die()
sprites.remove(sprite)
# Add newsprites
sprites.extend(newsprites)
# Pump server
server.Pump()
# Self-evident?
def timeout():
darea.queue_draw()
return True
def quit(arg=None):
print "Quitting"
for sprite in sprites:
sprite.gameOver()
server.Pump()
gtk.main_quit()
def quit_handler(handler, frame):
quit()
def motion_notify_event(widget, event):
x = event.x
y = event.y
state = event.state
if state & gtk.gdk.BUTTON1_MASK:
if not motion_notify_event.button_down:
# Start dragging
motion_notify_event.startCoords = x,y
motion_notify_event.button_down = True
return True
motion_notify_event.endCoords = x,y
windowloc[0] -= (motion_notify_event.endCoords[0] - motion_notify_event.startCoords[0])*GameWorld.invscale
windowloc[1] -= (motion_notify_event.endCoords[1] - motion_notify_event.startCoords[1])*GameWorld.invscale
motion_notify_event.startCoords = x,y
else:
motion_notify_event.button_down = False
if state & gtk.gdk.BUTTON2_MASK:
mwidth = 120
#x -= 0.5*windowsize[0]/float(worldsize[0]) * mwidth
#y -= 0.5*windowsize[1]/float(worldsize[1]) * mwidth
#TODO proper dragging of the red square
windowloc[0] = (x - (windowsize[0]-mwidth)) / float(mwidth) * worldsize[0]
windowloc[1] = (y - (windowsize[1]-mwidth)) / float(mwidth) * worldsize[1]
return True
return True
motion_notify_event.button_down = False
motion_notify_event.startCoords = 0,0
motion_notify_event.endCoords = 0,0
def configure_event(widget, event):
newrect = darea.get_allocation()
windowsize[0] = newrect[2]
windowsize[1] = newrect[3]
return True
def scroll_event(widget, event):
x = event.x
y = event.y
if event.direction == gtk.gdk.SCROLL_UP:
GameWorld.zoomIn((x,y))
elif event.direction == gtk.gdk.SCROLL_DOWN:
GameWorld.zoomOut((x,y))
GameWorld.moveWindowTowardsWorldCoords(darea.window.cairo_create(), (GameWorld.worldsize[0]/2.0, GameWorld.worldsize[1]/2.0))
return True
# Catch SIGINT
signal.signal(signal.SIGINT, quit_handler)
# Initialize the window.
window = gtk.Window()
window.resize(windowsize[0], windowsize[1])
window.connect("destroy", quit)
darea = gtk.DrawingArea()
darea.connect("configure-event", configure_event)
darea.connect("expose-event", expose)
darea.connect("motion_notify_event", motion_notify_event)
darea.connect("scroll_event", scroll_event)
darea.set_events(gtk.gdk.EXPOSURE_MASK | gtk.gdk.POINTER_MOTION_MASK | gtk.gdk.BUTTON_PRESS_MASK | gtk.gdk.SCROLL_MASK)
window.add(darea)
window.show_all()
# Self-evident?
#gobject.idle_add(timeout)
gobject.timeout_add(25, timeout)
gtk.main()