0
votes
import pygame
from pygame import *
import sys

pygame.init()

displayWidth = 800
displayHeight = 448

screen = pygame.display.set_mode((displayWidth, displayHeight))

clock = pygame.time.Clock()

black = (0, 0, 0)

level = [
        "PPPPPPPPPPPPPPPPPPPPPPPPP",
        "P                       P",
        "P                       P",
        "P                       P",
        "P                       P",
        "P                       P",
        "P                       P",
        "P           PPPPP       P",
        "P                       P",
        "P                       P",
        "P                       P",
        "P                       P",
        "P                       P",
        "PPPPPPPPPPPPPPPPPPPPPPPPP",]

playerImg = pygame.image.load('monkeyStanding.png').convert_alpha()
bananaImg = pygame.image.load('Banana.jpg').convert_alpha()

class Player(pygame.sprite.Sprite):
    def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self)
        self.dx = 0
        self.dy = 0
        self.onGround = False
        self.image = playerImg
        self.rect = self.image.get_rect()
        self.rect.left = x
        self.rect.top = y


    def update(self, up, down, left, right, platforms):
        if up:
            #Only jump if on the ground
            if self.onGround:
                self.dy = -5

        if left:
            self.dx = -2

        if right:
            self.dx = 2

        if not self.onGround:
            self.dy += .1

        if not (left or right):
            self.dx = 0

        self.rect.left += self.dx

        self.collide(self.dx, 0, platforms)

        self.rect.top += self.dy

        self.onGround = False

        self.collide(0, self.dy, platforms)

    def collide(self, dx, dy, platforms):
        for p in platforms:
            if pygame.sprite.collide_rect(self, p):

                if dx > 0:
                    self.rect.right = p.rect.left
                    print 'collide right'
                if dx < 0:
                    self.rect.left = p.rect.right
                    print 'collide left'
                if dy > 0:
                    self.rect.bottom = p.rect.top
                    self.onGround = True
                    self.yvel = 0
                if dy < 0:
                    self.rect.top = p.rect.bottom

class Platform(pygame.sprite.Sprite):
    def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self)
        self.image = Surface((32, 32))
        self.image.convert()
        self.image.fill((255, 255, 255))
        self.rect = Rect(x, y, 32, 32)

    def update(self):
        pass

class Banana(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = bananaImg
        self.rect = self.image.get_rect()
        self.rect.left = 100
        self.rect.top = 100
        self.dx = 2

    def update(self):
        self.rect.left += self.dx

def main():

    up = down = left = right = False

    bg = Surface((32, 32))
    bg.convert()

    bg.fill(black)

    player = Player(100, 100)
    banana = Banana()

    platforms = []

    allSprites = pygame.sprite.Group(player)
    banSprites = pygame.sprite.Group()

    x = y = 0

    # build the level
    for row in level:
        for col in row:
            if col == "P":
                p = Platform(x, y)
                platforms.append(p)
                allSprites.add(p)

            x += 32
        y += 32
        x = 0

    while True:
        for e in pygame.event.get():
            if e.type == QUIT:
                pygame.quit()
                sys.exit()

            if e.type == KEYDOWN:
                if e.key == K_UP:
                    up = True
                if e.key == K_LEFT:
                    left = True
                if e.key == K_RIGHT:
                    right = True

                if e.key == K_SPACE:
                    banSprites.add(banana)

                if e.key == 27:
                    pygame.quit()
                    sys.exit()

            if e.type == KEYUP:
                if e.key == K_UP:
                    up = False
                if e.key == K_RIGHT:
                    right = False
                if e.key == K_LEFT:
                    left = False
                if e.key == K_RIGHT:
                    right = False

        # draw background
        for y in range(32):
            for x in range(32):
                screen.blit(bg, (x * 32, y * 32))

        allSprites.clear(screen, bg)
        banSprites.clear(screen, bg)

        # update player, draw everything else
        player.update(up, down, left, right, platforms)
        banSprites.update()
        allSprites.draw(screen)
        banSprites.draw(screen)

        pygame.display.update()

if __name__ == '__main__':
    main()

So I am making a platform game and everything is working well so far, but when I try to make a banana image by pressing space it will make the first one, but once it flies off the screen, you cannot make anymore. Does anyone know what I am doing wrong? I am new to pygame and am still a little shaky on calling the class objects in the main game loop. Thanks!

1

1 Answers

1
votes

When the banana exits the screen, you never remove it from the group, thus making the program think that the banana is still there and will prevent anymore bananas from coming. Add this code with the code that deals with the banana exiting the screen:

banSprites.remove(banana)