Python Forum
moving bullets up the screen using y-coordinate
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
moving bullets up the screen using y-coordinate
#1
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?
Reply
#2
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}")
Reply
#3
(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!!
Reply
#4
You find interesting history facts about this here.
Reply
#5
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] When I hit the space bar from the home screen, it sends me to the game over screen JesusisKing 1 954 Apr-30-2023, 10:11 PM
Last Post: deanhystad
  [PyGame] pygame.draw.rect function stretches across the screen instead of moving BubblesTheGiraffe 2 3,576 Jun-11-2019, 08:32 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

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