-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.py
208 lines (163 loc) · 7.47 KB
/
Game.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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
165
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
import pygame
from pygame.locals import *
from Camera import Camera
from DiamondSquare import DiamondSquare
from EntityClasses import Player, Button, EnvironmentTile, Mouse
from ImageCache import ImageCache
class Game:
def __init__(self, window_size, block_size):
self.window_size = window_size
self.block_size = block_size
# Initialise the pygame variables
pygame.init()
self.screen = pygame.display.set_mode(window_size)
self.clock = pygame.time.Clock()
self.fps = 25
# Initialize the image cache
self.image_cache = ImageCache("data/images")
# Create entity groups for PyGame
self.environment_entities = pygame.sprite.Group()
self.player_entities = pygame.sprite.Group()
# Create the player
self.player = Player(32, 32, self.image_cache)
self.player_entities.add(self.player)
# Create the level
self.level = DiamondSquare(6, 0.05).get_grid_2D() # Random map created using Diamond Square
def run(self):
self.start_menu()
self.main_game()
def start_menu(self):
start_game = False
half_screen_width = self.window_size[0] / 2
half_screen_height = self.window_size[1] / 2
# Create the buttons for the start menu
new_game_button = Button("new_game",
half_screen_width, half_screen_height - 50,
self.image_cache.load_image("New_Game.png"),
self.image_cache.load_image("New_Game_Hover.png"))
quit_game_button = Button("exit_game",
half_screen_width, half_screen_height + 10,
self.image_cache.load_image("Quit_Game.png"),
self.image_cache.load_image("Quit_Game_Hover.png"))
button_entities = pygame.sprite.Group()
button_entities.add((new_game_button, quit_game_button))
# Load the start menu background
background = self.image_cache.load_image("Menu_Background.png")
self.screen.blit(background, (0, 0))
# Load the credit image
credit_image = self.image_cache.load_image("Credits.png")
self.screen.blit(credit_image, (20, self.window_size[1] - 50))
while not start_game:
self.clock.tick(self.fps)
current_fps = float(self.clock.get_fps())
# Update the title and display the current FPS
pygame.display.set_caption("The Forming | FPS: " + str(current_fps))
mouse_clicked = False
# Handle the events for the game
for event in pygame.event.get():
# Quit the game
if event.type == QUIT:
pygame.quit()
if event.type == MOUSEBUTTONDOWN:
mouse_clicked = True
if event.type == KEYDOWN:
if event.key == K_F1:
# Take screenshot
pass
# Reloading all images
self.screen.blit(background, (0, 0))
self.screen.blit(credit_image, (20, self.window_size[1] - 50))
# Handle the buttons
mouse_pos = pygame.mouse.get_pos()
new_game_button.update(mouse_pos)
quit_game_button.update(mouse_pos)
if new_game_button.mouse_collision() and mouse_clicked:
start_game = True
if quit_game_button.mouse_collision() and mouse_clicked:
pygame.quit()
button_entities.draw(self.screen)
pygame.display.update()
def main_game(self):
# Set the default control flags
up = down = left = right = False
exit_game = False
walking = False
self.load_level() # Create the environment assets
level_size = (len(self.level[0]) * self.block_size[0], len(self.level) * self.block_size[1])
camera = Camera(level_size, self.window_size)
mouse = Mouse(pygame.mouse.get_pos())
mouse_clicked = False
mosue_right_clicked = False
while not exit_game:
# Reset control flags each loop
mouse_clicked = False
mouse_right_clicked = False
# Handle pygame
self.clock.tick(self.fps)
# Load title displaying true FPS
current_fps = float(self.clock.get_fps())
pygame.display.set_caption("The Forming | FPS: " + str(current_fps))
# Event Handling
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
# Keydown events
if event.type == KEYDOWN:
if event.key == K_UP or event.key == K_w:
up = True
elif event.key == K_DOWN or event.key == K_s:
down = True
elif event.key == K_LEFT or event.key == K_a:
left = True
elif event.key == K_RIGHT or event.key == K_d:
right = True
if event.type == KEYUP:
if event.key == K_UP or event.key == K_w:
up = False
elif event.key == K_DOWN or event.key == K_s:
down = False
elif event.key == K_LEFT or event.key == K_a:
left = False
elif event.key == K_RIGHT or event.key == K_d:
right = False
# Mouse events
if event.type == MOUSEBUTTONDOWN:
if event.button == 1:
mouse_clicked = True
elif event.button == 3:
mouse_right_clicked = True
if event.type == MOUSEBUTTONUP:
if event.button == 1:
mouse_clicked = False
elif event.button == 3:
mouse_right_clicked = False
# Update the entities
camera.update(self.player)
self.player.update(up, down, left, right, level_size)
for tile in self.environment_entities:
self.screen.blit(tile.image, camera.apply(tile))
for player in self.player_entities:
self.screen.blit(player.image, camera.apply(player))
mouse.update(pygame.mouse.get_pos())
pygame.display.update()
def load_level(self):
x = 0
y = 0
for y_tile in self.level:
x = 0
for x_tile in y_tile:
print x_tile
if 0 == x_tile <= 0.5:
tile = EnvironmentTile(x, y, self.block_size[0], self.block_size[1], "Water", "Water",
self.image_cache.load_image("Water.png"))
elif 0.5 < x_tile <= 0.55:
tile = EnvironmentTile(x, y, self.block_size[0], self.block_size[1], "Sand", "Sand",
self.image_cache.load_image("Sand.png"))
else:
tile = EnvironmentTile(x, y, self.block_size[0], self.block_size[1], "Grass", "Grass",
self.image_cache.load_image("Grass1.png"))
self.environment_entities.add(tile)
x += 32
y += 32
game = Game((640, 640), (32, 32))
game.run()