Example #1
0
def runGame():
    ball,bumper,blocks = setup()
    score = 0
    blocksDestroyed = 0
    #use a random number to tell which way to start the ball off
    randVal = random.random()
    if(randVal<=.5):
        direction = DL
    else:
        direction = DR
    start = False

    #define the beginning ball values
    scoreImage = pygame.image.load('Images/score.png')
    scoreFont = pygame.font.Font('freesansbold.ttf',37)
    
    while True:
        if start:
            direction = move(ball,bumper,direction)
            #if the ball hit the bottom of the screen, game over
            if direction == -1:
                Screens.gameOver()
                Screens.highScoreScreen(score)
                return
            blockHitY, blockHitX, direction = hitBlock(ball,blocks,direction)
            if blockHitX>=0 and blockHitY>=0:
                blocks[blockHitX][blockHitY].alive = False
                blocksDestroyed+=1
                score += 10
                if blocksDestroyed >= BLOCKSROW*BLOCKSCOLUMN:
                    blocks = resetBoard()
                    blocksDestroyed = 0

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.MOUSEBUTTONDOWN and event.button == LEFT:
                start = not start
            elif event.type == pygame.MOUSEMOTION:
                #the y of the bumper isn't going to move, so let's just ignore its return value
                tempx, tempy = pygame.mouse.get_pos()
                #if it's less than 63 or greater than 578, don't let it run off the screen
                if tempx <= BUMPERWIDTH/2:
                    bumper.x = -2
                elif tempx >= WINDOWWIDTH - (BUMPERWIDTH/2):
                    bumper.x = 520
                #the 63 centers it - the sprite is approximately 125 pixels wide
                else:
                    bumper.x = tempx-(BUMPERWIDTH/2)
        
        DISPLAYSURF.fill(WHITE)
        DISPLAYSURF.blit(BACKGROUND,(0,0))
        DISPLAYSURF.blit(bumper.img,(bumper.x,bumper.y))
        DISPLAYSURF.blit(ball.img, (ball.x,ball.y))
        DISPLAYSURF.blit(scoreImage, (440,20))
        scoreSurface = scoreFont.render(str(score), True, WHITE)
        scoreRect = scoreSurface.get_rect()
        scoreRect.topleft = (445+SCOREWIDTH, 25)
        DISPLAYSURF.blit(scoreSurface,scoreRect)
        drawBlocks(blocks)
        pygame.display.update()
        fpsClock.tick(FPS)