-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.py
135 lines (98 loc) · 3.87 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
import pygame
from Clock import Clock
from Inventory import Inventory
from Config import Config
from Display import Display
from Objects import GameObject
from Contracts import InstanceOf
from Objects import Player
class GameState:
PAUSED = 0
PLAYING = 1
INVENTORY = 2
SCENE = 3
def __init__(self, mode = PAUSED, keys = []):
self.__mode = mode
self.__keys = keys
def GetKeys(self):
return self.__keys
def GetMode(self):
return self.__mode
def SetMode(self, mode):
self.__mode = mode
class Game:
Game = None
def __init__(self):
Game = self
pygame.init()
self.__previousState = GameState()
Config.Init()
Display.Init()
self.__screen = Display.GetDisplay()
Display.SetCaption(Config.Get("GameSettings")["Caption"])
self.Clock = Clock().SetTick(int(Config.Get("GameSettings")["Fps"]))
self.__gameObjects = []
def AddGameObjects(self, *objects):
for obj in objects:
InstanceOf(obj, GameObject.GameObject)
self.__gameObjects.append(obj)
def Start(self):
while True:
previousMode = self.__previousState.GetMode()
heldKeys = self.__previousState.GetKeys()
pressedKeys = pygame.key.get_pressed()
currentState = GameState(previousMode, pressedKeys)
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.Quit()
# Setting different modes
if (event.type == pygame.KEYDOWN):
if (previousMode == GameState.PAUSED):
if (event.key == pygame.K_ESCAPE and not heldKeys[pygame.K_ESCAPE]):
currentState.SetMode(GameState.PLAYING)
if (previousMode == GameState.INVENTORY):
if (event.key == pygame.K_e and not heldKeys[pygame.K_e]):
Inventory.Close()
currentState.SetMode(GameState.PLAYING)
if (previousMode == GameState.PLAYING):
if (event.key == pygame.K_ESCAPE and not heldKeys[pygame.K_ESCAPE]):
currentState.SetMode(GameState.PAUSED)
elif (event.key == pygame.K_e and not heldKeys[pygame.K_e]):
Inventory.Open()
currentState.SetMode(GameState.INVENTORY)
self.CheckCollisions()
self.Draw()
self.__previousState = currentState
self.Clock.Tick()
def Draw(self):
for obj in self.__gameObjects:
obj.Draw()
Display.Update()
def CheckCollisions(self):
objs = self.__gameObjects
for firstObject in range(len(objs)):
for secondObject in range(firstObject + 1, len(objs)):
collisionState = CollisionState()
if (objs[firstObject].x > objs[secondObject].x and objs[firstObject].x < objs[secondObject].x + objs[secondObject].width):
collisionState.Left = True
elif (objs[firstObject].x + objs[firstObject].width > objs[secondObject].x and objs[firstObject].x + objs[firstObject].width < objs[secondObject].x + objs[secondObject].width):
collisionState.Right = True
# We must not be colliding on the left or right
if (collisionState.Left or collisionState.Right):
continue
if (objs[firstObject].y > objs[secondObject].y and objs[secondObject].y < objs[secondObject].y + objs[secondObject].height):
collisionState.Top = True
elif (objs[firstObject].y + objs[firstObject].height > objs[secondObject].y and objs[firstObject].y + objs[firstObject].height < objs[secondObject].y + objs[secondObject].height):
collisionState.Bottom = True
# We must not be colliding on the top or bottom
if (collisionState.Top or collisionState.Bottom):
continue
objs[firstObject].Collides(objs[secondObject], collisionState)
def GetGameState(self):
return self.__previousState
def Quit(self):
pygame.quit()
quit()
game = Game()
game.AddGameObjects(Player.PlayerS((0, 0), (0, 0), (16, 16), Player.PlayerStats(100, 0)))
game.Start()