Course Content
PyGame Project: Arcade Game
PyGame Project: Arcade Game
Managing Memory and Removing Off-Screen Objects
In this section of your game code, we're dealing with removing enemies and bonuses that move off the screen. Here's a breakdown for beginners:
Removing Enemies
- Loop Through Enemies: the line
for enemy in enemies:
goes through each enemy in the enemies list one by one; - Check Position:
if enemy[1].left < 0:
checks if an enemy has moved past the left edge of the screen. The.left
refers to the x-coordinate of the enemy's rectangle; if it's less than 0, part of the enemy is off-screen; - Remove Off-screen Enemy:
enemies.pop(enemies.index(enemy))
removes the enemy from the list.enemies.index(enemy)
finds the position of the current enemy in the list, and.pop()
removes it. This keeps the game clean by only having active, on-screen enemies.
Removing Bonuses
- Loop Through Bonuses: similar to enemies,
for bonus in bonuses:
iterates over each bonus in the list; - Check Position:
if bonus[1].bottom > HEIGHT:
checks if a bonus has moved below the bottom edge of the screen..bottom
is the y-coordinate of the lowest part of the bonus; if it's greater thanHEIGHT
, the bonus is off-screen. - Remove Off-screen Bonus: like with enemies,
bonuses.pop(bonuses.index(bonus))
removes the bonus from the list. This prevents bonuses that have gone uncollected from cluttering the game.
By removing objects that are no longer visible or interactable, this code helps manage the game's performance and keeps the gameplay experience smooth and enjoyable.
Swipe to show code editor
- Iterate through enemies: use
for enemy in enemies:
to process each enemy; - Identify and remove off-screen enemy: find enemies with
if enemy[1].left < 0:
and remove usingenemies.pop(enemies.index(enemy))
;
- Iterate through bonuses: use
for bonus in bonuses:
to go through each bonus; - Identify and remove off-screen bonus: detect bonuses with
if bonus[1].bottom > HEIGHT
and remove withbonuses.pop(bonuses.index(bonus))
.
Thanks for your feedback!
In this section of your game code, we're dealing with removing enemies and bonuses that move off the screen. Here's a breakdown for beginners:
Removing Enemies
- Loop Through Enemies: the line
for enemy in enemies:
goes through each enemy in the enemies list one by one; - Check Position:
if enemy[1].left < 0:
checks if an enemy has moved past the left edge of the screen. The.left
refers to the x-coordinate of the enemy's rectangle; if it's less than 0, part of the enemy is off-screen; - Remove Off-screen Enemy:
enemies.pop(enemies.index(enemy))
removes the enemy from the list.enemies.index(enemy)
finds the position of the current enemy in the list, and.pop()
removes it. This keeps the game clean by only having active, on-screen enemies.
Removing Bonuses
- Loop Through Bonuses: similar to enemies,
for bonus in bonuses:
iterates over each bonus in the list; - Check Position:
if bonus[1].bottom > HEIGHT:
checks if a bonus has moved below the bottom edge of the screen..bottom
is the y-coordinate of the lowest part of the bonus; if it's greater thanHEIGHT
, the bonus is off-screen. - Remove Off-screen Bonus: like with enemies,
bonuses.pop(bonuses.index(bonus))
removes the bonus from the list. This prevents bonuses that have gone uncollected from cluttering the game.
By removing objects that are no longer visible or interactable, this code helps manage the game's performance and keeps the gameplay experience smooth and enjoyable.
Swipe to show code editor
- Iterate through enemies: use
for enemy in enemies:
to process each enemy; - Identify and remove off-screen enemy: find enemies with
if enemy[1].left < 0:
and remove usingenemies.pop(enemies.index(enemy))
;
- Iterate through bonuses: use
for bonus in bonuses:
to go through each bonus; - Identify and remove off-screen bonus: detect bonuses with
if bonus[1].bottom > HEIGHT
and remove withbonuses.pop(bonuses.index(bonus))
.