def update(self): from engine.game_env import Game if self.had_mship: self.last_mship += Game.instance.get_delta_time() if self.last_mship > self.mship_cooldown: self.last_mship = 0 self.had_mship = False if not self.can_have_mship and "Mothership" not in self.game_objects: self.can_have_mship = True self.last_mship = 0 if not self.had_mship and self.can_have_mship: self.had_mship = True self.can_have_mship = False ms_go = GameObject() ms_go.name = "Mothership" ms_go.add_behaviour(Mothership()) self.add_game_object(ms_go) if "Mothership" not in self.game_objects: if "EnemyLine" in self.game_objects: fl = open("assets/txt/SCORE.txt", "w") pd = self.game_objects["Player"].get_behaviour("PlayerData") fl.write(str(pd.score)) fl.close() el = self.game_objects["EnemyLine"] if not el.get_behaviour("EnemyLine").populated: print("All Enemies and Motherships are gone!") print("Starting a new level") gs = GameScreen() gs.level = self.level + 1 Game.instance.set_screen(gs) else: raise Exception("No EnemyLine in GameScreen") super().update()
def __init__(self, size=(0, 0)): super().__init__(size, (0, 0, 0, 255)) self._txt_col = Color(25, 75, 255) self._hsva_buf = self._txt_col.hsva self._start = 0 self._speed = 1 self._should_advance = False title = GameObject("Title") title.add_behaviour(FontRenderer("HErC presents", 100, self._txt_col)) self.add_game_object(title, UI)
def __init__(self, size = (0, 0)): super().__init__(size) self.add_game_object(GameObject("Overlay"), BACKGROUND) self._back_objects["Overlay"].add_behaviour(Overlay(self._size)) self.add_game_object(GameObject("Meteors"), BACKGROUND) self._back_objects["Meteors"].add_behaviour(Meteors(self._size)) self.add_game_object(GameObject("Title"), UI) self._ui_objects["Title"].get_behaviour("Transform").position =\ (int(self._size[0]/2), int(self._size[1]/3)) self._ui_objects["Title"].add_behaviour(\ FontRenderer("SIMPLE GAME MEDLEY", 64))
def shoot(self): self.has_shot = True from engine.game_object import GameObject bullet = GameObject() bullet.name = "Bullet" + str(self.bullet_counter) tr = self.game_object.get_behaviour("Transform") bullet.get_behaviour("Transform").position.x = tr.position.x - 12 bullet.get_behaviour("Transform").position.y = tr.position.y - 54 bullet.get_behaviour("Transform").rotation = tr.rotation bullet.add_behaviour(BulletMovement()) bullet.add_behaviour(BulletRenderer()) GameObject.add_to_screen(bullet) self.bullet_counter += 1
def __init__(self, size=(0, 0)): super().__init__(size, Color(100, 150, 255)) self.add_game_object(GameObject("test")) self._game_objects["test"].transform.set_position( (self._size[0] >> 1, self._size[1] >> 1)) from engine.box_collider import BoxCollider from engine.box_renderer import BoxRenderer from engine.button_behaviour import ButtonBehaviour self._game_objects["test"].add_behaviour(BoxCollider(True, (100, 100))) self._game_objects["test"].add_behaviour(BoxRenderer((100, 100))) self._game_objects["test"].add_behaviour(ButtonBehaviour(None, None)) from src.behaviours.button_manager import ButtonManager self._game_objects["test"].add_behaviour(ButtonManager()) from engine.circle_collider import CircleCollider self.add_game_object(GameObject("mouse")) self._game_objects["mouse"].add_behaviour(CircleCollider(True, 75))
def update(self): self.level.unoccupy_space(self.position) old_pos = self.position.clone() self.update_position() if self.position not in self.level.free_space: self.on_collision() self.level.occupy_space(self.position) GameObject.instantiate(self.game_interface, SnakePiece, level=self.level, position=old_pos, head=self) if self.position == self.level.cherry.position: self.on_cherry_eaten()
def test_deregister(self): """ Make sure that we can properly deregister objects. """ player = Player() card = Card() zone = Zone() other = GameObject() unregistered = GameObject() self.game.register([player, card, zone, other]) self.game.deregister([player, card, zone, other, unregistered]) print self.game.registered_objects self.assertEqual(self.game.registered_objects["Player"][0], 2) self.assertEqual(self.game.registered_objects["Card"][0], 1) self.assertEqual(self.game.registered_objects["Zone"][0], 1) self.assertEqual(self.game.registered_objects["GameObject"][0], 1)
def test_replace_game_objects(self): """ Make sure that the GameObject has a method to replace iterables of GameObjects with their IDs. """ test_object = GameObject() test_object2 = GameObject() test_object.game_id = 10 test_object2.game_id = 11 self.assertEqual(self.game_object.replace_game_objects(test_object), test_object.game_id) # Make sure it works on lists test_list = self.game_object.replace_game_objects( [test_object, test_object2]) self.assertListEqual(test_list, [test_object.game_id, test_object2.game_id]) # Make sure it works on nested lists nested_list = [[test_object], [[test_object2]]] expected_list = [[test_object.game_id], [[test_object2.game_id]]] test_list = self.game_object.replace_game_objects(nested_list) self.assertListEqual(test_list, expected_list) # Make sure it works on dictionaries test_dict = self.game_object.replace_game_objects({"foo": test_object}) self.assertDictEqual(test_dict, {"foo": test_object.game_id})
def start(self): # this is the player's gameobject player = GameObject() player.add_behaviour(RectRenderer()) player.add_behaviour(Movement()) self.add_game_object(player) super().start()
def test_to_dict(self): """ Make sure that a game object can be converted to a dict with GameObjects replaced by their game_id. """ test_object = GameObject() test_object2 = GameObject() test_object.game_id = 10 test_object2.game_id = 11 self.game_object.foo = "bar" self.game_object.g1 = test_object self.game_object.g2 = [test_object, test_object2] self.assertDictEqual( self.game_object.to_dict(), { "game_id": 1, "foo": "bar", "g1": test_object.game_id, "g2": [test_object.game_id, test_object2.game_id] })
class GameObjectTestCase(TestCase): """ A simple test case for testing anything related to game objects that have important state. """ def setUp(self): self.game_object = GameObject() self.game_object.game_id = 1 def test_to_dict(self): """ Make sure that a game object can be converted to a dict with GameObjects replaced by their game_id. """ test_object = GameObject() test_object2 = GameObject() test_object.game_id = 10 test_object2.game_id = 11 self.game_object.foo = "bar" self.game_object.g1 = test_object self.game_object.g2 = [test_object, test_object2] self.assertDictEqual( self.game_object.to_dict(), { "game_id": 1, "foo": "bar", "g1": test_object.game_id, "g2": [test_object.game_id, test_object2.game_id] }) def test_replace_game_objects(self): """ Make sure that the GameObject has a method to replace iterables of GameObjects with their IDs. """ test_object = GameObject() test_object2 = GameObject() test_object.game_id = 10 test_object2.game_id = 11 self.assertEqual(self.game_object.replace_game_objects(test_object), test_object.game_id) # Make sure it works on lists test_list = self.game_object.replace_game_objects( [test_object, test_object2]) self.assertListEqual(test_list, [test_object.game_id, test_object2.game_id]) # Make sure it works on nested lists nested_list = [[test_object], [[test_object2]]] expected_list = [[test_object.game_id], [[test_object2.game_id]]] test_list = self.game_object.replace_game_objects(nested_list) self.assertListEqual(test_list, expected_list) # Make sure it works on dictionaries test_dict = self.game_object.replace_game_objects({"foo": test_object}) self.assertDictEqual(test_dict, {"foo": test_object.game_id})
def start(self): self.image = pygame.image.load("assets/images/space.jpg") test_go = GameObject() test_go.name = "Ship" test_go.get_behaviour("Transform").position.x = 512 test_go.get_behaviour("Transform").position.y = 388 test_go.add_behaviour(BoxCollider()) test_go.get_behaviour("BoxCollider").is_debug = False test_go.get_behaviour("BoxCollider").extent = Vector2(150) test_go.add_behaviour(ShipRenderer()) test_go.add_behaviour(ShieldRenderer()) test_go.add_behaviour(Movement()) test_go.add_behaviour(ShipCommands()) self.add_game_object(test_go) another_go = GameObject() another_go.name = "Obstacle" another_go.get_behaviour("Transform").position.x = 200 another_go.get_behaviour("Transform").position.y = 200 another_go.add_behaviour(BoxCollider()) another_go.get_behaviour("BoxCollider").is_debug = True another_go.get_behaviour("BoxCollider").extent = Vector2(350) self.add_game_object(another_go) overlap_go = GameObject() overlap_go.name = "Overlap" overlap_go.add_behaviour(RectRenderer()) overlap_go.get_behaviour("RectRenderer").colour = (255, 0, 0) overlap_go.get_behaviour("RectRenderer").rect = Rect(0, 0, 0, 0) self.add_game_object(overlap_go) text = GameObject() text.add_behaviour(TextRenderer()) text.get_behaviour("TextRenderer").set_size(30) self.add_game_object(text) super().start()
def start(self): self.image = pygame.image.load("assets/images/space.jpg") test_go = GameObject() test_go.name = "Ship" test_go.get_behaviour("Transform").position.x = 512 test_go.get_behaviour("Transform").position.y = 388 test_go.add_behaviour(BoxCollider()) test_go.get_behaviour("BoxCollider").is_debug = False test_go.get_behaviour("BoxCollider").extent = pygame.math.Vector2(150) test_go.add_behaviour(ShipRenderer()) test_go.add_behaviour(ShieldRenderer()) test_go.add_behaviour(Movement()) test_go.add_behaviour(ShipCommands()) self.add_game_object(test_go) another_go = GameObject() another_go.name = "Obstacle" another_go.get_behaviour("Transform").position.x = 200 another_go.get_behaviour("Transform").position.y = 200 another_go.add_behaviour(BoxCollider()) another_go.get_behaviour("BoxCollider").is_debug = True another_go.get_behaviour("BoxCollider").extent = Vector2(350) self.add_game_object(another_go) overlap_go = GameObject() overlap_go.name = "Overlap" overlap_go.add_behaviour(RectRenderer()) overlap_go.get_behaviour("RectRenderer").colour = (255, 0, 0) overlap_go.get_behaviour("RectRenderer").rect = Rect(0, 0, 0, 0) self.add_game_object(overlap_go) #Create Text Game Object, name it "Text", #Set Game Object position #Add TextRenderer behaviour #Set Message #Set Size #Add Game Object to screen super().start()
def create_snake_head(self): return GameObject.instantiate(self.game_interface, SnakeHead, level=self)
def create_cherry(self): return GameObject.instantiate(self.game_interface, Cherry, level=self, position=self.cherry_position_lottery())
def on_being_eaten(self): self.level.on_cherry_eaten() GameObject.destroy(self)
def late_update(self): if self.head.length - self.age <= 0: GameObject.destroy(self)
def shoot_player(self, x): from engine.game_object import GameObject bullet = GameObject() bullet.name = "EnemyBullet" + str(self.bullet_counter) bullet.get_behaviour("Transform").position.x = x bullet.get_behaviour("Transform").position.y = 200 bullet.add_behaviour(EnemyBulletMovement()) bullet.add_behaviour(BulletRenderer()) bullet.get_behaviour("BulletRenderer").do_rotate(180) GameObject.add_to_screen(bullet) self.bullet_counter += 1
def start(self): #Background setting functionality self.image = pygame.image.load("assets/images/space.jpg") #player character setting: #-> Sets the Transform so that the ship begins the game # in the middle of the screen #-> Adds a box collider and sets its size #-> Also adds Movement, ShipCommands and ShipRenderer # functionality #-> Adds the Game Object to the game_objects list test_go = GameObject() test_go.name = "Ship" test_go.get_behaviour("Transform").position = Vector2(512, 388) test_go.add_behaviour(BoxCollider()) test_go.get_behaviour("BoxCollider").extent = pygame.math.Vector2(150) test_go.add_behaviour(ShipRenderer()) test_go.add_behaviour(ShieldRenderer()) test_go.add_behaviour(Movement()) test_go.add_behaviour(ShipCommands()) self.add_game_object(test_go) #insert obstacle below: #-> Name it "Obstacle" #-> Position its Transform at 200, 200 #-> Add a BoxCollider behaviour #-> Set the BoxCollider's is_debug flag to True #-> Set the BoxCollider's extent to 350, 350 #-> Add the Game Object to the screen's game_objects list #insert the overlap game object here: #-> Name it "Overlap" #-> Add a RectRenderer behaviour #-> Set the RectRenderer's colour #-> Add the Game Object to the screen's game_objects list #base class start() call super().start()
def update(self): self.position += (0, 1) if self.position.y >= PLAY_AREA[1]: GameObject.destroy(self) GameObject.instantiate(self.game_interface, get_random_piece_class())
def shoot(self): self.has_shot = True from engine.game_object import GameObject bullet = GameObject() bullet.name = "Bullet" + str(self.bullet_counter) bullet.get_behaviour( "Transform").position.x = self.game_object.get_behaviour( "Transform").position.x bullet.get_behaviour( "Transform").position.y = self.game_object.get_behaviour( "Transform").position.y bullet.get_behaviour( "Transform").rotation = self.game_object.get_behaviour( "Transform").rotation bullet.add_behaviour(BulletMovement()) bullet.add_behaviour(BoxCollider()) bullet.get_behaviour("BoxCollider").is_debug = True bullet.get_behaviour("BoxCollider").extent = pygame.math.Vector2(10) bullet.add_behaviour(BulletRenderer()) GameObject.add_to_screen(bullet) self.bullet_counter += 1
def start(self): print("(?) src.game_screen: start() called") self.image = pygame.image.load("assets/images/background.jpg") def player_death(): from engine.game_env import Game from src.intro_screen import IntroScreen print("You died") Game.instance.set_screen(IntroScreen()) lvl_go = GameObject() lvl_go.name = "LevelText" h = pygame.display.get_surface().get_height() lvl_go.get_behaviour("Transform").position = Vector2(10, h - 70) lvl_go.add_behaviour(TextRenderer()) lvl_go.get_behaviour("TextRenderer").text = "Level " + str(self.level) lvl_go.get_behaviour("TextRenderer").set_size(90) self.add_game_object(lvl_go) play_go = GameObject() play_go.name = "Player" play_go.get_behaviour("Transform").position = Vector2(512, 700) play_go.add_behaviour(BoxCollider()) play_go.get_behaviour("BoxCollider").is_debug = False play_go.get_behaviour("BoxCollider").extent = Vector2(150) play_go.add_behaviour(ImageRenderer()) play_go.add_behaviour(PlayerCommands()) play_go.add_behaviour(LinearMovement()) play_go.add_behaviour(PlayerData()) play_go.get_behaviour("PlayerData").death_func = player_death play_go.add_behaviour(Boundaries()) play_go.get_behaviour("Boundaries").set_offset(Vector2(60, 0)) play_go.add_behaviour(ScreenshotTaker()) self.add_game_object(play_go) for i in range(3): sr_go = GameObject() sr_go.name = "Shield" + str(i) sr_go.get_behaviour("Transform").position.x = 200 + i * 250 sr_go.get_behaviour("Transform").position.y = 500 sr_go.add_behaviour(ShieldRenderer()) self.add_game_object(sr_go) el_go = GameObject() el_go.name = "EnemyLine" el_go.add_behaviour(EnemyLine()) self.add_game_object(el_go) print("(?) src.game_screen: start() done") super().start()
def start(self): #Text GO text_go = GameObject() text_go.name = "Title" text_go.get_behaviour("Transform").position = Vector2(512, 250) text_go.add_behaviour(TextRenderer()) text_go.get_behaviour("TextRenderer").text = "Random Game" text_go.get_behaviour("TextRenderer").set_size(63) self.add_game_object(text_go) #button GO button_go = GameObject() button_go.name = "StartButton" button_go.get_behaviour("Transform").position = Vector2(512, 475) button_go.add_behaviour(BoxCollider()) button_go.add_behaviour(ButtonBehaviour()) button_go.add_behaviour(ButtonRenderer()) button_go.add_behaviour(TextRenderer()) button_go.get_behaviour("TextRenderer").text = "START" button_go.get_behaviour("TextRenderer").set_size(50) self.add_game_object(button_go) super().start()
def start(self): #Set image self.image = pygame.image.load("assets/images/nebula.jpg") #Create Button Game Object, name it "Button", set position #Add BoxCollider behaviour and set size #Add ButtonBehaviour #Add RectRenderer #Add game object to screen button_go = GameObject() button_go.name = "Button" button_go.get_behaviour("Transform").position = Vector2(512, 384) button_go.add_behaviour(BoxCollider()) button_go.get_behaviour("BoxCollider").extent = Vector2(200, 100) button_go.add_behaviour(ButtonBehaviour()) button_go.add_behaviour(RectRenderer()) self.add_game_object(button_go) super().start()
def start(self): self.image = pygame.image.load("assets/images/background.jpg") def btnclick(): from engine.game_env import Game from src.game_screen import GameScreen Game.instance.set_screen(GameScreen()) btn_go = GameObject() btn_go.name = "Button" btn_go.get_behaviour("Transform").position = Vector2(512, 384) btn_go.add_behaviour(BoxCollider()) btn_go.get_behaviour("BoxCollider").extent = Vector2(200, 100) btn_go.add_behaviour(TextRenderer()) btn_go.get_behaviour("TextRenderer").text = "Start" btn_go.get_behaviour("TextRenderer").set_size(90) btn_go.add_behaviour(ButtonBehaviour()) btn_go.get_behaviour("ButtonBehaviour").compute_text_offset() btn_go.get_behaviour("ButtonBehaviour").f_onclick = btnclick btn_go.add_behaviour(RectRenderer()) self.add_game_object(btn_go) text_go = GameObject() text_go.name = "GameText" text_go.get_behaviour("Transform").position = Vector2(280, 200) text_go.add_behaviour(TextRenderer()) text_go.get_behaviour("TextRenderer").text = "Space Reinvaders" text_go.get_behaviour("TextRenderer").set_size(100) self.add_game_object(text_go) i1_go = GameObject() i1_go.name = "Instr1" i1_go.get_behaviour("Transform").position = Vector2(280, 500) i1_go.add_behaviour(TextRenderer()) i1_go.get_behaviour("TextRenderer").text = "Press space to shoot" i1_go.get_behaviour("TextRenderer").set_size(50) self.add_game_object(i1_go) i2_go = GameObject() i2_go.name = "Instr2" i2_go.get_behaviour("Transform").position = Vector2(280, 550) i2_go.add_behaviour(TextRenderer()) i2_go.get_behaviour("TextRenderer").text = "Press A and D to move" i2_go.get_behaviour("TextRenderer").set_size(50) self.add_game_object(i2_go) super().start()
def setUp(self): self.game_object = GameObject() self.game_object.game_id = 1
def start(self): player = GameObject() player.name = "Player" # this is the player gameobject transform = player.get_behaviour("Transform") transform.position.x = 100 transform.position.y = 500 # add a renderer to the player object so that we can see it renderer = SpriteRenderer() renderer.resize((50, 100)) # resize the renderer behaviour renderer.center = True # make the renderer center the sprite player.add_behaviour(renderer) rigidbody = Rigidbody() rigidbody.experience_gravity = True player.add_behaviour(rigidbody) jetpack = Jetpack() player.add_behaviour(jetpack) jetpack_particles = ParticleSystem() player.add_behaviour(jetpack_particles) self.add_game_object(player) scientist = GameObject() scientist.add_behaviour(SpriteRenderer()) scientist.add_behaviour( Nerd()) # chose to call this behaviour nerd for fun scientist.get_behaviour("Nerd").target = transform self.add_game_object(scientist) super().start()
def start(self): self.image = pygame.image.load("assets/Images/Space.jpg") test_go = GameObject() test_go.name = "Ship" test_go.get_behaviour("Transform").position.x = 512 test_go.get_behaviour("Transform").position.y = 388 test_go.add_behaviour(ShipRenderer()) test_go.add_behaviour(ShieldRenderer()) test_go.add_behaviour(Movement()) test_go.add_behaviour(ShipCommands()) self.add_game_object(test_go) super().start()