Python Forum

Full Version: moving bullets up the screen using y-coordinate
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, python world.
I'm studying python using the book called 'Python Crash Course'.
There's a portion of the book making a game using Pygame and I'm at the part to code bullet module for a ship to fire a bullet.
Here's a bit of the codes I'm confused about.
def __init__(self, ai_game):
    --snip--
    # Create a bullet rect at (0, 0) and then set correct position.
    self.rect = pygame.Rect(0, 0, self.settings.bullet_width,
        self.settings.bullet_height)
    self.rect.midtop = ai_game.ship.rect.midtop
    --snip--
def update(self):
    """Move the bullet up the screen."""
    # Update the decimal position of the bullet.
    self.y -= self.settings.bullet_speed
and, the last line "self.y -= self.settings.bullet_speed",
Why minus? the bullet's starting position is at the bottom and it's supposed to go up along the y-axis. what am i missing?
We are used to the origin in a coordinate system (0, 0) being in the bottom-left corner of a grid. In Pygame, and in many graphical frameworks, the origin is at the top left corner. So on a 1200x800 game window, the top left corner is (0, 0), and the bottom left corner is (0, 800). If you want to move up from the screen, you steadily decrease the y-value.

The origin is placed at the top because the top of a window is typically static on a screen. When people resize their windows, they typically drag the bottom of the screen, and any overflow happens at the bottom.

If you're curious to see the actual coordinates of any game object, you can throw a line into the _update_screen() method like this:

print(f"Ship coordinates: {ship.rect.x} {ship.rect.y}")
(Nov-09-2019, 02:08 AM)ehmatthes Wrote: [ -> ]In Pygame, and in many graphical frameworks, the origin is at the top left corner.

I re-read the book and the same explanation is here. I can't believe I missed it ha.
Thank you!!
(Nov-09-2019, 09:34 AM)ThomasL Wrote: [ -> ]You find interesting history facts about this here.

This is cool. It makes me want to learn about the hardware side of computer science field.