Skip to content

Commit

Permalink
Merge pull request #16 from letsbuilda/10-add-target
Browse files Browse the repository at this point in the history
Add target
  • Loading branch information
Anonymous4045 authored Mar 20, 2024
2 parents f851c1c + d41f005 commit f2ffe46
Show file tree
Hide file tree
Showing 6 changed files with 236 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,13 @@ celerybeat.pid
# Environments
.env
.venv
bin/
env/
venv/
ENV/
env.bak/
venv.bak/
pyvenv.cfg

# Spyder project settings
.spyderproject
Expand Down
Binary file added assets/sprites/entities/target_balloon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
192 changes: 191 additions & 1 deletion assets/tiled/map.tmx

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions src/pyweek37/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

SCREEN_TITLE = "PyMunk Platformer"
SPRITE_IMAGE_SIZE = 128
SPRITE_SCALING_TARGET = 1
SPRITE_SCALING_PLAYER = 0.5
SPRITE_SCALING_TILES = 0.5
SPRITE_SIZE = int(SPRITE_IMAGE_SIZE * SPRITE_SCALING_PLAYER)
Expand Down Expand Up @@ -34,3 +35,5 @@
BULLET_MOVE_FORCE = 4500
BULLET_MASS = 0.1
BULLET_GRAVITY = 300

BALLOON_MASS = 0.01
25 changes: 21 additions & 4 deletions src/pyweek37/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from .constants import (
ASSETS_DIR,
BALLOON_MASS,
BULLET_GRAVITY,
BULLET_MASS,
BULLET_MOVE_FORCE,
Expand All @@ -21,6 +22,7 @@
)
from .sprites.bullet import BulletSprite
from .sprites.player import PlayerSprite
from .sprites.target import TargetSprite


class GameWindow(arcade.Window):
Expand All @@ -43,17 +45,24 @@ def setup(self):
self.scene = arcade.Scene()
self.scene.add_sprite_list("player")
self.scene.add_sprite_list("bullet")
self.scene.add_sprite_list("target")

map_file = ASSETS_DIR / "tiled" / "map.tmx"
# Make Entities layer use PlayerSprite
options = {"Entities": {"custom_class": PlayerSprite}}

options = {
"Entities": {"custom_class": PlayerSprite},
"Targets": {"custom_class": TargetSprite},
}
tile_map = arcade.load_tilemap(
map_file, SPRITE_SCALING_TILES, layer_options=options
)

self.scene.add_sprite_list(
"blocks", sprite_list=tile_map.sprite_lists["Blocks"]
)
self.scene.add_sprite_list(
"targets", sprite_list=tile_map.sprite_lists["Targets"]
)

self.scene.add_sprite("player", tile_map.sprite_lists["Entities"][0])
self.player_sprite = self.scene.get_sprite_list("player")[0]
Expand All @@ -65,6 +74,9 @@ def setup(self):
self.physics_engine.add_collision_handler(
"bullet", "wall", post_handler=BulletSprite.wall_hit_handler
)
self.physics_engine.add_collision_handler(
"target", "bullet", post_handler=TargetSprite.bullet_hit_handler
)

self.physics_engine.add_sprite(
self.player_sprite,
Expand All @@ -83,8 +95,13 @@ def setup(self):
body_type=arcade.PymunkPhysicsEngine.STATIC,
)

def on_mouse_motion(self, x: int, y: int, dx: int, dy: int):
"""User moves mouse"""
self.physics_engine.add_sprite_list(
self.scene.get_sprite_list("targets"),
friction=WALL_FRICTION,
mass=BALLOON_MASS,
collision_type="target",
body_type=arcade.PymunkPhysicsEngine.STATIC,
)

def on_mouse_press(self, x, y, button, modifiers):
"""User clicks mouse"""
Expand Down
19 changes: 19 additions & 0 deletions src/pyweek37/sprites/target.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""Target Sprite"""

import arcade

from ..constants import SPRITE_SCALING_TARGET


class TargetSprite(arcade.Sprite):
"""Target Sprite"""

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.scale = SPRITE_SCALING_TARGET

def bullet_hit_handler(self, _bullet_sprite, _arbiter, _space, _data):
"""Called for bullet/target collision"""

self.remove_from_sprite_lists()
# _bullet_sprite.remove_from_sprite_lists()

0 comments on commit f2ffe46

Please sign in to comment.