def generate(self): if not self.running: return window = system.get_window() interface = window.current_interface projectiles = randint(0, self.projectile_per_attack) for i in range(projectiles): # Generate projectiles right above the player x = randint(int(interface.border.x), int(interface.border.x + interface.border.width)) y = window.height - 100 projectile = Projectile(src=join('images', 'meteor_sequence.png'), x=x, y=y, damage=self.base_damage, speed=self.base_speed, acceleration=self.base_acceleration, batch=self.batch, animated=True, animation_rows=1, animation_columns=4, animation_item_width=202, animation_item_height=103, animation_main_frame=0, animation_period=.25) # Rotate it so it points right below projectile.rotate(3 * pi / 2) self.projectiles.append(projectile)
def generate(self): window: Window = system.get_window() player = system.get_player() max_range = min(window.width // 2, window.height // 2) diameter = randint(300, max_range) degrees = randint(0, 359) for i in range(self.projectile_per_attack): theta = radians(degrees) + ( (2 * pi) / self.projectile_per_attack) * i x = player.x + cos(theta) * diameter y = player.y + sin(theta) * diameter projectile = Projectile( src=join('images', 'staryu.png'), x=x, y=y, damage=self.base_damage, speed=self.base_speed, acceleration=self.base_acceleration, batch=self.batch, ) projectile.point(player.x, player.y) self.set_movement(projectile, diameter) self.projectiles.append(projectile) sword_sharpen.play()
def attack(self): laser.play() print(system.get_enemy()) if system.get_enemy(): damage = config.get_config('damage') or 5 self.projectile = Projectile(src=join('images', 'laser-04.png'), x=self.x, y=self.y, damage=damage, speed=5, acceleration=50) self.projectile.point(system.get_enemy().x, system.get_enemy().y)
def generate(self): window: Window = system.get_window() for i in range(self.projectile_per_attack): # Generate a projectile anywhere on the right side of the window x = randint(window.width // 2, window.width) y = randint(window.height // 2, window.height) projectile = Projectile(src=join('images', 'bonus-11.png'), x=x, y=y, damage=self.base_damage, speed=self.base_speed, acceleration=self.base_acceleration, batch=self.batch) player = system.get_player() projectile_generate_one.play() if player: projectile.point(player.x, player.y) self.projectiles.append(projectile)
def check_for_collision(self, projectile: Projectile): if not self.check_for_rectangle_collision(projectile): return False elif projectile == self.projectile: return False else: # Just let the powerup collide if the rectangles collide. Less to worry about if isinstance(projectile, Powerup): return True px1, py1, px2, py2 = projectile.get_coordinates() x1, y1, x2, y2 = self.get_coordinates() if x1 < px1: # The Player is on the left of the projectile if x2 < px2: # The projectile overlaps and overflows width = px2 - x1 left_vertical, right_vertical = x1, px2 else: # x2 >= px2 # width = self.get_right_bound() - px1 width = self.width left_vertical, right_vertical = x1, x2 else: # x1 >= px2 # The player is on the right of the projectile if x2 < px2: width = projectile.width left_vertical, right_vertical = px1, px2 else: # x2 >= px2 width = x2 - px1 left_vertical, right_vertical = px1, x2 if y1 < py1: # The Player is on the bottom of the projectile if y2 < py2: # The projectile overlaps and overflows height = py2 - y1 top_horizontal, bottom_horizontal = projectile.get_top_bound( ), y1 else: height = self.height top_horizontal, bottom_horizontal = self.get_top_bound( ), py1 else: if y2 < py2: height = projectile.height top_horizontal, bottom_horizontal = projectile.get_top_bound( ), y1 else: height = y2 - py1 top_horizontal, bottom_horizontal = self.get_top_bound( ), py1 width, height = int(round(width)), int(round(height)) empty = np.zeros((height, width)) # print(f'shape: {empty.shape}') img1 = empty.copy() img2 = empty.copy() x1, y1, x2, y2 = projectile.get_bounds(left_vertical, top_horizontal) print(left_vertical, x1, x2) img1[y1:y2, x1:x2] = projectile.threshold x1, y1, x2, y2 = self.get_bounds(left_vertical, top_horizontal) # print(f'Left vertical: {left_vertical} Top horizontal: {top_horizontal}') # print('self.get_bounds()', x1, y1, x2, y2) img2[y1:y2, x1:x2] = self.gray overlap = cv.bitwise_and(img1, img2) start = time.time() for y, row in enumerate(overlap): for x, cell in enumerate(row): if cell >= 1: # print(time.time() - start) return True # print(time.time() - start) return False
class Player(Projectile): damage_modifier: float = 1.0 health: int = 100 max_health: int = 100 health_bar: Bar energy_bar: Bar projectile: Projectile = None on_move: Callable = None def __init__(self, health=100, *args, **kwargs): self.max_health = self.health = config.get_config('health') or health super().__init__(*args, **kwargs) self.health_bar = Bar(value=self.health, maximum=self.health, x=self.get_window().width / 2, y=100, width=self.get_window().width * 0.8, height=50) self.energy_bar = Bar(value=0, maximum=100, x=100, y=self.get_window().height / 2, width=50, height=self.get_window().height * 0.8, orientation=Orientation.VERTICAL, value_color=(25, 25, 112, 255), max_color=(16, 16, 16, 255)) clock.schedule_interval(self.increment_energy_bar, 1 / 60) def increment_energy_bar(self, dt): if self.energy_bar.value >= self.energy_bar.max and system.get_enemy(): self.energy_bar.set(0) self.attack() else: self.energy_bar += dt * 15 def check_for_collision(self, projectile: Projectile): if not self.check_for_rectangle_collision(projectile): return False elif projectile == self.projectile: return False else: # Just let the powerup collide if the rectangles collide. Less to worry about if isinstance(projectile, Powerup): return True px1, py1, px2, py2 = projectile.get_coordinates() x1, y1, x2, y2 = self.get_coordinates() if x1 < px1: # The Player is on the left of the projectile if x2 < px2: # The projectile overlaps and overflows width = px2 - x1 left_vertical, right_vertical = x1, px2 else: # x2 >= px2 # width = self.get_right_bound() - px1 width = self.width left_vertical, right_vertical = x1, x2 else: # x1 >= px2 # The player is on the right of the projectile if x2 < px2: width = projectile.width left_vertical, right_vertical = px1, px2 else: # x2 >= px2 width = x2 - px1 left_vertical, right_vertical = px1, x2 if y1 < py1: # The Player is on the bottom of the projectile if y2 < py2: # The projectile overlaps and overflows height = py2 - y1 top_horizontal, bottom_horizontal = projectile.get_top_bound( ), y1 else: height = self.height top_horizontal, bottom_horizontal = self.get_top_bound( ), py1 else: if y2 < py2: height = projectile.height top_horizontal, bottom_horizontal = projectile.get_top_bound( ), y1 else: height = y2 - py1 top_horizontal, bottom_horizontal = self.get_top_bound( ), py1 width, height = int(round(width)), int(round(height)) empty = np.zeros((height, width)) # print(f'shape: {empty.shape}') img1 = empty.copy() img2 = empty.copy() x1, y1, x2, y2 = projectile.get_bounds(left_vertical, top_horizontal) print(left_vertical, x1, x2) img1[y1:y2, x1:x2] = projectile.threshold x1, y1, x2, y2 = self.get_bounds(left_vertical, top_horizontal) # print(f'Left vertical: {left_vertical} Top horizontal: {top_horizontal}') # print('self.get_bounds()', x1, y1, x2, y2) img2[y1:y2, x1:x2] = self.gray overlap = cv.bitwise_and(img1, img2) start = time.time() for y, row in enumerate(overlap): for x, cell in enumerate(row): if cell >= 1: # print(time.time() - start) return True # print(time.time() - start) return False def draw(self): super().draw() if self.projectile: self.projectile.forward() self.projectile.draw() if system.get_enemy().check_for_rectangle_collision( self.projectile): # if self.projectile.check_for_rectangle_collision(system.get_enemy()): print('Enemy took damage') system.get_enemy().take_damage(self.projectile.damage) self.projectile.delete() self.health_bar.draw() self.energy_bar.draw() def take_damage(self, damage: float) -> float: actual_damage = damage * self.damage_modifier self.health -= actual_damage self.update_health_bar() if self.health <= 0: system.get_window().current_interface.enemy.attack_pattern.stop() system.get_window().load_interface( interfaces.game_over_interface.GameOverInterface()) return actual_damage def update_health_bar(self): self.health_bar.set(self.health) def attack(self): laser.play() print(system.get_enemy()) if system.get_enemy(): damage = config.get_config('damage') or 5 self.projectile = Projectile(src=join('images', 'laser-04.png'), x=self.x, y=self.y, damage=damage, speed=5, acceleration=50) self.projectile.point(system.get_enemy().x, system.get_enemy().y) def move(self, x, y): super().move(x, y) if self.on_move: self.on_move(x, y)
import pyglet import time from math import sqrt, sin, cos, acos, pi, radians from pyglet.window import get_platform from widgets.event_window import EventWindow from game_objects.projectile import Projectile platform = get_platform() display = platform.get_default_display() screen = display.get_default_screen() window = pyglet.window.Window(width=screen.width, height=screen.height) window.maximize() projectile = Projectile(src='images/candy_cane.png') projectile.move(window.width / 2, window.height / 2 + 100) print(projectile.x, projectile.y) center_x, center_y = window.width / 2, window.height / 2 radius = sqrt((projectile.x - center_x)**2 + (projectile.y - center_y)**2) initial_x, initial_y = center_x + radius, center_y start = time.time() acceleration = 1 @window.event def on_draw(): window.clear() projectile.draw()
from game_objects.projectile import Projectile from matplotlib import pyplot as plt from math import pi if __name__ == '__main__': p = Projectile(src='images/meteor_1.png') images = 24 divisor = 6 ratio = pi/divisor for i in range(images): plt.subplot(6, 4, i+1) p.rotate(ratio) plt.imshow(p.np) plt.title(f'{i+1}pi/{images/2}') plt.xticks([]) plt.yticks([]) plt.show()
import pyglet from game_objects.projectile import Projectile from shapes.rectangle import Rectangle # image = cv2.imread('projectiles/minecraft_sword.png') window = pyglet.window.Window(fullscreen=True) projectile = Projectile(src='projectiles/minecraft_sword.png') projectile.x = window.width / 2 projectile.y = window.height / 2 rectangle = Rectangle(x=projectile.x, y=projectile.y, width=projectile.width, height=projectile.height, color=(255, 255, 255, 255)) rectangle.x -= rectangle.width / 2 rectangle.y -= rectangle.height / 2 @window.event def on_draw(): window.clear() rectangle.draw() projectile.draw() # for row in image: # for cell in row: # print(cell) if __name__ == '__main__': pyglet.app.run()
import pyglet import cv2 as cv import time window = EventWindow(fullscreen=True) # soul_image = pyglet.image.load('soul.png') # soul = pyglet.sprite.Sprite(soul_image) soul = Player(src='soul.png') print(soul.width, soul.height) print(window.width / 2, window.height / 2) soul.move(window.width / 2 - soul.width, window.height / 2 - soul.height) soul.scale = 1.3 print('Soul: ', soul.x, soul.y, soul.width, soul.height) soul_np = cv.imread('soul.png') projectile = Projectile(src='projectiles/dull_knife.png', speed=10, x=window.width * 0.7, y=window.height) # projectile.x = window.width / 3 # projectile.rectangle.x = projectile.x # projectile.y = window.height / 2 # projectile.rectangle.y = projectile.y projectile.point(soul.x, soul.y) @window.event def on_draw(): window.clear() projectile.draw() soul.draw()