Created
February 24, 2022 12:24
-
-
Save dfbarrero/83294a5ea218ca866b68558d4d64ba46 to your computer and use it in GitHub Desktop.
Example of sprites usage in Arcade. You will need the sprite located in https://learn.arcade.academy/en/latest/_images/character1.png. The original code has been taken from https://learn.arcade.academy/en/latest/chapters/25_sprites_and_walls/sprites_and_walls.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ Sprite Sample Program """ | |
| import arcade | |
| # --- Constants --- | |
| SPRITE_SCALING_BOX = 0.5 | |
| SPRITE_SCALING_PLAYER = 0.5 | |
| SCREEN_WIDTH = 800 | |
| SCREEN_HEIGHT = 600 | |
| MOVEMENT_SPEED = 5 | |
| class MyGame(arcade.Window): | |
| """ This class represents the main window of the game. """ | |
| def __init__(self): | |
| """ Initializer """ | |
| # Call the parent class initializer | |
| super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, "Sprites With Walls Example") | |
| # Sprite lists | |
| self.player_list = None | |
| self.wall_list = None | |
| # Set up the player | |
| self.player_sprite = None | |
| # This variable holds our simple "physics engine" | |
| self.physics_engine = None | |
| def setup(self): | |
| # Set the background color | |
| arcade.set_background_color(arcade.color.AMAZON) | |
| # Sprite lists | |
| self.player_list = arcade.SpriteList() | |
| self.wall_list = arcade.SpriteList() | |
| # Reset the score | |
| self.score = 0 | |
| # Create the player | |
| self.player_sprite = arcade.Sprite("character.png", SPRITE_SCALING_PLAYER) | |
| self.player_sprite.center_x = 50 | |
| self.player_sprite.center_y = 64 | |
| self.player_list.append(self.player_sprite) | |
| def on_draw(self): | |
| arcade.start_render() | |
| self.wall_list.draw() | |
| self.player_list.draw() | |
| def main(): | |
| window = MyGame() | |
| window.setup() | |
| arcade.run() | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment