Как исправить контур вокруг слоев при загрузке плиточной карты с помощью Python Arcade?Python

Программы на Python
Ответить Пред. темаСлед. тема
Anonymous
 Как исправить контур вокруг слоев при загрузке плиточной карты с помощью Python Arcade?

Сообщение Anonymous »


Я делаю платформер с Python Аркада и плиточный. Первая картина - это то, как она появляется в плитке. Второе - то, как она появляется, когда программа запускается. У меня не было этой проблемы с другим плиткам, но я в замешательстве, почему на этот раз плиточный вид и представление программы отличаются. Есть ли способ избавиться от светлых контуров? Ниже приведен мой код для справки.
p>
import os
import arcade

# Constants
SCREEN_WIDTH = 1000
SCREEN_HEIGHT = 650
SCREEN_TITLE = "Princess Dungeon"

# Constants used to scale our sprites from their original size
CHARACTER_SCALING = 2.5
TILE_SCALING = 5
COIN_SCALING = 0.5
SPRITE_PIXEL_SIZE = 16
GRID_PIXEL_SIZE = SPRITE_PIXEL_SIZE * TILE_SCALING

# Movement speed of player, in pixels per frame
PLAYER_MOVEMENT_SPEED = 5
GRAVITY = 1
PLAYER_JUMP_SPEED = 30

PLAYER_START_X = 300
PLAYER_START_Y = 300

# How fast to move, and how fast to run the animation
MOVEMENT_SPEED = 4
UPDATES_PER_FRAME = 1

# Constants used to track if the player is facing left or right
RIGHT_FACING = 0
LEFT_FACING = 1

# Layer Names from our TileMap
LAYER_NAME_MOVING_PLATFORMS = "Moving Platforms"
LAYER_NAME_PLATFORMS = "Platforms"
LAYER_NAME_COINS = "Coins"
LAYER_NAME_BACKGROUND = "Background"
LAYER_NAME_LADDERS = "Ladders"
LAYER_NAME_PLAYER = "Player"
LAYER_NAME_BACKGROUND2 = "Background2"
LAYER_NAME_BACKGROUND3 = "Background3"
LAYER_NAME_BACKGROUND4 = "Object Layer 1"
LAYER_NAME_PARALLAX = "Parallax"
LAYER_NAME_PARALLAXBG = "Parallax Background"
LAYER_NAME_PARALLAXBG1 = "Parallax Background1"

def load_texture_pair(filename):
"""
Load a texture pair, with the second being a mirror image.
"""
return [
arcade.load_texture(filename),
arcade.load_texture(filename, flipped_horizontally=True)
]

crystalp = arcade.Sprite(":resources:images/tiles/crystalp.png")
crystalp_score = 2

class PlayerCharacter(arcade.Sprite):
def __init__(self):

# Set up parent class
super().__init__()

# Default to face-right
self.character_face_direction = RIGHT_FACING

# Used for flipping between image sequences
self.cur_texture = 0
self.scale = CHARACTER_SCALING

# Track our state
self.jumping = False
self.climbing = False
self.is_on_ladder = False

# --- Load Textures ---

# Images from Kenney.nl's Asset Pack 3
main_path = ":resources:images/animated_characters/dragon_princess/dragonprincess"

# Load textures for idle standing
self.idle_texture_pair = load_texture_pair(f"{main_path}_idle.png")
self.jump_texture_pair = load_texture_pair(f"{main_path}_jump.png")
self.fall_texture_pair = load_texture_pair(f"{main_path}_idle.png")

# Load textures for walking
self.walk_textures = []
for i in range(8):
texture = load_texture_pair(f"{main_path}_walk{i}.png")
self.walk_textures.append(texture)

# Load textures for climbing
self.climbing_textures = []
texture = arcade.load_texture(f"{main_path}_climb0.png")
self.climbing_textures.append(texture)
texture = arcade.load_texture(f"{main_path}_climb1.png")
self.climbing_textures.append(texture)

# Set the initial texture
self.texture = self.idle_texture_pair[0]

# Hit box will be set based on the first image used. If you want to specify
# a different hit box, you can do it like the code below.
# set_hit_box = [[-22, -64], [22, -64], [22, 28], [-22, 28]]
self.hit_box = self.texture.hit_box_points

def update_animation(self, delta_time: float = 1 / 60):

# Figure out if we need to flip face left or right
if self.change_x < 0 and self.character_face_direction == RIGHT_FACING:
self.character_face_direction = LEFT_FACING
elif self.change_x > 0 and self.character_face_direction == LEFT_FACING:
self.character_face_direction = RIGHT_FACING

# Climbing animation
if self.is_on_ladder:
self.climbing = True
if not self.is_on_ladder and self.climbing:
self.climbing = False
if self.climbing and abs(self.change_y) > 1:
self.cur_texture += 1
if self.cur_texture > 7:
self.cur_texture = 0
if self.climbing:
self.texture = self.climbing_textures[self.cur_texture // 4]
return

# Jumping animation
if self.change_y > 0 and not self.is_on_ladder:
self.texture = self.jump_texture_pair[self.character_face_direction]
return
elif self.change_y < 0 and not self.is_on_ladder:
self.texture = self.fall_texture_pair[self.character_face_direction]
return

# Idle animation
if self.change_x == 0:
self.texture = self.idle_texture_pair[self.character_face_direction]
return

# Walking animation
self.cur_texture += 1
if self.cur_texture > 7:
self.cur_texture = 0
self.texture = self.walk_textures[self.cur_texture][
self.character_face_direction
]

class MyGame(arcade.Window):
"""
Main application class.
"""

def __init__(self):
"""
Initializer for the game
"""

# Call the parent class and set up the window
super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)

# Set the path to start with this program
file_path = os.path.dirname(os.path.abspath(__file__))
os.chdir(file_path)

# Track the current state of what key is pressed
self.left_pressed = False
self.right_pressed = False
self.up_pressed = False
self.down_pressed = False
self.jump_needs_reset = False

# Our TileMap Object
self.tile_map = None

# Our Scene Object
self.scene = None

# Separate variable that holds the player sprite
self.player_sprite = None

# Our 'physics' engine
self.physics_engine = None

# A Camera that can be used for scrolling the screen
self.camera = None

# A Camera that can be used to draw GUI elements
self.gui_camera = None

self.end_of_map = 0

# Keep track of the score
self.score = 0

# Load sounds
self.collect_coin_sound = arcade.load_sound(":resources:sounds/coin1.wav")
self.jump_sound = arcade.load_sound(":resources:sounds/jump1.wav")
self.game_over = arcade.load_sound(":resources:sounds/gameover1.wav")

def setup(self):
"""Set up the game here. Call this function to restart the game."""

# Set up the Cameras
self.camera = arcade.Camera(self.width, self.height)
self.gui_camera = arcade.Camera(self.width, self.height)

# Map name
map_name = ":resources:tiled_maps/cavev2.tmj"

# Layer Specific Options for the Tilemap
layer_options = {
LAYER_NAME_PLATFORMS: {
"use_spatial_hash": True,
},
LAYER_NAME_MOVING_PLATFORMS: {
"use_spatial_hash": False,
},
LAYER_NAME_LADDERS: {
"use_spatial_hash": True,
},
LAYER_NAME_COINS: {
"use_spatial_hash": True,
},
}

# Load in TileMap
self.tile_map = arcade.load_tilemap(map_name, TILE_SCALING, layer_options)

# Initiate New Scene with our TileMap, this will automatically add all layers
# from the map as SpriteLists in the scene in the proper order.
self.scene = arcade.Scene.from_tilemap(self.tile_map)

# Set up the player, specifically placing it at these coordinates.
self.player_sprite = PlayerCharacter()
self.player_sprite.center_x = PLAYER_START_X
self.player_sprite.center_y = PLAYER_START_Y
self.scene.add_sprite(LAYER_NAME_PLAYER, self.player_sprite)

# Calculate the right edge of the my_map in pixels
self.end_of_map = self.tile_map.width * GRID_PIXEL_SIZE

# --- Other stuff
# Set the background color
##if self.tile_map.background_color:
arcade.set_background_color((27,21,35))

# Create the 'physics engine'
self.physics_engine = arcade.PhysicsEnginePlatformer(
self.player_sprite,
platforms=self.scene[LAYER_NAME_MOVING_PLATFORMS],
gravity_constant=GRAVITY,
ladders=self.scene[LAYER_NAME_LADDERS],
walls=self.scene[LAYER_NAME_PLATFORMS]
)

def on_draw(self):
"""Render the screen."""

# Clear the screen to the background color
self.clear()

# Activate the game camera
self.camera.use()

# Draw our Scene
self.scene.draw()

# Activate the GUI camera before drawing GUI elements
self.gui_camera.use()

# Draw our score on the screen, scrolling it with the viewport
score_text = f"Score: {self.score}"
arcade.draw_text(
score_text,
10,
10,
arcade.csscolor.BLACK,
18,
)

# Draw hit boxes.
# for wall in self.wall_list:
# wall.draw_hit_box(arcade.color.BLACK, 3)
#
# self.player_sprite.draw_hit_box(arcade.color.RED, 3)

self.coin_list = self.tile_map.sprite_lists["Coins"]

def process_keychange(self):
"""
Called when we change a key up/down or we move on/off a ladder.
"""
# Process up/down
if self.up_pressed and not self.down_pressed:
if self.physics_engine.is_on_ladder():
self.player_sprite.change_y = PLAYER_MOVEMENT_SPEED
elif (
self.physics_engine.can_jump(y_distance=10)
and not self.jump_needs_reset
):
self.player_sprite.change_y = PLAYER_JUMP_SPEED
self.jump_needs_reset = True
arcade.play_sound(self.jump_sound)
elif self.down_pressed and not self.up_pressed:
if self.physics_engine.is_on_ladder():
self.player_sprite.change_y = -PLAYER_MOVEMENT_SPEED

# Process up/down when on a ladder and no movement
if self.physics_engine.is_on_ladder():
if not self.up_pressed and not self.down_pressed:
self.player_sprite.change_y = 0
elif self.up_pressed and self.down_pressed:
self.player_sprite.change_y = 0

# Process left/right
if self.right_pressed and not self.left_pressed:
self.player_sprite.change_x = PLAYER_MOVEMENT_SPEED
elif self.left_pressed and not self.right_pressed:
self.player_sprite.change_x = -PLAYER_MOVEMENT_SPEED
else:
self.player_sprite.change_x = 0

def on_key_press(self, key, modifiers):
"""Called whenever a key is pressed."""

if key == arcade.key.UP or key == arcade.key.SPACE:
self.up_pressed = True
elif key == arcade.key.DOWN or key == arcade.key.S:
self.down_pressed = True
elif key == arcade.key.LEFT or key == arcade.key.A:
self.left_pressed = True
elif key == arcade.key.RIGHT or key == arcade.key.D:
self.right_pressed = True

self.process_keychange()

def on_key_release(self, key, modifiers):
"""Called when the user releases a key."""

if key == arcade.key.UP or key == arcade.key.SPACE:
self.up_pressed = False
self.jump_needs_reset = False
elif key == arcade.key.DOWN or key == arcade.key.S:
self.down_pressed = False
elif key == arcade.key.LEFT or key == arcade.key.A:
self.left_pressed = False
elif key == arcade.key.RIGHT or key == arcade.key.D:
self.right_pressed = False

self.process_keychange()

def center_camera_to_player(self):
screen_center_x = self.player_sprite.center_x - (self.camera.viewport_width / 2)
screen_center_y = self.player_sprite.center_y - (
self.camera.viewport_height / 2
)
if screen_center_x < 0:
screen_center_x = 0
if screen_center_y < 0:
screen_center_y = 0
player_centered = screen_center_x, screen_center_y

self.camera.move_to(player_centered, 0.2)

def on_update(self, delta_time):
"""Movement and game logic"""

# Move the player with the physics engine
self.physics_engine.update()

# Update animations
if self.physics_engine.can_jump():
self.player_sprite.can_jump = False
else:
self.player_sprite.can_jump = True

if self.physics_engine.is_on_ladder() and not self.physics_engine.can_jump():
self.player_sprite.is_on_ladder = True
self.process_keychange()
else:
self.player_sprite.is_on_ladder = False
self.process_keychange()

# Update Animations
self.scene.update_animation(
delta_time, [LAYER_NAME_COINS, LAYER_NAME_BACKGROUND, LAYER_NAME_PLAYER]
)

# Update walls, used with moving platforms
self.scene.update([LAYER_NAME_MOVING_PLATFORMS])

# See if we hit any coins
coin_hit_list = arcade.check_for_collision_with_list(
self.player_sprite, self.scene[LAYER_NAME_COINS]
)

# Loop through each coin we hit (if any) and remove it
for coin in coin_hit_list:

# Figure out how many points this coin is worth
if coin == crystalp:
self.score += 75

# Remove the coin
coin.remove_from_sprite_lists()
arcade.play_sound(self.collect_coin_sound)

# Position the camera
self.center_camera_to_player()

def main():
"""Main function"""
window = MyGame()
window.setup()
arcade.run()

if __name__ == "__main__":
main()


Подробнее здесь: https://stackoverflow.com/questions/793 ... hon-arcade
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «Python»