-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
143 lines (127 loc) · 4.7 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
import pygame
import math
from player import Player
from labyrinth import Labyrinth
from objects import Objects
from constantes import (
MAC_GYVER_IMAGE,
FLOOR_IMAGE,
SPRITE_SIZE,
SYRINGE_IMAGE,
ETHER_IMAGE,
NEEDLE_IMAGE,
MENU_IMAGE,
WIN_IMAGE,
LOOSE_IMAGE,
PLAY_BUTTON,
)
class Game:
def __init__(self, window):
self.is_playing = False
self.window = window
# self.pressed = {}
def initialization(self, window):
"""
Initialise new objects when call
"""
self.labyrinth = Labyrinth()
self.mac_gyver = Player(
MAC_GYVER_IMAGE, FLOOR_IMAGE, self.labyrinth, self.window
)
self.mac_gyver.position = [0, 0]
self.objects = Objects(
SYRINGE_IMAGE, ETHER_IMAGE, NEEDLE_IMAGE, self.labyrinth, self.window
)
# Labyrinth
self.labyrinth.labyrinth_construction()
self.labyrinth.display_level(window)
# Player
self.mac_gyver.blit(self.mac_gyver.position)
# Objects
self.objects.generate_random_position()
self.objects.display_objects()
self.objects.objects_collected = 0
self.run = True
self.response = None
def menu(self, menu_image, window):
"""
Display menu
"""
self.menu_i = pygame.image.load(menu_image).convert_alpha()
self.window.blit(self.menu_i, (0, 0))
play_button = pygame.image.load("assets/playbutton.png")
play_button = pygame.transform.scale(play_button, (200, 70))
play_button_rect = play_button.get_rect()
play_button_rect.x = math.ceil(window.get_width() / 3.333)
play_button_rect.y = math.ceil(window.get_height() / 1.4)
window.blit(play_button, play_button_rect)
continu = True
while continu:
pygame.display.update()
for event in pygame.event.get():
# Waiting for events
if event.type == pygame.QUIT:
continu = False
return False
if event.type == pygame.MOUSEBUTTONDOWN:
if play_button_rect.collidepoint(event.pos):
continu = False
return True
def play(self):
"""Manage all input player's input"""
continu = True
while continu:
pygame.display.update()
for event in pygame.event.get():
# Waiting for events
if event.type == pygame.QUIT:
continu = False
self.run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
self.mac_gyver.move("right")
elif event.key == pygame.K_LEFT:
self.mac_gyver.move("left")
elif event.key == pygame.K_UP:
self.mac_gyver.move("up")
elif event.key == pygame.K_DOWN:
self.mac_gyver.move("down")
self.objects.collect_objects(self.mac_gyver)
continu = self.labyrinth.end_game(self.mac_gyver)
self.response = self.labyrinth.response(
self.mac_gyver, self.objects
)
pygame.display.flip()
return self.response
def player_win(self, win_image):
"""Display Win Screen at the end of the game"""
self.win_image = pygame.image.load(win_image).convert_alpha()
self.window.blit(self.win_image, ((0, 0)))
continu = True
while continu:
pygame.display.update()
for event in pygame.event.get():
# Waiting for events
if event.type == pygame.QUIT:
continu = False
self.run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
continu = False
return True
def player_loose(self, loose_image):
"""Display Loose Screen at the end of the game"""
self.loose_image = pygame.image.load(loose_image).convert_alpha()
self.window.blit(self.loose_image, ((0, 0)))
continu = True
while continu:
pygame.display.update()
for event in pygame.event.get():
# Waiting for events
if event.type == pygame.QUIT:
continu = False
self.run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
continu = False
return True