diff --git a/triple-dungeon/main.py b/triple-dungeon/main.py index 48bc7af..3945523 100644 --- a/triple-dungeon/main.py +++ b/triple-dungeon/main.py @@ -6,10 +6,13 @@ Holds the main game window, as well as manages basic functions for organizing th import random import arcade +import math from config import Config from map import Dungeon -from mobs import Player +from mobs import Player, Enemy +from config import Config +from projectiles import Temp class Game(arcade.Window): @@ -26,6 +29,7 @@ class Game(arcade.Window): self.wall_list = None self.floor_list = None self.enemy_list = None + self.bullet_list = None self.player = None self.dungeon = None @@ -47,6 +51,7 @@ class Game(arcade.Window): # Create the Sprite lists self.enemy_list = arcade.SpriteList() + self.bullet_list = arcade.SpriteList() # Set up the player, specifically placing it at these coordinates. self.player = Player() @@ -75,10 +80,10 @@ class Game(arcade.Window): self.dungeon.render() self.player.draw() self.enemy_list.draw() - self.wall_list.draw() + self.bullet_list.draw() x, y = self.player.center_x, self.player.center_y + 100 - arcade.draw_text(f"({x}, {y})", x, y, arcade.color.WHITE, 15) + # arcade.draw_text(f"({x}, {y})", x, y, arcade.color.WHITE, 15) def on_key_press(self, key, modifiers): """Called whenever a key is pressed. """ @@ -116,13 +121,47 @@ class Game(arcade.Window): if self.prev_keypress: self.on_key_press(self.prev_keypress.pop(0), 0) + def on_mouse_press(self, x, y, button, modifiers): + """ + Called whenever the mouse is clicked. + """ + # Create a bullet TEMP SPRITE, currently wielding frog slingshot + bullet = Temp() + # Position the bullet at the player's current location + start_x = self.player.center_x + start_y = self.player.center_y + bullet.center_x = start_x + bullet.center_y = start_y + + # Get from the mouse the destination location for the bullet + dest_x = x+self.view_left + dest_y = y+self.view_bottom + + # Do math to calculate how to get the bullet to the destination. + # Calculation the angle in radians between the start points + # and end points. This is the angle the bullet will travel. + x_diff = dest_x - start_x + y_diff = dest_y - start_y + angle = math.atan2(y_diff, x_diff) + + # Angle the bullet sprite so it doesn't look like it is flying sideways. + bullet.angle = math.degrees(angle) + + # Taking into account the angle, calculate our change_x + # and change_y. Velocity is how fast the bullet travels. + bullet.change_x = math.cos(angle) * bullet.speed + bullet.change_y = math.sin(angle) * bullet.speed + + # Add the bullet to the appropriate lists + self.bullet_list.append(bullet) + def on_update(self, delta_time): """ Movement and game logic """ # Move the player with the physics engine self.physics_engine.update() - self.player.update_animation() + changed = False # Track if we need to change the viewport # Below manages all scrolling mechanics @@ -159,6 +198,25 @@ class Game(arcade.Window): self.view_bottom, Config.SCREEN_HEIGHT + self.view_bottom) + # Projectile updates + self.bullet_list.update() + for bullet in self.bullet_list: + # Collision Checks + hit_list = arcade.check_for_collision_with_list(bullet, self.dungeon.getWalls()) + + # If it did, get rid of the bullet + if len(hit_list) > 0: + bullet.remove_from_sprite_lists() + + # If the bullet flies off-screen, remove it. TEMP change to range calc + if ( + bullet.bottom < self.view_bottom or + bullet.top > self.view_bottom+Config.SCREEN_HEIGHT or + bullet.right > self.view_left+Config.SCREEN_WIDTH or + bullet.left < self.view_left + ): + bullet.remove_from_sprite_lists() + def main() -> None: """ diff --git a/triple-dungeon/projectiles.py b/triple-dungeon/projectiles.py new file mode 100644 index 0000000..8cfa101 --- /dev/null +++ b/triple-dungeon/projectiles.py @@ -0,0 +1,35 @@ +""" +projectiles.py +Organizes classes related to projectiles +""" + +import arcade + + +class Projectile(arcade.Sprite): + """ + Represents a Projectile. Damage, sprite, speed, range, collision list? + """ + def __init__(self, speed=7, damage=0, range=100, *args, **kwargs) -> None: + # Set up parent class + super().__init__() + + self.speed = speed + self.damage = damage # unimplemented + self.texture = None + self.range = range # unimplemented + self.collision_list = [] + + +class Temp(Projectile): + """ + Temporary extension of projectile to demonstrate usage + """ + def __init__(self, *args, **kwargs) -> None: + super(Temp, self).__init__(*args, **kwargs) + self.texture = arcade.load_texture("resources/images/monsters/frog/frog1.png") + self.speed = 20 + self.scale = 1 + # collision list for who/what to collide with: wall, player, enemy + + # Can place function for starting on player or enemy