-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshowdown.py
159 lines (138 loc) · 5.9 KB
/
showdown.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
import pygame
import pygame.gfxdraw
import sys
from racecar import Racecar
from scoreboard import Scoreboard
class Showdown:
# Main class to manage game assets and behaviorm
def __init__(self):
# Initialization of base elements for game
pygame.init()
self.image = pygame.image.load('track.png')
pygame.mixer.music.load('BackgroundMusic.mp3')
pygame.mixer.music.play(-1, 0.0)
WINDOWWIDTH = self.image.get_rect().width
WINDOWHEIGHT = self.image.get_rect().height
self.screen = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
# Setting up elements related to the timer
self.clock = pygame.time.Clock()
self.timer = 30
self.dt = 0
self.font = pygame.font.Font(None, 40)
# Variable to tell if the game is currently active
self.game_active = False
# Variable for if the player has a continue they can use
self.continue_allowed = True
# Audio objects for the boost sound
self.boostchannel = pygame.mixer.Channel(3)
self.boostsound = pygame.mixer.Sound('Boost.mp3')
# Setting the window caption to the game title
pygame.display.set_caption("Slinger Showdown")
# Setting references to game objects and pushing the first frame to the screen
self.racecar = Racecar(self)
self.sb = Scoreboard(self)
self._update_screen()
def _update_screen(self):
# Redraw the screen during each pass through the loop
# Make the most recently drawn screen visible
# See each method for details as to what they do
self.racecar.blitme()
self.sb.prep_score()
self.sb.prep_timeboosts()
self.sb.prep_lapcount()
self.sb.show_score()
self._update_timer()
pygame.display.flip()
def _update_timer(self):
# Handles events based on current value of the timer
if self.timer <= 0:
self.txt = self.font.render(
'Game Over', True, (0, 0, 0), (230, 230, 230))
self.screen.blit(self.txt, (450, 335))
self.game_active = False
pygame.mixer.music.stop()
if self.racecar.lapcount > 0 and self.continue_allowed == True:
self.txt = self.font.render(
'Continue?', True, (0, 0, 0), (230, 230, 230))
self.screen.blit(self.txt, (450, 335))
else:
self.txt = self.font.render(
'Timer: ' + str(round(self.timer, 2)), True, (0, 0, 0), (230, 230, 230))
self.screen.blit(self.txt, (450, 335))
def _check_events(self):
# Respond to keypresses and mouse events
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
self._check_keydown_events(event)
elif event.type == pygame.KEYUP:
self._check_keyup_events(event)
def _check_keydown_events(self, event):
# Handles events for when a particular key is hit given respective conditions for each
# Gameplay Controls
if event.key == pygame.K_w:
if self.game_active:
self.racecar.is_moving = True
self.racecar.chan1.play(self.racecar.enginenoise)
elif event.key == pygame.K_q:
sys.exit()
elif event.key == pygame.K_ESCAPE:
if self.game_active:
self.game_active = False
pygame.mixer.music.stop()
elif self.game_active == False:
self.game_active = True
pygame.mixer.music.play(-1, 0.0)
elif event.key == pygame.K_SPACE:
if (self.game_active == True and self.racecar.is_moving == False):
if self.racecar.timeboosts > 0:
self.racecar.timeboosts -= 1
self.boostchannel.play(self.boostsound)
self.timer += 1
elif event.key == pygame.K_RETURN:
if (self.game_active == False and self.timer <= 0 and self.continue_allowed == True):
self.timer = (self.racecar.lapcount) * 5
self.racecar.lapcount = 0
self.racecar.timeboosts = 0
self.continue_allowed = False
# Audio Controls
elif event.key == pygame.K_e:
if self.racecar.chan1.get_volume() > 0:
self.racecar.chan1.set_volume(0)
elif self.racecar.chan1.get_volume() == 0:
self.racecar.chan1.set_volume(1)
elif event.key == pygame.K_m:
if pygame.mixer.music.get_volume() > 0:
pygame.mixer.music.set_volume(0)
elif pygame.mixer.music.get_volume() == 0:
pygame.mixer.music.set_volume(1)
elif event.key == pygame.K_c:
if self.racecar.chan2.get_volume() > 0:
self.racecar.chan2.set_volume(0)
elif self.racecar.chan2.get_volume() == 0:
self.racecar.chan2.set_volume(1)
elif event.key == pygame.K_b:
if self.boostchannel.get_volume() > 0:
self.boostchannel.set_volume(0)
elif self.boostchannel.get_volume() == 0:
self.boostchannel.set_volume(1)
def _check_keyup_events(self, event):
# Check when a key is released
if event.key == pygame.K_w:
self.racecar.is_moving = False
def run_game(self):
# Start the main loop for the game
while True:
# Watch for keyboard and mouse events
self.screen.blit(self.image, (0, 0))
self.racecar.blitme()
self._check_events()
if self.game_active:
self.racecar.update()
self.timer -= self.dt
self._update_screen()
self.dt = self.clock.tick(150) / 1000
if __name__ == "__main__":
sd = Showdown()
sd.run_game()