From a7da0723370dec0ba64cbbe8fef3c9579ffc37b2 Mon Sep 17 00:00:00 2001 From: Erick Bickler <60521336+erickbickler@users.noreply.github.com> Date: Sun, 30 May 2021 16:39:28 -0500 Subject: [PATCH] Create shooter object (#25) * Created moving object * Added number checking to moving object, and tests. Fixed from_json for moving object * Added some comments * add player object * added proper super init call and finish to and from json methods * reformatting code to align with pep8 * reformatting more code to align with pep8 * changed player to shooter, added list generation for visible and inventory to_json * add to_json call for visible list in to_json() * change to collidable = True upon further thought, it's better than a comment Co-authored-by: Sean Hagen <49661807+HagenSR@users.noreply.github.com> --- game/common/enums.py | 10 ++-- game/common/moving/moving_object.py | 2 +- game/common/moving/shooter.py | 54 +++++++++++++++++++++ game/common/stats.py | 5 +- game/test_suite/tests/test_moving_object.py | 15 +++--- 5 files changed, 73 insertions(+), 13 deletions(-) create mode 100644 game/common/moving/shooter.py diff --git a/game/common/enums.py b/game/common/enums.py index 0cd31962..73880954 100644 --- a/game/common/enums.py +++ b/game/common/enums.py @@ -10,9 +10,11 @@ class ObjectType: action = 1 player = 2 game_board = 3 - map_object = 4 - item = 5 - gun = 6 + shooter = 4 + map_object = 5 + item = 6 + gun = 7 + class GunType: none = 0 @@ -21,12 +23,14 @@ class GunType: shotgun = 3 sniper = 4 + class GunLevel: level_zero = 0 level_one = 1 level_two = 2 level_three = 3 + class ShotPattern: none = 0 single = 1 diff --git a/game/common/moving/moving_object.py b/game/common/moving/moving_object.py index af6906fe..b9c1bc2a 100644 --- a/game/common/moving/moving_object.py +++ b/game/common/moving/moving_object.py @@ -19,7 +19,7 @@ def get_speed(self): # setter for heading. Should be degrees between 0 and 360 inclusive def set_heading(self, val): - if val >= 0 and val <= 360: + if 0 <= val <= 360: self.__heading = val # Set speed must be greater than 0 diff --git a/game/common/moving/shooter.py b/game/common/moving/shooter.py new file mode 100644 index 00000000..dcef5bd5 --- /dev/null +++ b/game/common/moving/shooter.py @@ -0,0 +1,54 @@ +from game.common.moving.moving_object import MovingObject +from game.common.stats import GameStats +from game.common.enums import * + + +class Shooter(MovingObject): + def __init__(self, heading=0, speed=0, coordinates=GameStats.player_stats['starting_coordinates']): + super().__init__( + heading, + speed, + GameStats.player_stats['starting_health'], + coordinates, + GameStats.player_stats['hitbox'], + collidable = True + ) + self.object_type = ObjectType.shooter + self.inventory = [] + self.money = GameStats.player_stats['starting_money'] + self.armor = None + self.visible = [] + self.view_radius = GameStats.player_stats['view_radius'] + self.moving = False + + # set the heading and direction in a controlled way, might need to add distance attribute later + def move(self, heading): + super().set_heading(heading) + super().set_speed(GameStats.player_stats['move_speed']) + self.moving = True + + def stop(self): + super().set_speed(0) + self.moving = False + + def to_json(self): + data = super().to_json() + + data['inventory'] = [item.to_json() for item in self.inventory] + data['visible'] = [obj.to_json() for obj in self.visible] + + data['money'] = self.money + data['armor'] = self.armor + data['view_radius'] = self.view_radius + data['moving'] = self.moving + + return data + + def from_json(self, data): + super().from_json(data) + self.inventory = data['inventory'] + self.money = data['money'] + self.armor = data['armor'] + self.visible = data['visible'] + self.view_radius = data['view_radius'] + self.moving = data['moving'] diff --git a/game/common/stats.py b/game/common/stats.py index 5db19b15..77b9e96e 100644 --- a/game/common/stats.py +++ b/game/common/stats.py @@ -8,8 +8,11 @@ class GameStats: player_stats = { 'starting_health': 10, + 'starting_money': 10, 'starting_coordinates': [{'x': 450, 'y': 450}, {'x': 50, 'y': 50}], - 'hitbox': {'width': 10, 'height': 10} + 'hitbox': {'width': 10, 'height': 10}, + 'view_radius': 10, + 'move_speed': 10, } moving_object_stats = { diff --git a/game/test_suite/tests/test_moving_object.py b/game/test_suite/tests/test_moving_object.py index 73707c38..fac9abb9 100644 --- a/game/test_suite/tests/test_moving_object.py +++ b/game/test_suite/tests/test_moving_object.py @@ -3,13 +3,13 @@ # to insure your tests are run. import unittest -from game.common.moving import moving_object from game.common.moving.moving_object import MovingObject from game.common.stats import GameStats -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. +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 @@ -63,14 +63,13 @@ def test_set_get_speed_boundary_low(self): def test_moving_obj_parent_params(self): self.setUp() - testMov = MovingObject(10, 10, health=1, coordinates=[{'x': 450, 'y': 450}, {'x': 50, 'y': 50}], hitbox={'width': 10, 'height': 10}, collidable=True) - self.assertIsNotNone(testMov.coordinates) - self.assertIsNotNone(testMov.hitbox) - self.assertIsNotNone(testMov.collidable) + test_mov = MovingObject(10, 10, health=1, coordinates=[{'x': 450, 'y': 450}, {'x': 50, 'y': 50}], hitbox={'width': 10, 'height': 10}, collidable=True) + self.assertIsNotNone(test_mov.coordinates) + self.assertIsNotNone(test_mov.hitbox) + self.assertIsNotNone(test_mov.collidable) self.assertIsNone(self.movObj.coordinates) self.assertIsNone(self.movObj.hitbox) self.assertIsNone(self.movObj.collidable) - # 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