1
1
#!/usr/bin/env python
2
2
# coding=utf-8
3
+ import time
4
+
3
5
import pygame as pg
4
6
from pygame .locals import (
5
7
K_ESCAPE ,
10
12
QUIT ,
11
13
)
12
14
13
- from pysurvive .config import FPS , GRAY_LIGHT2 , MAP_DIR , RED_LIGHT , SCREEN_RECT
14
- from pysurvive .game .core import Camera , Screen
15
+ from pysurvive .config import FPS , GRAY_LIGHT2 , MAP_DIR , SCREEN_RECT
16
+ from pysurvive .game .core import Camera
15
17
from pysurvive .logger import Logger
16
18
from pysurvive .map .level import Level
17
- from pysurvive .player import PlayerGroup
19
+ from pysurvive .player .player import PlayerGroup
20
+ from pysurvive .player .viewpoint import Viewpoint
18
21
19
22
logger = Logger ()
20
23
21
24
22
25
class Game :
23
-
24
26
running = True
25
27
26
28
def __init__ (self ) -> None :
@@ -30,11 +32,10 @@ def __init__(self) -> None:
30
32
self .clock = pg .time .Clock ()
31
33
self .fps_font = pg .font .SysFont ("Arial" , 14 )
32
34
33
- # Sprite that represent the screen.
34
- # Used to determine whether elements are in the viewing area.
35
- self .screen = Screen (SCREEN_RECT )
36
- # Set the height and width of the screen.
37
- self .window_surface = pg .display .set_mode (self .screen .size )
35
+ # Set the height and width of the camera/screen.
36
+ self .window_surface = pg .display .set_mode (
37
+ (SCREEN_RECT .width , SCREEN_RECT .height )
38
+ )
38
39
# Set the window title.
39
40
pg .display .set_caption ("pysurvive" )
40
41
pg .transform .set_smoothscale_backend ("SSE" )
@@ -45,125 +46,59 @@ def __init__(self) -> None:
45
46
[QUIT , KEYDOWN , K_ESCAPE , MOUSEBUTTONDOWN , MOUSEBUTTONUP , MOUSEMOTION ]
46
47
)
47
48
48
- # Absolute (start) position of the player (camera) in game world.
49
- self .camera = Camera (150 , 600 )
50
-
51
- # Prepare the shadow surface / screen.
52
- # self.screen_shadow = pg.Surface(self.screen.size)
53
- # self.screen_shadow = self.screen_shadow.convert()
54
- # self.screen_shadow.set_alpha(240)
55
- # self.screen_shadow.set_colorkey(COLORKEY)
56
-
57
- #
58
- # Prepare game objects
59
- #
60
-
61
- # Initialize the navmesh based on the map.
62
- # self.navmesh = NavMesh(self)
63
-
64
- # self.enemy_sprites = pg.sprite.RenderPlain(
65
- # Enemy(self, 100, 100),
66
- # )
67
- # A sprite group that contains all close enemy sprites (render only).
68
- # self.enemy_render_sprites = pg.sprite.RenderPlain()
69
- # A sprite group that contains all close sprites (collision only).
70
- # self.collide_sprites = pg.sprite.RenderPlain()
71
-
72
- # Get all unique points (corners) of block segments.
73
- # Prevent duplication of x, y coordinates.
74
- # self.unique_block_points = []
75
- # for block in self.block_sprites.sprites():
76
- # for block_point in block.get_points():
77
- # point = (block_point[0], block_point[1])
78
- # if point not in self.unique_block_points:
79
- # self.unique_block_points.append(point)
80
-
81
- # Map
49
+ self .camera = Camera ()
50
+ self .interface = pg .sprite .Group ()
51
+
82
52
self .level = Level (f"{ MAP_DIR } /map.json" )
83
- # Player
84
- self .player_sprites = PlayerGroup (self .level )
53
+ self .viewpoint = Viewpoint (self .interface )
54
+ self .player_sprites = PlayerGroup (
55
+ camera = self .camera ,
56
+ viewpoint = self .viewpoint ,
57
+ )
85
58
86
59
def start (self ) -> None :
87
60
"""
88
61
This function is called when the program starts. It initializes
89
62
everything it needs, then runs in a loop until the function returns.
90
63
"""
91
64
92
- while self . running :
65
+ prev_time = time . time ()
93
66
67
+ while self .running :
94
68
# The number of milliseconds that passed between the
95
69
# previous two calls to Clock.tick().
96
- dt = self .clock .get_time ()
70
+ dt = time .time () - prev_time
71
+ prev_time = time .time ()
97
72
98
- # @todo: Get keyboard inputs by player object.
99
73
for event in pg .event .get ():
100
74
if event .type == QUIT :
101
75
self .running = False
102
76
elif event .type == KEYDOWN and event .key == K_ESCAPE :
103
77
self .running = False
104
- # elif event.type == MOUSEBUTTONDOWN:
105
- # if event.button == 1:
106
- # self.player_sprites.player.shot()
107
- # elif event.button == 3:
108
- # self.player_sprites.player.reload()
109
78
elif event .type == MOUSEBUTTONUP :
110
79
pass
111
80
112
- keystate = pg .key .get_pressed ()
113
- direction_x = keystate [pg .K_d ] - keystate [pg .K_a ]
114
- direction_y = keystate [pg .K_s ] - keystate [pg .K_w ]
115
-
116
- # Fill the window surface with the default background color.
81
+ # Default background color.
117
82
self .window_surface .fill (GRAY_LIGHT2 )
118
83
119
84
#
120
85
# Updating
121
86
#
122
87
123
- self .level .update ()
124
- self .player_sprites .update (dt , (direction_x , direction_y ))
88
+ self .level .update (self .camera )
89
+ self .player_sprites .update (dt , self .level )
90
+ self .interface .update ()
125
91
126
92
#
127
93
# Drawing
128
94
#
129
95
130
- self .level .draw (self .window_surface )
131
-
132
- # if FLASHLIGHT_ENABLE:
133
- # # Currently, all vertices within a virtual screen of
134
- # # 3x width and 3x height of the screen are used. Later
135
- # # when the visibility is limited, this can be further reduced.
136
- # self.screen_shadow.fill(BLACK)
137
- # self.player_sprites.light.draw(self.screen_shadow)
138
- # self.window_surface.blit(self.screen_shadow, (0, 0))
139
-
140
- self .player_sprites .draw (self .window_surface )
141
-
142
- # Debugging
143
- # Draw navmesh
144
- # for tri in self.navmesh.mesh:
145
- # triangle = [(p[0] - self.game_x, p[1] - self.game_y)
146
- # for p in tri.triangle]
147
- # pg.draw.polygon(self.window_surface, (255, 0, 0), triangle, 1)
148
- # for node in tri.nodes:
149
- # pg.draw.circle(self.window_surface, (0, 255, 0),
150
- # (node.position[0] - self.game_x,
151
- # node.position[1] - self.game_y), 2)
152
-
153
- # path = self.enemy_sprites.sprites()[0].path
154
- # if path:
155
- # path = [(p[0] - self.game_x, p[1] - self.game_y) for p in path]
156
- # pg.draw.lines(self.window_surface, (0, 0, 255), False, path)
157
-
158
- self .window_surface .blit (self .update_fps (), (5 , 5 ))
96
+ self .level .draw (self .window_surface , self .camera )
97
+ self .player_sprites .draw (self .window_surface , self .camera )
98
+ self .interface .draw (self .window_surface )
159
99
160
100
# Go ahead and update the window surface with what we've drawn.
161
101
# This MUST happen after all the other drawing commands.
162
102
pg .display .flip ()
163
103
# This limits the while loop to a max of FPS times per second.
164
104
self .clock .tick (FPS )
165
-
166
- def update_fps (self ):
167
- fps = str (int (self .clock .get_fps ()))
168
- fps_text = self .fps_font .render (fps , 1 , RED_LIGHT )
169
- return fps_text
0 commit comments