I am getting this error when trying to run my code, the error.
Traceback (most recent call last):
File "D:/Colin Lewis/pythonProject3/Lib/import_tileset.py", line 264, in <module>
main()
File "D:/Colin Lewis/pythonProject3/Lib/import_tileset.py", line 259, in main
window.setup()
File "D:/Colin Lewis/pythonProject3/Lib/import_tileset.py", line 181, in setup
self.scene.add_sprite(LAYER_NAME_PLAYER, self.player_sprite)
File "D:\Colin Lewis\pythonProject3\lib\site-packages\arcade\scene.py", line 95, in add_sprite
new_list.append(sprite)
File "D:\Colin Lewis\pythonProject3\lib\site-packages\arcade\sprite_list\sprite_list.py", line 625, in append
self._atlas.add(texture)
File "D:\Colin Lewis\pythonProject3\lib\site-packages\arcade\texture_atlas.py", line 284, in add
if self.has_texture(texture):
File "D:\Colin Lewis\pythonProject3\lib\site-packages\arcade\texture_atlas.py", line 452, in has_texture
return texture.name in self._atlas_regions
AttributeError: 'str' object has no attribute 'name'
If anyone knows how to solve this, please help me.
here is my code:
# Colin A. Lewis
# Path of Torment
# Friday, April 1st 2022
# Coding, App & Game Design - A.M.
import os
import arcade
# Constants
SCREEN_WIDTH = 1900
SCREEN_HEIGHT = 1200
SCREEN_TITLE = "Forest_of_Acid_&_Spikes"
# Constants used to scale our sprites from their original size
TILE_SCALING = 0.75
CHARACTER_SCALING = TILE_SCALING * 2
SPRITE_PIXEL_SIZE = 64
GRID_PIXEL_SIZE = SPRITE_PIXEL_SIZE * TILE_SCALING
# How many pixels to keep as a minimum margin between the character
# and the edge of the screen.
LEFT_VIEWPORT_MARGIN = 200
RIGHT_VIEWPORT_MARGIN = 200
BOTTOM_VIEWPORT_MARGIN = 150
TOP_VIEWPORT_MARGIN = 100
# Movement speed of player, in pixels per frame
PLAYER_MOVEMENT_SPEED = 3
GRAVITY = 1.5
PLAYER_JUMP_SPEED = 30
PLAYER_START_X = 1
PLAYER_START_Y = 30
# Constants used to track if the player is facing left or right
RIGHT_FACING = 1
LEFT_FACING = 0
LAYER_NAME_TERRAIN = "Terrain"
LAYER_NAME_HAZARDS = "hazards"
LAYER_NAME_PLAYER = "player"
def load_frame(filename):
return [
arcade.load_texture(filename),
arcade.load_texture(filename, flipped_horizontally=True),
]
class Entity(arcade.Sprite):
def __init__(self):
super().__init__()
self.facing_direction = RIGHT_FACING
self.currentTexture = 0
self.scale = CHARACTER_SCALING
character = f"./Character/Run (1).png"
self.runFrames = []
for i in range(1, 8):
textures = f"{character}/Character/Run (1){i}.png"
self.runFrames.append(textures)
self.textures = self.runFrames[0][0]
class PlayerCharacter(Entity):
def __init__(self):
super().__init__()
def updateAnimation(self, delta_time: float = 1 / 8):
if self.change_x < 0 and self.facing_direction == RIGHT_FACING:
self.facing_direction = LEFT_FACING
elif self.change_x < 0 and self.facing_direction == LEFT_FACING:
self.facing_direction = RIGHT_FACING
# player run animation
if self.change_x != 0:
self.currentTexture += 1
if self.currentTexture > len(self.runFrames) - 1:
self.currentTexture = 0
self.currentTexture = self.runFrames[self.currentTexture][self.facing_direction]
class MyGame(arcade.Window):
def __init__(self):
# Calls from the parent class and sets up the main window
super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
self.left_pressed = False
self.right_pressed = False
self.jump_needs_reset = False
self.up_pressed = False
self.down_pressed = False
# Set the path to start with this program
# File assertion
file_path = os.path.dirname(
os.path.abspath("D:\Colin Lewis\pythonProject3\Lib"))
os.chdir(file_path)
# Our TileMap Object
self.tile_map = ""
# 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
path = r"D:\\Colin Lewis\\pythonProject3\\Lib\\Forest_of_Acid_&_Spikes.tmx"
assert os.path.isfile(path)
with open(path, "r") as r:
pass
def setup(self):
"""Set up the game here. Call this function to restart the game."""
# Setup the Cameras
self.camera = arcade.Camera(self.width, self.height)
self.gui_camera = arcade.Camera(self.width, self.height)
# Map name
map_name = "D:\\Colin Lewis\\pythonProject3\\Lib\\Forest_of_Acid_&_Spikes.tmx"
# Layer Specific Options for the Tilemap
layer_options = {
LAYER_NAME_TERRAIN: {
"use_spatial_hash": True,
},
LAYER_NAME_HAZARDS: {
"use_spatial_hash": True,
},
LAYER_NAME_PLAYER: {
"use_spacial_hash": True
},
}
# Load in TileMap
print("Loading Tile Map")
print(map_name)
self.tile_map = arcade.load_tilemap(map_name, TILE_SCALING, layer_options)
print("Done loading Tile Map")
self.scene = arcade.Scene.from_tilemap(self.tile_map)
self.end_of_map = self.tile_map.width * GRID_PIXEL_SIZE
self.player_sprite = PlayerCharacter()
self.player_sprite.center_x = (
self.tile_map.tile_width * TILE_SCALING * PLAYER_START_X)
self.player_sprite.center_y = (
self.tile_map.tile_height * TILE_SCALING * PLAYER_START_Y
)
self.scene.add_sprite(LAYER_NAME_PLAYER, self.player_sprite)
self.end_of_map = self.tile_map.width * GRID_PIXEL_SIZE
# Create the 'physics engine'
self.physics_engine = arcade.PhysicsEnginePlatformer(
self.player_sprite,
gravity_constant=GRAVITY,
walls=(self.scene[LAYER_NAME_TERRAIN]))
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()
def process_keychange(self):
# 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):
if key == arcade.key.LEFT:
self.left_pressed = True
elif key == arcade.key.RIGHT:
self.right_pressed = True
self.process_keychange()
def on_key_release(self, key, modifiers):
if key == arcade.key.LEFT:
self.left_pressed = False
elif key == arcade.key.RIGHT:
self.right_pressed = False
self.process_keychange()
def center_camera_to_player(self, speed=0.2):
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, speed)
# 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)
def main():
"""Main function"""
window = MyGame()
window.setup()
arcade.run()
if __name__ == "__main__":
main()
This is where I think the error is coming from in the code:
print("Loading Tile Map")
print(map_name)
self.tile_map = arcade.load_tilemap(map_name, TILE_SCALING, layer_options)
print("Done loading Tile Map")
self.scene = arcade.Scene.from_tilemap(self.tile_map)
self.end_of_map = self.tile_map.width * GRID_PIXEL_SIZE
self.player_sprite = PlayerCharacter()
self.player_sprite.center_x = (
self.tile_map.tile_width * TILE_SCALING * PLAYER_START_X)
self.player_sprite.center_y = (
self.tile_map.tile_height * TILE_SCALING * PLAYER_START_Y
)
self.scene.add_sprite(LAYER_NAME_PLAYER, self.player_sprite)
self.end_of_map = self.tile_map.width * GRID_PIXEL_SIZE
# Create the 'physics engine'
self.physics_engine = arcade.PhysicsEnginePlatformer(
self.player_sprite,
gravity_constant=GRAVITY,
walls=(self.scene[LAYER_NAME_TERRAIN]))
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()