-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.py
More file actions
executable file
·528 lines (429 loc) · 19.4 KB
/
start.py
File metadata and controls
executable file
·528 lines (429 loc) · 19.4 KB
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
#!/usr/bin/env python
# Filename: start.py
# vim:set sts=4 et sw=4 ts=4 ci ai:
"""
Open Bridge Builder -- a python+pygame based remake of Bridge Builder
Copyright 2008 Camillo Dell'mour (cdellmour@gmail.com)
This program 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 2 of the License, or (at
your option) any later version.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
USA
"""
import pygame
import sys
import os
import logging
from pygame.locals import *
logging.basicConfig(level=logging.DEBUG)
###############################################################################
#############################Game Class Muhaha!################################
###############################################################################
class Game:
FRAME_RATE = 60
SCREEN_W = 640
SCREEN_H = 480
def __init__(self):
self.logger = logging.getLogger('GameLogger')
self.logger.setLevel(logging.DEBUG)
self.running = False
self.quit = False
self.zoomfactor = 1.00
self.zoomchanged = True
self.zoompoint = 0, 0
self.InZoom = False
self.gravity = 9.80665
self.c_rect = None
self.t_rect = None
self.begintraeger = False
self.catchpoint = (0,0)
self.endcatchpoint = (0,0)
self.drawcatchpoint = False
self.colorcatchpoint = (0,0,0)
self.buttondown = False
self.simulatemode = 0
pygame.init()
if not pygame.font:
logger.warning( 'fonts disabled' )
if not pygame.mixer:
logger.warning( 'sound disabled' )
size = Game.SCREEN_W, Game.SCREEN_H
#self.window = pygame.display.set_mode(size, pygame.FULLSCREEN)
self.window = pygame.display.set_mode(size)
pygame.display.set_caption('Open Bridge Builder')
self.screen = pygame.display.get_surface()
self.background = pygame.Surface(size).convert()
self.backgroundgrid = BackgroundGrid()
self.clock = pygame.time.Clock()
# Set Start Funktions #
self.Change = False
self.GetInputfn = self.CommonGetInput
self.Movefn = self.CommonMove
self.CollisionDetectionfn = self.CommonCollisonDetection
self.Drawfn = self.CommonDraw
# Set Test Sprite #
self.allSprites = pygame.sprite.Group()
self.testtraeger = Traeger(self,2 , 2, 3, 3)
test2 = Traeger(self,5 , 1, 8, 1)
test3 = Traeger(self,5 , 1, 5, 5)
test4 = Traeger(self,8 , 1, 8, 5)
test5 = Traeger(self,5 , 5, 8, 5)
test6 = Traeger(self,5 , 5, 8, 1)
test7 = Traeger(self,5, 1, 8, 5)
#self.allSprites.add(self.testtraeger)
#self.allSprites.add(test2)
#self.allSprites.add(test3)
#self.allSprites.add(test4)
#self.allSprites.add(test5)
#self.allSprites.add(test6)
#self.allSprites.add(test7)
self.isDirtyGroup = pygame.sprite.Group()
self.isVisibleGroup = pygame.sprite.Group()
def common_event(self,event):
if event.type == pygame.QUIT or \
(event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
self.logger.debug ("Exit the Programm")
self.quit = True
else:
self.logger.debug (event)
def CommonGetInput(self):
for event in pygame.event.get():
if event.type == MOUSEBUTTONDOWN:
if event.button == 4:
self.zoomfactor += 0.05
self.zoompoint = event.pos
self.zoomchanged = True
self.InZoom = True
elif event.button == 5:
if self.zoomfactor > 0.4:
self.zoomfactor -= 0.05
self.zoompoint = event.pos
self.zoomchanged = True
elif event.button == 1:
if self.drawcatchpoint == True:
self.traegerpos = self.catchpoint
self.begintraeger = True
else:
self.common_event(event)
elif event.type == MOUSEBUTTONUP:
if event.button == 1:
if self.begintraeger == True:
self.buttondown = False
self.allSprites.add(Traeger(self,self.upos[0] , self.upos[1], self.endupos[0], self.endupos[1]))
else:
self.common_event(event)
elif event.type == MOUSEMOTION:
if event.buttons[2] == 1 or event.buttons[1]:
self.backgroundgrid.translate(event.rel)
self.zoomchanged = True
elif event.buttons[0] == 1:
tmp = self.backgroundgrid.InGridCatch(event.pos)
isIn = tmp[0]
pos = tmp[1]
col = tmp[2]
upos = tmp[3]
if isIn == True and self.begintraeger == True:
self.endcatchpoint = pos
self.drawcatchpoint = True
self.colorcatchpoint = col
self.endupos = upos
self.buttondown = True
else:
self.drawcatchpoint = False
else:
tmp = self.backgroundgrid.InGridCatch(event.pos)
isIn = tmp[0]
pos = tmp[1]
col = tmp[2]
upos = tmp[3]
if isIn == True:
self.catchpoint = pos
self.drawcatchpoint = True
self.colorcatchpoint = col
self.upos = upos
else:
self.drawcatchpoint = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
self.simulatemode = ~self.simulatemode
self.logger.debug(self.simulatemode)
else:
self.common_event(event)
else:
self.common_event(event)
def CommonMove(self):
self.allSprites.update()
def CommonCollisonDetection(self):
pass
def CommonDraw(self):
if self.zoomchanged == True:
self.background.fill((0,0,0))
self.background.blit(self.backgroundgrid\
.getGrid(self.zoomfactor, \
self.zoompoint , \
self.InZoom),[0,0])
self.screen.blit(self.background,[0,0])
self.allSprites.draw(self.screen)
self.zoomchanged = False
self.InZoom = False
else:
if self.c_rect != None:
self.screen.blit(self.background, self.c_rect, self.c_rect)
if self.t_rect != None:
self.screen.blit(self.background, self.t_rect, self.t_rect)
if self.buttondown == True:
self.t_rect = pygame.draw.line(self.screen, (0,255,0), self.catchpoint,self.endcatchpoint)
if self.drawcatchpoint == True:
self.c_rect = pygame.draw.circle(self.screen, self.colorcatchpoint , self.catchpoint, 4 * self.zoomfactor)
self.allSprites.clear(self.screen, self.background)
self.allSprites.draw(self.screen)# sollte mit dirty sein
pygame.display.flip()
def Changefn(self):
if not self.Change:
return
self.logger.debug("Change:::")
def run(self):
"""Main Game Loop"""
self.running = True
while not self.quit:
self.GetInputfn()
self.Movefn()
self.CollisionDetectionfn()
self.Drawfn()
self.Changefn()
self.clock.tick(Game.FRAME_RATE)
pygame.quit()
###############################################################################
############################Background Grid####################################
###############################################################################
class BackgroundGrid:
UNITS_X = 100
UNITS_Y = 50
pUNITSIZE_X = 50
pUNITSIZE_Y = 50
COLOR = 130, 130, 130
COLOR_SMALL = 70,70,70
rUNITSIZE_X = 5 # ein Unit = 5 meter
rUNITSIZE_Y = 5
def __init__(self):
self.pfirstBendx = -400
self.pfirstBendy = -200 #p for Pixel u for Unit
self.zoomfactor = 1.00
self.zoompoint = 0,0
self.backgroundgrid = pygame.Surface((Game.SCREEN_W, Game.SCREEN_H))\
.convert()
def translate(self, tr):
self.pfirstBendx += tr[0]
self.pfirstBendy += tr[1]
def getGrid(self, zoomfactor, zoompoint, InZoom ):
self.zoomfactor = zoomfactor
self.zoompoint = zoompoint
self.pzoompointX = self.zoompoint[0]
self.pzoompointY = self.zoompoint[1]
if InZoom == True:
tmpzoom = self.zoomfactor - 0.05
else:
tmpzoom = self.zoomfactor
pDX = -self.pfirstBendx + self.pzoompointX
self.uzoompointX = pDX / (BackgroundGrid.pUNITSIZE_X * tmpzoom)
pDY = -self.pfirstBendy + self.pzoompointY
self.uzoompointY = pDY / (BackgroundGrid.pUNITSIZE_Y * tmpzoom)
logging.debug(self.uzoompointX)
logging.debug(self.uzoompointY)
if InZoom == True:
self.pfirstBendx = self.pzoompointX - (pDX + pDX * 0.05)
self.pfirstBendy = self.pzoompointY - (pDY + pDY * 0.05)
#else:
# self.pfirstBendx = self.pzoompointX - (pDX - pDX * 0.025)
# self.pfirstBendy = self.pzoompointY - (pDY - pDY * 0.025)
self.backgroundgrid = pygame.Surface((Game.SCREEN_W, Game.SCREEN_H))\
.convert()
for i in range(0,BackgroundGrid.UNITS_X):
pX = self.pfirstBendx + i * BackgroundGrid.pUNITSIZE_X * zoomfactor
if pX > 0 and pX < Game.SCREEN_W:
pygame.draw.line(self.backgroundgrid, \
BackgroundGrid.COLOR, \
(pX,0),(pX,Game.SCREEN_H),2 )
for j in range (1, 5, 1):
X = pX - j * 0.2 * BackgroundGrid.pUNITSIZE_X * zoomfactor
pygame.draw.line(self.backgroundgrid, \
BackgroundGrid.COLOR_SMALL, \
(X ,0),(X,Game.SCREEN_H),1 )
for i in range(0,BackgroundGrid.UNITS_Y):
pY = self.pfirstBendy + i * BackgroundGrid.pUNITSIZE_Y * zoomfactor
if pY > 0 and pY < Game.SCREEN_H:
pygame.draw.line(self.backgroundgrid, \
BackgroundGrid.COLOR, \
(0,pY),(Game.SCREEN_W,pY),2 )
for j in range (1, 5, 1):
Y = pY - j * 0.2 * BackgroundGrid.pUNITSIZE_Y * zoomfactor
pygame.draw.line(self.backgroundgrid, \
BackgroundGrid.COLOR_SMALL, \
(0,Y),(Game.SCREEN_W,Y),1 )
return self.backgroundgrid
def GetBeamRect(self, t):
tmp_rect = t.rect
tmp_rect.topleft = self.pfirstBendx \
+ min(t.ustartx, t.uendx) \
* BackgroundGrid.pUNITSIZE_X \
* self.zoomfactor \
, self.pfirstBendy \
+ min(t.ustarty, t.uendy) \
* BackgroundGrid.pUNITSIZE_Y \
* self.zoomfactor
tmp_rect.width = t.uwidth \
* BackgroundGrid.pUNITSIZE_X \
* self.zoomfactor
tmp_rect.height = t.uheight \
* self.zoomfactor
return tmp_rect
def GetImage(self,t):
if t.uwidth == 0:
image = pygame\
.Surface([Traeger.T_HEIGHT * t.game.zoomfactor \
, t.uheight * BackgroundGrid.pUNITSIZE_Y * t.game.zoomfactor]) \
.convert()
image.fill((0,255,0))
return image
if t.uheight == 0:
image = pygame\
.Surface([t.uwidth * BackgroundGrid.pUNITSIZE_X * t.game.zoomfactor \
, Traeger.T_HEIGHT * t.game.zoomfactor]) \
.convert()
image.fill((0,255,0))
return image
image = pygame\
.Surface([t.uwidth * BackgroundGrid.pUNITSIZE_X * t.game.zoomfactor \
, t.uheight * BackgroundGrid.pUNITSIZE_Y * t.game.zoomfactor]) \
.convert()
image.set_colorkey((0,0,0))
image.fill((0,0,0))
if(t.ustartx < t.uendx and t.ustarty < t.uendy) or (t.ustartx > t.uendx and t.ustarty > t.uendy):
pygame.draw.line(image, (0,255,0) ,(0,0),(image.get_rect().width, image.get_rect().height), 5)
else:
pygame.draw.line(image, (0,255,0) ,(0,image.get_rect().height),(image.get_rect().width,0 ), 5)
return image
def InGridCatch(self,ppos):
pposX = ppos[0]
pposY = ppos[1]
pDX = -self.pfirstBendx + pposX
uposX = pDX / (BackgroundGrid.pUNITSIZE_X * self.zoomfactor)
pDY = -self.pfirstBendy + pposY
uposY = pDY / (BackgroundGrid.pUNITSIZE_Y * self.zoomfactor)
ruposX = round(uposX,0)
ruposY = round(uposY,0)
ret = (False,(0,0),(0,0,0), (0,0))
if (ruposX + 0.1) > uposX and (ruposX - 0.1) < uposX and (ruposY + 0.1) > uposY and (ruposY - 0.1) < uposY:
pX = self.pfirstBendx + ruposX * BackgroundGrid.pUNITSIZE_X * self.zoomfactor
pY = self.pfirstBendy + ruposY * BackgroundGrid.pUNITSIZE_Y * self.zoomfactor
ret = (True,(pX,pY),(255,0,0), (ruposX,ruposY))
return ret
for i in range(-2,3):
for j in range(-2,3):
if (ruposX + (i * 0.2) + 0.05) > uposX \
and (ruposX + (i * 0.2) - 0.05) < uposX \
and (ruposY + (j * 0.2) + 0.05) > uposY \
and (ruposY + (j * 0.2) - 0.05) < uposY:
pX = self.pfirstBendx + (ruposX + i * 0.2) * BackgroundGrid.pUNITSIZE_X * self.zoomfactor
pY = self.pfirstBendy + (ruposY + j * 0.2) * BackgroundGrid.pUNITSIZE_Y * self.zoomfactor
ret = (True,(pX,pY),(0,255,0),(ruposX + i * 0.2,ruposY + j * 0.2))
break
return ret
###############################################################################
################################Traeger########################################
###############################################################################
class Traeger(pygame.sprite.DirtySprite):
T_HEIGHT = 5
def __init__(self, game, ustartx, ustarty, uendx, uendy):
pygame.sprite.DirtySprite.__init__(self)
self.game = game
self.ustartx = ustartx
self.ustarty = ustarty
self.uendx = uendx
self.uendy = uendy
self.uwidth = abs ( max(ustartx,uendx) - min(ustartx, uendx) )
self.uheight = abs ( max(ustarty,uendy) - min(ustarty, uendy) )
self.image = game.backgroundgrid.GetImage(self)
self.rect = self.image.get_rect()
self.rect = game.backgroundgrid.GetBeamRect(self)
self.drehimpuls = 0
self.dichte = 7,874 #g/cm3 = kg/dm3
self.traegheitstensor = ( ((self.dichte*1000) * (16.0/3.0) * (self.uwidth / BackgroundGrid.rUNITSIZE_X)**5, 0) ,\
(0 , (self.dichte*1000) * (16.0/3.0) * (self.uheight / BackgroundGrid.rUNITSIZE_X)**5 ) )
self.winkelgeschwindigkeit = 0
self.geschwindigkeitvektor = [0,0] #(m/s, m/s)
self.oldtickets = pygame.time.get_ticks()
self.newtickets = pygame.time.get_ticks()
self.pasttime = self.newtickets - self.oldtickets
game.logger.debug(self.image)
game.logger.debug(self.rect)
def update(self):
#self.y += 0.02
#if self.y > Game.SCREEN_H:
# self.y = 0
tmp_r = self.rect
self.image = game.backgroundgrid.GetImage(self)
self.rect = game.backgroundgrid.GetBeamRect(self)
#self.game.logger.debug(self.image)
#self.game.logger.debug(self.rect)
if(tmp_r != self.rect):
self.game.logger.debug(self.image)
self.game.logger.debug(self.rect)
if self.game.simulatemode != 0:
self.newtickets = pygame.time.get_ticks()
self.pasttime = self.newtickets - self.oldtickets
if self.pasttime > 0:
sec = self.pasttime / 1000.0
speed = self.game.gravity * sec # m/s
self.geschwindigkeitvektor[1] += speed
dx = ( self.geschwindigkeitvektor[0] / BackgroundGrid.rUNITSIZE_X ) * sec
dy = ( self.geschwindigkeitvektor[1] / BackgroundGrid.rUNITSIZE_Y ) * sec
self.game.logger.debug( self.geschwindigkeitvektor[1])
self.ustartx += dx
self.uendx += dx
self.ustarty += dy
self.uendy += dy
self.uwidth = abs ( max(self.ustartx,self.uendx) - min(self.ustartx, self.uendx) )
self.uheight = abs ( max(self.ustarty,self.uendy) - min(self.ustarty, self.uendy) )
self.oldtickets = self.newtickets
else:
self.newtickets = pygame.time.get_ticks()
self.oldtickets = self.newtickets
#self.game.isDirtyGroup.add(self)
#self.game.logger.debug(self.rect.width)
###############################################################################
##############################Global Functions#################################
###############################################################################
def load_sound(name):
class NoneSound:
def play(self,n=1): pass
def stop(self): pass
if not pygame.mixer:
return NoneSound()
#return NoneSound()
fullname = os.path.join('data',name)
try:
sound = pygame.mixer.Sound(fullname)
except pygame.error, message:
print "Error: can not load sound: ", fullname
raise SystemExit, message
return sound
def load_image(name):
fullname = os.path.join("data", name)
img = pygame.image.load(fullname)
return img.convert(img)
###############################################################################
#############################Beginn of Evil :-)################################
###############################################################################
if __name__=='__main__':
logging.info ("Programm gestartet")
game = Game()
game.run()
logging.info ("Programm beendet")