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

Create moving object #17

Merged
merged 3 commits into from
May 25, 2021
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
Empty file added game/common/moving/__init__.py
Empty file.
43 changes: 43 additions & 0 deletions game/common/moving/moving_object.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from game.common.map_object import MapObject
from game.common.enums import *

# Inherits MapObject
class MovingObject(MapObject):
def __init__(self, heading=0, speed=0):
super().__init__()
# Double underscore 'name mangles' the variable. The closest to private we can get in python
self.__heading = heading
self.__speed = speed

def get_heading(self):
return self.__heading

def get_speed(self):
return self.__speed

# setter for heading. Should be degrees between 0 and 360 inclusive
def set_heading(self, val):
if val >= 0 and val <= 360:
self.__heading = val

# Set speed must be greater than 0, potential speed limit in the future?
def set_speed(self, val):
if val >= 0:
self.__speed = val

# To_json creates a dictionary representation of the object.
# super().to_json() calls MapObject.to_json(), which calls gameObject.to_json()
# This dictionary can then easily be converted to json by the game engine
def to_json(self):
data = super().to_json()
data['heading'] = self.heading
data['speed'] = self.speed

return data

# Not actually necessary, but the idea is that it takes a json representation (dictionary)
# And converts it back into an object
def from_json(self, data):
super().from_json(data)
self.heading = data['heading']
self.speed = data['speed']
4 changes: 3 additions & 1 deletion game/test_suite/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
# Simply import the class from your file, and then add that class to the '__all__' array.

from game.test_suite.tests.test_game_board import TestGameBoard
from game.test_suite.tests.test_moving_object import TestMovingObject

__all__ = [
'TestGameBoard'
'TestGameBoard',
'TestMovingObject'
]
66 changes: 66 additions & 0 deletions game/test_suite/tests/test_moving_object.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# This is a quick example test file to show you the basics.
# Always remember to add the proper details to the __init__.py file in the 'tests' folder
# to insure your tests are run.

import unittest
from game.common.moving.moving_object import MovingObject

class TestMovingObject(unittest.TestCase): # Your test class is a subclass of unittest.Testcase, this is important

def setUp(self): # This method is used to set up anything you wish to test prior to every test method below.
self.movObj = MovingObject(10, 10)

# Test if a valid heading set works
def test_set_get_heading_valid(self):
self.setUp()
self.movObj.set_heading(90)
self.assertEqual(self.movObj.get_heading(), 90)

# Test if a valid speed set works
def test_set_get_speed_valid(self):
self.setUp()
self.movObj.set_speed(100)
self.assertEqual(self.movObj.get_speed(), 100)

# Test if invalid sets don't work
def test_set_get_heading_invalid_low(self):
self.setUp()
self.movObj.set_heading(-10)
self.assertEqual(self.movObj.get_heading(), 10)

def test_set_get_speed_invalid_low(self):
self.setUp()
self.movObj.set_speed(-1)
self.assertEqual(self.movObj.get_speed(), 10)

def test_set_get_heading_invalid_high(self):
self.setUp()
self.movObj.set_heading(370)
self.assertEqual(self.movObj.get_heading(), 10)

# Check if boundary sets do work
def test_set_get_heading_boundary_high(self):
self.setUp()
self.movObj.set_heading(360)
self.assertEqual(self.movObj.get_heading(), 360)

def test_set_get_heading_boundary_low(self):
self.setUp()
self.movObj.set_heading(0)
self.assertEqual(self.movObj.get_heading(), 0)

def test_set_get_speed_boundary_low(self):
self.setUp()
self.movObj.set_speed(0)
self.assertEqual(self.movObj.get_speed(), 0)





# This is just the very basics of how to set up a test file
# For more info: https://docs.python.org/3/library/unittest.html


if __name__ == '__main__':
unittest.main