def infect(self): if self.immune: return self.currently_infected = True self.ever_infected = True self.texture = arcade.make_circle_texture(self.simulation_parameters.person_size * 2, INFECTED_COLOR)
def createParticleBurst(x0, y0, partInterval, totalDuration, partSize, partScale, partSpeed, color, startAlpha, endAlpha, imagePath=None): e = arcade.Emitter( center_xy=(x0, y0), emit_controller=arcade.EmitterIntervalWithTime(partInterval, totalDuration), particle_factory=lambda emitter: arcade.FadeParticle( filename_or_texture=imagePath if imagePath is not None else arcade.make_circle_texture( partSize, color), change_xy=arcade.rand_in_circle((0.0, 0.0), partSpeed), scale=partScale, lifetime=uniform(totalDuration / 4, totalDuration), start_alpha=startAlpha, end_alpha=endAlpha, ), ) return e
def update(self): """ Update the particle """ if self.my_alpha <= PARTICLE_FADE_RATE: # Faded out, remove self.remove_from_sprite_lists() else: # Update self.my_alpha -= PARTICLE_FADE_RATE self.alpha = self.my_alpha self.center_x += self.change_x self.center_y += self.change_y self.change_y -= PARTICLE_GRAVITY # Should we sparkle this? if random.random() <= PARTICLE_SPARKLE_CHANCE: self.alpha = 255 self.texture = arcade.make_circle_texture( self.width, arcade.color.WHITE) else: self.texture = self.normal_texture # Leave a smoke particle? if random.random() <= SMOKE_CHANCE: smoke = Smoke(5) smoke.position = self.position self.my_list.append(smoke)
def update(self): if self.currently_infected: self.infected_frame_count += 1 if self.currently_infected and self.infected_frame_count > self.simulation_parameters.frames_to_result: if random.random() < self.simulation_parameters.chance_for_death: self.remove_from_sprite_lists() if random.random() < self.simulation_parameters.chance_for_immunity: self.currently_infected = False self.immune = True self.texture = arcade.make_circle_texture(self.simulation_parameters.person_size * 2, IMMUNE_COLOR)
def __init__(self, radius: int, color: Color, soft: bool = False): """ :param float radius: Radius of the circle :param Color color: Color of the circle :param bool soft: If True, will add a alpha gradient """ super().__init__() if soft: self.texture = make_soft_circle_texture(radius * 2, color) else: self.texture = make_circle_texture(radius * 2, color) self._points = self.texture.hit_box_points
def createParticleEmitter(x0, y0, partNB, partSize, partScale, partSpeed, color, startAlpha, endAlpha): e = arcade.Emitter( center_xy=(x0, y0), emit_controller=arcade.EmitMaintainCount(partNB), particle_factory=lambda emitter: arcade.FadeParticle( filename_or_texture=arcade.make_circle_texture(partSize, color), change_xy=arcade.rand_in_circle((0.0, 0.0), partSpeed), lifetime=uniform(0.01, 0.4), scale=partScale, start_alpha=startAlpha, end_alpha=endAlpha, ), ) return e
def update_animation(self, delta_time): super().update_animation(delta_time) if self.my_alpha <= PARTICLE_FADE_RATE: # フェードアウトしたら消去 self.remove_from_sprite_lists() else: # update self.my_alpha -= PARTICLE_FADE_RATE self.alpha = self.my_alpha self.center_x += self.change_x self.center_y += self.change_y if random.random() <= PARTICLE_SPARKLE_CHANCE: self.alpha = 255 self.texture = arcade.make_circle_texture(self.width, arcade.color.WHITE) else: self.texture = self.normal_texture
def update(self): # First check that the particle isn't faded out. It is not necessary to continue if it is. if self.my_alpha <= PARTICLE_FADE_RATE: self.remove_from_sprite_lists() # Otherwise we need to do some math with the particle else: # Changes the positition of the particle for when update is called. self.my_alpha -= PARTICLE_FADE_RATE self.alpha = self.my_alpha self.center_x += self.change_x self.center_y += self.change_y self.change_y -= PARTICLE_GRAVITY # Gives a random chance for the sparkle effect on the particle. if random.random() <= PARTICLE_SPARKLE_CHANCE: self.alpha = 255 # Change the alpha self.texture = arcade.make_circle_texture( self.width, arcade.color.WHITE) else: self.texture = self.normal_texture
def update(self) -> None: """ Update the shield particles. """ if self.my_alpha <= PARTICLE_FADE_RATE: self.remove_from_sprite_lists() else: self.my_alpha -= PARTICLE_FADE_RATE self.alpha = self.my_alpha self.center_x += self.change_x self.center_y += self.change_y if random.random() <= PARTICLE_SPARKLE_CHANCE: self.alpha = 255 self.texture = arcade.make_circle_texture( int(self.width), arcade.color.SAPPHIRE_BLUE) else: self.texture = self.normal_texture
def __init__(self, width, height, title): super().__init__(width, height, title) arcade.set_background_color(arcade.color.AMAZON) self.texture = arcade.load_texture(":resources:images/space_shooter/playerShip1_orange.png") assert self.texture.width == 99 assert self.texture.height == 75 self.circle_texture = arcade.make_circle_texture(10, arcade.color.RED) self.soft_circle_texture = arcade.make_soft_circle_texture(10, arcade.color.RED, 255, 0) self.soft_square_texture = arcade.make_soft_square_texture(10, arcade.color.RED, 255, 0) columns = 16 count = 60 sprite_width = 256 sprite_height = 256 file_name = ":resources:images/spritesheets/explosion.png" # Load the explosions from a sprite sheet self.explosion_texture_list = arcade.load_spritesheet(file_name, sprite_width, sprite_height, columns, count)
def __init__(self, width, height, title): super().__init__(width, height, title) arcade.set_background_color(arcade.color.WHITE) circle_texture = arcade.make_circle_texture(50, arcade.color.BLUE) square_texture = arcade.make_soft_square_texture(50, arcade.color.ORANGE, outer_alpha=255) self.sprites = arcade.SpriteList() for _ in range(1000): x, y = random.randrange(WIDTH), random.randrange(HEIGHT) sprite = arcade.Sprite(center_x=x, center_y=y) sprite.collides_with_list dx = random.randrange(-3, 3) dy = random.randrange(-3, 3) sprite.change_x = dx sprite.change_y = dy if random.randrange(2) == 0: # circle texture sprite.texture = circle_texture else: # square texture sprite.texture = square_texture self.sprites.append(sprite)
WINDOW_WIDTH = 500 WINDOW_HEIGHT = 500 BACKGROUND_COLOR = arcade.color.BLACK GAME_TITLE = "Asteroids!" PLAYER = arcade.load_texture('player.png') PLAYER_SIZE = 50 ENEMY = arcade.load_texture('foe.png') ENEMY_SIZE = 50 DEATH = arcade.load_texture('pop.png') ASTRONAUT = arcade.load_texture('astro.png') GAME_OVER = arcade.load_texture('game over.png') VICTORY = arcade.load_texture('win.png') BULLET_SIZE = 10 BULLET = arcade.make_circle_texture(BULLET_SIZE, arcade.color.WHITE) BULLET_SPEED = 10 CURSOR_SIZE = 10 SPAWN_TIMER = 100 BACKGROUND = arcade.load_texture('background.jpg') ################################################################################ ## Record definitions Position = { # A position is an X/Y coordinate pair. 'x': float, 'y': float }
def immunize(self): self.immune = True self.texture = arcade.make_circle_texture(self.simulation_parameters.person_size * 2, IMMUNE_COLOR)
WIDTH = 640 HEIGHT = 480 window = arcade.open_window(WIDTH, HEIGHT, "My Arcade Game") player = arcade.Sprite(center_x=0, center_y=0) # Set player texture to some created square texture player.texture = arcade.make_soft_square_texture(40, arcade.color.BLUE, outer_alpha=255) player.velocity = [1, 1] # save a coin texture for later use coin_texture = arcade.make_circle_texture(40, arcade.color.GOLD) # generate coins coins = arcade.SpriteList() for _ in range(100): # Get random location and speeds x = random.randrange(0, WIDTH) y = random.randrange(0, HEIGHT) dx = random.randrange(-5, 5) dy = random.randrange(-5, 5) coin = arcade.Sprite(center_x=x, center_y=y) coin.velocity = [dx, dy] # set the new coin's texture as the saved texture coin.texture = coin_texture
#!/usr/bin/env python from functools import lru_cache from typing import Dict, KeysView, Optional, Set from arcade import Sprite, SpriteList, make_circle_texture from numba import njit from utils.colors import BLACK, FOG from utils.data_types import GridPosition from game import Game, TILE_HEIGHT, TILE_WIDTH OFFSET_X = TILE_WIDTH // 2 OFFSET_Y = TILE_HEIGHT // 2 DARK_TEXTURE = make_circle_texture(2 * TILE_WIDTH, BLACK) FOG_TEXTURE = make_circle_texture(2 * TILE_WIDTH, FOG) class FogSprite(Sprite): def __init__(self, position, texture): super().__init__(center_x=position[0], center_y=position[1]) self.texture = texture class FogOfWar: """ TODO: merge this class with MiniMap (they use same GridPosition set) """ game: Optional[Game] = None
SCREEN_WIDTH = 800 SCREEN_HEIGHT = 600 SCREEN_TITLE = "Particle based fireworks" RAINBOW_COLORS = ( arcade.color.ELECTRIC_CRIMSON, arcade.color.FLUORESCENT_ORANGE, arcade.color.ELECTRIC_YELLOW, arcade.color.ELECTRIC_GREEN, arcade.color.ELECTRIC_CYAN, arcade.color.MEDIUM_ELECTRIC_BLUE, arcade.color.ELECTRIC_INDIGO, arcade.color.ELECTRIC_PURPLE, ) SPARK_TEXTURES = [arcade.make_circle_texture(8, clr) for clr in RAINBOW_COLORS] SPARK_PAIRS = [ [SPARK_TEXTURES[0], SPARK_TEXTURES[3]], [SPARK_TEXTURES[1], SPARK_TEXTURES[5]], [SPARK_TEXTURES[7], SPARK_TEXTURES[2]], ] def firework_spark_mutator(particle: arcade.FadeParticle): """mutation_callback shared by all fireworks sparks""" # gravity particle.change_y += -0.03 # drag particle.change_x *= 0.92 particle.change_y *= 0.92
def change_color(self, color): self.texture = arcade.make_circle_texture(CIRCLE_RADIUS * 2, color)
SCREEN_TITLE = "Particle based fireworks" LAUNCH_INTERVAL_MIN = 1.5 LAUNCH_INTERVAL_MAX = 2.5 TEXTURE = "images/pool_cue_ball.png" RAINBOW_COLORS = ( arcade.color.ELECTRIC_CRIMSON, arcade.color.FLUORESCENT_ORANGE, arcade.color.ELECTRIC_YELLOW, arcade.color.ELECTRIC_GREEN, arcade.color.ELECTRIC_CYAN, arcade.color.MEDIUM_ELECTRIC_BLUE, arcade.color.ELECTRIC_INDIGO, arcade.color.ELECTRIC_PURPLE, ) SPARK_TEXTURES = [ arcade.make_circle_texture(15, clr) for clr in RAINBOW_COLORS ] SPARK_PAIRS = [ [SPARK_TEXTURES[0], SPARK_TEXTURES[3]], [SPARK_TEXTURES[1], SPARK_TEXTURES[5]], [SPARK_TEXTURES[7], SPARK_TEXTURES[2]], ] ROCKET_SMOKE_TEXTURE = arcade.make_soft_circle_texture(15, arcade.color.GRAY) PUFF_TEXTURE = arcade.make_soft_circle_texture(80, (40, 40, 40)) FLASH_TEXTURE = arcade.make_soft_circle_texture(70, (128, 128, 90)) CLOUD_TEXTURES = [ arcade.make_soft_circle_texture(50, arcade.color.WHITE), arcade.make_soft_circle_texture(50, arcade.color.LIGHT_GRAY), arcade.make_soft_circle_texture(50, arcade.color.LIGHT_BLUE), ] STAR_TEXTURES = [
118 def update(self): 119 """ Update the particle """ 120 if self.my_alpha <= PARTICLE_FADE_RATE: 121 # Faded out, remove 122 self.remove_from_sprite_lists() 123 else: 124 # Update 125 self.my_alpha -= PARTICLE_FADE_RATE 126 self.alpha = self.my_alpha 127 self.center_x += self.change_x 128 self.center_y += self.change_y 129 self.change_y -= PARTICLE_GRAVITY 130 131 # Should we sparkle this? 132 if random.random() <= PARTICLE_SPARKLE_CHANCE: 133 self.alpha = 255 134 self.texture = arcade.make_circle_texture(self.width, arcade.color.WHITE) 135 else: 136 self.texture = self.normal_texture 137 138 # Leave a smoke particle? 139 if random.random() <= SMOKE_CHANCE: 140 smoke = Smoke(5) 141 smoke.position = self.position 142 self.my_list.append(smoke) 143 144 145class MyGame(arcade.Window): 146 """ Main application class. """ 147 148 def __init__(self): 149 """ Initializer """ 150 # Call the parent class initializer 151 super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE) 152 153 # Variables that will hold sprite lists 154 self.player_list = None 155 self.coin_list = None 156 self.bullet_list = None 157 self.explosions_list = None 158 159 # Set up the player info 160 self.player_sprite = None 161 self.score = 0 162 163 # Don't show the mouse cursor 164 self.set_mouse_visible(False) 165 166 # Load sounds. Sounds from kenney.nl 167 self.gun_sound = arcade.sound.load_sound(":resources:sounds/laser2.wav") 168 self.hit_sound = arcade.sound.load_sound(":resources:sounds/explosion2.wav") 169 170 arcade.set_background_color(arcade.color.BLACK) 171 172 def setup(self): 173 174 """ Set up the game and initialize the variables. """ 175 176 # Sprite lists 177 self.player_list = arcade.SpriteList() 178 self.coin_list = arcade.SpriteList() 179 self.bullet_list = arcade.SpriteList() 180 self.explosions_list = arcade.SpriteList() 181 182 # Set up the player 183 self.score = 0 184 185 # Image from kenney.nl 186 self.player_sprite = arcade.Sprite(":resources:images/space_shooter/playerShip2_orange.png", SPRITE_SCALING_PLAYER) 187 self.player_sprite.center_x = 50 188 self.player_sprite.center_y = 70 189 self.player_list.append(self.player_sprite) 190 191 # Create the coins 192 for coin_index in range(COIN_COUNT): 193 194 # Create the coin instance 195 # Coin image from kenney.nl 196 coin = arcade.Sprite(":resources:images/space_shooter/playerShip1_green.png", SPRITE_SCALING_COIN) 197 coin.angle = 180 198 199 # Position the coin 200 coin.center_x = random.randrange(SCREEN_WIDTH) 201 coin.center_y = random.randrange(150, SCREEN_HEIGHT) 202 203 # Add the coin to the lists 204 self.coin_list.append(coin) 205 206 def on_draw(self): 207 """ 208 Render the screen. 209 """ 210 211 # This command has to happen before we start drawing 212 arcade.start_render() 213 214 # Draw all the sprites. 215 self.coin_list.draw() 216 self.bullet_list.draw() 217 self.player_list.draw() 218 self.explosions_list.draw() 219 220 # Render the text 221 arcade.draw_text(f"Score: {self.score}", 10, 20, arcade.color.WHITE, 14) 222 223 def on_mouse_motion(self, x, y, dx, dy): 224 """ 225 Called whenever the mouse moves. 226 """ 227 self.player_sprite.center_x = x 228 229 def on_mouse_press(self, x, y, button, modifiers): 230 """ 231 Called whenever the mouse button is clicked. 232 """ 233 234 # Gunshot sound 235 arcade.sound.play_sound(self.gun_sound) 236 237 # Create a bullet 238 bullet = arcade.Sprite(":resources:images/space_shooter/laserBlue01.png", SPRITE_SCALING_LASER) 239 240 # The image points to the right, and we want it to point up. So 241 # rotate it. 242 bullet.angle = 90 243 244 # Give it a speed 245 bullet.change_y = BULLET_SPEED 246 247 # Position the bullet 248 bullet.center_x = self.player_sprite.center_x 249 bullet.bottom = self.player_sprite.top 250 251 # Add the bullet to the appropriate lists 252 self.bullet_list.append(bullet) 253 254 def on_update(self, delta_time): 255 """ Movement and game logic """ 256 257 # Call update on bullet sprites 258 self.bullet_list.update() 259 self.explosions_list.update() 260 261 # Loop through each bullet 262 for bullet in self.bullet_list: 263 264 # Check this bullet to see if it hit a coin 265 hit_list = arcade.check_for_collision_with_list(bullet, self.coin_list) 266 267 # If it did... 268 if len(hit_list) > 0: 269 270 # Get rid of the bullet 271 bullet.remove_from_sprite_lists() 272 273 # For every coin we hit, add to the score and remove the coin 274 for coin in hit_list: 275 # Make an explosion 276 for i in range(PARTICLE_COUNT): 277 particle = Particle(self.explosions_list) 278 particle.position = coin.position 279 self.explosions_list.append(particle) 280 281 smoke = Smoke(50) 282 smoke.position = coin.position 283 self.explosions_list.append(smoke) 284 285 coin.remove_from_sprite_lists() 286 self.score += 1 287 288 # Hit Sound 289 arcade.sound.play_sound(self.hit_sound) 290 291 # If the bullet flies off-screen, remove it. 292 if bullet.bottom > SCREEN_HEIGHT: 293 bullet.remove_from_sprite_lists() 294 295 296def main(): 297 window = MyGame() 298 window.center_window() 299 window.setup() 300 arcade.run() 301 302 303if __name__ == "__main__": 304 main()