Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Point class consistently #48

Merged
merged 13 commits into from
May 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions src/BeeBot.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,54 +98,54 @@ def move_backward(self):
# The origin is in the top left corner
if self.heading == Heading.SOUTH:
for _ in range(0, self.step):
self.screen_location.y = self.screen_location.y - 1
self.screen_location = self.screen_location - (0, 1)
sleep(0.01) # sleep prevents the BeeBot moving too quickly
self.logical_position.y = self.logical_position.y - 1
self.logical_position = self.logical_position - (0, 1)

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

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

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

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

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

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

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

def move_right(self):
"""Turn the BeeBot right."""
Expand Down
24 changes: 14 additions & 10 deletions src/Board.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,22 @@ def display(self, screen):
# Draw lines over Board background image
if self.border_colour is not None:
for iter_width in range(0, self.board_width + 1, self.step):
pygame.draw.line(screen,
self.border_colour,
(iter_width, 0),
(iter_width, self.board_height),
5)

line_start = Point(iter_width, 0)
line_end = Point(iter_width, self.board_height)

# Draw a line from line_start to line_end.
pygame.draw.line(screen, self.border_colour,
line_start, line_end, 5)

for iter_height in range(0, self.board_height + 1, self.step):
pygame.draw.line(screen,
self.border_colour,
(0, iter_height),
(self.board_width, iter_height),
5)

line_start = Point(0, iter_height)
line_end = Point(self.board_width, iter_height)

# Draw a line from line_start to line_end.
pygame.draw.line(screen, self.border_colour,
line_start, line_end, 5)

def is_equal_to(self, other_component):
"""Compare this Board for equality with other_component."""
Expand Down
15 changes: 9 additions & 6 deletions src/CommandLog.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ def _convert_memory_to_icons(self, beebot_memory):
# The location to draw the first Icon in the CommandLog.
location = self.screen_location

if self.horizontal:
# Make the next Icon appear right of the current one.
increment = (self.icon_size[0], 0)
else:
# Make the next Icon appear below the current one.
increment = (0, self.icon_size[1])

for index, entry in enumerate(beebot_memory):
# Convert Events into Icon Arrows.
text = self._event_type_to_text(entry.type)
Expand All @@ -52,12 +59,8 @@ def _convert_memory_to_icons(self, beebot_memory):
# that would otherwise override each other.
self.add(tmp_log, key=index)

if self.horizontal:
# Make the next Icon appear right of the current one.
location = (location[0] + self.icon_size[0], location[1])
else:
# Make the next Icon appear below the current one.
location = (location[0], location[1] + self.icon_size[1])
# Put the next Icon in the right place
location = location + increment

@classmethod
def _event_type_to_text(cls, event_type):
Expand Down
5 changes: 2 additions & 3 deletions src/Component.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def __init__(self,
self.sprite = sprite

# The position of the Component in terms of squares on the screen
self.logical_position = start_logical_position
self.logical_position = start_logical_position.copy()

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

def is_equal_to(self, other_component):
"""Compare this Component for equality with other_component."""
Expand Down
74 changes: 39 additions & 35 deletions src/GameWindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from src.ButtonGroup import ButtonGroup
from src.CommandLog import CommandLog
from src.CustomEvent import CustomEvent
from src.Point import Point
from src.Scenario import Scenario
from src import __version__

Expand Down Expand Up @@ -128,7 +129,7 @@ def run(self):
# Display the logo (if any)
if self.logo is not None:
self.screen.blit(self.logo,
(self.width + 69, self.height - 85))
Point(self.width + 69, self.height - 85))

# Display any Buttons
self.buttons.display(self.screen)
Expand Down Expand Up @@ -229,7 +230,7 @@ def choose_scenario(self):
temp = Button(os.path.splitext(scenario_file)[0],
GameWindow.BLACK,
GameWindow.WHITE,
(width_counter, height_counter),
Point(width_counter, height_counter),
(120, 120))

# Add temp Button to ButtonGroup
Expand Down Expand Up @@ -321,14 +322,16 @@ def load_scenario(self):
# - the CommandLog below the map.
self.size = (self.width + 400, self.height + 30)
# Create the empty CommandLog
self.command_log = CommandLog((0, self.height), (self.width, 30))
self.command_log = CommandLog(Point(0, self.height),
(self.width, 30))
else:
# Make space for:
# - the Buttons below the map.
# - the CommandLog to the right of the map.
self.size = (self.width + 30, self.height + 400)
# Create the empty CommandLog
self.command_log = CommandLog((self.width, 0), (30, self.height))
self.command_log = CommandLog(Point(self.width, 0),
(30, self.height))

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

self.buttons.add(forward_button)

backward_button = Button('Backward',
GameWindow.BLACK,
GameWindow.WHITE,
(self.width + 140,
float(self.height)/2 + 20),
Point(self.width + 140,
float(self.height)/2 + 20),
(120, 120))

self.buttons.add(backward_button)

turn_left_button = Button('Turn Left',
GameWindow.BLACK,
GameWindow.WHITE,
(self.width + 10,
float(self.height)/2 - 110),
Point(self.width + 10,
float(self.height)/2 - 110),
(120, 120))

self.buttons.add(turn_left_button)

turn_right_button = Button('Turn Right',
GameWindow.BLACK,
GameWindow.WHITE,
(self.width + 270,
float(self.height)/2 - 110),
Point(self.width + 270,
float(self.height)/2 - 110),
(120, 120))

self.buttons.add(turn_right_button)

go_button = Button('Go',
GameWindow.BLACK,
GameWindow.WHITE,
(self.width + 140,
float(self.height)/2 - 110),
Point(self.width + 140,
float(self.height)/2 - 110),
(120, 120))

self.buttons.add(go_button)

stop_button = Button('Stop',
GameWindow.WHITE,
GameWindow.RED,
(self.width + 140,
float(self.height)/2 - 110),
Point(self.width + 140,
float(self.height)/2 - 110),
(120, 120),
False)

Expand All @@ -400,17 +403,17 @@ def create_buttons(self, buttons_on_the_left):
reset_button = Button('Reset',
GameWindow.BLACK,
GameWindow.WHITE,
(self.width + 10,
float(self.height)/2 + 20),
Point(self.width + 10,
float(self.height)/2 + 20),
(120, 120))

self.buttons.add(reset_button)

clear_button = Button('Clear',
GameWindow.BLACK,
GameWindow.WHITE,
(self.width + 270,
float(self.height)/2 + 20),
Point(self.width + 270,
float(self.height)/2 + 20),
(120, 120))

self.buttons.add(clear_button)
Expand All @@ -419,52 +422,53 @@ def create_buttons(self, buttons_on_the_left):
forward_button = Button('Forward',
GameWindow.BLACK,
GameWindow.WHITE,
(float(self.width)/2 - 60,
self.height + 10),
Point(float(self.width)/2 - 60,
self.height + 10),
(120, 120))

self.buttons.add(forward_button)

backward_button = Button('Backward',
GameWindow.BLACK,
GameWindow.WHITE,
(float(self.width)/2 - 60,
self.height + 270),
Point(float(self.width)/2 - 60,
self.height + 270),
(120, 120))

self.buttons.add(backward_button)

turn_left_button = Button('Turn Left',
GameWindow.BLACK,
GameWindow.WHITE,
(float(self.width)/2 - 190,
self.height + 140),
Point(float(self.width)/2 - 190,
self.height + 140),
(120, 120))

self.buttons.add(turn_left_button)

turn_right_button = Button('Turn Right',
GameWindow.BLACK,
GameWindow.WHITE,
(float(self.width)/2 + 70,
self.height + 140),
Point(float(self.width)/2 + 70,
self.height + 140),
(120, 120))

self.buttons.add(turn_right_button)

go_button = Button('Go',
GameWindow.BLACK,
GameWindow.WHITE,
(float(self.width)/2 - 60, self.height + 140),
Point(float(self.width)/2 - 60,
self.height + 140),
(120, 120))

self.buttons.add(go_button)

stop_button = Button('Stop',
GameWindow.WHITE,
GameWindow.RED,
(float(self.width)/2 - 60,
self.height + 140),
Point(float(self.width)/2 - 60,
self.height + 140),
(120, 120),
False)

Expand All @@ -473,17 +477,17 @@ def create_buttons(self, buttons_on_the_left):
reset_button = Button('Reset',
GameWindow.BLACK,
GameWindow.WHITE,
(float(self.width)/2 - 190,
self.height + 270),
Point(float(self.width)/2 - 190,
self.height + 270),
(120, 120))

self.buttons.add(reset_button)

clear_button = Button('Clear',
GameWindow.BLACK,
GameWindow.WHITE,
(float(self.width)/2 + 70,
self.height + 270),
Point(float(self.width)/2 + 70,
self.height + 270),
(120, 120))

self.buttons.add(clear_button)
Expand Down
Loading