Have Enemies Periodically Shoot¶
sprite_bullets_periodic.py¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 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 update(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()
|