Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def recreate_grid(self):
lin = np.linspace(0, 5, ROW_COUNT, endpoint=False)
y, x = np.meshgrid(lin, lin)
self.grid = (perlin(x, y, seed=0))
self.grid *= 255
self.grid += 128
# for row in range(ROW_COUNT):
# for column in range(COLUMN_COUNT):
# print(f"{self.grid[row][column]:7.1f} ", end="")
# print()
im = Image.fromarray(np.uint8(self.grid), "L")
background_sprite = arcade.Sprite()
background_sprite.center_x = SCREEN_WIDTH / 2
background_sprite.center_y = SCREEN_HEIGHT / 2
background_sprite.append_texture(arcade.Texture("dynamic noise image", im))
background_sprite.set_texture(0)
self.background_list = arcade.SpriteList()
self.background_list.append(background_sprite)
def check_for_collision(sprite1: Sprite, sprite2: Sprite) -> bool:
"""
Check for a collision between two sprites.
:param sprite1: First sprite
:param sprite2: Second sprite
:Returns: True or False depending if the sprites intersect.
"""
if not isinstance(sprite1, Sprite):
raise TypeError("Parameter 1 is not an instance of the Sprite class.")
if isinstance(sprite2, SpriteList):
raise TypeError("Parameter 2 is a instance of the SpriteList instead of a required Sprite. See if you meant to "
"call check_for_collision_with_list instead of check_for_collision.")
elif not isinstance(sprite2, Sprite):
raise TypeError("Parameter 2 is not an instance of the Sprite class.")
return _check_for_collision(sprite1, sprite2)
def setup(self):
""" Set up the game and initialize the variables. """
# Sprite lists
self.all_sprites_list = arcade.SpriteList()
self.coin_list = arcade.SpriteList()
# Score
self.score = 0
# Set up the player
# Character image from kenney.nl
self.player_sprite = arcade.Sprite("character.png", SPRITE_SCALING_PLAYER)
self.player_sprite.center_x = 50
self.player_sprite.center_y = 50
self.all_sprites_list.append(self.player_sprite)
# Create the coins
for i in range(50):
# Create the coin instance
# Coin image from kenney.nl
coin = Coin("coin_01.png", SPRITE_SCALING_COIN)
# Position the coin
coin.center_x = random.randrange(SCREEN_WIDTH)
coin.center_y = random.randrange(SCREEN_HEIGHT)
coin.change_x = random.randrange(-3, 4)
coin.change_y = random.randrange(-3, 4)
# Put some crates on the ground
# This shows using a coordinate list to place sprites
coordinate_list = [[512, 96],
[256, 96],
[768, 96]]
for coordinate in coordinate_list:
# Add a crate on the ground
wall = arcade.Sprite(":resources:images/tiles/boxCrate_double.png", TILE_SCALING)
wall.position = coordinate
self.wall_list.append(wall)
# Use a loop to place some coins for our character to pick up
for x in range(128, 1250, 256):
coin = arcade.Sprite(":resources:images/items/coinGold.png", COIN_SCALING)
coin.center_x = x
coin.center_y = 96
self.coin_list.append(coin)
# Create the 'physics engine'
self.physics_engine = arcade.PhysicsEnginePlatformer(self.player_sprite,
self.wall_list,
GRAVITY)
def setup(self):
""" Set up the game and initialize the variables. """
# Sprite lists
self.player_list = arcade.SpriteList()
self.coin_list = arcade.SpriteList()
# Score
self.score = 0
# Set up the player
# Character image from kenney.nl
self.player_sprite = arcade.Sprite(":resources:images/animated_characters/female_person/femalePerson_idle.png", SPRITE_SCALING_PLAYER)
self.player_sprite.center_x = 50
self.player_sprite.center_y = 50
self.player_list.append(self.player_sprite)
# Create the coins
for i in range(COIN_COUNT):
# Create the coin instance
# Coin image from kenney.nl
coin = arcade.Sprite(":resources:images/items/coinGold.png", SPRITE_SCALING_COIN)
# Position the coin
coin.center_x = random.randrange(SCREEN_WIDTH)
coin.center_y = random.randrange(SCREEN_HEIGHT)
# Set up the initial angle, and the "spin"
coin.angle = random.randrange(360)
def setup(self):
""" Set up the game and initialize the variables. """
# Sprite lists
self.all_sprites_list = arcade.SpriteList()
self.wall_list = arcade.SpriteList()
# Set up the player
self.player_sprite = arcade.Sprite("images/character.png",
SPRITE_SCALING)
# Starting position of the player
self.player_sprite.center_x = 64
self.player_sprite.center_y = 270
self.all_sprites_list.append(self.player_sprite)
# Draw the walls on the bottom
for x in range(0, SCREEN_WIDTH, int(SPRITE_NATIVE_SIZE * SPRITE_SCALING)):
wall = arcade.Sprite("images/grassMid.png", SPRITE_SCALING)
wall.bottom = 0
wall.left = x
self.all_sprites_list.append(wall)
self.wall_list.append(wall)
If Python and Arcade are installed, this example can be run from the command line with:
python -m arcade.examples.sprite_collect_coins_diff_levels
"""
import random
import arcade
import os
SPRITE_SCALING = 0.5
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
SCREEN_TITLE = "Sprite Collect Coins with Different Levels Example"
class FallingCoin(arcade.Sprite):
""" Simple sprite that falls down """
def update(self):
""" Move the coin """
# Fall down
self.center_y -= 2
# Did we go off the screen? If so, pop back to the top.
if self.top < 0:
self.bottom = SCREEN_HEIGHT
class RisingCoin(arcade.Sprite):
""" Simple sprite that falls up """
while column < len(maze) and maze[row][column] == 1:
column += 1
end_column = column - 1
column_count = end_column - start_column + 1
column_mid = (start_column + end_column) / 2
wall = arcade.Sprite(":resources:images/tiles/grassCenter.png", SPRITE_SCALING,
repeat_count_x=column_count)
wall.center_x = column_mid * SPRITE_SIZE + SPRITE_SIZE / 2
wall.center_y = row * SPRITE_SIZE + SPRITE_SIZE / 2
wall.width = SPRITE_SIZE * column_count
self.wall_list.append(wall)
# Set up the player
self.player_sprite = arcade.Sprite(":resources:images/animated_characters/female_person/femalePerson_idle.png", SPRITE_SCALING)
self.player_list.append(self.player_sprite)
# Randomly place the player. If we are in a wall, repeat until we aren't.
placed = False
while not placed:
# Randomly position
self.player_sprite.center_x = random.randrange(MAZE_WIDTH * SPRITE_SIZE)
self.player_sprite.center_y = random.randrange(MAZE_HEIGHT * SPRITE_SIZE)
# Are we in a wall?
walls_hit = arcade.check_for_collision_with_list(self.player_sprite, self.wall_list)
if len(walls_hit) == 0:
# Not in a wall! Success!
placed = True
# Score
self.score = 0
# Set up the player
# Character image from kenney.nl
self.player_sprite = arcade.Sprite(":resources:images/animated_characters/female_person/femalePerson_idle.png", SPRITE_SCALING_PLAYER)
self.player_sprite.center_x = 50
self.player_sprite.center_y = 50
self.player_list.append(self.player_sprite)
# Create the coins
for i in range(COIN_COUNT):
# Create the coin instance
# Coin image from kenney.nl
coin = arcade.Sprite(":resources:images/items/coinGold.png", SPRITE_SCALING_COIN)
# Position the coin
coin.center_x = random.randrange(SCREEN_WIDTH)
coin.center_y = random.randrange(SCREEN_HEIGHT)
# Set up the initial angle, and the "spin"
coin.angle = random.randrange(360)
coin.change_angle = random.randrange(-5, 6)
# Add the coin to the lists
self.coin_list.append(coin)
self.static_wall_list.append(wall)
self.all_wall_list.append(wall)
# Create platform side to side
wall = arcade.Sprite(":resources:images/tiles/grassMid.png", SPRITE_SCALING)
wall.center_y = 3 * GRID_PIXEL_SIZE
wall.center_x = 3 * GRID_PIXEL_SIZE
wall.boundary_left = 2 * GRID_PIXEL_SIZE
wall.boundary_right = 5 * GRID_PIXEL_SIZE
wall.change_x = 2 * SPRITE_SCALING
self.all_wall_list.append(wall)
self.moving_wall_list.append(wall)
# Create platform side to side
wall = arcade.Sprite(":resources:images/tiles/grassMid.png", SPRITE_SCALING)
wall.center_y = 3 * GRID_PIXEL_SIZE
wall.center_x = 7 * GRID_PIXEL_SIZE
wall.boundary_left = 5 * GRID_PIXEL_SIZE
wall.boundary_right = 9 * GRID_PIXEL_SIZE
wall.change_x = -2 * SPRITE_SCALING
self.all_wall_list.append(wall)
self.moving_wall_list.append(wall)
# Create platform moving up and down
wall = arcade.Sprite(":resources:images/tiles/grassMid.png", SPRITE_SCALING)
wall.center_y = 5 * GRID_PIXEL_SIZE
wall.center_x = 5 * GRID_PIXEL_SIZE
wall.boundary_top = 8 * GRID_PIXEL_SIZE
wall.boundary_bottom = 4 * GRID_PIXEL_SIZE
wall.change_y = 2 * SPRITE_SCALING