-
Notifications
You must be signed in to change notification settings - Fork 2
/
view.py
68 lines (60 loc) · 2.26 KB
/
view.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
63
64
65
66
67
68
"""
view.py contains the class that sets the screen and
draws the player, map, and text to the screen
"""
import pygame
from Model.constants import TILE_SIZE, SCREEN_WIDTH, SCREEN_HEIGHT
from Model.player import Player
from Model.room import Room
class View: # pylint: disable=too-few-public-methods
"""
Sets the screen and draws the map, player, and text onto the screen
Attributes:
_screen: a Surface representing the screen that will be drawn on.
"""
def __init__(self):
"""
Initializes an instance of View and sets the screen to a size
based on constants defined in constants.py
"""
self._screen = pygame.display.set_mode(
(SCREEN_WIDTH * TILE_SIZE, SCREEN_HEIGHT * TILE_SIZE)
)
def draw(self, text_box, player=Player, current_room=Room):
"""
Draws the map and player or text to the screen.
Args:
text_box: a Surface representing the text that will
be drawn, or None if there is no text
player: a Player instance that contains all of the player's
information
current_room: a Room instance that contains all of the
current room's information
"""
# fill with black
self._screen.fill((0, 0, 0))
# if no textbox
if text_box is None:
# draw the current room's lower tiles
current_room.tile_groups["Lower"].draw(self._screen)
# draw the player
player_image = player.sprite_list[player.current_sprite]
self._screen.blit(player_image, player.coordinates)
# draw the current room's NPCs
current_room.tile_groups["NPC"].draw(self._screen)
# draw the current room's upper tiles
current_room.tile_groups["Upper"].draw(self._screen)
# if there is a textbox
else:
# Find value to center the textbox
x_val = text_box.get_width()
# Draw the textbox
self._screen.blit(
text_box,
(
(SCREEN_WIDTH * TILE_SIZE - x_val) // 2,
SCREEN_HEIGHT * TILE_SIZE // 2,
),
)
# Update the display
pygame.display.flip()