예제 #1
0
class RackerTest(unittest.TestCase):
    def setUp(self):
        self.racket = Racket(constants.SCREEN_MARGIN, constants.SCREEN_MARGIN)

    def test_update_position(self):
        """Tests the position update in the regular way"""
        self.racket.update_position(1)
        self.assertEqual(constants.SCREEN_MARGIN + 4, self.racket.position["y"])

    def test_update_position_with_acceleration(self):
        """Tests the position update with an acceleration factor"""
        self.racket.update_position(1, 30)
        self.assertEqual(constants.SCREEN_MARGIN + 4, self.racket.position["y"])
        self.assertEqual(self.racket.acceleration, 4)

    def test_update_position_with_deceleration(self):
        """Tests the position update with a deceleration factor"""
        self.racket.update_position(1, -22)
        self.assertEqual(constants.SCREEN_MARGIN + 3, self.racket.position["y"])
        self.assertEqual(self.racket.acceleration, 3)

    def test_limit_top(self):
        """Test that the racket cannot go beyond top limit"""
        self.racket.update_position(-3)
        self.assertEqual(constants.SCREEN_MARGIN, self.racket.position["y"])

    def test_limit_bottom(self):
        """Test that the racket cannot go beyond top limit"""
        position = constants.SCREEN_HEIGHT
        position -= constants.SCREEN_MARGIN
        position -= self.racket.dimension["height"]
        self.racket.update_position(position + 1)
        self.assertEqual(position, self.racket.position["y"])
예제 #2
0
    def __init__(self, screen, clock, res):

        self.res = res
        self.screen = screen
        self.clock = clock

        self.bg = pygame.image.load('bg.bmp')
        pygame.transform.scale(self.bg ,(self.res[0], self.res[1]))
        self.linethickness = rounder(self.res[0]/160)

        pygame.draw.line(self.bg, pygame.Color("white"),
            (self.res[0]*0.2375-self.linethickness, self.res[0]/16),
                (self.res[0]*0.2375-self.linethickness,self.res[1]), self.linethickness)

        pygame.draw.line(self.bg, pygame.Color("white"),
            (self.res[0]*0.7625+self.linethickness, self.res[0]/16),
                (self.res[0]*0.7625+self.linethickness, self.res[1]), self.linethickness)

        pygame.draw.line(self.bg, pygame.Color("white"),
            (self.res[0]*0.2375-self.linethickness,
                (self.res[0]/16 + self.linethickness*0.4)),
                    (self.res[0]*0.7625+self.linethickness,
                        (self.res[0]/16 + self.linethickness*0.4)), self.linethickness)

        self.screen.blit(self.bg, (0,0))
        pygame.display.flip()

        self.levelcount = 0

        self.racket = Racket('yellow', (self.res[0]*0.5, self.res[1]-40), self.res)

        self.score = Score(self.res)

        self.lives = Lives(self.res)

        self.currentpowerup = None
        self.powerupdrop = 60 * 15

        self.enteringname = False
예제 #3
0
class Breakout(object):

    def __init__(self, screen, clock, res):

        self.res = res
        self.screen = screen
        self.clock = clock

        self.bg = pygame.image.load('bg.bmp')
        pygame.transform.scale(self.bg ,(self.res[0], self.res[1]))
        self.linethickness = rounder(self.res[0]/160)

        pygame.draw.line(self.bg, pygame.Color("white"),
            (self.res[0]*0.2375-self.linethickness, self.res[0]/16),
                (self.res[0]*0.2375-self.linethickness,self.res[1]), self.linethickness)

        pygame.draw.line(self.bg, pygame.Color("white"),
            (self.res[0]*0.7625+self.linethickness, self.res[0]/16),
                (self.res[0]*0.7625+self.linethickness, self.res[1]), self.linethickness)

        pygame.draw.line(self.bg, pygame.Color("white"),
            (self.res[0]*0.2375-self.linethickness,
                (self.res[0]/16 + self.linethickness*0.4)),
                    (self.res[0]*0.7625+self.linethickness,
                        (self.res[0]/16 + self.linethickness*0.4)), self.linethickness)

        self.screen.blit(self.bg, (0,0))
        pygame.display.flip()

        self.levelcount = 0

        self.racket = Racket('yellow', (self.res[0]*0.5, self.res[1]-40), self.res)

        self.score = Score(self.res)

        self.lives = Lives(self.res)

        self.currentpowerup = None
        self.powerupdrop = 60 * 15

        self.enteringname = False


    def run(self):

        self.ball = Ball("yellow", (self.res[0]*0.475, self.res[1]-45), self.racket,
                    self.levelLoader(self.levelcount), self.res)

        self.keymap = {
            pygame.K_SPACE: [self.ball.start, nop],
            pygame.K_LEFT: [self.racket.left, self.racket.right],
            pygame.K_RIGHT: [self.racket.right, self.racket.left]
            }

        self.isrunning = True

        self.sprites = pygame.sprite.RenderUpdates([self.score, self.lives, self.ball, self.racket])

        while self.isrunning:

            self.blockimages = pygame.sprite.RenderUpdates(self.blocks)

            self.managePowerups()

            self.blockimages.update()
            self.blockimages.draw(self.screen)

            self.sprites.update()
            self.sprites.draw(self.screen)

            pygame.display.flip()
            self.sprites.clear(self.screen, self.bg)
            self.blockimages.clear(self.screen, self.bg)


            self.events = pygame.event.get()
            for event in self.events:
                if (event.type == QUIT) or ((event.type == KEYUP) and (event.key == K_ESCAPE)):
                    menu(self.screen, self.clock, self.res)

                if (event.type == pygame.KEYDOWN) and (event.key in self.keymap):
                    self.keymap[event.key][0]()
                if (event.type == pygame.KEYUP) and (event.key in self.keymap):
                    self.keymap[event.key][1]()
                if (event.type == pygame.USEREVENT):
                    if event.event == 'score':
                        self.score.update(event.score)
                    elif event.event == 'lives':
                        self.lives.update(event.lives)
                        if self.lives.lives == 0:
                            screen_message("Game Over!", self.screen, self.res)
                            time.sleep(1)
                            self.screen.blit(self.bg, (0,0))
                            pygame.display.flip()
                            self.gameOver()
                    else:
                      print event

            if len(self.blocks) == 0 and self.levelcount < 4:
                screen_message("Level complete", self.screen, self.res)
                self.levelcount += 1
                time.sleep(1)
                self.screen.blit(self.bg, (0,0))
                pygame.display.flip()
                self.ball.reset()
                self.ball.blocks = self.levelLoader(self.levelcount)

            elif len(self.blocks) == 0 and self.levelcount == 4:
                screen_message("You win!!!", self.screen, self.res)
                time.sleep(1)
                self.screen.blit(self.bg, (0,0))
                pygame.display.flip()
                self.gameOver()

            self.clock.tick(60)


    def levelLoader(self, levelcount):

            self.racket.reset()

            screen_message("Level " + str(self.levelcount+1), self.screen, self.res)
            time.sleep(1)
            self.screen.blit(self.bg, (0,0))
            pygame.display.flip()

            levels = [
                [0, 4, 4, 10],
                [2, 5, 3, 11],
                [1, 5, 2, 12],
                [0, 6, 1, 13],
                [0, 6, 0, 14]
                #[0, 1, 0, 1],
                #[0, 1, 0, 1],
                #[0, 1, 0, 1],
                #[0, 1, 0, 1],
                #[0, 1, 0, 1]
            ]

            self.blocks = []
            for i in xrange(levels[self.levelcount][0], levels[self.levelcount][1]):
                for j in xrange(levels[self.levelcount][2], levels[self.levelcount][3]):
                    self.blocks.append(Block(1, (randint(5,240),randint(5,240),randint(5,240)),
                        (i,j), self.res))

            return self.blocks

    def managePowerups(self):

        if self.ball.dead and self.currentpowerup is not None:
            if self.currentpowerup.collected is True:
                self.currentpowerup.countdown = 0

        if self.currentpowerup is None:
            if not self.ball.dead:
                self.powerupdrop -= 1

                if self.powerupdrop <= 0:

                    droppercentages = [
                    (10, '1up'),
                    (0, 'slowball'),
                    (100, 'bigracket')
                    ]

                    choice = uniform(0, 100)
                    for chance, type in droppercentages:
                        if choice <= chance:
                            self.currentpowerup = Powerup(type, self.res)
                            self.sprites.add(self.currentpowerup)
                            break
            return

        if not self.currentpowerup.collected:

            if self.racket.rect.colliderect(self.currentpowerup.rect):
                if self.currentpowerup.type == 'bigracket':
                    self.racket.grow()
                elif self.currentpowerup.type == 'slowball':
                    self.ball.slowDown()
                elif self.currentpowerup.type == '1up':
                    self.lives.addLife()

                self.currentpowerup.collected = True
                self.sprites.remove(self.currentpowerup)

            else:
                alive = self.currentpowerup.update()
                if not alive:
                    self.sprites.remove(self.currentpowerup)
                    self.currentpowerup = None
                    self.powerupdrop = randint(60*5, 60*15)

        elif self.currentpowerup.countdown > 0:
            self.currentpowerup.countdown -= 1

        else:
            if self.currentpowerup is not None:

                if self.currentpowerup.type == 'bigracket':
                    self.racket.shrink()
                elif self.currentpowerup.type == 'slowball':
                    self.ball.speedUp()

                self.currentpowerup = None

                self.powerupdrop = randint(60*15, 60*25)

    def gameOver(self):

        highscores = self.parseHighScores()

        if self.score.score > int(highscores[-1][1]):
            screen_message("Enter Name:", self.screen, self.res)

            self.enteringname = True

            self.name = NameSprite(self.res, (self.res[0]/2, self.res[1]/3), rounder(self.res[0]*0.1))
            self.namesprite = pygame.sprite.RenderUpdates(self.name)

            while self.enteringname:

                for event in pygame.event.get():
                    if event.key == pygame.K_BACKSPACE:
                        self.name.removeLetter()
                    elif event.key == pygame.K_RETURN:
                        self.nameEntered()
                    elif event.type == pygame.KEYDOWN:
                        try:
                            char = chr(event.key)
                            if str(char) in 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_':
                                 self.name.addLetter(char)
                        except:
                            pass

                    self.namesprite.update()
                    self.namesprite.draw(self.screen)
                    pygame.display.flip()
                    self.namesprite.clear(self.screen, self.bg)
        else:
            self.showHighScores(highscores)

    def nameEntered(self):
        username = self.name.text

        self.screen.blit(self.bg, (0,0))
        pygame.display.flip

        highscores = self.parseHighScores()

        newscores = []

        for name, score in highscores:
            if self.score.score > int(score):
                newscores.append((username, str(self.score.score)))
                self.score.score = 0
            newscores.append((name, score))
        newscores = newscores[0:10]

        highscorefile = 'highscores.txt'
        f = open(highscorefile, 'w')
        for name, score in newscores:
            f.write("%s: %s\n" % (name, score))
        f.close()

        self.showHighScores(newscores)

    def parseHighScores(self):
        highscorefile = 'highscores.txt'
        if os.path.isfile(highscorefile):
            f = open(highscorefile, 'r')
            lines = f.readlines()
            scores = []

            for line in lines:
                scores.append( line.strip().split(':') )
            return scores
        else:
            f = open (highscorefile, 'w')
            f.write("""JJJ:0000
III:0000
HHH:0000
GGG:0000
FFF:0000
EEE:0000
DDD:0000
CCC:0000
BBB:0000
AAA:0000""")
            f.close()
            return self.parseHighScores()

    def showHighScores(self, scores):

        font = pygame.font.Font(None, rounder(self.res[0]*0.05))
        color = pygame.Color("white")

        for i in range(len(scores)):
            name, score = scores[i]
            nameimage = font.render(name, True, color)
            namerect = nameimage.get_rect()
            namerect.left = rounder(self.res[0]*0.3)
            namerect.centery = rounder(self.res[0]/8)+(i*(namerect.height + rounder(self.res[0]/40)))
            self.screen.blit(nameimage, namerect)

            scoreimage = font.render(score, True, color)
            scorerect = scoreimage.get_rect()
            scorerect.right = rounder(self.res[0]*0.7)
            scorerect.centery = namerect.centery
            self.screen.blit(scoreimage, scorerect)

        pygame.display.flip()
        time.sleep(3)
        menu(self.screen, self.clock, self.res)
예제 #4
0
 def setUp(self):
     self.racket = Racket(constants.SCREEN_MARGIN, constants.SCREEN_MARGIN)