Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I am creating an asteroids game for my object-oriented programming class, and I keep getting this error when my textures get loaded in: New texture
I am creating an asteroids game for my object-oriented programming class, and I keep getting this error when my textures get loaded in:
New texture end:True
[':resources:images/space_shooter/playerShip1_orange.png0000FalseFalse']
[]
My code with notes is attached.
Also, the images are located here if it helps:
https://github.com/byui-burton/cs241p-course/tree/master/projects/asteroids/sample_code/images
import arcade import math import random from random import randint from abc import ABC from abc import abstractmethod # These are Global constants to use throughout the game SCREEN_WIDTH = 800 SCREEN_HEIGHT = 600 BULLET_RADIUS = 30 BULLET_SPEED = 10 BULLET_LIFE = 60 SHIP_TURN_AMOUNT = 3 SHIP_THRUST_AMOUNT = 0.25 SHIP_RADIUS = 30 INITIAL_ROCK_COUNT = 5 BIG_ROCK_SPIN = 1 BIG_ROCK_SPEED = 1.5 BIG_ROCK_RADIUS = 15 MEDIUM_ROCK_SPIN = -2 MEDIUM_ROCK_RADIUS = 5 SMALL_ROCK_SPIN = 5 SMALL_ROCK_RADIUS = 2 ASTEROID_SPAWN_TIMER = 300 class Point: def __init__(self): self.x = 0 self.y = 0 class Velocity: def __init__(self): self.dx = 0 self.dy = 0 class FlyingObject(ABC): """ Creates class for Flying Objects. This is a base class for other flying objects classes that will be created. Contains methods: advance draw is_off_screen fire hit """ def __init__(self): """ Creates and sets class variables. """ self.center = Point() self.velocity = Velocity() self.center.x = random.uniform(1, SCREEN_WIDTH - 1) self.center.y = random.uniform(1, SCREEN_HEIGHT - 1) self.radius = 0 self.angle = 0 self.velocity.dx = 0 self.velocity.dy = 0 self.speed = 0 self.alive = True self.alpha = 1 def advance(self): """ Changes the position of a flying object :param multiplier: Value can be changed to increase speed of flying object """ self.center.x += self.velocity.dx self.center.y += self.velocity.dy @abstractmethod def draw(self): """ Draws a flying object """ pass def check_off_screen(self): if self.center.x <= 0: self.center.x = SCREEN_WIDTH - 1 elif self.center.x >= SCREEN_WIDTH: self.center.x = 1 elif self.center.y <= 0: self.center.y = SCREEN_HEIGHT - 1 elif self.center.y >= SCREEN_HEIGHT: self.center.y = 1 class Laser(FlyingObject): def __init__(self): super().__init__() self.image = "/Users/nathanackley/images/laserBlue01.png" self.center.x = 0 self.center.y = 0 self.radius = BULLET_RADIUS self.life = BULLET_LIFE self.speed = BULLET_SPEED def draw(self): texture = arcade.load_texture(self.image) arcade.draw_texture_rectangle(self.center.x, self.center.y, self.length, self.width, texture, self.angle, self.alpha) def fire(self, ship): self.velocity.dx = ship.velocity.dx + (math.cos(math.radians(ship.angle)) * self.speed) self.velocity.dy = ship.velocity.dy + (math.sin(math.radians(ship.angle)) * self.speed) def advance(self): self.center.x += self.velocity.dx self.center.y += self.velocity.dy class Ship(FlyingObject): def __init__(self): super().__init__() self.radius = SHIP_RADIUS self.ship_turn = SHIP_TURN_AMOUNT self.ship_thrust = SHIP_THRUST_AMOUNT self.speed = 1 self.image = "/Users/nathanackley/images/playerShip1_orange.png" def draw(self): texture = arcade.load_texture(self.image) arcade.draw_texture_rectangle(self.center.x, self.center.y, self.radius * 2, self.radius * 2, texture,self.angle, self.alpha) def move(self): self.velocity.dx = math.cos(math.radians(self.angle)) * self.speed self.velocity.dy = math.sin(math.radians(self.angle)) * self.speed def accelerate(self): self.speed += ship.thrust self.move() def decelerate(self): self.speed -= ship.thrust self.move() class Asteroid(FlyingObject): def __init__(self): super().__init__() self.velocity_angle = random.uniform(0, 359) self.velocity.dx = math.cos(math.radians(self.velocity_angle)) * self.speed self.velocity.dy = math.sin(math.radians(self.velocity_angle)) * self.speed self.type = 0 def draw(self): texture = arcade.load_texture(self.image) arcade.draw_texture_rectangle(self.center.x, self.center.y, self.radius * 2, self.radius * 2, texture,self.angle, self.alpha) def advance(self): self.center.x += self.velocity.dx self.center.y += self.velocity.dy class BigAsteroid(Asteroid): def __init__(self): super().__init__() self.image = "/Users/nathanackley/images/meteorGrey_big1.png" self.radius = BIG_ROCK_RADIUS self.spin = BIG_ROCK_SPIN self.type = 1 self.speed = BIG_ROCK_SPEED def rotate(self): self.angle += self.spin def advance(self): self.rotate() super().advance() class MediumAsteroid(Asteroid): def __init__(self): super().__init__() self.image = "/Users/nathanackley/images/meteorGrey_med1.png" self.radius = MEDIUM_ROCK_RADIUS self.spin = MEDIUM_ROCK_SPIN self.type = 2 def rotate(self): self.angle += self.spin def advance(self): self.rotate() super().advance() class SmallAsteroid(Asteroid): def __init__(self): super().__init__() self.image = "/Users/nathanackley/images/meteorGrey_small1.png" self.radius = SMALL_ROCK_RADIUS self.spin = SMALL_ROCK_SPIN self.type = 3 def rotate(self): self.angle += self.spin def advance(self): self.rotate() super().advance() class Game(arcade.Window): """ This class handles all the game callbacks and interaction This class will then call the appropriate functions of each of the above classes. You are welcome to modify anything in this class. """ def __init__(self, width, height): """ Sets up the initial conditions of the game :param width: Screen width :param height: Screen height """ super().__init__(width, height) arcade.set_background_color(arcade.color.SMOKY_BLACK) self.held_keys = set() # TODO: declare anything here you need the game class to track self.ship = Ship() #self.asteroid = Asteroid() self.asteroids = [] self.bullets = [] self.count = INITIAL_ROCK_COUNT def on_draw(self): """ Called automatically by the arcade framework. Handles the responsibility of drawing all elements. """ # clear the screen to begin drawing arcade.start_render() # TODO: draw each object self.ship.draw() for bullet in self.bullets: bullet.draw() for asteroid in self.asteroids: asteroid.draw() def update(self, delta_time): """ Update each object in the game. :param delta_time: tells us how much time has actually elapsed """ self.check_keys() self.check_collisions() self.check_off_screen() self.ship.advance() if self.count > 0: asteroid = Asteroid() self.asteroids.append(asteroid) self.count -= 1 else: rand = random.randint(1, ASTEROID_SPAWN_TIMER) if rand >= 1: asteroid = Asteroid() self.asteroids.append(asteroid) else: pass for bullet in self.bullets: if bullet.life > 0: bullet.advance() bullet.life -= 1 else: bullet.alive = False for asteroid in self.asteroids: asteroid.advance() # TODO: THIS def asteroid_split(self, asteroid): if asteroid.type == 1: big = BigAsteroid() big.center.x = asteroid.center.x big.center.y = asteroid.center.y big.velocity.dx = asteroid.velocity.dx big.velocity.dy = asteroid.velocity.dy + 2 med = MediumAsteroid() med.center.x = asteroid.center.x med.center.y = asteroid.center.y med.velocity.dx = asteroid.velocity.dx med.velocity.dy = asteroid.velocity.dy - 2 small = SmallAsteroid() small.center.x = asteroid.center.x small.center.y = asteroid.center.y small.velocity.dx = asteroid.velocity.dx + 5 small.velocity.dy = asteroid.velocity.dy self.asteroids.extend([big, med, small]) elif asteroid.type == 2: med1 = MediumAsteroid() med1.center.x = asteroid.center.x med1.center.y = asteroid.center.y med1.velocity.dx = asteroid.velocity.dx + 1.5 med1.velocity.dy = asteroid.velocity.dy + 1.5 small1 = SmallAsteroid() small1.center.x = asteroid.center.x small1.center.y = asteroid.center.y small1.velocity.dx = asteroid.velocity.dx - 1.5 small1.velocity.dy = asteroid.velocity.dy - 1.5 self.asteroids.extend([med1, small1]) elif asteroid.type == 3: small2 = SmallAsteroid() small2.center.x = asteroid.center.x small2.center.y = asteroid.center.y small2.velocity.dx = asteroid.velocity.dx - 1.5 small2.velocity.dy = asteroid.velocity.dy - 1.5 self.asteroids.extend([small2]) return asteroid asteroid.alive = False def check_collisions(self): """ Checks to see if bullets have hit targets. Updates scores and removes dead items. :return: """ # NOTE: This assumes you named your targets list "targets" for bullet in self.bullets: for asteroid in self.asteroids: # Make sure they are all alive before checking for a collision if bullet.alive and asteroid.alive: too_close = bullet.radius + asteroid.radius if (abs(bullet.center.x - asteroid.center.x) < too_close and abs(bullet.center.y - asteroid.center.y) < too_close): # its a hit! bullet.alive = False self.asteroid_split(asteroid) self.cleanup_zombies() def cleanup_zombies(self): """ Removes any dead bullets or asteroids from the list. """ for bullet in self.bullets: if not bullet.alive: self.bullets.remove(bullet) for asteroid in self.asteroids: if not asteroid.alive: self.asteroids.remove(asteroid) def check_off_screen(self): """ Checks to see if bullets or targets have left the screen and if so, removes them from their lists. """ self.ship.check_off_screen() for bullet in self.bullets: bullet.check_off_screen() for asteroid in self.asteroids: asteroid.check_off_screen() def check_keys(self): """ This function checks for keys that are being held down. You will need to put your own method calls in here. """ if arcade.key.LEFT in self.held_keys: self.ship.angle += SHIP_TURN_AMOUNT if arcade.key.RIGHT in self.held_keys: self.ship.angle -= SHIP_TURN_AMOUNT if arcade.key.UP in self.held_keys: self.ship.accelerate() if arcade.key.DOWN in self.held_keys: self.ship.decelerate() # Machine gun mode... if arcade.key.SPACE in self.held_keys: if self.ship.alive: bullet = Laser() bullet.center.x = self.ship.center.x bullet.center.y = self.ship.center.y bullet.angle = self.ship.angle bullet.fire(self.ship) self.bullets.append(bullet) def on_key_press(self, key: int, modifiers: int): """ Puts the current key in the set of keys that are being held. You will need to add things here to handle firing the bullet. """ if self.ship.alive: self.held_keys.add(key) if key == arcade.key.SPACE: # TODO: Fire the bullet here! bullet = Laser() bullet.center.x = self.ship.center.x bullet.center.y = self.ship.center.y bullet.angle = self.ship.angle bullet.fire(self.ship) self.bullets.append(bullet) def on_key_release(self, key: int, modifiers: int): """ Removes the current key from the set of held keys. """ if key in self.held_keys: self.held_keys.remove(key) def main(): # Creates the game and starts it going window = Game(SCREEN_WIDTH, SCREEN_HEIGHT) arcade.run() if __name__ == "__main__": main()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started