Merge pull request #30 from n0remac/projectiles

Projectiles
This commit is contained in:
Lief9100
2020-04-20 16:24:54 -07:00
committed by GitHub
2 changed files with 97 additions and 4 deletions

View File

@@ -6,10 +6,13 @@ Holds the main game window, as well as manages basic functions for organizing th
import random import random
import arcade import arcade
import math
from config import Config from config import Config
from map import Dungeon 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): class Game(arcade.Window):
@@ -26,6 +29,7 @@ class Game(arcade.Window):
self.wall_list = None self.wall_list = None
self.floor_list = None self.floor_list = None
self.enemy_list = None self.enemy_list = None
self.bullet_list = None
self.player = None self.player = None
self.dungeon = None self.dungeon = None
@@ -47,6 +51,7 @@ class Game(arcade.Window):
# Create the Sprite lists # Create the Sprite lists
self.enemy_list = arcade.SpriteList() self.enemy_list = arcade.SpriteList()
self.bullet_list = arcade.SpriteList()
# Set up the player, specifically placing it at these coordinates. # Set up the player, specifically placing it at these coordinates.
self.player = Player() self.player = Player()
@@ -75,10 +80,10 @@ class Game(arcade.Window):
self.dungeon.render() self.dungeon.render()
self.player.draw() self.player.draw()
self.enemy_list.draw() self.enemy_list.draw()
self.wall_list.draw() self.bullet_list.draw()
x, y = self.player.center_x, self.player.center_y + 100 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): def on_key_press(self, key, modifiers):
"""Called whenever a key is pressed. """ """Called whenever a key is pressed. """
@@ -116,13 +121,47 @@ class Game(arcade.Window):
if self.prev_keypress: if self.prev_keypress:
self.on_key_press(self.prev_keypress.pop(0), 0) 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): def on_update(self, delta_time):
""" Movement and game logic """ """ Movement and game logic """
# Move the player with the physics engine # Move the player with the physics engine
self.physics_engine.update() self.physics_engine.update()
self.player.update_animation() self.player.update_animation()
changed = False # Track if we need to change the viewport changed = False # Track if we need to change the viewport
# Below manages all scrolling mechanics # Below manages all scrolling mechanics
@@ -159,6 +198,25 @@ class Game(arcade.Window):
self.view_bottom, self.view_bottom,
Config.SCREEN_HEIGHT + 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: def main() -> None:
""" """

View File

@@ -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