Python Forum
[PyGame] Confused with Pygame documentation. (Newbie here)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] Confused with Pygame documentation. (Newbie here)
#1
Sorry, I'm new and really confused by a few things with the tutorial I'm using. I've been looking for answers for like 3 hours. Please help!

# Define the enemy object by extending pygame.sprite.Sprite
# The surface you draw on the screen is now an attribute of 'enemy'
class Enemy(pygame.sprite.Sprite):
    def __init__(self):
        super(Enemy, self).__init__()
        self.surf = pygame.Surface((20, 10))
        self.surf.fill((255, 255, 255))
        self.rect = self.surf.get_rect(
            center=(
                random.randint(SCREEN_WIDTH + 20, SCREEN_WIDTH + 100),
                random.randint(0, SCREEN_HEIGHT),
            )
        )
        self.speed = random.randint(5, 20)

    # Move the sprite based on speed
    # Remove the sprite when it passes the left edge of the screen
    def update(self):
        self.rect.move_ip(-self.speed, 0)
        if self.rect.right < 0:
            self.kill()
My confusion is in the last part...
 self.rect.move_ip(-self.speed, 0)
Why minus self for speed? And what is the zero for?

if self.rect.right < 0:
            self.kill()
And what does the "right" here mean? I understand it's to wipe the sprite enemy from the screen. Does "right" mean the screen border or something? Super confused! )=

The website I'm using:
https://realpython.com/pygame-a-primer/
Reply
#2
There is a section in the forum devoted to writing games. You'll get the best response posting there.

https://python-forum.io/forum-11.html

And here's some pretty good Pygame documentation.

https://www.pygame.org/docs/

From the documentation:
Quote:move_ip()
moves the rectangle, in place
move_ip(x, y) -> None
Same as the Rect.move() method, but operates in place.
So this code:
self.rect.move_ip(-self.speed, 0)
Moves the rectangle, self.rect in the x direction by -self.speed and in the y direction by 0.

And there is documentation about rectangles here.

https://www.pygame.org/docs/ref/rect.html
monkeydesu likes this post
Reply
#3
(Sep-10-2022, 04:07 AM)monkeydesu Wrote:
if self.rect.right < 0:
            self.kill()
And what does the "right" here mean? I understand it's to wipe the sprite enemy from the screen. Does "right" mean the screen border or something? Super confused! )=

The "right" is the right side of the your rectangle. "0" means the left side of the screen, as the y value is 0. So if the right side of your sprite is less than 0, that means your whole sprite is off the screen to the left.
monkeydesu likes this post
Reply
#4
(Sep-10-2022, 02:36 PM)XavierPlatinum Wrote:
(Sep-10-2022, 04:07 AM)monkeydesu Wrote:
if self.rect.right < 0:
            self.kill()
And what does the "right" here mean? I understand it's to wipe the sprite enemy from the screen. Does "right" mean the screen border or something? Super confused! )=

The "right" is the right side of the your rectangle. "0" means the left side of the screen, as the y value is 0. So if the right side of your sprite is less than 0, that means your whole sprite is off the screen to the left.

Ah! That makes so much sense! Thank you haha
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020