Merge branch 'master' into perf

This commit is contained in:
2020-04-20 21:54:16 -05:00
committed by GitHub
5 changed files with 149 additions and 25 deletions

View File

@@ -8,10 +8,13 @@ import random
import time
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 FPSCounter:
@@ -45,6 +48,7 @@ class Game(arcade.Window):
# These are 'lists' that keep track of our sprites. Each sprite should
# go into a list.
self.enemy_list = None
self.bullet_list = None
self.player = None
self.dungeon = None
@@ -67,6 +71,7 @@ class Game(arcade.Window):
self.enemy_list = arcade.SpriteList()
self.fps = FPSCounter()
self.bullet_list = arcade.SpriteList()
# Create the dungeon
self.dungeon = Dungeon(0, 5)
@@ -103,6 +108,7 @@ class Game(arcade.Window):
self.dungeon.render()
self.player.draw()
self.enemy_list.draw()
self.bullet_list.draw()
self.player.draw_hit_box()
x, y = self.player.center_x, self.player.center_y
@@ -150,13 +156,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
@@ -193,6 +233,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:
"""