-
Notifications
You must be signed in to change notification settings - Fork 1
/
terrain_view.py
104 lines (82 loc) · 3.5 KB
/
terrain_view.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
'''
Created on Jun 4, 2013
Handles the rendering of the terrain.
@author: Collin
'''
import global_vars
import layers
import pygame
import events
import time
class TerrainView(layers.Layer, events.EventUser):
''' Handles the rendering of the terrain '''
def __init__(self, image, terrain):
self.terrain = terrain
self.image = image
self.eventManager = global_vars.eventManager
self.eventManager.subscribe(self, 'draw terrain')
self.eventManager.subscribe(self, 'impact')
layers.Layer.__init__(self, image)
def processEvent(self, e):
if e.type == 'draw terrain':
self.drawTerrain(True)
elif e.type == 'impact':
self.drawTerrain()
def getPixel(self, x, y, returnsToManager=False):
'''
Returns either white (meaning transparent) or a pixel from the
background image
'''
try:
if self.terrain.ter[y][x] == 0 and returnsToManager == False:
return global_vars.layerManager.getPixel(x, y)
elif self.terrain.ter[y][x] == 0 and returnsToManager == True:
return pygame.Color('white')
elif self.terrain.ter[y][x] == 1:
return self.image.get_at((x, y))
except:
if returnsToManager:
return pygame.Color('white')
else: return global_vars.layerManager.getPixel(x, y)
def render(self):
self.drawTerrain(True)
def drawTerrain(self, fullRedraw=False):
''' Draws terrain from info in terrain class '''
if self.terrain.changed == False and fullRedraw == False: return
pix = pygame.PixelArray(self.image.copy())
if isinstance(self.terrain.changed, pygame.Rect) and fullRedraw == False:
lowx = self.terrain.changed.left
highx=self.terrain.changed.right
lowy = self.terrain.changed.top
highy = self.terrain.changed.bottom
#print 'REDRAW'
else:
lowx = 0
highx=len(self.terrain.ter[0])
lowy = 0
highy = len(self.terrain.ter)
#print 'FULL REDRAW'
startTime = time.time()
#check to avoid indexError
if lowy < 0: lowy = 0
if lowx < 0: lowx = 0
if highy < lowy: highy = lowy + self.terrain.changed.width
if highx < lowx: highx = lowx + self.terrain.changed.height
if highy >= len(self.terrain.ter): highy = len(self.terrain.ter)
if highx >= len(self.terrain.ter[0]): highx = len(self.terrain.ter[0])
for i in range(lowy, highy):
for j in range(lowx, highx):
if i < 0 or j < 0:
return False
if self.terrain.ter[i][j] == 0: pix[j][i] = global_vars.background.get_at((j, i))
#SHOULD USE IMPLEMENTATION BELOW, but too slow
#pix[j][i] = global_vars.layerManager.getPixel(j, i)
#print 'Redraw Time: ', time.time() - startTime
pix = pix[lowx:highx, lowy:highy]
try:
news = pix.make_surface()
del pix
except: return
global_vars.window.blit(news, pygame.Rect(lowx, lowy, highx - lowx, highy - lowy))
self.terrain.changed = False
#global_vars.eventManager.post(events.Event(type='render tanks'))