-
Notifications
You must be signed in to change notification settings - Fork 0
/
Raspigame.py
155 lines (109 loc) · 4.95 KB
/
Raspigame.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
import pygame
import sys
"""----------------------------------------------------------------------------------------------------
GameState
The game state class defines an interface that is used by the RaspberryPiGame class. Each state
manages a particular function of the game. For example; main menu, the actual game play, and
interstitial screens.
----------------------------------------------------------------------------------------------------
"""
class GameState(object):
"""
Initialise the Game state class. Each sub-type must call this method. Takes one parameter, which
is the game instance.
"""
def __init__(self, game):
self.game = game
"""
Called by the game when entering the state for the first time.
"""
def onEnter(self, previousState): # Placeholder for actions to perform when entering this state
pass
"""
Called by the game when leaving the state.
"""
def onExit(self): # Placeholder for actions to perform when exiting this state
pass
"""
Called by the game allowing the state to update itself. The game time (in milliseconds) since
the last call is passed.
"""
def update(self, gameTime): # Placeholder for updating the state based on game time
pass
def update_onPress(self, gameTime, value): # Placeholder for handling mouse button press events
pass
def update_onRelease(self, gameTime): # Placeholder for handling mouse button release events
pass
def getIndex(self, gameTime): # Placeholder for retrieving the index of the current state
pass
def button(self): # Placeholder for button-related functionality
pass
"""
Called by the game allowing the state to draw itself. The surface that is passed is the
current drawing surface.
"""
def draw(self, surface):
pass
"""
----------------------------------------------------------------------------------------------------
Raspberry Pi Game
Basic game object-oriented framework for the Raspberry Pi. Users create 'states' that alter what is
being displayed on-screen / updated at any particular time.
----------------------------------------------------------------------------------------------------
"""
class RaspberryPiGame(object):
"""
Initialise the Raspberry Pi Game class.
"""
def __init__(self, gameName, width, height):
pygame.init()
pygame.display.set_caption(gameName)
self.fpsClock = pygame.time.Clock() # Create a clock to manage the frame rate
self.main_window = pygame.display.set_mode((width, height)) # Set the dimensions of the game window
self.background = pygame.Color(255, 255, 255) # Set the initial background color to white
self.currentState = None
self.game_over = False # Flag to indicate if the game is over
self.done = False # Flag to indicate if the game loop should continue
"""
Change the current state. If the newState is 'None' then the game will terminate.
"""
def changeState(self, newState):
if self.currentState is not None:
self.currentState.onExit()
if newState is None:
pygame.quit()
sys.exit()
old_state = self.currentState
self.currentState = newState
newState.onEnter(old_state)
"""
Run the game. Initial state must be supplied.
"""
def run(self, initialState):
self.changeState(initialState)
value = 0
gameTime = 0
while not self.done:
if self.currentState.getIndex(gameTime) is not None:
value = self.currentState.getIndex(gameTime)
for event in pygame.event.get():
if event.type == pygame.QUIT or (
event.type == pygame.KEYDOWN and event.key == pygame.K_q): # # If user presses 'q' or closes the window, quit the game:
self.done = True
pygame.quit() # Quit pygame
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
self.currentState.update_onPress(gameTime, value)
if event.type == pygame.MOUSEBUTTONUP:
self.currentState.update_onRelease(gameTime)
gameTime = self.fpsClock.get_time() # Update game time
if self.currentState is not None: # Update the current state
self.currentState.update(gameTime)
self.main_window.fill(self.background)
bimage = pygame.image.load('background.png') # Load the background image
self.main_window.blit(bimage, (0, 0)) # Draw the background image on the window
if self.currentState is not None:
self.currentState.draw(self.main_window) # Draw the current state on the window
pygame.display.flip() # Update the display
self.fpsClock.tick(20) # Control the frame rate to 20 FPS
pygame.quit() # Quit pygame when the loop ends