diff --git a/arcade/geometry.py b/arcade/geometry.py index 0891d4915..710331261 100644 --- a/arcade/geometry.py +++ b/arcade/geometry.py @@ -7,10 +7,9 @@ from typing import List from arcade.arcade_types import PointList -import math - PRECISION = 2 + def are_polygons_intersecting(poly_a: PointList, poly_b: PointList) -> bool: """ @@ -53,17 +52,17 @@ def are_polygons_intersecting(poly_a: PointList, for poly in poly_a: projected = normal[0] * poly[0] + normal[1] * poly[1] - if min_a == None or projected < min_a: + if min_a is None or projected < min_a: min_a = projected - if max_a == None or projected > max_a: + if max_a is None or projected > max_a: max_a = projected for poly in poly_b: projected = normal[0] * poly[0] + normal[1] * poly[1] - if min_b == None or projected < min_b: + if min_b is None or projected < min_b: min_b = projected - if max_b == None or projected > max_b: + if max_b is None or projected > max_b: max_b = projected if max_a <= min_b or max_b <= min_a: diff --git a/arcade/joysticks.py b/arcade/joysticks.py index 214b5941b..e35a55302 100644 --- a/arcade/joysticks.py +++ b/arcade/joysticks.py @@ -1,5 +1,5 @@ import pyglet.input -def get_joysticks(): - return pyglet.input.get_joysticks() +def get_joysticks(): + return pyglet.input.get_joysticks() diff --git a/arcade/physics_engines.py b/arcade/physics_engines.py index bb44e8958..4b464203d 100644 --- a/arcade/physics_engines.py +++ b/arcade/physics_engines.py @@ -151,7 +151,6 @@ def update(self): self.player_sprite.center_y = round(self.player_sprite.center_y, 2) # print(f"Spot Q ({self.player_sprite.center_x}, {self.player_sprite.center_y})") - # --- Move in the x direction self.player_sprite.center_x += self.player_sprite.change_x diff --git a/arcade/sound.py b/arcade/sound.py index cbcf3b2af..6244be071 100644 --- a/arcade/sound.py +++ b/arcade/sound.py @@ -7,10 +7,10 @@ import pyglet import typing -pyglet.lib.load_library('C:/Program Files/Python36/Lib/site-packages/arcade/Win32/avbin') -pyglet.have_avbin=True -pyglet.debug_lib=True -pyglet.debug_trace=True +# pyglet.lib.load_library('C:/Program Files/Python36/Lib/site-packages/arcade/Win32/avbin') +# pyglet.have_avbin=True +# pyglet.debug_lib=True +# pyglet.debug_trace=True def load_sound_library(): diff --git a/arcade/sprite.py b/arcade/sprite.py index 2cea32822..c0b89427a 100644 --- a/arcade/sprite.py +++ b/arcade/sprite.py @@ -703,19 +703,19 @@ def update_animation(self): if self.state == FACE_LEFT: texture_list = self.walk_left_textures - if texture_list == None or len(texture_list) == 0: + if texture_list is None or len(texture_list) == 0: raise RuntimeError("update_animation was called on a sprite that doesn't have a list of walk left textures.") elif self.state == FACE_RIGHT: texture_list = self.walk_right_textures - if texture_list == None or len(texture_list) == 0: + if texture_list is None or len(texture_list) == 0: raise RuntimeError("update_animation was called on a sprite that doesn't have a list of walk right textures.") elif self.state == FACE_UP: texture_list = self.walk_up_textures - if texture_list == None or len(texture_list) == 0: + if texture_list is None or len(texture_list) == 0: raise RuntimeError("update_animation was called on a sprite that doesn't have a list of walk up textures.") elif self.state == FACE_DOWN: texture_list = self.walk_down_textures - if texture_list == None or len(texture_list) == 0: + if texture_list is None or len(texture_list) == 0: raise RuntimeError( "update_animation was called on a sprite that doesn't have a list of walk down textures.") @@ -814,7 +814,6 @@ def _create_rects(rect_list: Iterable[Sprite]) -> List[float]: return v2f - def _render_rect_filled(offset: int, texture_id: str, texture_coord_vbo: gl.GLuint, batch_count): """ @@ -845,7 +844,7 @@ def _draw_rects(shape_list: Iterable[Sprite], vertex_vbo_id: gl.GLuint, gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) - gl.glEnable(gl.GL_TEXTURE_2D) # As soon as this happens, can't use drawing commands + gl.glEnable(gl.GL_TEXTURE_2D) # As soon as this happens, can't use drawing commands gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_S, gl.GL_CLAMP_TO_EDGE) gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_T, gl.GL_CLAMP_TO_EDGE) gl.glTexParameterf(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_NEAREST) @@ -907,5 +906,4 @@ def _draw_rects(shape_list: Iterable[Sprite], vertex_vbo_id: gl.GLuint, texture_coord_vbo_id, batch_count) - gl.glDisable(gl.GL_TEXTURE_2D) diff --git a/doc/examples/asteroid_smasher.png b/doc/examples/asteroid_smasher.png new file mode 100644 index 000000000..dfb70148f Binary files /dev/null and b/doc/examples/asteroid_smasher.png differ diff --git a/doc/examples/examples_to_do.rst b/doc/examples/examples_to_do.rst index 0242cccef..3bb534e4c 100644 --- a/doc/examples/examples_to_do.rst +++ b/doc/examples/examples_to_do.rst @@ -5,7 +5,6 @@ These are examples we need, that haven't been created. - Show how to do 'levels' based on list length hitting 0 - Show how to do 'levels' based on timer -- Text rotate - Animating snow - Move game controller - Joystick calls @@ -16,10 +15,8 @@ These are examples we need, that haven't been created. - Destroy all sprites with a property - See if there are any sprites with a property - See if all items have a property -- Move sprite with game controller - Pong - Centipede - Move between rooms - Physics engine -- Enemies that shoot randomly - Enemies that follow the user diff --git a/doc/examples/index.rst b/doc/examples/index.rst index 29fc20656..44684884d 100644 --- a/doc/examples/index.rst +++ b/doc/examples/index.rst @@ -171,6 +171,22 @@ Bullets :ref:`sprite_bullets_aimed` +.. figure:: thumbs/sprite_bullets_periodic.png + :figwidth: 170px + + :ref:`sprite_bullets_periodic` + +.. figure:: thumbs/sprite_bullets_random.png + :figwidth: 170px + + :ref:`sprite_bullets_random` + +.. figure:: thumbs/sprite_bullets_enemy_aims.png + :figwidth: 170px + + :ref:`sprite_bullets_enemy_aims` + + Platformers ^^^^^^^^^^^ @@ -235,6 +251,11 @@ Other :ref:`asteroid_smasher` +.. figure:: thumbs/instruction_and_game_over_screens.png + :figwidth: 170px + + :ref:`instruction_and_game_over_screens` + diff --git a/doc/examples/sprite_bullets_enemy_aims.png b/doc/examples/sprite_bullets_enemy_aims.png new file mode 100644 index 000000000..f894ab7a9 Binary files /dev/null and b/doc/examples/sprite_bullets_enemy_aims.png differ diff --git a/doc/examples/sprite_bullets_enemy_aims.rst b/doc/examples/sprite_bullets_enemy_aims.rst new file mode 100644 index 000000000..79ff782fc --- /dev/null +++ b/doc/examples/sprite_bullets_enemy_aims.rst @@ -0,0 +1,16 @@ +:orphan: + +.. _sprite_bullets_enemy_aims: + +Have Enemies Aim at Player +========================== + +.. image:: sprite_bullets_enemy_aims.png + :width: 600px + :height: 600px + :align: center + :alt: Screen shot of using sprites to shoot things + +.. literalinclude:: ../../examples/sprite_bullets_enemy_aims.py + :caption: sprite_bullets_enemy_aims.py + :linenos: diff --git a/doc/examples/sprite_bullets_periodic.png b/doc/examples/sprite_bullets_periodic.png new file mode 100644 index 000000000..5a02ac435 Binary files /dev/null and b/doc/examples/sprite_bullets_periodic.png differ diff --git a/doc/examples/sprite_bullets_periodic.rst b/doc/examples/sprite_bullets_periodic.rst new file mode 100644 index 000000000..d92371c70 --- /dev/null +++ b/doc/examples/sprite_bullets_periodic.rst @@ -0,0 +1,16 @@ +:orphan: + +.. _sprite_bullets_periodic: + +Have Enemies Periodically Shoot +=============================== + +.. image:: sprite_bullets_periodic.png + :width: 600px + :height: 600px + :align: center + :alt: Screenshot of using sprites to shoot things + +.. literalinclude:: ../../examples/sprite_bullets_periodic.py + :caption: sprite_bullets_periodic.py + :linenos: diff --git a/doc/examples/sprite_bullets_random.png b/doc/examples/sprite_bullets_random.png new file mode 100644 index 000000000..4352053f0 Binary files /dev/null and b/doc/examples/sprite_bullets_random.png differ diff --git a/doc/examples/sprite_bullets_random.rst b/doc/examples/sprite_bullets_random.rst new file mode 100644 index 000000000..7fd567c77 --- /dev/null +++ b/doc/examples/sprite_bullets_random.rst @@ -0,0 +1,16 @@ +:orphan: + +.. _sprite_bullets_random: + +Have Enemies Randomly Shoot +=========================== + +.. image:: sprite_bullets_random.png + :width: 600px + :height: 600px + :align: center + :alt: Screenshot of using sprites to shoot things + +.. literalinclude:: ../../examples/sprite_bullets_random.py + :caption: sprite_bullets_random.py + :linenos: diff --git a/doc/examples/update_thumbnails.bat b/doc/examples/update_thumbnails.bat index aeb93ee46..d3508fb4b 100644 --- a/doc/examples/update_thumbnails.bat +++ b/doc/examples/update_thumbnails.bat @@ -1 +1 @@ -mogrify -resize 200x200 -extent 200x200 -background transparent -path thumbs *.png \ No newline at end of file +mogrify -resize 200x158 -extent 200x158 -background transparent -path thumbs *.png diff --git a/examples/asteroid_smasher.py b/examples/asteroid_smasher.py index 9f9cd2d46..b6aa253f4 100644 --- a/examples/asteroid_smasher.py +++ b/examples/asteroid_smasher.py @@ -10,9 +10,9 @@ import math import arcade -SCALE = 1 +SCALE = 0.5 OFFSCREEN_SPACE = 300 -SCREEN_WIDTH = 1024 +SCREEN_WIDTH = 800 SCREEN_HEIGHT = 600 LEFT_LIMIT = -OFFSCREEN_SPACE RIGHT_LIMIT = SCREEN_WIDTH + OFFSCREEN_SPACE @@ -109,16 +109,12 @@ def update(self): super().update() if self.center_x < LEFT_LIMIT: self.center_x = RIGHT_LIMIT - print("Left") if self.center_x > RIGHT_LIMIT: self.center_x = LEFT_LIMIT - print("Right") if self.center_y > TOP_LIMIT: self.center_y = BOTTOM_LIMIT - print("Bottom") if self.center_y < BOTTOM_LIMIT: self.center_y = TOP_LIMIT - print("Top") class BulletSprite(TurningSprite): @@ -339,8 +335,6 @@ def animate(self, x): """ Move everything """ self.frame_count += 1 - if self.frame_count % 60 == 0: - print(self.frame_count) if not self.game_over: self.all_sprites_list.update() diff --git a/examples/basketball.py b/examples/basketball.py deleted file mode 100644 index 6838c53e4..000000000 --- a/examples/basketball.py +++ /dev/null @@ -1,130 +0,0 @@ -# import arcade to use the library -import arcade - - -# this function is called repeatedly in order to draw the shapes and update -# their position -def on_draw(delta_time): - """ Use this function to draw everything to the screen. """ - - # start the render. This must happen before any drawing - # commands. We do NOT need an stop render command. - arcade.start_render() - - # draw shapes - on_draw.ball.draw() - on_draw.center_line.draw() - on_draw.right_arc.draw() - on_draw.left_arc.draw() - on_draw.floor.draw() - on_draw.left_leg.draw() - on_draw.right_leg.draw() - on_draw.body.draw() - on_draw.neck.draw() - on_draw.head.draw() - on_draw.left_arm.draw() - on_draw.right_arm_1.draw() - on_draw.right_arm_2.draw() - on_draw.hoop.draw() - on_draw.backboard.draw() - on_draw.shot_display.draw() - - # update shape positions for objects that will move - on_draw.ball.update() - on_draw.center_line.update() - on_draw.right_arc.update() - on_draw.left_arc.update() - on_draw.right_arm_2.update() - - # code to direct movement here - on_draw.ball.change_x = on_draw.ball_move_x - on_draw.ball.change_y = on_draw.ball_move_y - on_draw.center_line.change_x = on_draw.ball_move_x - on_draw.center_line.change_y = on_draw.ball_move_y - on_draw.right_arc.change_x = on_draw.ball_move_x - on_draw.right_arc.change_y = on_draw.ball_move_y - on_draw.left_arc.change_x = on_draw.ball_move_x - on_draw.left_arc.change_y = on_draw.ball_move_y - - # shoot ball - if on_draw.ball.center_x < 710: - on_draw.ball_move_y -= 0.006 - elif on_draw.ball.center_x >= 710: - on_draw.ball_move_x = 0 - on_draw.ball_move_y -= 0.25 - - # flick arm - if on_draw.right_arm_2.tilt_angle > 45: - on_draw.right_arm_2.change_tilt_angle = on_draw.arm_flick - else: - on_draw.right_arm_2.change_tilt_angle = 0 - - # reset the ball and arm when it goes off the screen - if on_draw.ball.center_y < -30: - on_draw.ball.center_x = 236 - on_draw.ball.center_y = 105 - - on_draw.center_line.start_x = 236 - on_draw.center_line.end_x = 236 - on_draw.center_line.start_y = 85 - on_draw.center_line.end_y = 125 - - on_draw.left_arc.center_x = 236 - on_draw.left_arc.center_y = 105 - - on_draw.right_arc.center_x = 236 - on_draw.right_arc.center_y = 105 - - on_draw.right_arm_2.tilt_angle = 130 - - on_draw.ball_move_x = 2 - on_draw.ball_move_y = 2 - - -# open new window and set the background -arcade.open_window("Basketball Example", 800, 600) -arcade.set_background_color(arcade.color.WHITE) - -# all shapes created will require the prefix 'on_draw' in order to be -# accessible to the on_draw function - -# this is the ball -on_draw.ball = arcade.Circle(236, 105, 20, arcade.color.ORANGE) -on_draw.center_line = arcade.Line(236, 85, 236, 125, arcade.color.BLACK, 1) -on_draw.right_arc = arcade.Arc(236, 105, 20, 15, - arcade.color.BLACK, 0, 180, 1, 270) -on_draw.left_arc = arcade.Arc(236, 105, 20, 15, - arcade.color.BLACK, 0, 180, 1, 90) - -# this is the floor -on_draw.floor = arcade.Line(0, 5, 800, 5, arcade.color.BLACK, 5) - -# this is the shooter -on_draw.left_leg = arcade.Rectangle(100, 10, 30, 7, arcade.color.RED, 0, 80) -on_draw.right_leg = arcade.Rectangle(110, 10, 30, 7, arcade.color.RED, 0, 100) -on_draw.body = arcade.Rectangle(105, 25, 30, 20, arcade.color.YELLOW, 0, 90) -on_draw.neck = arcade.Rectangle(207, 60, 7, 4, arcade.color.BLACK, 1, 90) -on_draw.head = arcade.Circle(210, 82, 10, arcade.color.BLACK, 3) -on_draw.left_arm = arcade.Rectangle(170, 20, 30, 5, arcade.color.BLACK, 2, 70) -on_draw.right_arm_1 = arcade.Rectangle(210, 50, 15, 5, - arcade.color.BLACK, 2, 50) -on_draw.right_arm_2 = arcade.Rectangle(220, 60, 15, 5, - arcade.color.BLACK, 2, 130) - -# this draws the hoop -on_draw.backboard = arcade.Line(730, 370, 730, 470, arcade.color.BLACK, 4) -on_draw.hoop = arcade.Circle(700, 400, 30, arcade.color.DARK_GREEN, 2) - -# this is the text on the screen -on_draw.shot_display = arcade.Text("Basketball", 50, 550, 50, - arcade.color.PURPLE) - -# declare any variables that will move an object -on_draw.ball_move_x = 2 -on_draw.ball_move_y = 2 - -on_draw.arm_flick = -5 - -arcade.schedule(on_draw, 1 / 80) - -arcade.run() diff --git a/examples/images/playerShip1_green.png b/examples/images/playerShip1_green.png new file mode 100644 index 000000000..2eb6f9c06 Binary files /dev/null and b/examples/images/playerShip1_green.png differ diff --git a/examples/platformer.py b/examples/platformer.py index 386abce9b..2e0307412 100644 --- a/examples/platformer.py +++ b/examples/platformer.py @@ -1,5 +1,3 @@ -import random -import math import arcade import copy diff --git a/examples/shapes.py b/examples/shapes.py index 13e2e2bbe..ff8af1ebd 100644 --- a/examples/shapes.py +++ b/examples/shapes.py @@ -18,7 +18,7 @@ RECT_HEIGHT = 50 -class Shape(): +class Shape: def __init__(self, x, y, width, height, angle, delta_x, delta_y, delta_angle, color): @@ -55,6 +55,10 @@ def draw(self): class MyApplication(arcade.Window): """ Main application class. """ + def __init__(self): + super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, title="Shapes!") + self.shape_list = None + def setup(self): """ Set up the game and initialize the variables. """ self.shape_list = [] @@ -100,7 +104,7 @@ def on_draw(self): for shape in self.shape_list: shape.draw() -window = MyApplication(SCREEN_WIDTH, SCREEN_HEIGHT, title="Shapes!") +window = MyApplication() window.setup() arcade.run() diff --git a/examples/sprite_bullets_enemy_aims.py b/examples/sprite_bullets_enemy_aims.py new file mode 100644 index 000000000..36b8aa2bc --- /dev/null +++ b/examples/sprite_bullets_enemy_aims.py @@ -0,0 +1,113 @@ +""" +Show how to have enemies shoot bullets aimed at the player. +""" +import arcade +import math + +SCREEN_WIDTH = 800 +SCREEN_HEIGHT = 600 +BULLET_SPEED = 4 + +class MyApplication(arcade.Window): + """ Main application class """ + + def __init__(self, width, height): + super().__init__(width, height) + + arcade.set_background_color(arcade.color.BLACK) + + self.frame_count = 0 + + self.all_sprites_list = arcade.SpriteList() + self.enemy_list = arcade.SpriteList() + self.bullet_list = arcade.SpriteList() + + self.player = arcade.Sprite("images/playerShip1_orange.png", 0.5) + self.all_sprites_list.append(self.player) + + enemy = arcade.Sprite("images/playerShip1_green.png", 0.5) + enemy.center_x = 120 + enemy.center_y = SCREEN_HEIGHT - enemy.height + enemy.angle = 180 + self.all_sprites_list.append(enemy) + self.enemy_list.append(enemy) + + enemy = arcade.Sprite("images/playerShip1_green.png", 0.5) + enemy.center_x = SCREEN_WIDTH - 120 + enemy.center_y = SCREEN_HEIGHT - enemy.height + enemy.angle = 180 + self.all_sprites_list.append(enemy) + self.enemy_list.append(enemy) + + def on_draw(self): + """Render the screen. """ + + arcade.start_render() + + self.all_sprites_list.draw() + + def animate(self, delta_time): + """All the logic to move, and the game logic goes here. """ + + self.frame_count += 1 + + for enemy in self.enemy_list: + + # First, calculate the angle to the player. We could do this + # only when the bullet fires, but in this case we will rotate + # the enemy to face the player each frame, so we'll do this + # each frame. + + # Position the start at the enemy's current location + start_x = enemy.center_x + start_y = enemy.center_y + + # Get the destination location for the bullet + dest_x = self.player.center_x + dest_y = self.player.center_y + + # Do math to calculate how to get the bullet to the destination. + # Calculation the angle in radians between the start points + # and end points. This is the angle the bullet will travel. + x_diff = dest_x - start_x + y_diff = dest_y - start_y + angle = math.atan2(y_diff, x_diff) + + # Set the enemy to face the player. + enemy.angle = math.degrees(angle)-90 + + # Shoot every 60 frames change of shooting each frame + if self.frame_count % 60 == 0: + bullet = arcade.Sprite("images/laserBlue01.png") + bullet.center_x = start_x + bullet.center_y = start_y + + # Angle the bullet sprite + bullet.angle = math.degrees(angle) + + # Taking into account the angle, calculate our change_x + # and change_y. Velocity is how fast the bullet travels. + bullet.change_x = math.cos(angle) * BULLET_SPEED + bullet.change_y = math.sin(angle) * BULLET_SPEED + + self.bullet_list.append(bullet) + self.all_sprites_list.append(bullet) + + # Get rid of the bullet when it flies off-screen + for bullet in self.bullet_list: + if bullet.top < 0: + bullet.kill() + + self.bullet_list.update() + + + + def on_mouse_motion(self, x, y, delta_x, delta_y): + """Called whenever the mouse moves. """ + self.player.center_x = x + self.player.center_y = y + + +window = MyApplication(SCREEN_WIDTH, SCREEN_HEIGHT) + +arcade.run() diff --git a/examples/sprite_bullets_periodic.py b/examples/sprite_bullets_periodic.py new file mode 100644 index 000000000..e2b4d457e --- /dev/null +++ b/examples/sprite_bullets_periodic.py @@ -0,0 +1,83 @@ +""" +Show how to have enemies shoot bullets at regular intervals. +""" +import arcade + +SCREEN_WIDTH = 800 +SCREEN_HEIGHT = 600 + + +class MyApplication(arcade.Window): + """ Main application class """ + + def __init__(self, width, height): + super().__init__(width, height) + + arcade.set_background_color(arcade.color.BLACK) + + self.frame_count = 0 + + self.all_sprites_list = arcade.SpriteList() + self.enemy_list = arcade.SpriteList() + self.bullet_list = arcade.SpriteList() + + self.player = arcade.Sprite("images/playerShip1_orange.png", 0.5) + self.all_sprites_list.append(self.player) + + enemy = arcade.Sprite("images/playerShip1_green.png", 0.5) + enemy.center_x = 120 + enemy.center_y = SCREEN_HEIGHT - enemy.height + enemy.angle = 180 + self.all_sprites_list.append(enemy) + self.enemy_list.append(enemy) + + enemy = arcade.Sprite("images/playerShip1_green.png", 0.5) + enemy.center_x = SCREEN_WIDTH - 120 + enemy.center_y = SCREEN_HEIGHT - enemy.height + enemy.angle = 180 + self.all_sprites_list.append(enemy) + self.enemy_list.append(enemy) + + def on_draw(self): + """Render the screen. """ + + arcade.start_render() + + self.all_sprites_list.draw() + # Draw the text + arcade.draw_text("This is a simple template to start your game.", + 10, SCREEN_HEIGHT // 2, arcade.color.BLACK, 20) + + def animate(self, delta_time): + """All the logic to move, and the game logic goes here. """ + + self.frame_count += 1 + + for enemy in self.enemy_list: + if self.frame_count % 120 == 0: + bullet = arcade.Sprite("images/laserBlue01.png") + bullet.center_x = enemy.center_x + bullet.angle = -90 + bullet.top = enemy.bottom + bullet.change_y = -2 + self.bullet_list.append(bullet) + self.all_sprites_list.append(bullet) + + # Get rid of the bullet when it flies off-screen + for bullet in self.bullet_list: + if bullet.top < 0: + bullet.kill() + + self.bullet_list.update() + + def on_mouse_motion(self, x, y, delta_x, delta_y): + """ + Called whenever the mouse moves. + """ + self.player.center_x = x + self.player.center_y = 20 + + +window = MyApplication(SCREEN_WIDTH, SCREEN_HEIGHT) + +arcade.run() diff --git a/examples/sprite_bullets_random.py b/examples/sprite_bullets_random.py new file mode 100644 index 000000000..593735f99 --- /dev/null +++ b/examples/sprite_bullets_random.py @@ -0,0 +1,86 @@ +""" +Show how to have enemies shoot bullets at random intervals. +""" +import arcade +import random + +SCREEN_WIDTH = 800 +SCREEN_HEIGHT = 600 + + +class MyApplication(arcade.Window): + """ Main application class """ + + def __init__(self, width, height): + super().__init__(width, height) + + arcade.set_background_color(arcade.color.BLACK) + + self.frame_count = 0 + + self.all_sprites_list = arcade.SpriteList() + self.enemy_list = arcade.SpriteList() + self.bullet_list = arcade.SpriteList() + + self.player = arcade.Sprite("images/playerShip1_orange.png", 0.5) + self.all_sprites_list.append(self.player) + + enemy = arcade.Sprite("images/playerShip1_green.png", 0.5) + enemy.center_x = 120 + enemy.center_y = SCREEN_HEIGHT - enemy.height + enemy.angle = 180 + self.all_sprites_list.append(enemy) + self.enemy_list.append(enemy) + + enemy = arcade.Sprite("images/playerShip1_green.png", 0.5) + enemy.center_x = SCREEN_WIDTH - 120 + enemy.center_y = SCREEN_HEIGHT - enemy.height + enemy.angle = 180 + self.all_sprites_list.append(enemy) + self.enemy_list.append(enemy) + + def on_draw(self): + """Render the screen. """ + + arcade.start_render() + + self.all_sprites_list.draw() + # Draw the text + arcade.draw_text("This is a simple template to start your game.", + 10, SCREEN_HEIGHT // 2, arcade.color.BLACK, 20) + + def animate(self, delta_time): + """All the logic to move, and the game logic goes here. """ + + self.frame_count += 1 + + for enemy in self.enemy_list: + + # Have a random 1 in 200 change of shooting each frame + if random.randrange(200) == 0: + bullet = arcade.Sprite("images/laserBlue01.png") + bullet.center_x = enemy.center_x + bullet.angle = -90 + bullet.top = enemy.bottom + bullet.change_y = -2 + self.bullet_list.append(bullet) + self.all_sprites_list.append(bullet) + + # Get rid of the bullet when it flies off-screen + for bullet in self.bullet_list: + if bullet.top < 0: + bullet.kill() + + self.bullet_list.update() + + def on_mouse_motion(self, x, y, delta_x, delta_y): + """ + Called whenever the mouse moves. + """ + self.player.center_x = x + self.player.center_y = 20 + + +window = MyApplication(SCREEN_WIDTH, SCREEN_HEIGHT) + +arcade.run() diff --git a/examples/sprite_collect_coins_background.py b/examples/sprite_collect_coins_background.py index b9bb997ec..be422e177 100644 --- a/examples/sprite_collect_coins_background.py +++ b/examples/sprite_collect_coins_background.py @@ -29,6 +29,9 @@ def __init__(self, width, height): # Call the parent class initializer super().__init__(width, height) + # Background image will be stored in this variable + self.background = None + # Variables that will hold sprite lists self.all_sprites_list = None self.coin_list = None @@ -43,7 +46,6 @@ def __init__(self, width, height): # Set the background color arcade.set_background_color(arcade.color.AMAZON) - def setup(self): """ Set up the game and initialize the variables. """ diff --git a/examples/sprite_face_left_or_right.py b/examples/sprite_face_left_or_right.py index 2644672f3..6d53ded3e 100644 --- a/examples/sprite_face_left_or_right.py +++ b/examples/sprite_face_left_or_right.py @@ -6,7 +6,6 @@ Artwork from http://kenney.nl """ -import random import arcade SPRITE_SCALING = 0.5 @@ -50,6 +49,7 @@ def update(self): elif self.top > SCREEN_HEIGHT - 1: self.top = SCREEN_HEIGHT - 1 + class MyApplication(arcade.Window): """ Main application class. @@ -73,7 +73,6 @@ def __init__(self, width, height): # Set the background color arcade.set_background_color(arcade.color.AMAZON) - def setup(self): """ Set up the game and initialize the variables. """ @@ -87,7 +86,6 @@ def setup(self): self.player_sprite.center_y = SCREEN_HEIGHT / 2 self.all_sprites_list.append(self.player_sprite) - def on_draw(self): """ Render the screen.