Skip to content

Commit 2fe5d02

Browse files
author
XenGi
committed
merged newer surface and tests that can blit.
1 parent 65cc854 commit 2fe5d02

File tree

2 files changed

+43
-8
lines changed

2 files changed

+43
-8
lines changed

pymlgame/surface.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ def draw_rect(self, pos, size, color, fillcolor=None, brightness=0.1):
8686

8787
def draw_circle(self, pos, radius, color, fillcolor=None, brightness=0.1):
8888
"""
89-
Draw a circle with the given color on the screen and optionally fill it with fillcolor.
89+
Draw a circle with the given color on the screen and optionally fill it
90+
with fillcolor.
9091
"""
9192
#TODO: This still produces rubbish but it's on a good way to success
9293
def dist(d, p, r):
@@ -107,6 +108,26 @@ def dist(d, p, r):
107108
for point in points:
108109
self.draw_dot(point, color, brightness)
109110

111+
def blit(self, surface, pos=(0, 0)):
112+
"""
113+
Blits a surface on this surface at pos
114+
"""
115+
for x in range(surface.width):
116+
for y in range(surface.height):
117+
px = x + pos[0]
118+
py = y + pos[1]
119+
if 0 < px < self.width and 0 < py < self.height:
120+
self.matrix[px][py] = surface.matrix[x][y]
121+
122+
def replace_color(self, before, after):
123+
"""
124+
Replaces a color on a surface with another one.
125+
"""
126+
for x in range(self.width):
127+
for y in range(self.height):
128+
if self.matrix[x][y] == before:
129+
self.matrix[x][y] = after
130+
110131
@staticmethod
111132
def get_color(color, brightness=0.1):
112133
"""

tests.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,16 @@ def test_update(self):
6868
pass
6969

7070
def test_blit(self):
71-
surface = pymlgame.Surface(int(TEST_WIDTH / 2), int(TEST_HEIGHT / 2))
72-
surface.fill(pymlgame.WHITE)
73-
self.screen.blit(surface, (0, 0))
71+
surface = pymlgame.Surface(TEST_WIDTH - 2, TEST_HEIGHT - 2)
72+
surface.fill(pymlgame.WHITE, 1.0)
73+
self.screen.blit(surface, (1, 1))
7474

75-
self.assertEqual(self.screen.matrix[int(TEST_WIDTH / 2 - 1)][int(TEST_HEIGHT / 2 - 1)],
76-
surface.get_color(pymlgame.WHITE, 0.1))
77-
self.assertEqual(self.screen.matrix[int(TEST_WIDTH / 2)][int(TEST_HEIGHT / 2)],
78-
surface.get_color(pymlgame.BLACK, 0.1))
75+
for x in range(TEST_WIDTH):
76+
for y in range(TEST_HEIGHT):
77+
if 0 < x < TEST_WIDTH - 1 and 0 < y < TEST_HEIGHT - 1:
78+
self.assertEqual(self.screen.matrix[x][y], pymlgame.WHITE)
79+
else:
80+
self.assertEqual(self.screen.matrix[x][y], pymlgame.BLACK)
7981

8082
def test_point_on_screen(self):
8183
self.assertTrue(self.screen.point_on_screen((0, 0)))
@@ -180,6 +182,18 @@ def test_draw_circle(self):
180182
radius = int(min(TEST_WIDTH, TEST_HEIGHT) / 2) - 2
181183
self.surface.draw_circle(pos, radius, pymlgame.GREEN, None, 1.0)
182184

185+
def test_blit(self):
186+
surface = pymlgame.Surface(TEST_WIDTH - 2, TEST_HEIGHT - 2)
187+
surface.fill(pymlgame.WHITE, 1.0)
188+
self.surface.blit(surface, (1, 1))
189+
190+
for x in range(TEST_WIDTH):
191+
for y in range(TEST_HEIGHT):
192+
if 0 < x < TEST_WIDTH - 1 and 0 < y < TEST_HEIGHT - 1:
193+
self.assertEqual(self.surface.matrix[x][y], pymlgame.WHITE)
194+
else:
195+
self.assertEqual(self.surface.matrix[x][y], pymlgame.BLACK)
196+
183197
def test_get_color(self):
184198
self.assertEqual(pymlgame.Surface.get_color(pymlgame.GREEN, 1.0),
185199
pymlgame.GREEN)

0 commit comments

Comments
 (0)