Bottom Page

Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Pygame Class Sprite Placement Confusion
I'm feeling so angry right now over something that I assume is trivial. First I hope that whoever has the misfortune of reading my question to bear with me because I have to post code in an awkward manner due to the object oriented nature of this lesson in the book. I've been having trouble working through the pygame API, namely the classes and the direction in which the ship would be placed but let me show the code.
Main file or Alien
from pygame 
from settings import Settings
#I'll omit settings for the sake of brevity. Game width 1200, game height = 800, bg_color = white

def run_game():
ai_settings = Settings() #A little shaky on this. I presume it allows the instance to access the attributes of the Settings class which seems to make the most sense to me.

screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height))
pygame.display.set_caption("Alien Invasion")

ship = Ship(screen) #Why do you need to pass in screen? Is the Ship class able to understand what all the parameters are doing? Ie the width, height, and background? What does that have to do with the other attributes in the ship class?

while True:

run_game() new python file containing the class

class Ship()
self.screen = screen

self.image = pygame.image.load("shimp.bmp")
self.rect = self.image.get_rect()
self.screen_rect = screen.get_rect()

self.rect.centerx = self.screen_rect.centerx #What the hell is this doing? I have no idea.
self.rect.bottom = self.screen_rect.bottom #Yet again the books explanation doesn't really help nor does google

def bliteme(self):
Basically I don't understand what self.rect.centerx = self.screen_rect.centerx is doing in the ship class. I do not know what self.rect.bottom = self.screen_rect.bottom is doing either. And for ship = Ship(Screen) this also haunts me... What is the screen parameter doing with the ship class that makes you need it? Sorry if this post was huge but I'm really confused.
ive got little time so i will be short to at least answer your question.

you need to pass in screen because the Ship object needs to know the size of the screen to be able to put hte ship in the center of the screen.

(Sep-11-2018, 06:22 AM)TheHumbleIdiot Wrote: self.rect.centerx = self.screen_rect.centerx #What the hell is this doing? I have no idea.
self.rect.bottom = self.screen_rect.bottom #Yet again the books explanation doesn't really help nor does google
you are setting the center screen x position (from left to right) to the Ship's object center and setting the rects bottom to the bottom of the screen. This will put the ship in the center of the screen width wise, but at the top-down wise. Pygame rects have positions and you can move the image with the pygame rect instead of saving an x and y coordinate yourself. This is why you blit the image to the rect position because you are moving the rect.

Regardless of the what you think currently, putting the ship content like this in the Ship class makes it much more organized and easier to identify and fix problems. As well as for others like us to understand. So its not complex by any means to us. The ships position, image, drawing ,etc are done inside the class. This clears up your main game loop which is what you want to constantly keep doing. As the Ship class starts to get bigger and bigger you will understand why its all in a class.
I appreciate the time that you took to look at my post and answer my question. I know know better to argue against the wisdom of more seasoned programmers, like in the book, but I think I was being impatient with my understanding of the topic which isn't a good thing by any means. Thank you for your help because I can now see the logic behind the code. You are a godsend my man. I also looked at the recommended tutorials you threw down and will make sure to articulate my question better in the future. THANKS.

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  [PyGame] My Pygame Sprite not appearing... noodlespinbot 2 351 Apr-08-2020, 12:48 PM
Last Post: noodlespinbot
  My Pygame Sprite not appearing... noodlespinbot 1 282 Apr-08-2020, 11:25 AM
Last Post: pyzyx3qwerty
  [PyGame] Sprite image.get_rect() moves sprite to 0, 0 michael1789 2 491 Dec-13-2019, 08:37 PM
Last Post: michael1789
  Pygame sprite not moving michael1789 1 476 Nov-10-2019, 03:54 AM
Last Post: michael1789
  Python BattleShips Random Ship placement FnaticPutin 2 3,644 Oct-13-2017, 11:33 PM
Last Post: Windspar

Forum Jump:

Users browsing this thread: 1 Guest(s)