0

So I'm starting with classes, groups and sprites. Everything seems to be right in my code but it just does not run. I have tried detecting that what runs by using print("hello") command but haven't gotten anything back.

class Spaceship(pygame.sprite.Sprite): ###thigs that fly arround when GUI == 4

def __init__(self, ships, GUIstatus):

    # ships is a Group for all ships 
    pygame.sprite.Sprite.__init__(self, ships)
    self.shipUp = pygame.image.load("icons/shipUP.png")
    self.shipRight = pygame.image.load("icons/shipRIGHT.png")
    self.shipLeft = pygame.image.load("icons/shipLEFT.png")
    self.shipDown = pygame.image.load("icons/shipDOWN.png")

    # the image has to be in self.image
    self.image = self.shipLeft

    # no need for shipnpc_x and shipnpc_y, since we store
    # our position in self.rect
    self.rect = self.image.get_rect()
    self.GUIstatus = GUIstatus

    self.shipType = random.randrange(0,5)

# this has to be called update instead of update
# to work with groups
def update(self):

    # to change the position, we just alter self.rect

    if self.exists == 0 and self.GUIstatus == 4:
        if dirrection == LEFT:
            #self.rect.move_ip(-self.speed, 0)
            self.shipImage = self.shipLeft
            print("Hello i'm a spacecraft")

        if dirrection == RIGHT:
            #self.rect.move_ip(self.speed, 0)
            self.shipImage = self.shipRight

        if dirrection == UP:
            #self.rect.move_ip(0, -self.speed)
            self.shipImage = self.shipUp

        if dirrection == DOWN:
            #self.rect.move_ip(0, self.speed)
            self.shipImage = self.shipDown

Then I'm using this:

ships = pygame.sprite.Group()
for _ in range(30):
    Spaceship(ships, GUIstatus)

and then for drawing and updating:

ships.update()
ships.draw(screen)

But nothing is running it seems? Why?

Ted Klein Bergman
  • 9,146
  • 4
  • 29
  • 50
cmdtvt
  • 71
  • 1
  • 10
  • 4
    Is that your actual indentation? Please fix your formatting if so. Furthermore, do you see any error messages at all? – Ken Y-N Dec 21 '16 at 07:48
  • No i get no error messages atall – cmdtvt Dec 21 '16 at 07:56
  • 1
    Well then, your formatting must be suspect and `update(self)` is not being called, as because there is no member variable `self.exists` you would see an error if `update(self)` were actually called. – Ken Y-N Dec 21 '16 at 08:15
  • Ok that seems to help it alot i added self.excist=0 and then i move ship update to main loop. It know draws a ship tp upper left corner but movement still does not register? – cmdtvt Dec 21 '16 at 08:53
  • BTW: `Group()` draws image from `self.image` but you assign to `self.shipImage` – furas Dec 21 '16 at 12:47
  • ye i assign thaht self.image = self.shipImage that should work right? – cmdtvt Dec 22 '16 at 06:58
  • and still can't get the things moving on screen – cmdtvt Dec 22 '16 at 06:58

0 Answers0