Example #1
0
class DotRunWindow(arcade.Window):
    def __init__(self, width, height):
        super().__init__(width, height)

        arcade.set_background_color(arcade.color.GRAY)

        self.start()

    def start(self):
        self.world = World(SCREEN_WIDTH, SCREEN_HEIGHT)

        self.dot_sprite = ModelSprite('images/dot.png', model=self.world.dot)

        self.coin_texture = arcade.load_texture('images/coin.png')

    def update(self, delta):
        self.world.update(delta)
        if self.world.is_dead():
            self.start()

    def draw_platforms(self, platforms):
        for p in platforms:
            arcade.draw_rectangle_filled(p.x + p.width // 2,
                                         p.y - p.height // 2, p.width,
                                         p.height, arcade.color.WHITE)

    def draw_coins(self, coins):
        for c in coins:
            if not c.is_collected:
                arcade.draw_texture_rectangle(c.x, c.y, c.width, c.height,
                                              self.coin_texture)

    def on_draw(self):
        arcade.set_viewport(self.world.dot.x - SCREEN_WIDTH // 2,
                            self.world.dot.x + SCREEN_WIDTH // 2, 0,
                            SCREEN_HEIGHT)

        arcade.start_render()
        self.draw_platforms(self.world.platforms)
        self.draw_coins(self.world.coins)

        self.dot_sprite.draw()

        arcade.draw_text(str(self.world.score),
                         self.world.dot.x + (SCREEN_WIDTH // 2) - 60,
                         self.height - 30, arcade.color.WHITE, 20)

    def on_key_press(self, key, key_modifiers):
        if not self.world.is_started():
            self.world.start()
        self.world.on_key_press(key, key_modifiers)
Example #2
0
class FlappyDotWindow(arcade.Window):
    def __init__(self, width, height):
        super().__init__(width, height)

        arcade.set_background_color(arcade.color.WHITE)

        self.start()

    def start(self):
        self.world = World(SCREEN_WIDTH, SCREEN_HEIGHT)

        self.dot_sprite = ModelSprite('images/1.png', model=self.world.player)

        self.pillar_pair_sprites = [
            PillarPairSprite(model=self.world.pillar_pairs[0]),
            PillarPairSprite(model=self.world.pillar_pairs[1])
        ]

    def on_key_press(self, key, key_modifiers):
        if not self.world.is_started():
            self.world.start()

        self.world.on_key_press(key, key_modifiers)

    def update(self, delta):
        self.world.update(delta)
        if self.world.is_dead():
            self.start()

    def on_draw(self):
        arcade.start_render()

        self.dot_sprite.draw()
        for pillar_pair_sprite in self.pillar_pair_sprites:
            pillar_pair_sprite.draw()

        arcade.draw_text(str(self.world.score), self.width - 30,
                         self.height - 30, arcade.color.BLACK, 20)
Example #3
0
class MazeWindow(arcade.Window):
    def __init__(self, width, height):
        self.car_list = []
        super().__init__(width, height)
        self.world = World(SCREEN_WIDTH, SCREEN_HEIGHT)
        self.background = arcade.load_texture("Wallpaper2.jpg")
        self.spot_sprite = Spot(100, 50, angle=0)
        self.spot_sprite.add_texture('car_move.png')
        self.world.add_car(self.spot_sprite)
        park_car_list = ['parking_car1.png', 'parking_car2.png']
        for i in self.world.car_park:
            self.car_list.append(
                ModelSprite(random.choice(park_car_list), model=i))

    def update(self, delta):
        self.world.update(delta)

    def on_draw(self):
        arcade.start_render()
        arcade.draw_texture_rectangle(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2,
                                      SCREEN_WIDTH, SCREEN_HEIGHT,
                                      self.background)
        self.world.spot.draw()
        if self.world.state != self.world.STATE_FROZEN:
            arcade.draw_text(f"Time: {self.world.start_time:.0f}", 23, 450,
                             arcade.color.RED, 30)

        if self.world.state == self.world.STATE_FROZEN:

            arcade.draw_text(f"CAR PARKING GAME", 210, 440, arcade.color.RED,
                             30)
            arcade.draw_text(f"Press any key to start", 180, 30,
                             arcade.color.BLACK, 15)
        for i in self.car_list:
            i.draw()
        if self.world.state == self.world.STATE_DEAD:
            self.background = arcade.load_texture("Wallpaper2END.jpg")
            if self.world.start_time < 15:
                ModelSprite('star_score3.png', model=self.world.win).draw()
                arcade.draw_text(f"You get 3 star!!!!!", 300, 250,
                                 arcade.color.BLUE, 25)

            elif self.world.start_time < 20:
                ModelSprite('star_score2.png', model=self.world.win).draw()
                arcade.draw_text(f"You get 2 star!!!!!", 300, 250,
                                 arcade.color.BLUE, 25)

            elif self.world.start_time > 20:
                ModelSprite('star_score1.png', model=self.world.win).draw()
                arcade.draw_text(f"You get 1 star!!!!!", 300, 250,
                                 arcade.color.BLUE, 25)

            arcade.draw_text(f"Thank you for playing", 240, 200,
                             arcade.color.BLUE, 30)
            arcade.draw_text("Press any key to restart.", 235, 150,
                             arcade.color.BLACK, 30)
            self.world.die()

    def restart(self):
        self.car_list = []
        self.world = World(SCREEN_WIDTH, SCREEN_HEIGHT)
        self.background = arcade.load_texture("Wallpaper2.jpg")
        self.spot_sprite = Spot(100, 50, angle=0)
        self.spot_sprite.add_texture('car_move.png')
        self.world.add_car(self.spot_sprite)
        park_car_list = ['parking_car1.png', 'parking_car2.png']
        for i in self.world.car_park:
            self.car_list.append(
                ModelSprite(random.choice(park_car_list), model=i))

    def on_key_press(self, key, key_modifiers):
        self.world.on_key_press(key, key_modifiers)
        if self.world.state == World.STATE_RESTART:
            self.restart()

        if not self.world.is_dead():
            self.world.start()

    def on_key_release(self, key, key_modifiers):
        self.world.on_key_release(key, key_modifiers)
Example #4
0
class Window(arcade.Window):
    show_credit = False

    def __init__(self, width, height, title):
        super().__init__(width, height, title)

        self.start()

        self.menu = {
            'score':
            arcade.load_texture(
                ".././images/Back ground/loser-score-board.png"),
            'play':
            arcade.load_texture(".././images/button/replay.png"),
            'credit':
            arcade.load_texture(".././images/button/credit.png"),
            'quit':
            arcade.load_texture(".././images/button/QuitButton.png")
        }

        self.credit_background = arcade.load_texture(
            ".././images/Back ground/credit.jpg")

    def start(self):
        self.world = World(SCREEN_WIDTH, SCREEN_HEIGHT)
        self.offset = 0
        self.credit_offset = 0
        self.check_credit = False
        self.arrows = []
        self.fires = []
        self.numArrow = 5
        self.bottomFires = []
        self.background = arcade.load_texture(
            ".././images/Back ground/sky.jpg")

        for i in range(self.numArrow):
            self.arrows.append(ArrowSprite(model=self.world.arrow[i]))
        self.arrow_sprite = self.arrows

        for n in range(self.world.fireNumbers):
            self.fires.append(FireSprite(model=self.world.fire[n]))
        self.fire_sprite = self.fires

        for i in range(10):
            self.bottomFires.append(BottomFire(model=self.world.bottomfire[i]))
        self.bottomfires_sprite = self.bottomFires

        self.player_sprite = PlayerSprite(model=self.world.player)

    def on_key_press(self, key, key_modifiers):
        if not self.world.is_started():
            self.world.start()

        self.world.on_key_press(key, key_modifiers)

    def draw_background(self):
        arcade.draw_texture_rectangle(SCREEN_WIDTH // 2,
                                      SCREEN_HEIGHT // 2 + self.offset,
                                      SCREEN_WIDTH, SCREEN_HEIGHT,
                                      self.background)
        arcade.draw_texture_rectangle(SCREEN_WIDTH // 2,
                                      (SCREEN_HEIGHT // 2 + self.offset) -
                                      SCREEN_HEIGHT, SCREEN_WIDTH,
                                      SCREEN_HEIGHT, self.background)

    def draw_lp(self):
        arcade.draw_rectangle_filled(
            (self.width - 120) + (100 - self.world.player.lp), 30,
            self.world.player.lp * 2, 20, arcade.color.CHERRY)

    def draw_credit(self):
        self.show_credit = True
        arcade.draw_texture_rectangle(SCREEN_WIDTH // 2,
                                      SCREEN_HEIGHT // 2 + self.credit_offset,
                                      SCREEN_WIDTH, SCREEN_HEIGHT,
                                      self.credit_background)
        arcade.draw_texture_rectangle(
            SCREEN_WIDTH // 2,
            (SCREEN_HEIGHT // 2 + self.credit_offset) - SCREEN_HEIGHT,
            SCREEN_WIDTH, SCREEN_HEIGHT, self.credit_background)

    def update(self, delta):
        self.world.update(delta)
        if self.world.is_dead():
            self.world.freeze()

        if self.world.is_started():
            self.offset -= 1
            self.offset %= SCREEN_HEIGHT

        if (self.world.arrowNumbers != self.numArrow):
            self.arrows.append(ArrowSprite(model=self.world.arrow[-1]))
            self.numArrow = self.world.arrowNumbers

        if self.check_credit:
            self.credit_offset += 1
            self.credit_offset %= SCREEN_HEIGHT

    def on_draw(self):
        arcade.start_render()

        self.draw_background()

        for arrow in self.arrow_sprite:
            arrow.draw()

        for fire in self.fire_sprite:
            fire.draw()

        for bf in self.bottomfires_sprite:
            bf.draw()

        arcade.draw_text(str(int(self.world.score)), 25, self.height - 40,
                         arcade.color.BLACK, 20)

        arcade.draw_text("LP", self.width - 35, 45, arcade.color.BLACK, 10)

        self.player_sprite.draw()

        self.draw_lp()

        if self.world.player.lp == -1:
            self.draw_gui()

    def draw_gui(self):
        texture = self.menu['score']
        arcade.draw_texture_rectangle(self.width // 2, self.height // 2 + 200,
                                      texture.width, texture.height, texture,
                                      0)
        arcade.draw_text(str(int(self.world.score)), self.width // 2 - 80, 475,
                         arcade.color.WHITE, 100)
        texture = self.menu['play']
        arcade.draw_texture_rectangle(self.width // 2, self.height // 2,
                                      texture.width, texture.height, texture,
                                      0)
        texture = self.menu['credit']
        arcade.draw_texture_rectangle(self.width // 2, self.height // 2 - 200,
                                      texture.width, texture.height, texture,
                                      0)
        texture = self.menu['quit']
        arcade.draw_texture_rectangle(self.width - 60, self.height - 55,
                                      texture.width, texture.height, texture,
                                      0)

        if self.check_credit:
            self.show_credit = True
            self.draw_credit()

    def replay(self, x, y):
        if self.world.player.lp == -1:
            if self.width // 2 - 205 <= x <= self.width // 2 + 205 and self.height // 2 - 70 <= y <= self.height // 2 + 70:
                self.start()
                self.world.start()

    def credit(self, x, y):
        if ((self.height // 2) - 200) - (95) <= y <= (
            (self.height // 2) - 200) + (
                95) and self.width // 2 - 95 <= x <= self.width // 2 + 95:
            self.check_credit = True

    def quit_game(self, x, y):
        if self.world.player.lp == -1:
            if self.height - 90 <= y <= self.height - 20 and self.width - 108 <= x <= self.width - 10:
                exit()

    def draw_gui_again(self, x, y):
        if y <= self.height and x <= self.width:
            self.check_credit = False
            self.show_credit = False

    def on_mouse_press(self, x, y, button, modifiers):
        if not self.show_credit:
            self.replay(x, y)
            self.credit(x, y)
            self.quit_game(x, y)
        else:
            self.draw_gui_again(x, y)
Example #5
0
class MyGame(arcade.Window):
    def __init__(self, width, height):
        super().__init__(width, height)
 
        self.current_route = routes['menu']
        self.selecting_choice = 0
        self.car_choice = 1
        self.background = arcade.load_texture("images/Background.png")
        self.cartexture = Car[self.car_choice]
        self.menu_setup()
        self.car_setup()
        self.game_setup(width,height)

    def menu_setup(self):
        self.choice_list = arcade.SpriteList()

        self.start = MenuChoiceSprite()
        self.start.textures.append(arcade.load_texture("images/start.png"))
        self.start.textures.append(arcade.load_texture("images/start1.png"))
        self.start.set_texture(0)
        self.start.texture_change_frames = 10

        self.car = MenuChoiceSprite()
        self.car.textures.append(arcade.load_texture("images/car.png"))
        self.car.textures.append(arcade.load_texture("images/car1.png"))
        self.car.set_texture(1)
        self.car.texture_change_frames = 10

        self.exit = MenuChoiceSprite()
        self.exit.textures.append(arcade.load_texture("images/exit.png"))
        self.exit.textures.append(arcade.load_texture("images/exit1.png"))
        self.exit.set_texture(1)
        self.exit.texture_change_frames = 10

        self.start.center_x,self.start.center_y = self.width//2,self.height//2 +50
        self.car.center_x,self.car.center_y = self.width//2,self.height//2 -20
        self.exit.center_x,self.exit.center_y = self.width//2,self.height//2 -90
        
        self.start.select()
        self.choice_list.append(self.start)
        self.choice_list.append(self.car)
        self.choice_list.append(self.exit)

    def car_setup(self):
        self.car_choice_list = arcade.SpriteList()
        self.car_show = arcade.Sprite(Car[self.car_choice])
        self.car_show.set_position(self.width//2,self.height//2 +50)

    def game_setup(self, width, height):
        self.world = World(SCREEN_WIDTH, SCREEN_HEIGHT)
        self.background_sprite = ModelSprite("images/Background.png",
                                      model=self.world.background)
        self.background_sprite2 = ModelSprite("images/Background.png",
                                      model=self.world.background2)                              
        self.car_sprite = ModelSprite(self.cartexture,
                                      model=self.world.car)
        self.enemylist = []                              
        self.fpscounter = Fpscounter()
        self.set_update_rate(1/70)
    
    def car_sprite_selected(self):
        self.car_sprite = ModelSprite(self.cartexture,
                                      model=self.world.car)

    def draw_menu(self):
        self.choice_list.draw()

    def draw_car_menu(self):
        self.car_show.draw()                                  
        
    def update(self, delta):
        if self.current_route == routes['menu']:
            for choice in self.choice_list:
                if choice.is_select == True:
                    choice.update()
                    choice.update_animation()
                   
        elif self.current_route == routes['car']:
            self.draw_car_menu()

        elif self.current_route == routes['exit']:
            sys.exit()    

        elif self.current_route == routes['game']:
            self.creteenemy()
            self.update_enemylist()
            self.world.update(delta)
        
    def on_draw(self):
        arcade.start_render()
        arcade.draw_texture_rectangle(self.width//2 , self.height//2 ,self.width, self.height,self.background)

        if self.current_route == routes['menu']:
            self.draw_menu()  

        elif self.current_route == routes['car']:
            self.car_setup()
            self.draw_car_menu()    
       
        elif self.current_route == routes['game']:
            self.car_sprite_selected()
            self.background_sprite.draw()
            self.background_sprite2.draw()
            self.car_sprite.draw()
            self.fpscounter.tick()
            self.check_state()

            fps = f"fps{self.fpscounter.fps():.2f}"
            score = f"Score {self.world.score}"
            arcade.draw_text(score,670,770,arcade.color.DARK_CANDY_APPLE_RED,24)
            arcade.draw_text(fps,750,560,arcade.color.BLACK)
            for enemy in self.enemylist:
                enemy.draw()
            
    def update_selected_choice(self):
        for choice in self.choice_list:
            choice.unselect()
            choice.set_texture(1)
        self.choice_list[self.selecting_choice].select()    

    def on_key_press(self, key, key_modifiers):
        if self.current_route == routes['menu']:
            if key == arcade.key.DOWN:
                if self.selecting_choice < 2:
                    self.selecting_choice += 1
                else:
                    self.selecting_choice = 0
                self.update_selected_choice()
                press_sound = arcade.load_sound("soundtrack/pressmusic.wav")
                arcade.play_sound(press_sound)
                
            elif key == arcade.key.UP:
                if self.selecting_choice > 0 :  
                    self.selecting_choice -= 1
                else:
                    self.selecting_choice = 2
                self.update_selected_choice()
                press_sound = arcade.load_sound("soundtrack/pressmusic.wav")
                arcade.play_sound(press_sound)        
            elif key == arcade.key.ENTER:
                self.current_route = routes[choices[self.selecting_choice]]

        elif self.current_route == routes['car']:
            if key == arcade.key.RIGHT:
                if self.car_choice < 6:
                    self.car_choice += 1
                else:
                    self.car_choice = 0    
            
            elif key == arcade.key.LEFT:
                if self.car_choice > 0:
                    self.car_choice -= 1
                else:
                    self.car_choice = 4
            elif key == arcade.key.ENTER:
                self.cartexture = Car[self.car_choice]
                self.current_route = routes['menu']
                
        elif self.current_route == routes['game']:
            self.world.on_key_press(key, key_modifiers)
            if not self.world.is_dead():
                self.world.start()       
            elif key == arcade.key.R and self.world.state == World.STATE_DEAD:
                self.game_setup(SCREEN_WIDTH,SCREEN_HEIGHT)

            elif key == arcade.key.M and self.world.state == World.STATE_DEAD:
                self.current_route = routes['menu']
                self.draw_menu()
                self.game_setup(SCREEN_WIDTH,SCREEN_HEIGHT)

    def on_key_release(self, key, key_modifiers):
        if self.current_route == routes['game']:
            self.world.on_key_release(key, key_modifiers)

    def creteenemy(self):
        for enemy in self.world.enemylist:
            if enemy in (enemy_sprite.model for enemy_sprite in self.enemylist):
                pass
            else:
                self.enemylist.append(ModelSprite(self.randomsprite(), 
                model=enemy))
   
    def randomsprite(self):
        enemyspritelist = ['images/EnemyCar2.png','images/Enemycarwhite.png','images/redcar.png','images/pinkcar.png']
        randomnum = randint(0,3)
        return enemyspritelist[randomnum]

    def update_enemylist(self):
        for enemy_sprite in [_ for _ in self.enemylist]:
            if enemy_sprite.model not in self.world.enemylist:
                self.enemylist.remove(enemy_sprite)

    def draw_game_over(self):
        output = f"Score {self.world.score}"
        arcade.draw_text(output, 285, 500, arcade.color.BALL_BLUE, 54)
        
        output = "Game Over"
        arcade.draw_text(output, 225, 400, arcade.color.BALL_BLUE, 54)

        output = "Press R to restart"
        arcade.draw_text(output, 280, 300, arcade.color.BALL_BLUE, 24)

        output = "Press M to mainmenu"
        arcade.draw_text(output,250,230,arcade.color.BALL_BLUE, 24)

    def check_state(self):
        if self.world.state == World.STATE_DEAD:
            self.draw_game_over()