Conteúdo do Curso
PyGame Project: Arcade Game
PyGame Project: Arcade Game
Generating and Managing Enemies
In our game world, we need challengers for our players, and that's where our create_enemy
function comes into play. Let's break down how it adds a new enemy to the game, step by step:
Load and Size the Enemy Image
First, we use pygame.transform.scale(pygame.image.load('rocket.png'), (100, 50))
to load an image called 'rocket.png'.
We resize this image to 100 pixels wide and 50 pixels tall. This makes sure our enemy looks just right in the game;
Place the Enemy
Next, we use pygame.Rect(WIDTH, random.randint(0, HEIGHT), *enemy.get_size())
to decide where the enemy appears.
This creates a rectangle (a space on the screen) where our enemy will be. It starts at the right edge (WIDTH) and at a random height between the top and bottom of the screen (random.randint(0, HEIGHT)). The size matches our enemy's image size (enemy.get_size()).
Set the Enemy in Motion
We then determine how the enemy moves with [random.randint(-6, -1), 0]
.
This makes the enemy move leftwards at a random speed between 1 and 6 pixels per frame (the negative numbers mean moving left). The 0 means there is no up or down movement.
Send the Enemy into the Game:
Finally, return [enemy, enemy_rect, enemy_move]
sends our newly created enemy into the game.
This gives back the enemy's image, its position, and how it should move, so we can use them in the game.
This function is like a recipe for making enemies in our game. It adds fun and challenge by bringing new foes for players to encounter.
Note
*enemy.get_size()
The asterisk before the tuple unpacks it into two dimensions: height and width of the picture (in our case, 100 and 50 pixels).
Tarefa
- Start by defining a function called
create_enemy
that will handle the creation of enemy objects in the game; - Use
pygame.transform.scale(pygame.image.load( 'rocket.png'), (100, 50))
to load the 'rocket.png' image and resize it to 100x50 pixels, preparing the visual representation of the enemy; - Create a
pygame.Rect
withenemy_rect = pygame.Rect( WIDTH, random.randint(0, HEIGHT), *enemy.get_size())
to set the initial position of the enemy at the right edge of the screen (WIDTH) and at a random height between the top and bottom (HEIGHT). The size of the rectangle matches the size of the enemy image; - Assign
enemy_move = [random.randint(-6, -1), 0]
to define how the enemy will move. This makes the enemy move left at a random speed between 1 and 6 pixels per frame (indicated by the negative horizontal value) without moving up or down (vertical value is 0); - Conclude the function with
return [enemy, enemy_rect, enemy_move]
, providing the necessary elements (image, position, and movement vector) to interact with and display the enemy in the game.
Obrigado pelo seu feedback!
In our game world, we need challengers for our players, and that's where our create_enemy
function comes into play. Let's break down how it adds a new enemy to the game, step by step:
Load and Size the Enemy Image
First, we use pygame.transform.scale(pygame.image.load('rocket.png'), (100, 50))
to load an image called 'rocket.png'.
We resize this image to 100 pixels wide and 50 pixels tall. This makes sure our enemy looks just right in the game;
Place the Enemy
Next, we use pygame.Rect(WIDTH, random.randint(0, HEIGHT), *enemy.get_size())
to decide where the enemy appears.
This creates a rectangle (a space on the screen) where our enemy will be. It starts at the right edge (WIDTH) and at a random height between the top and bottom of the screen (random.randint(0, HEIGHT)). The size matches our enemy's image size (enemy.get_size()).
Set the Enemy in Motion
We then determine how the enemy moves with [random.randint(-6, -1), 0]
.
This makes the enemy move leftwards at a random speed between 1 and 6 pixels per frame (the negative numbers mean moving left). The 0 means there is no up or down movement.
Send the Enemy into the Game:
Finally, return [enemy, enemy_rect, enemy_move]
sends our newly created enemy into the game.
This gives back the enemy's image, its position, and how it should move, so we can use them in the game.
This function is like a recipe for making enemies in our game. It adds fun and challenge by bringing new foes for players to encounter.
Note
*enemy.get_size()
The asterisk before the tuple unpacks it into two dimensions: height and width of the picture (in our case, 100 and 50 pixels).
Tarefa
- Start by defining a function called
create_enemy
that will handle the creation of enemy objects in the game; - Use
pygame.transform.scale(pygame.image.load( 'rocket.png'), (100, 50))
to load the 'rocket.png' image and resize it to 100x50 pixels, preparing the visual representation of the enemy; - Create a
pygame.Rect
withenemy_rect = pygame.Rect( WIDTH, random.randint(0, HEIGHT), *enemy.get_size())
to set the initial position of the enemy at the right edge of the screen (WIDTH) and at a random height between the top and bottom (HEIGHT). The size of the rectangle matches the size of the enemy image; - Assign
enemy_move = [random.randint(-6, -1), 0]
to define how the enemy will move. This makes the enemy move left at a random speed between 1 and 6 pixels per frame (indicated by the negative horizontal value) without moving up or down (vertical value is 0); - Conclude the function with
return [enemy, enemy_rect, enemy_move]
, providing the necessary elements (image, position, and movement vector) to interact with and display the enemy in the game.