Exemplo n.º 1
0
 def __init__(self, divider_width, divider_height, colour, target_surface):
     super(Road_sprite, self).__init__()
     self.dividers = []
     self.divider_width = divider_width
     self.divider_height = divider_height
     self.spacing = int(divider_height / 4)
     self.colour = colour
     self.x_max = target_surface.get_size()[0]
     self.y_max = target_surface.get_size()[1]
     self.blocks = []
     self.type = "road"
     self.dodged_count = 0
     self.dodged = Text_sprite(
         "Dodged: " + str(0),
         constants.getFilePath("FasterOne.ttf"),
         constants.corner_small_text_size,
         0,
         25,
         constants.BLACK,
         0,
     )
     try:
         with open("highscores.dat", "rb") as file:
             self.highscore = pickle.load(file)
     except FileNotFoundError:
         self.highscore = 0
     self.global_highscore = Text_sprite(
         "Highscore: " + str(self.highscore),
         constants.getFilePath("FasterOne.ttf"),
         constants.corner_small_text_size,
         0,
         0,
         constants.BLACK,
         0,
     )
     self.DodgeCar_corner = Text_sprite(
         "DodgeCar",
         constants.getFilePath("EndeavourForever.ttf"),
         constants.corner_small_text_size,
         constants.display_width - 140,
         10,
         constants.BLACK,
         2500,
     )
     self.spacebar_again = Text_sprite(
         "Press space to restart.",
         constants.FasterOneFontPath,
         25,
         250,
         0,
         constants.GREEN,
         0,
     )
Exemplo n.º 2
0
 def update(self, event):
     i = 0
     active_sprite_index = 0
     if type(event) != int:
         if event.type == pygame.USEREVENT + 2:
             while i < (len(self.sequence_sprite) - 1):
                 if self.sequence_sprite[i][0].visible:
                     self.sequence_sprite[i][0].visible = False
                     self.sequence_sprite[i + 1][0].visible = True
                     active_sprite_index = i + 1
                     i += 2
                 else:
                     i += 1
             pygame.time.set_timer(
                 pygame.USEREVENT + 2,
                 self.sequence_sprite[active_sprite_index][0].time, True)
         if active_sprite_index == 2:
             pygame.mixer.music.load(constants.getFilePath("321GO.ogg"))
             pygame.mixer.music.play(0)
         if active_sprite_index == len(self.sequence_sprite) - 1:
             pygame.time.set_timer(
                 pygame.USEREVENT + 1,
                 self.sequence_sprite[active_sprite_index][0].time + 1,
                 True,
             )
Exemplo n.º 3
0
def game_loop(intro_world, main_world, target_surface):
    gameExit = False
    end_music = False
    bgMusicPlaying = False
    clock = pygame.time.Clock()
    global introComplete
    if not introComplete:
        world = intro_world
    while not gameExit:
        if introComplete:
            world = main_world

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameExit = True
            elif event.type == pygame.USEREVENT + 1:
                introComplete = True
            elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE and introComplete:
                bgMusicPlaying = False
                end_music = False
                pygame.mixer.music.stop()
                world = create_world(target_surface)
                main_world = world
            else:
                world.update(event)
            if introComplete and not bgMusicPlaying:
                pygame.mixer.music.stop()
                pygame.mixer.music.load(
                    constants.getFilePath("Cyber_Race.ogg"))
                pygame.mixer.music.play(-1)
                bgMusicPlaying = True
            if world.ended:
                if not end_music:
                    pygame.mixer.music.stop()
                    pygame.mixer.music.stop()
                    pygame.mixer.music.load(
                        constants.getFilePath("Game_End_Fade_Out.ogg"))
                    pygame.mixer.music.play(0)
                    end_music = True
        world.update(pygame.NOEVENT)
        world.ended = world.detect_collision()
        world.draw(target_surface)
        pygame.display.flip()
        clock.tick(60)
Exemplo n.º 4
0
def create_world(target_surface):
    w = models.World(constants.GREY)
    dividers = models.Road_sprite(constants.divider_width,
                                  constants.divider_height,
                                  constants.WHITE_SMOKE, target_surface)
    dividers.initialise_road()
    car = models.Car_sprite(
        constants.getFilePath("red_flaming_up.png"),
        constants.getFilePath("red_flaming_crashed.png"),
        target_surface,
    )
    crashed_text = models.Text_sprite(
        "You Crashed",
        constants.FasterOneFontPath,
        80,
        0,
        constants.display_height / 2,
        constants.RED,
        0,
    )
    end_emoji = models.Text_sprite(
        constants.emojis[random.randint(3, 15)],
        constants.NotoColorEmojiFontPath,
        80,
        constants.display_width - 140,
        constants.display_height / 2 - 30,
        constants.BLACK,
        0,
    )
    end_emoji.type = "end_world_emoji"
    crashed_text.type = "end_world_crashed"
    end_emoji.visible = False
    crashed_text.visible = False
    w.add_sprite(dividers)
    w.add_sprite(car)
    w.add_sprite(crashed_text)
    w.add_sprite(end_emoji)
    return w
Exemplo n.º 5
0
def main():
    pygame.mixer.pre_init()
    pygame.init()

    gameDisplay = pygame.display.set_mode(
        (constants.display_width, constants.display_height))
    pygame.display.set_caption("DodgeCar!")
    intro_world = create_intro_world(gameDisplay)
    world = create_world(gameDisplay)
    pygame.time.set_timer(pygame.USEREVENT + 2, 2000, True)
    pygame.time.set_timer(pygame.USEREVENT + 3,
                          constants.intro_time_before_countdown, True)
    pygame.mixer.music.load(constants.getFilePath("Cyber_Race.ogg"))
    pygame.mixer.music.play(-1)
    game_loop(intro_world, world, gameDisplay)
Exemplo n.º 6
0
def create_intro_world(target_surface):
    iw = models.Intro_world(constants.GREY)
    dividers = models.Road_sprite(constants.divider_width,
                                  constants.divider_height,
                                  constants.WHITE_SMOKE, target_surface)
    dividers.initialise_road()
    DodgeCar_title = models.Text_sprite(
        "DodgeCar",
        constants.getFilePath("EndeavourForever.ttf"),
        constants.title_big_text_size,
        0,
        constants.display_height / 2 - constants.title_big_text_size / 2,
        constants.DARK_BLUE,
        constants.title_time,
    )

    Instructions = [
        models.Text_sprite(
            "Use",
            constants.FasterOneFontPath,
            constants.instructions_text_size,
            50,
            200,
            constants.BLACK,
            constants.instructions_time,
        ),
        models.Text_sprite(
            constants.left_arrow,
            constants.NotoColorEmojiFontPath,
            constants.instructions_text_size,
            250,
            200,
            constants.BLACK,
            constants.instructions_time,
        ),
        models.Text_sprite(
            constants.right_arrow,
            constants.NotoColorEmojiFontPath,
            constants.instructions_text_size,
            410,
            200,
            constants.BLACK,
            constants.instructions_time,
        ),
        models.Text_sprite(
            "to",
            constants.FasterOneFontPath,
            constants.instructions_text_size,
            550,
            200,
            constants.BLACK,
            constants.instructions_time,
        ),
        models.Text_sprite(
            "move left/right",
            constants.FasterOneFontPath,
            constants.instructions_text_size,
            0,
            400,
            constants.BLACK,
            constants.instructions_time,
        ),
    ]
    Number3 = models.Text_sprite(3, constants.FasterOneFontPath, 300, 0 + 25,
                                 30, constants.RED, constants.countdown_time)
    Number2 = models.Text_sprite(2, constants.FasterOneFontPath, 300, 250 + 25,
                                 30, constants.RED, constants.countdown_time)
    Number1 = models.Text_sprite(
        1,
        constants.FasterOneFontPath,
        300,
        500 + 20,
        30,
        constants.RED,
        constants.countdown_time - 50,
    )
    GO = models.Text_sprite(
        "GO!",
        constants.getFilePath("Thunderbold.otf"),
        300,
        constants.display_width / 2 - 230,
        constants.display_height / 2,
        constants.GREEN,
        constants.countdown_time,
    )
    iw.add_sprite(dividers)
    iw.add_sequence_sprite([DodgeCar_title])
    iw.add_sequence_sprite(Instructions)
    iw.add_sequence_sprite([Number3])
    iw.add_sequence_sprite([Number2])
    iw.add_sequence_sprite([Number1])
    iw.add_sequence_sprite([GO])
    return iw
Exemplo n.º 7
0
import models
import constants
import pygame
import random

introComplete = False
carUP = pygame.image.load(constants.getFilePath("red_flaming_up.png"))

# width of car = 39px
# height of car = 100px


def create_intro_world(target_surface):
    iw = models.Intro_world(constants.GREY)
    dividers = models.Road_sprite(constants.divider_width,
                                  constants.divider_height,
                                  constants.WHITE_SMOKE, target_surface)
    dividers.initialise_road()
    DodgeCar_title = models.Text_sprite(
        "DodgeCar",
        constants.getFilePath("EndeavourForever.ttf"),
        constants.title_big_text_size,
        0,
        constants.display_height / 2 - constants.title_big_text_size / 2,
        constants.DARK_BLUE,
        constants.title_time,
    )

    Instructions = [
        models.Text_sprite(
            "Use",