2

I made this game and it WORKS!! Well, it works occasionally. For some reasons, sometimes either the gas sprite or the redCar sprite just disappears for no reason...

Here is the code:

"""
Player Car
"""

import pygame, random
pygame.init()

screen = pygame.display.set_mode((640, 480))

class RedCar(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("red.png")
        self.image = pygame.transform.scale(self.image,(50,75))
        self.rect = self.image.get_rect()

        if not pygame.mixer:
            print("problem with sound")
        else:
            pygame.mixer.init()
            self.sndYay = pygame.mixer.Sound("yay.ogg")
            self.sndThunder = pygame.mixer.Sound("thunder.ogg")

    def update(self):
        mousex, mousey = pygame.mouse.get_pos()
        self.rect.center = (mousex, 430)

class GasCan(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("gas.png")
        self.image = pygame.transform.scale(self.image,(50, 50))
        self.image = self.image.convert_alpha()
        self.rect = self.image.get_rect()
        self.reset()

        self.dy = 5

    def update(self):
        self.rect.centery += self.dy
        if self.rect.top > screen.get_height():
            self.reset()

    def reset(self):
        self.rect.top = 0
        self.rect.centerx = random.randrange(0, screen.get_width())

class YellowCar(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("yellow.png")
        self.image = pygame.transform.scale(self.image,(50, 75))
        self.rect = self.image.get_rect()

    def update(self):
        self.rect.centery += self.dy
        #self.rect.centery += random.randrange(5, 20)
        if self.rect.top > screen.get_height():
            self.reset()

    def reset(self):
        self.rect.bottom = 0
        self.rect.centerx = random.randrange(190, screen.get_width())
        self.dy = random.randrange(10, 30)

class Bush(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("bush.png")
        self.image = pygame.transform.scale(self.image,(50, 50))
        self.rect = self.image.get_rect()

    def update(self):
        self.rect.centery += 10
        if self.rect.top > screen.get_height():
            self.reset()

    def reset(self):
        self.rect.bottom = 0
        self.rect.centerx = random.randrange(0, 150)

class Road(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("road.png")
        #self.image = pygame.transform.scale(self.image,(640, 480))
        self.rect = self.image.get_rect()
        self.dy = 10
        self.reset()

    def update(self):
        self.rect.bottom += self.dy
        if self.rect.top >= 0:
            self.reset()

    def reset(self):
        self.rect.bottom = screen.get_height()

class Scoreboard(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.lives = 11
        self.score = 0
        self.level = 0
        self.font = pygame.font.SysFont("None", 50)

    def update(self):
        self.text = "Car: %d, score: %d, level: %d" % (self.lives, self.score, self.level)
        self.image = self.font.render(self.text, 1, (255, 255, 0))
        self.rect = self.image.get_rect()

def Game():
    pygame.display.set_caption("Car")

   # level =
    background = pygame.Surface(screen.get_size())
    background.fill((0, 0, 0))
    screen.blit(background, (0, 0))
    redCar = RedCar()
    gas = GasCan()
    yellowCar1 = YellowCar()
    yellowCar2 = YellowCar()
    yellowCar3 = YellowCar()
    yellowCar4 = YellowCar()
    yellowCar5 = YellowCar()
    bush1 = Bush()
    bush2 = Bush()
    bush3 = Bush()
    road = Road()
    scoreboard = Scoreboard()

    goodSprites = pygame.sprite.Group(gas, road, redCar)
    scoreSprite = pygame.sprite.Group(scoreboard)
    badSprites = pygame.sprite.Group(yellowCar1, yellowCar2, yellowCar3, yellowCar4, bush1, bush2, bush3)

    clock = pygame.time.Clock()
    keepGoing = True
    while keepGoing:
        clock.tick(30)
        pygame.mouse.set_visible(False)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                keepGoing = False

      #  if scoreboard.score < 500:
      #      badSprites = pygame.sprite.Group(yellowCar1, yellowCar2, yellowCar3, bush1)
      #      scoreboard.level = 1
      #  elif scoreboard.score < 1000:
      #      badSprites.add(yellowCar4)
      #      badSprites.add(bush2)
      #      scoreboard.level = 2
      #  elif scoreboard.score < 1500:
      #      badSprites.add(yellowCar5)
      #      badSprites.add(bush3)
      #      scoreboard.level = 3

        if redCar.rect.colliderect(gas.rect):
            redCar.sndYay.play()
            gas.reset()
            scoreboard.score += 100

        if scoreboard.score % 5 == 0:
            scoreboard.lives + 1

        hitCar = pygame.sprite.spritecollide(redCar, badSprites, False)

        if hitCar:
            redCar.sndThunder.play()
            scoreboard.lives -= 1
            if scoreboard.lives <= 0:
                keepGoing = False
            for theCar in hitCar:
                theCar.reset()

        goodSprites.clear(screen, background)
        badSprites.clear(screen, background)
        scoreSprite.clear(screen, background)

        goodSprites.update()
        badSprites.update()
        scoreSprite.update()

        goodSprites.draw(screen)
        badSprites.draw(screen)
        scoreSprite.draw(screen)

        pygame.display.flip()

    pygame.mouse.set_visible(True)
    return scoreboard.score

def instructions(score):
    redCar = RedCar()
    road = Road()

    allSprites = pygame.sprite.Group(road, redCar)
    insFont = pygame.font.SysFont(None, 30)

    instructions = (
    "Car Survival.     Last score: %d" % score ,
    "Instructions:  Drive your car,",
    "and avoid cars and bushes.",
    "",
    "Gain points by driving over the gas cans.",
    "",
    "good luck!",
    "",
    "click to start, escape to quit."
    )

    insLabels = []
    # Create a rendered list of instructions, ready for display
    for line in instructions:
        tempLabel = insFont.render(line, 1, (255, 0, 0))
        insLabels.append(tempLabel)

    keepGoing = True
    clock = pygame.time.Clock()
    pygame.mouse.set_visible(False)
    while keepGoing:
        clock.tick(30)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                keepGoing = False
                donePlaying = True

            # Check for a mouse click
            if event.type == pygame.MOUSEBUTTONDOWN:
                keepGoing = False
                donePlaying = False
            # .... or a quit
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    keepGoing = False
                    donePlaying = True

        allSprites.update()
        allSprites.draw(screen)

        # Display the instructions
        for i in range(len(insLabels)):
            screen.blit(insLabels[i], (50, 30*i))

        pygame.display.flip()

    pygame.mouse.set_visible(True)

    return donePlaying

def main():
    donePlaying = False
    score = 0
    while not donePlaying:
        donePlaying = instructions(score)
        if not donePlaying:
            score = Game()
    pygame.quit()

if __name__ == "__main__":
    main()

Sometimes the redCar appears, but sometimes it doesnt. Does anybody have any clue?

4

0 回答 0