Skip to content

Tairitsu.Scene

Scene in the game.

Scene

Classes

BossScene

BossScene(window: pygame.surface.Surface)

Bases: Scene

Source code in Tairitsu/Scene.py
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
def __init__(self, window: pygame.surface.Surface):
    super().__init__(window=window)
    self.state = GameState.GAME_PLAY_BOSS

    self.bgm = BgmPlayer()
    self.bgm.play(GamePath.bgm["RYTHM"])

    # Parse the taiko chart
    _ = open(r".\assets\bgm\shed a light.tja").read()
    _ = list(filter(lambda x: x != "", _.split("\n")))
    self.__data = _[_.index("#START"):_.index("#END") + 1]
    self.measures = list(filter(lambda x: "," in x, self.__data))
    self.beat = 0
    self.measure = 0
    self.line = 0
    self.meter = (4, 4) # 4/4
    offset = 0
    beat_timings = []
    for measureIt in self.measures:
        data = measureIt[:-1]
        len_note = len(data)
        dur_meter = self.bgm.bpm / 60 / self.meter[1] # (meter per sec)
        timings = [offset + i * self.meter[1] / len_note for i in range(len_note) if data[i] != "0"]
        offset += 4
        beat_timings += timings
    print(beat_timings)
    self.beat_timings = beat_timings
    self.length = len(self.beat_timings)

    self.tileXnum = math.ceil(self.beat_timings[-1]) * 2
    self.width = self.tileXnum * SceneSettings.tileWidth

    self.infoBox = InfoBox("Press the SPACE!!!", 5)
    self.gen_Boss()
    self.logoOpacity = 0
    self.logo = pygame.transform.scale(pygame.image.load(GamePath.logo), (WindowSettings.width, WindowSettings.height))
    self.logo.set_alpha(0)
Attributes
BgmPlayer instance-attribute
BgmPlayer = BgmPlayer()
battleBox instance-attribute
battleBox = None
beat instance-attribute
beat = 0
beat_timings instance-attribute
beat_timings = beat_timings
bgm instance-attribute
bgm = BgmPlayer()
cameraX instance-attribute
cameraX = 0
cameraY instance-attribute
cameraY = 0
height instance-attribute
height = WindowSettings.height
infoBox instance-attribute
infoBox = InfoBox('Press the SPACE!!!', 5)
length instance-attribute
length = len(self.beat_timings)
line instance-attribute
line = 0
logo = pygame.transform.scale(pygame.image.load(GamePath.logo), (WindowSettings.width, WindowSettings.height))
logoOpacity instance-attribute
logoOpacity = 0
map instance-attribute
map = None
measure instance-attribute
measure = 0
measures instance-attribute
measures = list(filter(lambda : ',' in x, self.__data))
meter instance-attribute
meter = (4, 4)
monsters instance-attribute
monsters = pygame.sprite.Group()
npcs instance-attribute
npcs = pygame.sprite.Group()
obstacles instance-attribute
obstacles = pygame.sprite.Group()
portals instance-attribute
portals = pygame.sprite.Group()
shoppingBox instance-attribute
shoppingBox = None
state instance-attribute
state = GameState.GAME_PLAY_BOSS
tileXnum instance-attribute
tileXnum = math.ceil(self.beat_timings[-1]) * 2
tileYnum instance-attribute
tileYnum = SceneSettings.tileYnum
width instance-attribute
width = self.tileXnum * SceneSettings.tileWidth
window instance-attribute
window = window
Functions
debug_Decorator
debug_Decorator(func)
Source code in Tairitsu/Scene.py
536
537
538
539
540
541
542
def debug_Decorator(func):
    def wrapper(*args, **kwargs):
        print("Before the function is called")
        result = func(*args, **kwargs)
        print("After the function is called")
        return result
    return wrapper
end_battle
end_battle()

End the current battle.

Source code in Tairitsu/Scene.py
93
94
95
96
97
def end_battle(self):
    """
    End the current battle.
    """
    pass
end_dialog
end_dialog()

End the current dialog.

Source code in Tairitsu/Scene.py
78
79
80
81
82
def end_dialog(self):
    """
    End the current dialog.
    """
    pass
end_shop
end_shop()

End the current shop.

Source code in Tairitsu/Scene.py
109
110
111
112
113
def end_shop(self):
    """
    End the current shop.
    """
    pass
gen_Boss
gen_Boss()
Source code in Tairitsu/Scene.py
457
458
459
460
461
462
463
464
465
466
def gen_Boss(self):

    self.map = pygame.Surface((WindowSettings.width, WindowSettings.height))
    self.map.fill((0,0,0))
    self.mask = pygame.Surface((WindowSettings.width, WindowSettings.height), pygame.SRCALPHA)
    self.map.fill((0,0,0,0))
    self.obstacles = pygame.sprite.Group()# self.gen_Boss_obstacle()
    # self.npcs.add(NPC(self.width // 3, self.height // 3, "Boss5"))
    for it in self.beat_timings:
        self.monsters.add(Monster((it + 0.5) * 2 * SceneSettings.tileWidth, self.height // 2 - 25))
gen_Boss_map
gen_Boss_map()
Source code in Tairitsu/Scene.py
469
470
def gen_Boss_map(self):
    return self.gen_random_map(GamePath.cityTiles)
gen_Boss_obstacle
gen_Boss_obstacle()
Source code in Tairitsu/Scene.py
472
473
def gen_Boss_obstacle(self):
    return self.gen_random_obstacles(GamePath.tree)
gen_random_map
gen_random_map(TilesPath: GamePath.groundTiles) -> list

Generate a random map.

PARAMETER DESCRIPTION
TilesPath

A list of paths of tiles.

TYPE: GamePath.groundTiles

RETURNS DESCRIPTION
list

A 2D list representing the map.

Source code in Tairitsu/Scene.py
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
def gen_random_map(self, TilesPath: GamePath.groundTiles) -> list:
    """
    Generate a random map.

    Args:
        TilesPath: A list of paths of tiles.

    Returns:
        A 2D list representing the map.
    """
    images = [pygame.image.load(tile) for tile in TilesPath]
    images = [pygame.transform.scale(image, (SceneSettings.tileWidth, SceneSettings.tileHeight)) for image in images]

    mapObj = []
    for i in range(self.tileXnum):
        tmp = []
        for j in range(self.tileYnum):
            tmp.append(images[randint(0, len(images) - 1)])
        mapObj.append(tmp)

    return mapObj
gen_random_obstacles
gen_random_obstacles(imagePath=GamePath.tree)

Generate random obstacles.

PARAMETER DESCRIPTION
imagePath

The path of the obstacle image.

DEFAULT: GamePath.tree

RETURNS DESCRIPTION

A group of obstacle sprites.

Source code in Tairitsu/Scene.py
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
def gen_random_obstacles(self, imagePath = GamePath.tree):
    """
    Generate random obstacles.

    Args:
        imagePath: The path of the obstacle image.

    Returns:
        A group of obstacle sprites.
    """
    image = pygame.image.load(imagePath) 

    obstacles = pygame.sprite.Group()
    midx = SceneSettings.tileXnum//2
    midy = SceneSettings.tileYnum//2
    for i in range(SceneSettings.tileXnum):
        for j in range(SceneSettings.tileYnum):
            if random() < SceneSettings.obstacleDensity and not(i < midx and j < midy) and (i not in range(midx-3, midx+3)) and (j not in range(midy-3, midy+3)):
                obstacles.add(Block(image, SceneSettings.tileWidth * i, SceneSettings.tileHeight * j))
    return obstacles  
get_height
get_height()

Get the height of the scene.

RETURNS DESCRIPTION

The height of the scene.

Source code in Tairitsu/Scene.py
124
125
126
127
128
129
130
131
def get_height(self):
    """
    Get the height of the scene.

    Returns:
        The height of the scene.
    """
    return WindowSettings.height * WindowSettings.outdoorScale
get_width
get_width()

Get the width of the scene.

RETURNS DESCRIPTION

The width of the scene.

Source code in Tairitsu/Scene.py
115
116
117
118
119
120
121
122
def get_width(self):
    """
    Get the width of the scene.

    Returns:
        The width of the scene.
    """
    return self.width * WindowSettings.outdoorScale
render
render(player: Player)
Source code in Tairitsu/Scene.py
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
def render(self, player: Player):
    # player.speed = SceneSettings.tileWidth / 5
    player.fixed = True
    # player.rect = player.rect.move(-player.dx, -player.dy)
    value = (int)(math.floor(255 * (1 - len(self.beat_timings) / self.length)))
    # value = 1 - len(self.beat_timings) / self.length
    print(value)
    super().render(player)
    self.mask.fill((255,255,255,value))
    self.window.blit(self.mask, (0, 0))
    # TODO: Change measure
    self.beat = self.bgm.position_beat() % self.meter[1]
    self.measure = math.floor(self.bgm.position_beat() / self.meter[1])
    try:
        data = self.measures[self.measure][:-1]
    except: data = [0]
    pygame.display.set_caption(f"{self.bgm.position_beat() + 1:.1f} | {self.beat + 1:.1f} - {self.measure} | {data}")
    for monster in self.monsters:
        if(monster.rect.topleft[0] < 250):
            monster.kill()
            self.infoBox = InfoBox("MISS", 1)
            self.beat_timings.pop(0)
    if(value >= 255):
        if(self.logoOpacity < 255):
            self.logoOpacity = self.logoOpacity + 3
            self.logo.set_alpha(self.logoOpacity)
        self.window.blit(self.logo, (0,0))
scene_event
scene_event(event: Event)
Source code in Tairitsu/Scene.py
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
def scene_event(self, event: Event):
    super().scene_event(event)
    # beat_timings = [i * self.meter[1] / len_note for i in range(len_note) if data[i] != "0"]

    # Key down space
    if event.type == pygame.KEYDOWN and event.key in [pygame.K_SPACE, pygame.K_d, pygame.K_f, pygame.K_j, pygame.K_k]:
    # key = pygame.key.get_pressed()
    # if key[pygame.K_SPACE]:
        player_input_time = self.bgm.position_beat()
        # player_input_time = self.beat
        print(f"DOWN AT {player_input_time:.1f}")
        # closest_timing = min(self.beat_timings, key=lambda x: abs(x - player_input_time))
        try:
            closest_timing = self.beat_timings[0]
            closest_timing_diff = closest_timing - player_input_time
            print(f"CLOSEST DIFF {closest_timing_diff:.1f}")

            if(abs(closest_timing_diff) < 0.2): self.infoBox = InfoBox("PERFECT", 1)
            elif(abs(closest_timing_diff) < 0.6): self.infoBox = InfoBox("GOOD", 1)
            elif(abs(closest_timing_diff) < 1): self.infoBox = InfoBox("BAD", 1)
            elif(closest_timing_diff < -1.5): self.infoBox = InfoBox("MISS", 1)

            if(closest_timing_diff <= 1):
                self.monsters.remove(self.monsters.sprites()[self.beat_timings.index(closest_timing)])
                self.beat_timings.remove(closest_timing)
        except:
            pass
trigger_battle
trigger_battle(player: Player)

Trigger a battle with a player.

PARAMETER DESCRIPTION
player

The Player object to trigger the battle with.

TYPE: Player

Source code in Tairitsu/Scene.py
84
85
86
87
88
89
90
91
def trigger_battle(self, player: Player):
    """
    Trigger a battle with a player.

    Args:
        player: The Player object to trigger the battle with.
    """
    pass
trigger_dialog
trigger_dialog(npc: NPC)

Trigger a dialog with an NPC.

PARAMETER DESCRIPTION
npc

The NPC object to trigger the dialog with.

TYPE: NPC

Source code in Tairitsu/Scene.py
67
68
69
70
71
72
73
74
75
76
def trigger_dialog(self, npc: NPC):
    """
    Trigger a dialog with an NPC.

    Args:
        npc: The NPC object to trigger the dialog with.
    """
    dialogBoxTemp = DialogBox(self.window, npc,
        ["Happy","2023!"], self.talktype)
    dialogBoxTemp.draw()
trigger_shop
trigger_shop(npc: NPC, player: Player)

Trigger a shop with an NPC.

PARAMETER DESCRIPTION
npc

The NPC object to trigger the shop with.

TYPE: NPC

player

The Player object to interact with the shop.

TYPE: Player

Source code in Tairitsu/Scene.py
 99
100
101
102
103
104
105
106
107
def trigger_shop(self, npc: NPC, player: Player):
    """
    Trigger a shop with an NPC.

    Args:
        npc: The NPC object to trigger the shop with.
        player: The Player object to interact with the shop.
    """
    pass
update_camera
update_camera(player: Player)
Source code in Tairitsu/Scene.py
504
505
506
def update_camera(self, player: Player):
    player.rect.center = (WindowSettings.width / 4 * 1, WindowSettings.height // 2)
    self.cameraX = self.bgm.position_beat() * 2 * SceneSettings.tileWidth - WindowSettings.width / 4 * 1

CityScene

CityScene(window: pygame.surface.Surface)

Bases: Scene

CityScene (inherit from Scene)

Source code in Tairitsu/Scene.py
321
322
323
324
325
def __init__(self, window: pygame.surface.Surface):
    super().__init__(window=window)
    self.state = GameState.GAME_PLAY_CITY
    self.BgmPlayer.play(GamePath.bgm["CITY"])
    self.gen_CITY()
Attributes
BgmPlayer instance-attribute
BgmPlayer = BgmPlayer()
battleBox instance-attribute
battleBox = None
cameraX instance-attribute
cameraX = 0
cameraY instance-attribute
cameraY = 0
height instance-attribute
height = WindowSettings.height
infoBox instance-attribute
infoBox = None
map instance-attribute
map = None
monsters instance-attribute
monsters = pygame.sprite.Group()
npcs instance-attribute
npcs = pygame.sprite.Group()
obstacles instance-attribute
obstacles = pygame.sprite.Group()
portals instance-attribute
portals = pygame.sprite.Group()
shoppingBox instance-attribute
shoppingBox = None
state instance-attribute
state = GameState.GAME_PLAY_CITY
tileXnum instance-attribute
tileXnum = SceneSettings.tileXnum
tileYnum instance-attribute
tileYnum = SceneSettings.tileYnum
width instance-attribute
width = WindowSettings.width
window instance-attribute
window = window
Functions
end_battle
end_battle()

End the current battle.

Source code in Tairitsu/Scene.py
93
94
95
96
97
def end_battle(self):
    """
    End the current battle.
    """
    pass
end_dialog
end_dialog()

End the current dialog.

Source code in Tairitsu/Scene.py
78
79
80
81
82
def end_dialog(self):
    """
    End the current dialog.
    """
    pass
end_shop
end_shop()

End the current shop.

Source code in Tairitsu/Scene.py
109
110
111
112
113
def end_shop(self):
    """
    End the current shop.
    """
    pass
gen_CITY
gen_CITY()

Generate CITY map, obstacles, npcs, portals

Source code in Tairitsu/Scene.py
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
def gen_CITY(self):
    """
    Generate CITY map, obstacles, npcs, portals
    """
    self.map = self.gen_city_map()
    self.obstacles = self.gen_city_obstacle()
    self.npcs.add(DialogNPC(self.width // 3, self.height // 3, "和蔼的新手村导师",
                            dialog = lambda skinPath, name : DialogBox(self.window, skinPath, name, [f"你好,{os.getlogin()},欢迎来到靶场","你将扮演一位名为「旅行者」的神秘角色,逐步发掘原神的真相"], Archive.talkType)))

    self.npcs.add(ShopNPC(self.width // 5, self.height // 5,
                          name = "不坑爹的商人", 
                          dialog = lambda npc, name : ShoppingBox(self.window, npc, name, Archive.player, items = {"Attack +1": "Coin -15", "Defence +1": "Coin -15",
                            "HP +1": "Coin -15", "送你一程": "HP -5", "Exit": ""}, 
                            actions =  [{"addAttack": 1, "addCoins": -15},
                                        {"addDefence": 1, "addCoins": -15},
                                        {"addHP": 1, "addCoins": -15},
                                        {"addHP": -5, "addCoins": 0},
                                        {"addCoins": 0}])))
    self.portals.add(Portal(self.width // 3 * 2, self.height // 3 * 2, WildScene))
    self.monsters.add(Monster(WindowSettings.width // 4, WindowSettings.height // 4 + 180))
    self.monsters.add(Monster(WindowSettings.width // 8, WindowSettings.height // 4 + 180))
    self.monsters.add(Monster(WindowSettings.width // 4 * 3, WindowSettings.height // 4 + 180))
gen_city_map
gen_city_map()

Generate city map (by gen_random_map)

Source code in Tairitsu/Scene.py
351
352
353
354
355
def gen_city_map(self):
    """
    Generate city map (by gen_random_map)
    """
    return self.gen_random_map(GamePath.cityTiles)
gen_city_obstacle
gen_city_obstacle()

Generate city obstacles (by gen_random_obstacles)

Source code in Tairitsu/Scene.py
358
359
360
361
362
def gen_city_obstacle(self):
    """
    Generate city obstacles (by gen_random_obstacles)
    """
    return self.gen_random_obstacles(GamePath.tree)
gen_random_map
gen_random_map(TilesPath: GamePath.groundTiles) -> list

Generate a random map.

PARAMETER DESCRIPTION
TilesPath

A list of paths of tiles.

TYPE: GamePath.groundTiles

RETURNS DESCRIPTION
list

A 2D list representing the map.

Source code in Tairitsu/Scene.py
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
def gen_random_map(self, TilesPath: GamePath.groundTiles) -> list:
    """
    Generate a random map.

    Args:
        TilesPath: A list of paths of tiles.

    Returns:
        A 2D list representing the map.
    """
    images = [pygame.image.load(tile) for tile in TilesPath]
    images = [pygame.transform.scale(image, (SceneSettings.tileWidth, SceneSettings.tileHeight)) for image in images]

    mapObj = []
    for i in range(self.tileXnum):
        tmp = []
        for j in range(self.tileYnum):
            tmp.append(images[randint(0, len(images) - 1)])
        mapObj.append(tmp)

    return mapObj
gen_random_obstacles
gen_random_obstacles(imagePath=GamePath.tree)

Generate random obstacles.

PARAMETER DESCRIPTION
imagePath

The path of the obstacle image.

DEFAULT: GamePath.tree

RETURNS DESCRIPTION

A group of obstacle sprites.

Source code in Tairitsu/Scene.py
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
def gen_random_obstacles(self, imagePath = GamePath.tree):
    """
    Generate random obstacles.

    Args:
        imagePath: The path of the obstacle image.

    Returns:
        A group of obstacle sprites.
    """
    image = pygame.image.load(imagePath) 

    obstacles = pygame.sprite.Group()
    midx = SceneSettings.tileXnum//2
    midy = SceneSettings.tileYnum//2
    for i in range(SceneSettings.tileXnum):
        for j in range(SceneSettings.tileYnum):
            if random() < SceneSettings.obstacleDensity and not(i < midx and j < midy) and (i not in range(midx-3, midx+3)) and (j not in range(midy-3, midy+3)):
                obstacles.add(Block(image, SceneSettings.tileWidth * i, SceneSettings.tileHeight * j))
    return obstacles  
get_height
get_height()

Get the height of the scene.

RETURNS DESCRIPTION

The height of the scene.

Source code in Tairitsu/Scene.py
124
125
126
127
128
129
130
131
def get_height(self):
    """
    Get the height of the scene.

    Returns:
        The height of the scene.
    """
    return WindowSettings.height * WindowSettings.outdoorScale
get_width
get_width()

Get the width of the scene.

RETURNS DESCRIPTION

The width of the scene.

Source code in Tairitsu/Scene.py
115
116
117
118
119
120
121
122
def get_width(self):
    """
    Get the width of the scene.

    Returns:
        The width of the scene.
    """
    return self.width * WindowSettings.outdoorScale
render
render(player: Player)

Render the scene.

PARAMETER DESCRIPTION
player

The Player object.

TYPE: Player

Source code in Tairitsu/Scene.py
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
def render(self, player: Player):
    """
    Render the scene.

    Args:
        player: The Player object.
    """
    for each in self.npcs.sprites():
        each.update()
    for each in self.monsters.sprites():
        each.update()

    if(type(self.map) == list):
        # Only render visible tiles
        for i in range(self.tileXnum):
            if(SceneSettings.tileWidth * i - self.cameraX < -SceneSettings.tileWidth or SceneSettings.tileWidth * i - self.cameraX > WindowSettings.width): 
                continue
            for j in range(self.tileYnum):
                if(SceneSettings.tileHeight * j - self.cameraY < -SceneSettings.tileHeight or SceneSettings.tileHeight * j - self.cameraY > WindowSettings.height): 
                    continue
                self.window.blit(self.map[i][j], 
                                (SceneSettings.tileWidth * i - self.cameraX, 
                                SceneSettings.tileHeight * j - self.cameraY))
    else:
        self.window.blit(self.map, (0, 0))

    # newSurface2 = pygame.Surface((WindowSettings.width, WindowSettings.height), pygame.SRCALPHA)
    player.draw(self.window)

    for each in self.obstacles.sprites():
        each.rect.topleft = (each.actualRect.x - self.cameraX, each.actualRect.y - self.cameraY)
        if(each.actualRect.x - self.cameraX < -each.actualRect.width or each.actualRect.x - self.cameraX > WindowSettings.width):
            pass # TODO: other 
        else:
            each.draw(self.window)
    for each in self.npcs.sprites():
        each.rect.topleft = (each.actualRect.x - self.cameraX, each.actualRect.y - self.cameraY)
        if(each.actualRect.x - self.cameraX < -each.actualRect.width or each.actualRect.x - self.cameraX > WindowSettings.width):
            pass # TODO: other 
        else:
            each.draw(self.window)
    for each in self.monsters.sprites():
        each.rect.topleft = (each.actualRect.x - self.cameraX, each.actualRect.y - self.cameraY)
        # if(each.actualRect.x - self.cameraX < -each.actualRect.width or each.actualRect.x - self.cameraX > WindowSettings.width):
        #     pass # TODO: other 
        # else:
        each.draw(self.window)
    # self.monsters.draw(self.window)
    for each in self.portals.sprites():
        each.rect.topleft = (each.actualRect.x - self.cameraX, each.actualRect.y - self.cameraY)
        if(each.actualRect.x - self.cameraX < -each.actualRect.width or each.actualRect.x - self.cameraX > WindowSettings.width):
            pass # TODO: other 
        else:
            each.draw(self.window)
scene_event
scene_event(event: list[pygame.event.Event])
Source code in Tairitsu/Scene.py
64
65
def scene_event(self, event: list[pygame.event.Event]):
    pass
trigger_battle
trigger_battle(player: Player)

Trigger a battle with a player.

PARAMETER DESCRIPTION
player

The Player object to trigger the battle with.

TYPE: Player

Source code in Tairitsu/Scene.py
84
85
86
87
88
89
90
91
def trigger_battle(self, player: Player):
    """
    Trigger a battle with a player.

    Args:
        player: The Player object to trigger the battle with.
    """
    pass
trigger_dialog
trigger_dialog(npc: NPC)

Trigger a dialog with an NPC.

PARAMETER DESCRIPTION
npc

The NPC object to trigger the dialog with.

TYPE: NPC

Source code in Tairitsu/Scene.py
67
68
69
70
71
72
73
74
75
76
def trigger_dialog(self, npc: NPC):
    """
    Trigger a dialog with an NPC.

    Args:
        npc: The NPC object to trigger the dialog with.
    """
    dialogBoxTemp = DialogBox(self.window, npc,
        ["Happy","2023!"], self.talktype)
    dialogBoxTemp.draw()
trigger_shop
trigger_shop(npc: NPC, player: Player)

Trigger a shop with an NPC.

PARAMETER DESCRIPTION
npc

The NPC object to trigger the shop with.

TYPE: NPC

player

The Player object to interact with the shop.

TYPE: Player

Source code in Tairitsu/Scene.py
 99
100
101
102
103
104
105
106
107
def trigger_shop(self, npc: NPC, player: Player):
    """
    Trigger a shop with an NPC.

    Args:
        npc: The NPC object to trigger the shop with.
        player: The Player object to interact with the shop.
    """
    pass
update_camera
update_camera(player: Player)

Update the camera position based on the player's position.

PARAMETER DESCRIPTION
player

The Player object.

TYPE: Player

Source code in Tairitsu/Scene.py
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
def update_camera(self, player: Player):
    """
    Update the camera position based on the player's position.

    Args:
        player: The Player object.
    """
    if self.state in [GameState.GAME_PLAY_WILD, GameState.GAME_PLAY_CITY]:
        if player.rect.x > WindowSettings.width / 4 * 3:
            self.cameraX += player.speed
            if self.cameraX < self.get_width() - WindowSettings.width:
                player.fix_to_middle(player.speed, 0)
            else:
                self.cameraX = self.get_width() - WindowSettings.width
        elif player.rect.x < WindowSettings.width / 4:
            self.cameraX -= player.speed
            if self.cameraX > 0:
                player.fix_to_middle(-player.speed, 0)
            else:
                self.cameraX = 0
        if player.rect.y > WindowSettings.height / 4 * 3:
            self.cameraY += player.speed
            if self.cameraY < self.get_height() - WindowSettings.height:
                player.fix_to_middle(0, player.speed)
            else:
                self.cameraY = self.get_height() - WindowSettings.height
        elif player.rect.y < WindowSettings.height / 4:
            self.cameraY -= player.speed
            if self.cameraY > 0:
                player.fix_to_middle(0, -player.speed)
            else:
                self.cameraY = 0

DeathScene

DeathScene(window: pygame.surface.Surface)

Bases: Scene

Source code in Tairitsu/Scene.py
299
300
301
302
303
304
305
306
307
308
309
310
311
def __init__(self, window: pygame.surface.Surface):
    super().__init__(window)
    self.state = GameState.GAME_OVER
    self.window = window
    self.bg = pygame.Surface((WindowSettings.width, WindowSettings.height))
    self.bg.fill((0,0,0))
    self.font = pygame.font.Font(GlobalSettings.fontPath, MenuSettings.textSize)
    self.text = self.font.render(f"{os.getlogin()},你死了!\n请保持你的决心!",
                            True, (255, 255, 255))
    self.BgmPlayer.play(GamePath.bgm["DEATH"], 1)
    # SoundFX(GamePath.bgm["DEATH"])
    self.textRect = self.text.get_rect(center=(WindowSettings.width // 2, 
                            WindowSettings.height // 2))
Attributes
BgmPlayer instance-attribute
BgmPlayer = BgmPlayer()
battleBox instance-attribute
battleBox = None
bg instance-attribute
bg = pygame.Surface((WindowSettings.width, WindowSettings.height))
cameraX instance-attribute
cameraX = 0
cameraY instance-attribute
cameraY = 0
font instance-attribute
font = pygame.font.Font(GlobalSettings.fontPath, MenuSettings.textSize)
height instance-attribute
height = WindowSettings.height
infoBox instance-attribute
infoBox = None
map instance-attribute
map = None
monsters instance-attribute
monsters = pygame.sprite.Group()
npcs instance-attribute
npcs = pygame.sprite.Group()
obstacles instance-attribute
obstacles = pygame.sprite.Group()
portals instance-attribute
portals = pygame.sprite.Group()
shoppingBox instance-attribute
shoppingBox = None
state instance-attribute
state = GameState.GAME_OVER
text instance-attribute
text = self.font.render(f'{os.getlogin()},你死了!
请保持你的决心', True, (255, 255, 255))
textRect instance-attribute
textRect = self.text.get_rect(center=(WindowSettings.width // 2, WindowSettings.height // 2))
tileXnum instance-attribute
tileXnum = SceneSettings.tileXnum
tileYnum instance-attribute
tileYnum = SceneSettings.tileYnum
width instance-attribute
width = WindowSettings.width
window instance-attribute
window = window
Functions
end_battle
end_battle()

End the current battle.

Source code in Tairitsu/Scene.py
93
94
95
96
97
def end_battle(self):
    """
    End the current battle.
    """
    pass
end_dialog
end_dialog()

End the current dialog.

Source code in Tairitsu/Scene.py
78
79
80
81
82
def end_dialog(self):
    """
    End the current dialog.
    """
    pass
end_shop
end_shop()

End the current shop.

Source code in Tairitsu/Scene.py
109
110
111
112
113
def end_shop(self):
    """
    End the current shop.
    """
    pass
gen_random_map
gen_random_map(TilesPath: GamePath.groundTiles) -> list

Generate a random map.

PARAMETER DESCRIPTION
TilesPath

A list of paths of tiles.

TYPE: GamePath.groundTiles

RETURNS DESCRIPTION
list

A 2D list representing the map.

Source code in Tairitsu/Scene.py
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
def gen_random_map(self, TilesPath: GamePath.groundTiles) -> list:
    """
    Generate a random map.

    Args:
        TilesPath: A list of paths of tiles.

    Returns:
        A 2D list representing the map.
    """
    images = [pygame.image.load(tile) for tile in TilesPath]
    images = [pygame.transform.scale(image, (SceneSettings.tileWidth, SceneSettings.tileHeight)) for image in images]

    mapObj = []
    for i in range(self.tileXnum):
        tmp = []
        for j in range(self.tileYnum):
            tmp.append(images[randint(0, len(images) - 1)])
        mapObj.append(tmp)

    return mapObj
gen_random_obstacles
gen_random_obstacles(imagePath=GamePath.tree)

Generate random obstacles.

PARAMETER DESCRIPTION
imagePath

The path of the obstacle image.

DEFAULT: GamePath.tree

RETURNS DESCRIPTION

A group of obstacle sprites.

Source code in Tairitsu/Scene.py
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
def gen_random_obstacles(self, imagePath = GamePath.tree):
    """
    Generate random obstacles.

    Args:
        imagePath: The path of the obstacle image.

    Returns:
        A group of obstacle sprites.
    """
    image = pygame.image.load(imagePath) 

    obstacles = pygame.sprite.Group()
    midx = SceneSettings.tileXnum//2
    midy = SceneSettings.tileYnum//2
    for i in range(SceneSettings.tileXnum):
        for j in range(SceneSettings.tileYnum):
            if random() < SceneSettings.obstacleDensity and not(i < midx and j < midy) and (i not in range(midx-3, midx+3)) and (j not in range(midy-3, midy+3)):
                obstacles.add(Block(image, SceneSettings.tileWidth * i, SceneSettings.tileHeight * j))
    return obstacles  
get_height
get_height()

Get the height of the scene.

RETURNS DESCRIPTION

The height of the scene.

Source code in Tairitsu/Scene.py
124
125
126
127
128
129
130
131
def get_height(self):
    """
    Get the height of the scene.

    Returns:
        The height of the scene.
    """
    return WindowSettings.height * WindowSettings.outdoorScale
get_width
get_width()

Get the width of the scene.

RETURNS DESCRIPTION

The width of the scene.

Source code in Tairitsu/Scene.py
115
116
117
118
119
120
121
122
def get_width(self):
    """
    Get the width of the scene.

    Returns:
        The width of the scene.
    """
    return self.width * WindowSettings.outdoorScale
render
render(_)
Source code in Tairitsu/Scene.py
313
314
315
def render(self, _):
    self.window.blit(self.bg, (0, 0))
    self.window.blit(self.text, self.textRect)
scene_event
scene_event(event: list[pygame.event.Event])
Source code in Tairitsu/Scene.py
64
65
def scene_event(self, event: list[pygame.event.Event]):
    pass
trigger_battle
trigger_battle(player: Player)

Trigger a battle with a player.

PARAMETER DESCRIPTION
player

The Player object to trigger the battle with.

TYPE: Player

Source code in Tairitsu/Scene.py
84
85
86
87
88
89
90
91
def trigger_battle(self, player: Player):
    """
    Trigger a battle with a player.

    Args:
        player: The Player object to trigger the battle with.
    """
    pass
trigger_dialog
trigger_dialog(npc: NPC)

Trigger a dialog with an NPC.

PARAMETER DESCRIPTION
npc

The NPC object to trigger the dialog with.

TYPE: NPC

Source code in Tairitsu/Scene.py
67
68
69
70
71
72
73
74
75
76
def trigger_dialog(self, npc: NPC):
    """
    Trigger a dialog with an NPC.

    Args:
        npc: The NPC object to trigger the dialog with.
    """
    dialogBoxTemp = DialogBox(self.window, npc,
        ["Happy","2023!"], self.talktype)
    dialogBoxTemp.draw()
trigger_shop
trigger_shop(npc: NPC, player: Player)

Trigger a shop with an NPC.

PARAMETER DESCRIPTION
npc

The NPC object to trigger the shop with.

TYPE: NPC

player

The Player object to interact with the shop.

TYPE: Player

Source code in Tairitsu/Scene.py
 99
100
101
102
103
104
105
106
107
def trigger_shop(self, npc: NPC, player: Player):
    """
    Trigger a shop with an NPC.

    Args:
        npc: The NPC object to trigger the shop with.
        player: The Player object to interact with the shop.
    """
    pass
update_camera
update_camera(player: Player)

Update the camera position based on the player's position.

PARAMETER DESCRIPTION
player

The Player object.

TYPE: Player

Source code in Tairitsu/Scene.py
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
def update_camera(self, player: Player):
    """
    Update the camera position based on the player's position.

    Args:
        player: The Player object.
    """
    if self.state in [GameState.GAME_PLAY_WILD, GameState.GAME_PLAY_CITY]:
        if player.rect.x > WindowSettings.width / 4 * 3:
            self.cameraX += player.speed
            if self.cameraX < self.get_width() - WindowSettings.width:
                player.fix_to_middle(player.speed, 0)
            else:
                self.cameraX = self.get_width() - WindowSettings.width
        elif player.rect.x < WindowSettings.width / 4:
            self.cameraX -= player.speed
            if self.cameraX > 0:
                player.fix_to_middle(-player.speed, 0)
            else:
                self.cameraX = 0
        if player.rect.y > WindowSettings.height / 4 * 3:
            self.cameraY += player.speed
            if self.cameraY < self.get_height() - WindowSettings.height:
                player.fix_to_middle(0, player.speed)
            else:
                self.cameraY = self.get_height() - WindowSettings.height
        elif player.rect.y < WindowSettings.height / 4:
            self.cameraY -= player.speed
            if self.cameraY > 0:
                player.fix_to_middle(0, -player.speed)
            else:
                self.cameraY = 0

Scene

Scene(window: pygame.surface.Surface)

Initialize a Scene object.

PARAMETER DESCRIPTION
window

The pygame surface representing the game window.

TYPE: pygame.surface.Surface

ATTRIBUTE DESCRIPTION
type

The type of the scene.

map

The map of the scene.

obstacles

A group of obstacle sprites.

npcs

A group of NPC sprites.

portals

A group of portal sprites.

monsters

A group of monster sprites.

battleBox

The battle box object.

window

The game window surface.

width

The width of the game window.

height

The height of the game window.

cameraX

The X-coordinate of the camera.

cameraY

The Y-coordinate of the camera.

Source code in Tairitsu/Scene.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
def __init__(self, window: pygame.surface.Surface):
    """
    Initialize a Scene object.

    Args:
        window: The pygame surface representing the game window.

    Attributes:
        type: The type of the scene.
        map: The map of the scene.
        obstacles: A group of obstacle sprites.
        npcs: A group of NPC sprites.
        portals: A group of portal sprites.
        monsters: A group of monster sprites.
        battleBox: The battle box object.
        window: The game window surface.
        width: The width of the game window.
        height: The height of the game window.
        cameraX: The X-coordinate of the camera.
        cameraY: The Y-coordinate of the camera.
    """
    self.state = None

    self.map = None
    self.obstacles = pygame.sprite.Group()
    self.npcs = pygame.sprite.Group()
    self.portals = pygame.sprite.Group()
    self.monsters = pygame.sprite.Group()

    self.battleBox = None
    self.shoppingBox = None
    self.window = window
    self.width = WindowSettings.width
    self.height = WindowSettings.height    
    self.tileXnum = SceneSettings.tileXnum
    self.tileYnum = SceneSettings.tileYnum
    self.infoBox = None

    self.cameraX = 0
    self.cameraY = 0 

    self.BgmPlayer = BgmPlayer()
Attributes
BgmPlayer instance-attribute
BgmPlayer = BgmPlayer()
battleBox instance-attribute
battleBox = None
cameraX instance-attribute
cameraX = 0
cameraY instance-attribute
cameraY = 0
height instance-attribute
height = WindowSettings.height
infoBox instance-attribute
infoBox = None
map instance-attribute
map = None
monsters instance-attribute
monsters = pygame.sprite.Group()
npcs instance-attribute
npcs = pygame.sprite.Group()
obstacles instance-attribute
obstacles = pygame.sprite.Group()
portals instance-attribute
portals = pygame.sprite.Group()
shoppingBox instance-attribute
shoppingBox = None
state instance-attribute
state = None
tileXnum instance-attribute
tileXnum = SceneSettings.tileXnum
tileYnum instance-attribute
tileYnum = SceneSettings.tileYnum
width instance-attribute
width = WindowSettings.width
window instance-attribute
window = window
Functions
end_battle
end_battle()

End the current battle.

Source code in Tairitsu/Scene.py
93
94
95
96
97
def end_battle(self):
    """
    End the current battle.
    """
    pass
end_dialog
end_dialog()

End the current dialog.

Source code in Tairitsu/Scene.py
78
79
80
81
82
def end_dialog(self):
    """
    End the current dialog.
    """
    pass
end_shop
end_shop()

End the current shop.

Source code in Tairitsu/Scene.py
109
110
111
112
113
def end_shop(self):
    """
    End the current shop.
    """
    pass
gen_random_map
gen_random_map(TilesPath: GamePath.groundTiles) -> list

Generate a random map.

PARAMETER DESCRIPTION
TilesPath

A list of paths of tiles.

TYPE: GamePath.groundTiles

RETURNS DESCRIPTION
list

A 2D list representing the map.

Source code in Tairitsu/Scene.py
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
def gen_random_map(self, TilesPath: GamePath.groundTiles) -> list:
    """
    Generate a random map.

    Args:
        TilesPath: A list of paths of tiles.

    Returns:
        A 2D list representing the map.
    """
    images = [pygame.image.load(tile) for tile in TilesPath]
    images = [pygame.transform.scale(image, (SceneSettings.tileWidth, SceneSettings.tileHeight)) for image in images]

    mapObj = []
    for i in range(self.tileXnum):
        tmp = []
        for j in range(self.tileYnum):
            tmp.append(images[randint(0, len(images) - 1)])
        mapObj.append(tmp)

    return mapObj
gen_random_obstacles
gen_random_obstacles(imagePath=GamePath.tree)

Generate random obstacles.

PARAMETER DESCRIPTION
imagePath

The path of the obstacle image.

DEFAULT: GamePath.tree

RETURNS DESCRIPTION

A group of obstacle sprites.

Source code in Tairitsu/Scene.py
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
def gen_random_obstacles(self, imagePath = GamePath.tree):
    """
    Generate random obstacles.

    Args:
        imagePath: The path of the obstacle image.

    Returns:
        A group of obstacle sprites.
    """
    image = pygame.image.load(imagePath) 

    obstacles = pygame.sprite.Group()
    midx = SceneSettings.tileXnum//2
    midy = SceneSettings.tileYnum//2
    for i in range(SceneSettings.tileXnum):
        for j in range(SceneSettings.tileYnum):
            if random() < SceneSettings.obstacleDensity and not(i < midx and j < midy) and (i not in range(midx-3, midx+3)) and (j not in range(midy-3, midy+3)):
                obstacles.add(Block(image, SceneSettings.tileWidth * i, SceneSettings.tileHeight * j))
    return obstacles  
get_height
get_height()

Get the height of the scene.

RETURNS DESCRIPTION

The height of the scene.

Source code in Tairitsu/Scene.py
124
125
126
127
128
129
130
131
def get_height(self):
    """
    Get the height of the scene.

    Returns:
        The height of the scene.
    """
    return WindowSettings.height * WindowSettings.outdoorScale
get_width
get_width()

Get the width of the scene.

RETURNS DESCRIPTION

The width of the scene.

Source code in Tairitsu/Scene.py
115
116
117
118
119
120
121
122
def get_width(self):
    """
    Get the width of the scene.

    Returns:
        The width of the scene.
    """
    return self.width * WindowSettings.outdoorScale
render
render(player: Player)

Render the scene.

PARAMETER DESCRIPTION
player

The Player object.

TYPE: Player

Source code in Tairitsu/Scene.py
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
def render(self, player: Player):
    """
    Render the scene.

    Args:
        player: The Player object.
    """
    for each in self.npcs.sprites():
        each.update()
    for each in self.monsters.sprites():
        each.update()

    if(type(self.map) == list):
        # Only render visible tiles
        for i in range(self.tileXnum):
            if(SceneSettings.tileWidth * i - self.cameraX < -SceneSettings.tileWidth or SceneSettings.tileWidth * i - self.cameraX > WindowSettings.width): 
                continue
            for j in range(self.tileYnum):
                if(SceneSettings.tileHeight * j - self.cameraY < -SceneSettings.tileHeight or SceneSettings.tileHeight * j - self.cameraY > WindowSettings.height): 
                    continue
                self.window.blit(self.map[i][j], 
                                (SceneSettings.tileWidth * i - self.cameraX, 
                                SceneSettings.tileHeight * j - self.cameraY))
    else:
        self.window.blit(self.map, (0, 0))

    # newSurface2 = pygame.Surface((WindowSettings.width, WindowSettings.height), pygame.SRCALPHA)
    player.draw(self.window)

    for each in self.obstacles.sprites():
        each.rect.topleft = (each.actualRect.x - self.cameraX, each.actualRect.y - self.cameraY)
        if(each.actualRect.x - self.cameraX < -each.actualRect.width or each.actualRect.x - self.cameraX > WindowSettings.width):
            pass # TODO: other 
        else:
            each.draw(self.window)
    for each in self.npcs.sprites():
        each.rect.topleft = (each.actualRect.x - self.cameraX, each.actualRect.y - self.cameraY)
        if(each.actualRect.x - self.cameraX < -each.actualRect.width or each.actualRect.x - self.cameraX > WindowSettings.width):
            pass # TODO: other 
        else:
            each.draw(self.window)
    for each in self.monsters.sprites():
        each.rect.topleft = (each.actualRect.x - self.cameraX, each.actualRect.y - self.cameraY)
        # if(each.actualRect.x - self.cameraX < -each.actualRect.width or each.actualRect.x - self.cameraX > WindowSettings.width):
        #     pass # TODO: other 
        # else:
        each.draw(self.window)
    # self.monsters.draw(self.window)
    for each in self.portals.sprites():
        each.rect.topleft = (each.actualRect.x - self.cameraX, each.actualRect.y - self.cameraY)
        if(each.actualRect.x - self.cameraX < -each.actualRect.width or each.actualRect.x - self.cameraX > WindowSettings.width):
            pass # TODO: other 
        else:
            each.draw(self.window)
scene_event
scene_event(event: list[pygame.event.Event])
Source code in Tairitsu/Scene.py
64
65
def scene_event(self, event: list[pygame.event.Event]):
    pass
trigger_battle
trigger_battle(player: Player)

Trigger a battle with a player.

PARAMETER DESCRIPTION
player

The Player object to trigger the battle with.

TYPE: Player

Source code in Tairitsu/Scene.py
84
85
86
87
88
89
90
91
def trigger_battle(self, player: Player):
    """
    Trigger a battle with a player.

    Args:
        player: The Player object to trigger the battle with.
    """
    pass
trigger_dialog
trigger_dialog(npc: NPC)

Trigger a dialog with an NPC.

PARAMETER DESCRIPTION
npc

The NPC object to trigger the dialog with.

TYPE: NPC

Source code in Tairitsu/Scene.py
67
68
69
70
71
72
73
74
75
76
def trigger_dialog(self, npc: NPC):
    """
    Trigger a dialog with an NPC.

    Args:
        npc: The NPC object to trigger the dialog with.
    """
    dialogBoxTemp = DialogBox(self.window, npc,
        ["Happy","2023!"], self.talktype)
    dialogBoxTemp.draw()
trigger_shop
trigger_shop(npc: NPC, player: Player)

Trigger a shop with an NPC.

PARAMETER DESCRIPTION
npc

The NPC object to trigger the shop with.

TYPE: NPC

player

The Player object to interact with the shop.

TYPE: Player

Source code in Tairitsu/Scene.py
 99
100
101
102
103
104
105
106
107
def trigger_shop(self, npc: NPC, player: Player):
    """
    Trigger a shop with an NPC.

    Args:
        npc: The NPC object to trigger the shop with.
        player: The Player object to interact with the shop.
    """
    pass
update_camera
update_camera(player: Player)

Update the camera position based on the player's position.

PARAMETER DESCRIPTION
player

The Player object.

TYPE: Player

Source code in Tairitsu/Scene.py
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
def update_camera(self, player: Player):
    """
    Update the camera position based on the player's position.

    Args:
        player: The Player object.
    """
    if self.state in [GameState.GAME_PLAY_WILD, GameState.GAME_PLAY_CITY]:
        if player.rect.x > WindowSettings.width / 4 * 3:
            self.cameraX += player.speed
            if self.cameraX < self.get_width() - WindowSettings.width:
                player.fix_to_middle(player.speed, 0)
            else:
                self.cameraX = self.get_width() - WindowSettings.width
        elif player.rect.x < WindowSettings.width / 4:
            self.cameraX -= player.speed
            if self.cameraX > 0:
                player.fix_to_middle(-player.speed, 0)
            else:
                self.cameraX = 0
        if player.rect.y > WindowSettings.height / 4 * 3:
            self.cameraY += player.speed
            if self.cameraY < self.get_height() - WindowSettings.height:
                player.fix_to_middle(0, player.speed)
            else:
                self.cameraY = self.get_height() - WindowSettings.height
        elif player.rect.y < WindowSettings.height / 4:
            self.cameraY -= player.speed
            if self.cameraY > 0:
                player.fix_to_middle(0, -player.speed)
            else:
                self.cameraY = 0

StartMenu

StartMenu(window: pygame.surface.Surface)

Bases: Scene

Source code in Tairitsu/Scene.py
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
def __init__(self, window: pygame.surface.Surface):
    super().__init__(window)
    self.state = GameState.MAIN_MENU
    self.BgmPlayer.play(GamePath.bgm["MENU"])
    self.bg = pygame.image.load(GamePath.menu)
    self.bg = pygame.transform.scale(self.bg, 
            (WindowSettings.width, WindowSettings.height))

    self.font = pygame.font.Font(None, MenuSettings.textSize)
    self.text = self.font.render("W/S to move cursor, ENTER to select",
                            True, (255, 255, 255))
    self.textRect = self.text.get_rect(center=(WindowSettings.width // 2, 
                            WindowSettings.height - 50))

    self.blinkTimer = 0
    self.menu = MenuBox(window, items = {"Start":"","GKD":"gaokuaidian", "Exit": ""})
Attributes
BgmPlayer instance-attribute
BgmPlayer = BgmPlayer()
battleBox instance-attribute
battleBox = None
bg instance-attribute
bg = pygame.transform.scale(self.bg, (WindowSettings.width, WindowSettings.height))
blinkTimer instance-attribute
blinkTimer = 0
cameraX instance-attribute
cameraX = 0
cameraY instance-attribute
cameraY = 0
font instance-attribute
font = pygame.font.Font(None, MenuSettings.textSize)
height instance-attribute
height = WindowSettings.height
infoBox instance-attribute
infoBox = None
map instance-attribute
map = None
menu instance-attribute
menu = MenuBox(window, items={'Start': '', 'GKD': 'gaokuaidian', 'Exit': ''})
monsters instance-attribute
monsters = pygame.sprite.Group()
npcs instance-attribute
npcs = pygame.sprite.Group()
obstacles instance-attribute
obstacles = pygame.sprite.Group()
portals instance-attribute
portals = pygame.sprite.Group()
shoppingBox instance-attribute
shoppingBox = None
state instance-attribute
state = GameState.MAIN_MENU
text instance-attribute
text = self.font.render('W/S to move cursor, ENTER to select', True, (255, 255, 255))
textRect instance-attribute
textRect = self.text.get_rect(center=(WindowSettings.width // 2, WindowSettings.height - 50))
tileXnum instance-attribute
tileXnum = SceneSettings.tileXnum
tileYnum instance-attribute
tileYnum = SceneSettings.tileYnum
width instance-attribute
width = WindowSettings.width
window instance-attribute
window = window
Functions
end_battle
end_battle()

End the current battle.

Source code in Tairitsu/Scene.py
93
94
95
96
97
def end_battle(self):
    """
    End the current battle.
    """
    pass
end_dialog
end_dialog()

End the current dialog.

Source code in Tairitsu/Scene.py
78
79
80
81
82
def end_dialog(self):
    """
    End the current dialog.
    """
    pass
end_shop
end_shop()

End the current shop.

Source code in Tairitsu/Scene.py
109
110
111
112
113
def end_shop(self):
    """
    End the current shop.
    """
    pass
gen_random_map
gen_random_map(TilesPath: GamePath.groundTiles) -> list

Generate a random map.

PARAMETER DESCRIPTION
TilesPath

A list of paths of tiles.

TYPE: GamePath.groundTiles

RETURNS DESCRIPTION
list

A 2D list representing the map.

Source code in Tairitsu/Scene.py
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
def gen_random_map(self, TilesPath: GamePath.groundTiles) -> list:
    """
    Generate a random map.

    Args:
        TilesPath: A list of paths of tiles.

    Returns:
        A 2D list representing the map.
    """
    images = [pygame.image.load(tile) for tile in TilesPath]
    images = [pygame.transform.scale(image, (SceneSettings.tileWidth, SceneSettings.tileHeight)) for image in images]

    mapObj = []
    for i in range(self.tileXnum):
        tmp = []
        for j in range(self.tileYnum):
            tmp.append(images[randint(0, len(images) - 1)])
        mapObj.append(tmp)

    return mapObj
gen_random_obstacles
gen_random_obstacles(imagePath=GamePath.tree)

Generate random obstacles.

PARAMETER DESCRIPTION
imagePath

The path of the obstacle image.

DEFAULT: GamePath.tree

RETURNS DESCRIPTION

A group of obstacle sprites.

Source code in Tairitsu/Scene.py
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
def gen_random_obstacles(self, imagePath = GamePath.tree):
    """
    Generate random obstacles.

    Args:
        imagePath: The path of the obstacle image.

    Returns:
        A group of obstacle sprites.
    """
    image = pygame.image.load(imagePath) 

    obstacles = pygame.sprite.Group()
    midx = SceneSettings.tileXnum//2
    midy = SceneSettings.tileYnum//2
    for i in range(SceneSettings.tileXnum):
        for j in range(SceneSettings.tileYnum):
            if random() < SceneSettings.obstacleDensity and not(i < midx and j < midy) and (i not in range(midx-3, midx+3)) and (j not in range(midy-3, midy+3)):
                obstacles.add(Block(image, SceneSettings.tileWidth * i, SceneSettings.tileHeight * j))
    return obstacles  
get_height
get_height()

Get the height of the scene.

RETURNS DESCRIPTION

The height of the scene.

Source code in Tairitsu/Scene.py
124
125
126
127
128
129
130
131
def get_height(self):
    """
    Get the height of the scene.

    Returns:
        The height of the scene.
    """
    return WindowSettings.height * WindowSettings.outdoorScale
get_width
get_width()

Get the width of the scene.

RETURNS DESCRIPTION

The width of the scene.

Source code in Tairitsu/Scene.py
115
116
117
118
119
120
121
122
def get_width(self):
    """
    Get the width of the scene.

    Returns:
        The width of the scene.
    """
    return self.width * WindowSettings.outdoorScale
render
render(_)
Source code in Tairitsu/Scene.py
286
287
288
289
290
291
292
293
294
295
def render(self, _):
    self.window.blit(self.bg, (0, 0))

    self.blinkTimer += 1
    if self.blinkTimer >= MenuSettings.blinkInterval:
        self.window.blit(self.text, self.textRect)
        if self.blinkTimer >= MenuSettings.blinkInterval * 2:
            self.blinkTimer = 0

    self.menu.draw()
scene_event
scene_event(event: list[pygame.event.Event])
Source code in Tairitsu/Scene.py
64
65
def scene_event(self, event: list[pygame.event.Event]):
    pass
trigger_battle
trigger_battle(player: Player)

Trigger a battle with a player.

PARAMETER DESCRIPTION
player

The Player object to trigger the battle with.

TYPE: Player

Source code in Tairitsu/Scene.py
84
85
86
87
88
89
90
91
def trigger_battle(self, player: Player):
    """
    Trigger a battle with a player.

    Args:
        player: The Player object to trigger the battle with.
    """
    pass
trigger_dialog
trigger_dialog(npc: NPC)

Trigger a dialog with an NPC.

PARAMETER DESCRIPTION
npc

The NPC object to trigger the dialog with.

TYPE: NPC

Source code in Tairitsu/Scene.py
67
68
69
70
71
72
73
74
75
76
def trigger_dialog(self, npc: NPC):
    """
    Trigger a dialog with an NPC.

    Args:
        npc: The NPC object to trigger the dialog with.
    """
    dialogBoxTemp = DialogBox(self.window, npc,
        ["Happy","2023!"], self.talktype)
    dialogBoxTemp.draw()
trigger_shop
trigger_shop(npc: NPC, player: Player)

Trigger a shop with an NPC.

PARAMETER DESCRIPTION
npc

The NPC object to trigger the shop with.

TYPE: NPC

player

The Player object to interact with the shop.

TYPE: Player

Source code in Tairitsu/Scene.py
 99
100
101
102
103
104
105
106
107
def trigger_shop(self, npc: NPC, player: Player):
    """
    Trigger a shop with an NPC.

    Args:
        npc: The NPC object to trigger the shop with.
        player: The Player object to interact with the shop.
    """
    pass
update_camera
update_camera(player: Player)

Update the camera position based on the player's position.

PARAMETER DESCRIPTION
player

The Player object.

TYPE: Player

Source code in Tairitsu/Scene.py
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
def update_camera(self, player: Player):
    """
    Update the camera position based on the player's position.

    Args:
        player: The Player object.
    """
    if self.state in [GameState.GAME_PLAY_WILD, GameState.GAME_PLAY_CITY]:
        if player.rect.x > WindowSettings.width / 4 * 3:
            self.cameraX += player.speed
            if self.cameraX < self.get_width() - WindowSettings.width:
                player.fix_to_middle(player.speed, 0)
            else:
                self.cameraX = self.get_width() - WindowSettings.width
        elif player.rect.x < WindowSettings.width / 4:
            self.cameraX -= player.speed
            if self.cameraX > 0:
                player.fix_to_middle(-player.speed, 0)
            else:
                self.cameraX = 0
        if player.rect.y > WindowSettings.height / 4 * 3:
            self.cameraY += player.speed
            if self.cameraY < self.get_height() - WindowSettings.height:
                player.fix_to_middle(0, player.speed)
            else:
                self.cameraY = self.get_height() - WindowSettings.height
        elif player.rect.y < WindowSettings.height / 4:
            self.cameraY -= player.speed
            if self.cameraY > 0:
                player.fix_to_middle(0, -player.speed)
            else:
                self.cameraY = 0

WildScene

WildScene(window: pygame.surface.Surface)

Bases: Scene

WildScene (inherit from Scene)

Source code in Tairitsu/Scene.py
368
369
370
371
372
def __init__(self, window: pygame.surface.Surface):
    super().__init__(window=window)
    self.state = GameState.GAME_PLAY_WILD
    self.BgmPlayer.play(GamePath.bgm["WILD"])
    self.gen_WILD()
Attributes
BgmPlayer instance-attribute
BgmPlayer = BgmPlayer()
battleBox instance-attribute
battleBox = None
cameraX instance-attribute
cameraX = 0
cameraY instance-attribute
cameraY = 0
height instance-attribute
height = WindowSettings.height
infoBox instance-attribute
infoBox = None
map instance-attribute
map = None
monsters instance-attribute
monsters = pygame.sprite.Group()
npcs instance-attribute
npcs = pygame.sprite.Group()
obstacles instance-attribute
obstacles = pygame.sprite.Group()
portals instance-attribute
portals = pygame.sprite.Group()
shoppingBox instance-attribute
shoppingBox = None
state instance-attribute
state = GameState.GAME_PLAY_WILD
tileXnum instance-attribute
tileXnum = SceneSettings.tileXnum
tileYnum instance-attribute
tileYnum = SceneSettings.tileYnum
width instance-attribute
width = WindowSettings.width
window instance-attribute
window = window
Functions
end_battle
end_battle()

End the current battle.

Source code in Tairitsu/Scene.py
93
94
95
96
97
def end_battle(self):
    """
    End the current battle.
    """
    pass
end_dialog
end_dialog()

End the current dialog.

Source code in Tairitsu/Scene.py
78
79
80
81
82
def end_dialog(self):
    """
    End the current dialog.
    """
    pass
end_shop
end_shop()

End the current shop.

Source code in Tairitsu/Scene.py
109
110
111
112
113
def end_shop(self):
    """
    End the current shop.
    """
    pass
gen_WILD
gen_WILD()

Generate WILD map, obstacles, npcs, portals

Source code in Tairitsu/Scene.py
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
def gen_WILD(self):
    """
    Generate WILD map, obstacles, npcs, portals
    """
    self.map = self.gen_wild_map()
    self.obstacles = self.gen_wild_obstacle()
    self.npcs.add(ShopNPC(self.width // 5, self.height // 5,
                          name = "不!坑爹的商人", 
                          dialog = lambda npc, name : ShoppingBox(self.window, npc, name, Archive.player, items = {"Attack +3": "Coin -15", "Defence +1": "Coin -155",
                            "HP +0": "Coin -15", "送你一程": "HP -100", "Exit": ""}, 
                            actions =  [{"addAttack": 3, "addCoins": -15},
                                        {"addDefence": 1, "addCoins": -155},
                                        {"addHP": 0, "addCoins": -15},
                                        {"addHP": -100, "addCoins": 0},
                                        {"addCoins": 0}])))
    self.gen_monsters()
    self.portals.add(Portal(self.width // 2, self.height // 5, BossScene))
gen_monsters
gen_monsters(num=10)
Source code in Tairitsu/Scene.py
406
407
408
409
410
411
412
413
def gen_monsters(self, num = 10):
    self.monsters = pygame.sprite.Group()
    self.monsters.add(Monster(WindowSettings.width // 4, WindowSettings.height // 4 + 180, HP = randint(10,20), Attack = randint(5,10), Defence = randint(2,8), path = GamePath.monster2))
    self.monsters.add(Monster(WindowSettings.width // 5, WindowSettings.height // 4 + 180, HP = randint(10,20), Attack = randint(5,10), Defence = randint(2,8), path = GamePath.monster2))
    self.monsters.add(Monster(WindowSettings.width // 3, WindowSettings.height // 4 + 180, HP = randint(10,20), Attack = randint(5,10), Defence = randint(2,8), path = GamePath.monster2))
    self.monsters.add(Monster(WindowSettings.width, WindowSettings.height // 4 + 180, HP = randint(10,20), Attack = randint(5,10), Defence = randint(2,8)))

    self.battleBox = None
gen_random_map
gen_random_map(TilesPath: GamePath.groundTiles) -> list

Generate a random map.

PARAMETER DESCRIPTION
TilesPath

A list of paths of tiles.

TYPE: GamePath.groundTiles

RETURNS DESCRIPTION
list

A 2D list representing the map.

Source code in Tairitsu/Scene.py
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
def gen_random_map(self, TilesPath: GamePath.groundTiles) -> list:
    """
    Generate a random map.

    Args:
        TilesPath: A list of paths of tiles.

    Returns:
        A 2D list representing the map.
    """
    images = [pygame.image.load(tile) for tile in TilesPath]
    images = [pygame.transform.scale(image, (SceneSettings.tileWidth, SceneSettings.tileHeight)) for image in images]

    mapObj = []
    for i in range(self.tileXnum):
        tmp = []
        for j in range(self.tileYnum):
            tmp.append(images[randint(0, len(images) - 1)])
        mapObj.append(tmp)

    return mapObj
gen_random_obstacles
gen_random_obstacles(imagePath=GamePath.tree)

Generate random obstacles.

PARAMETER DESCRIPTION
imagePath

The path of the obstacle image.

DEFAULT: GamePath.tree

RETURNS DESCRIPTION

A group of obstacle sprites.

Source code in Tairitsu/Scene.py
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
def gen_random_obstacles(self, imagePath = GamePath.tree):
    """
    Generate random obstacles.

    Args:
        imagePath: The path of the obstacle image.

    Returns:
        A group of obstacle sprites.
    """
    image = pygame.image.load(imagePath) 

    obstacles = pygame.sprite.Group()
    midx = SceneSettings.tileXnum//2
    midy = SceneSettings.tileYnum//2
    for i in range(SceneSettings.tileXnum):
        for j in range(SceneSettings.tileYnum):
            if random() < SceneSettings.obstacleDensity and not(i < midx and j < midy) and (i not in range(midx-3, midx+3)) and (j not in range(midy-3, midy+3)):
                obstacles.add(Block(image, SceneSettings.tileWidth * i, SceneSettings.tileHeight * j))
    return obstacles  
gen_wild_map
gen_wild_map()

Generate wild map (by gen_random_map)

Source code in Tairitsu/Scene.py
400
401
402
403
404
def gen_wild_map(self):
    """
    Generate wild map (by gen_random_map)
    """
    return self.gen_random_map(GamePath.groundTiles)
gen_wild_obstacle
gen_wild_obstacle()

Generate wild obstacles (by gen_random_obstacles)

Source code in Tairitsu/Scene.py
393
394
395
396
397
def gen_wild_obstacle(self):
    """
    Generate wild obstacles (by gen_random_obstacles)
    """
    return self.gen_random_obstacles(GamePath.tree)
get_height
get_height()

Get the height of the scene.

RETURNS DESCRIPTION

The height of the scene.

Source code in Tairitsu/Scene.py
124
125
126
127
128
129
130
131
def get_height(self):
    """
    Get the height of the scene.

    Returns:
        The height of the scene.
    """
    return WindowSettings.height * WindowSettings.outdoorScale
get_width
get_width()

Get the width of the scene.

RETURNS DESCRIPTION

The width of the scene.

Source code in Tairitsu/Scene.py
115
116
117
118
119
120
121
122
def get_width(self):
    """
    Get the width of the scene.

    Returns:
        The width of the scene.
    """
    return self.width * WindowSettings.outdoorScale
render
render(player: Player)

Render the scene.

PARAMETER DESCRIPTION
player

The Player object.

TYPE: Player

Source code in Tairitsu/Scene.py
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
def render(self, player: Player):
    """
    Render the scene.

    Args:
        player: The Player object.
    """
    for each in self.npcs.sprites():
        each.update()
    for each in self.monsters.sprites():
        each.update()

    if(type(self.map) == list):
        # Only render visible tiles
        for i in range(self.tileXnum):
            if(SceneSettings.tileWidth * i - self.cameraX < -SceneSettings.tileWidth or SceneSettings.tileWidth * i - self.cameraX > WindowSettings.width): 
                continue
            for j in range(self.tileYnum):
                if(SceneSettings.tileHeight * j - self.cameraY < -SceneSettings.tileHeight or SceneSettings.tileHeight * j - self.cameraY > WindowSettings.height): 
                    continue
                self.window.blit(self.map[i][j], 
                                (SceneSettings.tileWidth * i - self.cameraX, 
                                SceneSettings.tileHeight * j - self.cameraY))
    else:
        self.window.blit(self.map, (0, 0))

    # newSurface2 = pygame.Surface((WindowSettings.width, WindowSettings.height), pygame.SRCALPHA)
    player.draw(self.window)

    for each in self.obstacles.sprites():
        each.rect.topleft = (each.actualRect.x - self.cameraX, each.actualRect.y - self.cameraY)
        if(each.actualRect.x - self.cameraX < -each.actualRect.width or each.actualRect.x - self.cameraX > WindowSettings.width):
            pass # TODO: other 
        else:
            each.draw(self.window)
    for each in self.npcs.sprites():
        each.rect.topleft = (each.actualRect.x - self.cameraX, each.actualRect.y - self.cameraY)
        if(each.actualRect.x - self.cameraX < -each.actualRect.width or each.actualRect.x - self.cameraX > WindowSettings.width):
            pass # TODO: other 
        else:
            each.draw(self.window)
    for each in self.monsters.sprites():
        each.rect.topleft = (each.actualRect.x - self.cameraX, each.actualRect.y - self.cameraY)
        # if(each.actualRect.x - self.cameraX < -each.actualRect.width or each.actualRect.x - self.cameraX > WindowSettings.width):
        #     pass # TODO: other 
        # else:
        each.draw(self.window)
    # self.monsters.draw(self.window)
    for each in self.portals.sprites():
        each.rect.topleft = (each.actualRect.x - self.cameraX, each.actualRect.y - self.cameraY)
        if(each.actualRect.x - self.cameraX < -each.actualRect.width or each.actualRect.x - self.cameraX > WindowSettings.width):
            pass # TODO: other 
        else:
            each.draw(self.window)
scene_event
scene_event(event: list[pygame.event.Event])
Source code in Tairitsu/Scene.py
64
65
def scene_event(self, event: list[pygame.event.Event]):
    pass
trigger_battle
trigger_battle(player: Player)

Trigger a battle with a player.

PARAMETER DESCRIPTION
player

The Player object to trigger the battle with.

TYPE: Player

Source code in Tairitsu/Scene.py
84
85
86
87
88
89
90
91
def trigger_battle(self, player: Player):
    """
    Trigger a battle with a player.

    Args:
        player: The Player object to trigger the battle with.
    """
    pass
trigger_dialog
trigger_dialog(npc: NPC)

Trigger a dialog with an NPC.

PARAMETER DESCRIPTION
npc

The NPC object to trigger the dialog with.

TYPE: NPC

Source code in Tairitsu/Scene.py
67
68
69
70
71
72
73
74
75
76
def trigger_dialog(self, npc: NPC):
    """
    Trigger a dialog with an NPC.

    Args:
        npc: The NPC object to trigger the dialog with.
    """
    dialogBoxTemp = DialogBox(self.window, npc,
        ["Happy","2023!"], self.talktype)
    dialogBoxTemp.draw()
trigger_shop
trigger_shop(npc: NPC, player: Player)

Trigger a shop with an NPC.

PARAMETER DESCRIPTION
npc

The NPC object to trigger the shop with.

TYPE: NPC

player

The Player object to interact with the shop.

TYPE: Player

Source code in Tairitsu/Scene.py
 99
100
101
102
103
104
105
106
107
def trigger_shop(self, npc: NPC, player: Player):
    """
    Trigger a shop with an NPC.

    Args:
        npc: The NPC object to trigger the shop with.
        player: The Player object to interact with the shop.
    """
    pass
update_camera
update_camera(player: Player)

Update the camera position based on the player's position.

PARAMETER DESCRIPTION
player

The Player object.

TYPE: Player

Source code in Tairitsu/Scene.py
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
def update_camera(self, player: Player):
    """
    Update the camera position based on the player's position.

    Args:
        player: The Player object.
    """
    if self.state in [GameState.GAME_PLAY_WILD, GameState.GAME_PLAY_CITY]:
        if player.rect.x > WindowSettings.width / 4 * 3:
            self.cameraX += player.speed
            if self.cameraX < self.get_width() - WindowSettings.width:
                player.fix_to_middle(player.speed, 0)
            else:
                self.cameraX = self.get_width() - WindowSettings.width
        elif player.rect.x < WindowSettings.width / 4:
            self.cameraX -= player.speed
            if self.cameraX > 0:
                player.fix_to_middle(-player.speed, 0)
            else:
                self.cameraX = 0
        if player.rect.y > WindowSettings.height / 4 * 3:
            self.cameraY += player.speed
            if self.cameraY < self.get_height() - WindowSettings.height:
                player.fix_to_middle(0, player.speed)
            else:
                self.cameraY = self.get_height() - WindowSettings.height
        elif player.rect.y < WindowSettings.height / 4:
            self.cameraY -= player.speed
            if self.cameraY > 0:
                player.fix_to_middle(0, -player.speed)
            else:
                self.cameraY = 0