Skip to content

Commit ebbad61

Browse files
authored
Merge pull request stfc#48 from gregcorbett/point_everywhere
Use Point class consistently
2 parents 7a81eb1 + 058a248 commit ebbad61

14 files changed

+246
-113
lines changed

src/BeeBot.py

+16-16
Original file line numberDiff line numberDiff line change
@@ -98,54 +98,54 @@ def move_backward(self):
9898
# The origin is in the top left corner
9999
if self.heading == Heading.SOUTH:
100100
for _ in range(0, self.step):
101-
self.screen_location.y = self.screen_location.y - 1
101+
self.screen_location = self.screen_location - (0, 1)
102102
sleep(0.01) # sleep prevents the BeeBot moving too quickly
103-
self.logical_position.y = self.logical_position.y - 1
103+
self.logical_position = self.logical_position - (0, 1)
104104

105105
elif self.heading == Heading.WEST:
106106
for _ in range(0, self.step):
107-
self.screen_location.x = self.screen_location.x + 1
107+
self.screen_location = self.screen_location + (1, 0)
108108
sleep(0.01) # sleep prevents the BeeBot moving too quickly
109-
self.logical_position.x = self.logical_position.x + 1
109+
self.logical_position = self.logical_position + (1, 0)
110110

111111
elif self.heading == Heading.NORTH:
112112
for _ in range(0, self.step):
113-
self.screen_location.y = self.screen_location.y + 1
113+
self.screen_location = self.screen_location + (0, 1)
114114
sleep(0.01) # sleep prevents the BeeBot moving too quickly
115-
self.logical_position.y = self.logical_position.y + 1
115+
self.logical_position = self.logical_position + (0, 1)
116116

117117
elif self.heading == Heading.EAST:
118118
for _ in range(0, self.step):
119-
self.screen_location.x = self.screen_location.x - 1
119+
self.screen_location = self.screen_location - (1, 0)
120120
sleep(0.01) # sleep prevents the BeeBot moving too quickly
121-
self.logical_position.x = self.logical_position.x - 1
121+
self.logical_position = self.logical_position - (1, 0)
122122

123123
def move_forward(self):
124124
"""Move the BeeBot forward."""
125125
# The origin is in the top left corner
126126
if self.heading == Heading.NORTH:
127127
for _ in range(0, self.step):
128-
self.screen_location.y = self.screen_location.y - 1
128+
self.screen_location = self.screen_location - (0, 1)
129129
sleep(0.01) # sleep prevents the BeeBot moving too quickly
130-
self.logical_position.y = self.logical_position.y - 1
130+
self.logical_position = self.logical_position - (0, 1)
131131

132132
elif self.heading == Heading.EAST:
133133
for _ in range(0, self.step):
134-
self.screen_location.x = self.screen_location.x + 1
134+
self.screen_location = self.screen_location + (1, 0)
135135
sleep(0.01) # sleep prevents the BeeBot moving too quickly
136-
self.logical_position.x = self.logical_position.x + 1
136+
self.logical_position = self.logical_position + (1, 0)
137137

138138
elif self.heading == Heading.SOUTH:
139139
for _ in range(0, self.step):
140-
self.screen_location.y = self.screen_location.y + 1
140+
self.screen_location = self.screen_location + (0, 1)
141141
sleep(0.01) # sleep prevents the BeeBot moving too quickly
142-
self.logical_position.y = self.logical_position.y + 1
142+
self.logical_position = self.logical_position + (0, 1)
143143

144144
elif self.heading == Heading.WEST:
145145
for _ in range(0, self.step):
146-
self.screen_location.x = self.screen_location.x - 1
146+
self.screen_location = self.screen_location - (1, 0)
147147
sleep(0.01) # sleep prevents the BeeBot moving too quickly
148-
self.logical_position.x = self.logical_position.x - 1
148+
self.logical_position = self.logical_position - (1, 0)
149149

150150
def move_right(self):
151151
"""Turn the BeeBot right."""

src/Board.py

+14-10
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,22 @@ def display(self, screen):
7373
# Draw lines over Board background image
7474
if self.border_colour is not None:
7575
for iter_width in range(0, self.board_width + 1, self.step):
76-
pygame.draw.line(screen,
77-
self.border_colour,
78-
(iter_width, 0),
79-
(iter_width, self.board_height),
80-
5)
76+
77+
line_start = Point(iter_width, 0)
78+
line_end = Point(iter_width, self.board_height)
79+
80+
# Draw a line from line_start to line_end.
81+
pygame.draw.line(screen, self.border_colour,
82+
line_start, line_end, 5)
8183

8284
for iter_height in range(0, self.board_height + 1, self.step):
83-
pygame.draw.line(screen,
84-
self.border_colour,
85-
(0, iter_height),
86-
(self.board_width, iter_height),
87-
5)
85+
86+
line_start = Point(0, iter_height)
87+
line_end = Point(self.board_width, iter_height)
88+
89+
# Draw a line from line_start to line_end.
90+
pygame.draw.line(screen, self.border_colour,
91+
line_start, line_end, 5)
8892

8993
def is_equal_to(self, other_component):
9094
"""Compare this Board for equality with other_component."""

src/CommandLog.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ def _convert_memory_to_icons(self, beebot_memory):
3838
# The location to draw the first Icon in the CommandLog.
3939
location = self.screen_location
4040

41+
if self.horizontal:
42+
# Make the next Icon appear right of the current one.
43+
increment = (self.icon_size[0], 0)
44+
else:
45+
# Make the next Icon appear below the current one.
46+
increment = (0, self.icon_size[1])
47+
4148
for index, entry in enumerate(beebot_memory):
4249
# Convert Events into Icon Arrows.
4350
text = self._event_type_to_text(entry.type)
@@ -52,12 +59,8 @@ def _convert_memory_to_icons(self, beebot_memory):
5259
# that would otherwise override each other.
5360
self.add(tmp_log, key=index)
5461

55-
if self.horizontal:
56-
# Make the next Icon appear right of the current one.
57-
location = (location[0] + self.icon_size[0], location[1])
58-
else:
59-
# Make the next Icon appear below the current one.
60-
location = (location[0], location[1] + self.icon_size[1])
62+
# Put the next Icon in the right place
63+
location = location + increment
6164

6265
@classmethod
6366
def _event_type_to_text(cls, event_type):

src/Component.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def __init__(self,
2020
self.sprite = sprite
2121

2222
# The position of the Component in terms of squares on the screen
23-
self.logical_position = start_logical_position
23+
self.logical_position = start_logical_position.copy()
2424

2525
# The position of the Goal in terms pixels
2626
self.screen_location = start_logical_position.scale(step)
@@ -31,8 +31,7 @@ def __init__(self,
3131
def display(self, screen):
3232
"""Draw the Component object on screen, if it has a sprite."""
3333
if self.sprite is not None:
34-
screen.blit(self.sprite, (self.screen_location.x,
35-
self.screen_location.y))
34+
screen.blit(self.sprite, self.screen_location)
3635

3736
def is_equal_to(self, other_component):
3837
"""Compare this Component for equality with other_component."""

src/GameWindow.py

+39-35
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from src.ButtonGroup import ButtonGroup
1717
from src.CommandLog import CommandLog
1818
from src.CustomEvent import CustomEvent
19+
from src.Point import Point
1920
from src.Scenario import Scenario
2021
from src import __version__
2122

@@ -128,7 +129,7 @@ def run(self):
128129
# Display the logo (if any)
129130
if self.logo is not None:
130131
self.screen.blit(self.logo,
131-
(self.width + 69, self.height - 85))
132+
Point(self.width + 69, self.height - 85))
132133

133134
# Display any Buttons
134135
self.buttons.display(self.screen)
@@ -229,7 +230,7 @@ def choose_scenario(self):
229230
temp = Button(os.path.splitext(scenario_file)[0],
230231
GameWindow.BLACK,
231232
GameWindow.WHITE,
232-
(width_counter, height_counter),
233+
Point(width_counter, height_counter),
233234
(120, 120))
234235

235236
# Add temp Button to ButtonGroup
@@ -321,14 +322,16 @@ def load_scenario(self):
321322
# - the CommandLog below the map.
322323
self.size = (self.width + 400, self.height + 30)
323324
# Create the empty CommandLog
324-
self.command_log = CommandLog((0, self.height), (self.width, 30))
325+
self.command_log = CommandLog(Point(0, self.height),
326+
(self.width, 30))
325327
else:
326328
# Make space for:
327329
# - the Buttons below the map.
328330
# - the CommandLog to the right of the map.
329331
self.size = (self.width + 30, self.height + 400)
330332
# Create the empty CommandLog
331-
self.command_log = CommandLog((self.width, 0), (30, self.height))
333+
self.command_log = CommandLog(Point(self.width, 0),
334+
(30, self.height))
332335

333336
# Only want to do this once, so sadly can't do it in the rendering
334337
# loop without a potential race condition as
@@ -345,53 +348,53 @@ def create_buttons(self, buttons_on_the_left):
345348
forward_button = Button('Forward',
346349
GameWindow.BLACK,
347350
GameWindow.WHITE,
348-
(self.width + 140,
349-
float(self.height)/2 - 240),
351+
Point(self.width + 140,
352+
float(self.height)/2 - 240),
350353
(120, 120))
351354

352355
self.buttons.add(forward_button)
353356

354357
backward_button = Button('Backward',
355358
GameWindow.BLACK,
356359
GameWindow.WHITE,
357-
(self.width + 140,
358-
float(self.height)/2 + 20),
360+
Point(self.width + 140,
361+
float(self.height)/2 + 20),
359362
(120, 120))
360363

361364
self.buttons.add(backward_button)
362365

363366
turn_left_button = Button('Turn Left',
364367
GameWindow.BLACK,
365368
GameWindow.WHITE,
366-
(self.width + 10,
367-
float(self.height)/2 - 110),
369+
Point(self.width + 10,
370+
float(self.height)/2 - 110),
368371
(120, 120))
369372

370373
self.buttons.add(turn_left_button)
371374

372375
turn_right_button = Button('Turn Right',
373376
GameWindow.BLACK,
374377
GameWindow.WHITE,
375-
(self.width + 270,
376-
float(self.height)/2 - 110),
378+
Point(self.width + 270,
379+
float(self.height)/2 - 110),
377380
(120, 120))
378381

379382
self.buttons.add(turn_right_button)
380383

381384
go_button = Button('Go',
382385
GameWindow.BLACK,
383386
GameWindow.WHITE,
384-
(self.width + 140,
385-
float(self.height)/2 - 110),
387+
Point(self.width + 140,
388+
float(self.height)/2 - 110),
386389
(120, 120))
387390

388391
self.buttons.add(go_button)
389392

390393
stop_button = Button('Stop',
391394
GameWindow.WHITE,
392395
GameWindow.RED,
393-
(self.width + 140,
394-
float(self.height)/2 - 110),
396+
Point(self.width + 140,
397+
float(self.height)/2 - 110),
395398
(120, 120),
396399
False)
397400

@@ -400,17 +403,17 @@ def create_buttons(self, buttons_on_the_left):
400403
reset_button = Button('Reset',
401404
GameWindow.BLACK,
402405
GameWindow.WHITE,
403-
(self.width + 10,
404-
float(self.height)/2 + 20),
406+
Point(self.width + 10,
407+
float(self.height)/2 + 20),
405408
(120, 120))
406409

407410
self.buttons.add(reset_button)
408411

409412
clear_button = Button('Clear',
410413
GameWindow.BLACK,
411414
GameWindow.WHITE,
412-
(self.width + 270,
413-
float(self.height)/2 + 20),
415+
Point(self.width + 270,
416+
float(self.height)/2 + 20),
414417
(120, 120))
415418

416419
self.buttons.add(clear_button)
@@ -419,52 +422,53 @@ def create_buttons(self, buttons_on_the_left):
419422
forward_button = Button('Forward',
420423
GameWindow.BLACK,
421424
GameWindow.WHITE,
422-
(float(self.width)/2 - 60,
423-
self.height + 10),
425+
Point(float(self.width)/2 - 60,
426+
self.height + 10),
424427
(120, 120))
425428

426429
self.buttons.add(forward_button)
427430

428431
backward_button = Button('Backward',
429432
GameWindow.BLACK,
430433
GameWindow.WHITE,
431-
(float(self.width)/2 - 60,
432-
self.height + 270),
434+
Point(float(self.width)/2 - 60,
435+
self.height + 270),
433436
(120, 120))
434437

435438
self.buttons.add(backward_button)
436439

437440
turn_left_button = Button('Turn Left',
438441
GameWindow.BLACK,
439442
GameWindow.WHITE,
440-
(float(self.width)/2 - 190,
441-
self.height + 140),
443+
Point(float(self.width)/2 - 190,
444+
self.height + 140),
442445
(120, 120))
443446

444447
self.buttons.add(turn_left_button)
445448

446449
turn_right_button = Button('Turn Right',
447450
GameWindow.BLACK,
448451
GameWindow.WHITE,
449-
(float(self.width)/2 + 70,
450-
self.height + 140),
452+
Point(float(self.width)/2 + 70,
453+
self.height + 140),
451454
(120, 120))
452455

453456
self.buttons.add(turn_right_button)
454457

455458
go_button = Button('Go',
456459
GameWindow.BLACK,
457460
GameWindow.WHITE,
458-
(float(self.width)/2 - 60, self.height + 140),
461+
Point(float(self.width)/2 - 60,
462+
self.height + 140),
459463
(120, 120))
460464

461465
self.buttons.add(go_button)
462466

463467
stop_button = Button('Stop',
464468
GameWindow.WHITE,
465469
GameWindow.RED,
466-
(float(self.width)/2 - 60,
467-
self.height + 140),
470+
Point(float(self.width)/2 - 60,
471+
self.height + 140),
468472
(120, 120),
469473
False)
470474

@@ -473,17 +477,17 @@ def create_buttons(self, buttons_on_the_left):
473477
reset_button = Button('Reset',
474478
GameWindow.BLACK,
475479
GameWindow.WHITE,
476-
(float(self.width)/2 - 190,
477-
self.height + 270),
480+
Point(float(self.width)/2 - 190,
481+
self.height + 270),
478482
(120, 120))
479483

480484
self.buttons.add(reset_button)
481485

482486
clear_button = Button('Clear',
483487
GameWindow.BLACK,
484488
GameWindow.WHITE,
485-
(float(self.width)/2 + 70,
486-
self.height + 270),
489+
Point(float(self.width)/2 + 70,
490+
self.height + 270),
487491
(120, 120))
488492

489493
self.buttons.add(clear_button)

0 commit comments

Comments
 (0)