forked from chiweiihsu/group17_project
-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.py
100 lines (87 loc) · 3.73 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
import pygame
import sys
from pygame.locals import *
import constants as SOKOBAN
from level import *
from player import *
from scores import *
from player_interface import *
class Game:
def __init__(self, window):
self.window = window
self.load_textures()
self.player = None
self.index_level = 1
self.load_level()
self.play = True
self.scores = Scores(self)
self.player_interface = PlayerInterface(self.player, self.level)
self.musicPlaying = True
def load_textures(self):
self.textures = {
SOKOBAN.WALL: pygame.image.load('assets/images/wall.png').convert_alpha(),
SOKOBAN.BOX: pygame.image.load('assets/images/box.png').convert_alpha(),
SOKOBAN.TARGET: pygame.image.load('assets/images/target.png').convert_alpha(),
SOKOBAN.TARGET_FILLED: pygame.image.load('assets/images/valid_box.png').convert_alpha(),
SOKOBAN.PLAYER: pygame.image.load('assets/images/player_sprites.png').convert_alpha()
}
def load_level(self):
self.level = Level(self.index_level)
self.board = pygame.Surface((self.level.width, self.level.height))
if self.player:
self.player.pos = self.level.position_player
self.player_interface.level = self.level
else:
self.player = Player(self.level)
def start(self):
while self.play:
self.process_event(pygame.event.wait())
self.update_screen()
def musicOnOff(self): # 切換音樂開關
if self.musicPlaying:
pygame.mixer.music.pause()
else:
pygame.mixer.music.unpause()
self.musicPlaying = not self.musicPlaying
def process_event(self, event):
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
# Quit game
self.play = False
if event.key in [K_UP, K_DOWN, K_LEFT, K_RIGHT, K_w, K_s, K_a, K_d]:
# Move player
self.player.move(event.key, self.level, self.player_interface)
if self.has_win():
self.index_level += 1
self.scores.save()
self.load_level()
if event.key == K_r:
# Restart current level
self.load_level()
if event.key == K_l:
# Cancel last move
self.level.cancel_last_move(self.player, self.player_interface)
if event.type == MOUSEBUTTONUP:
self.player_interface.click(event.pos, self.level, self)
if event.type == MOUSEMOTION:
self.player_interface.mouse_pos = event.pos
def update_screen(self):
pygame.draw.rect(self.board, SOKOBAN.WHITE, (0,0, self.level.width * SOKOBAN.SPRITESIZE, self.level.height * SOKOBAN.SPRITESIZE))
pygame.draw.rect(self.window, SOKOBAN.WHITE, (0,0,SOKOBAN.WINDOW_WIDTH,SOKOBAN.WINDOW_HEIGHT))
self.level.render(self.board, self.textures)
self.player.render(self.board, self.textures)
pox_x_board = (SOKOBAN.WINDOW_WIDTH / 2) - (self.board.get_width() / 2)
pos_y_board = (SOKOBAN.WINDOW_HEIGHT / 2) - (self.board.get_height() / 2)
self.window.blit(self.board, (pox_x_board, pos_y_board))
self.player_interface.render(self.window, self.index_level)
pygame.display.flip()
def has_win(self):
nb_missing_target = 0
for y in range(len(self.level.structure)):
for x in range(len(self.level.structure[y])):
if self.level.structure[y][x] == SOKOBAN.TARGET:
nb_missing_target += 1
return nb_missing_target == 0