Python Forum
Pygame Class Sprite Placement Confusion
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pygame Class Sprite Placement Confusion
#1
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 invasion.py
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():
pygame.init()
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:
screen.fill(ai_settings.bg_color)
ship.blitme()
pygame.display.flip()

run_game()

#Ship.py new python file containing the class

class Ship()
def__init__(self,screen):
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):
self.screen.blit(self,image,self.rect)
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.
Reply


Messages In This Thread
Pygame Class Sprite Placement Confusion - by TheHumbleIdiot - Sep-11-2018, 06:22 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] Sprites just randomly appear and dissapear in my pygame.sprite.GoupeSingle trueShadoWrr 2 1,979 Feb-13-2023, 09:34 AM
Last Post: Vadanane
  [PyGame] Pygame is treating blob_group as a surface, when I need it to treat it as a Sprite. Swagford 1 1,302 Jan-24-2023, 09:58 PM
Last Post: metulburr
  FileNotFoundError when I try putting sprite in Pygame zionkozisek 9 16,099 Dec-09-2020, 04:42 AM
Last Post: zionkozisek
  [PyGame] My Pygame Sprite not appearing... noodlespinbot 3 3,831 Oct-30-2020, 06:51 AM
Last Post: robinmurphy
  My Pygame Sprite not appearing... noodlespinbot 1 2,264 Apr-08-2020, 11:25 AM
Last Post: pyzyx3qwerty
  [PyGame] Sprite image.get_rect() moves sprite to 0, 0 michael1789 2 4,600 Dec-13-2019, 08:37 PM
Last Post: michael1789
  Pygame sprite not moving michael1789 1 2,832 Nov-10-2019, 03:54 AM
Last Post: michael1789
  Sprite not rendering Clunk_Head 2 2,140 Oct-03-2019, 11:27 AM
Last Post: Clunk_Head
  Need help making a sprite GalaxyCoyote 4 3,238 Aug-11-2019, 09:12 PM
Last Post: metulburr
  moving a sprite pfaber11 3 2,587 May-15-2019, 12:52 PM
Last Post: pfaber11

Forum Jump:

User Panel Messages

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