Example #1
0
def display_scores():
    with open("scores", "rb") as f:
        scores = loads(f.read())
    a = sorted([(scores[x], x) for x in scores], reverse=True)
    WIN.blit(BACKGROUND, (0, 0))
    renderfont("Usernames", 50, 100, 50)
    renderfont("Scores", 50, 850, 50)
    y = 90
    for i in range(10):
        renderfont(str(a[i][1]), 45, 100, y)
        renderfont(str(a[i][0]), 45, 850, y)
        y += 45
    update()
    wait()
Example #2
0
 def race_result(self, user_bet: str):
     winner_color = self.winner.pencolor().lower()
     user_bet = user_bet.lower()
     if winner_color == user_bet:
         print(WIN.format(user_bet))
     else:
         print(LOSE.format(winner_color))
Example #3
0
 def update_window() -> None:
     WIN.blit(BACKGROUND, (0, bgy))
     WIN.blit(BACKGROUND, (0, bgy2))
     level_label = font_.render(f"Score: {player.score}", True, (255, 255, 255))
     lives_label = font_.render(f"Lives: {lives}", True, (255, 255, 255))
     WIN.blit(lives_label, (10, 10))
     for enemy in enemies:
         enemy.draw(WIN)
     for laser in enemy_lasers:
         laser.draw(WIN)
     WIN.blit(level_label, (WIDTH - level_label.get_width()-10, 10))
     player.draw(WIN)
     update()
def main():
    run = True
    WIN.blit(BG, (0, 0))

    nodeCount = 15
    nodeList = createNodes(nodeCount)
    drawNodes(nodeList)
    pygame.display.update()
    generateMaze(nodeList, nodeList[0][0], prog=True)
    print("finished")

    while run:

        pygame.display.update()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
                quit()
                break

    pygame.quit()
Example #5
0
def menu():
    global shiftDown
    textbox = TextBox()
    textbox.rect.center = [320, 240]
    run = True
    username = ""
    while run:
        WIN.blit(BACKGROUND, (0, 0))
        WIN.blit(textbox.image, textbox.rect)
        pygame.display.flip()
        for e in pygame.event.get():
            if e.type == pygame.QUIT:
                run = False
            if e.type == pygame.KEYUP:
                if e.key in [pygame.K_RSHIFT, pygame.K_LSHIFT]:
                    shiftDown = False
            if e.type == pygame.KEYDOWN:
                textbox.add_chr(pygame.key.name(e.key))
                if e.key == pygame.K_SPACE:
                    textbox.text += " "
                    textbox.update()
                if e.key in [pygame.K_RSHIFT, pygame.K_LSHIFT]:
                    shiftDown = True
                if e.key == pygame.K_BACKSPACE:
                    textbox.text = textbox.text[:-1]
                    textbox.update()
                if e.key == pygame.K_RETURN:
                    if len(textbox.text) > 0:
                        username = textbox.text
                        run = False
    run = True
    titlefont = font.SysFont("comicsans", 70)
    while run:
        WIN.blit(BACKGROUND, (0, 0))
        titlelabel = titlefont.render("Press Spacebar to Begin...", True, (255, 255, 255))
        WIN.blit(titlelabel, (WIDTH//2 - titlelabel.get_width()//2, HEIGHT//2))
        update()
        for event in get():
            if event.type == pygame.QUIT:
                run = False
                break
            keys = get_pressed()
            if keys[K_SPACE]:
                if main(username) == 1:
                    WIN.blit(BACKGROUND, (0, 0))
                    titlelabel = titlefont.render("Game Over...", True, (255, 255, 255))
                    WIN.blit(titlelabel, (WIDTH//2 - titlelabel.get_width()//2, HEIGHT//2))
                    update()
                    pygame.time.wait(2000)
                    WIN.blit(BACKGROUND, (0, 0))
                    titlelabel = titlefont.render("Press Space To Restart/Escape To Quit...", True, (255, 255, 255))
                    WIN.blit(titlelabel, (WIDTH//2 - titlelabel.get_width()//2, HEIGHT//2))
                    update()
                    if wait() == 1:
                        run = False
                        break
    display_scores()
    pygame.quit()
Example #6
0
def renderfont(text, size, x, y, style="comicsans") -> None:
    obj1 = font.SysFont(style, size).render(text, True, (255, 255, 255))
    WIN.blit(obj1, (x, y))
Example #7
0
def main(username: str) -> int:
    global isMuted
    run, fps = True, 60
    lives, font_, clock = 5, SysFont("comicsans", 50), Clock()
    level = 0
    player = Player(300, 650, username)
    bgy, bgy2 = 0.0, -float(BACKGROUND.get_height())
    enemies, enemy_lasers = [], []
    player_laser_vel = -8
    wave_length, enemy_vel, laser_vel = 5, 1, 5
    mixer.music.load("assets/background.wav")
    mixer.music.play(-1)

    def update_window() -> None:
        WIN.blit(BACKGROUND, (0, bgy))
        WIN.blit(BACKGROUND, (0, bgy2))
        level_label = font_.render(f"Score: {player.score}", True, (255, 255, 255))
        lives_label = font_.render(f"Lives: {lives}", True, (255, 255, 255))
        WIN.blit(lives_label, (10, 10))
        for enemy in enemies:
            enemy.draw(WIN)
        for laser in enemy_lasers:
            laser.draw(WIN)
        WIN.blit(level_label, (WIDTH - level_label.get_width()-10, 10))
        player.draw(WIN)
        update()

    while run:
        clock.tick(fps)
        if len(enemies) == 0:
            if level == 10:
                update_scores(player)
                return 1
            wave_length += 1
            enemies.extend(spawn(wave_length))
            level += 1
            if level % 4 == 0:
                enemy_vel += 1
                player.v += 1
                player_laser_vel -= 1
            level_label = font_.render(F"Level {level}...", True, (255, 255, 255))
            WIN.blit(level_label, (WIDTH//2 - level_label.get_width()//2, HEIGHT//2))
            update()
            pygame.time.wait(2500)
        update_window()
        bgy += 1.4
        bgy2 += 1.4
        if bgy > HEIGHT:
            bgy = -BACKGROUND.get_height()
        if bgy2 > HEIGHT:
            bgy2 = -BACKGROUND.get_height()
        for event in get():
            if event.type == QUIT:
                quit()
        keys = get_pressed()
        if len(keys):
            player.move(keys)
        if keys[K_SPACE]:
            bulletSound.play()
            player.shoot()
        if keys[K_m]:
            isMuted = not isMuted
            if isMuted:
                mixer.music.stop()
            else:
                mixer.music.play(-1)
        for enemy in enemies[:]:
            enemy.move(enemy_vel)
            if randint(0, 2*30) == 1 and enemy.y > 0:
                enemy.shoot(enemy_lasers)
            if collide(enemy, player):
                enemies.remove(enemy)
                lives -= 1
            elif enemy.off_screen():
                enemies.remove(enemy)
                player.score -= 1
                player.score = max(player.score, 0)
        lives = move_lasers(laser_vel, player, enemy_lasers, lives)
        if lives <= 0:
            update_scores(player)
            return 1
        player.move_lasers(player_laser_vel, enemies)
    update_scores(player)
    return 1