-
Notifications
You must be signed in to change notification settings - Fork 8
/
game.py
executable file
·296 lines (255 loc) · 10 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# 2 Cars
# Copyright (C) 2015 Utkarsh Tiwari
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Contact information:
# Utkarsh Tiwari iamutkarshtiwari@gmail.com
import os
from gettext import gettext as _
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
import pygame
from elements import Element
from welcomescreen import Welcomescreen
from scorescreen import Scorescreen
FPS = 60
# Game states
WELCOME = 0
RULES = 1
PLAY = 2
PAUSE = 3
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
# SPEED of cars in the x-axis, lane change speed
ANGLE_LIMIT = 30
ANGLE_SPEED = 2
TICK_COUNTER_THRESHOLD = 40
FONT_PATH_ARIMO = os.path.join("fonts", "Arimo.ttf")
BACKGROUND_IMAGE_PATH = os.path.join("assets", "background.png")
REDCAR_IMAGE_PATH = os.path.join("assets", "redcar.png")
BLUECAR_IMAGE_PATH = os.path.join("assets", "bluecar.png")
SOUND_HIT_PATH = os.path.join("assets", "sounds", "hit.ogg")
SOUND_MISS_PATH = os.path.join("assets", "sounds", "miss.ogg")
SOUND_MUSIC_PATH = os.path.join("assets", "sounds", "music.ogg")
SOUND_SCORE_PATH = os.path.join("assets", "sounds", "score.ogg")
RED_CIRCLE_PATH = os.path.join("assets", "redcircle.png")
BLUE_CIRCLE_PATH = os.path.join("assets", "bluecircle.png")
RED_SQUARE_PATH = os.path.join("assets", "redsquare.png")
BLUE_SQUARE_PATH = os.path.join("assets", "bluesquare.png")
class Game:
def __init__(self):
self.state = WELCOME
pygame.font.init()
pygame.mixer.init()
self.load_assets()
def load_assets(self):
self.background_image = pygame.image.load(BACKGROUND_IMAGE_PATH)
self.leftcar_image = pygame.image.load(REDCAR_IMAGE_PATH)
self.rightcar_image = pygame.image.load(BLUECAR_IMAGE_PATH)
self.red_cricle_image = pygame.image.load(RED_CIRCLE_PATH)
self.blue_circle_image = pygame.image.load(BLUE_CIRCLE_PATH)
self.red_square_image = pygame.image.load(RED_SQUARE_PATH)
self.blue_square_image = pygame.image.load(BLUE_SQUARE_PATH)
self.hit = pygame.mixer.Sound(SOUND_HIT_PATH)
self.miss = pygame.mixer.Sound(SOUND_MISS_PATH)
self.music = pygame.mixer.Sound(SOUND_MUSIC_PATH)
self.scoresound = pygame.mixer.Sound(SOUND_SCORE_PATH)
self.sounds = [self.hit, self.miss, self.music, self.scoresound]
self.volume = 1
def toggle_mute(self):
self.volume = 0 if self.volume == 1 else 1
for sound in self.sounds:
sound.set_volume(self.volume)
def scale_images(self):
self.background = pygame.transform.scale(
self.background_image, (491, 768))
self.leftcar = pygame.transform.scale(
self.leftcar_image, (45, 90))
self.rightcar = pygame.transform.scale(
self.rightcar_image, (45, 90))
self.red_circle = pygame.transform.scale(
self.red_cricle_image, (40, 40))
self.blue_circle = pygame.transform.scale(
self.blue_circle_image, (40, 40))
self.red_square = pygame.transform.scale(
self.red_square_image, (40, 40))
self.blue_square = pygame.transform.scale(
self.blue_square_image, (40, 40))
def initialize(self):
self.score = 0
self.left = True
self.right = True
self.is_left_pressed = self.is_right_pressed = False
self.left_moved = False
self.right_moved = False
self.left_angle = self.right_angle = 0
self.leftcar_x = 390
self.rightcar_x = 760
self.speed = 10
self.tick_counter = 0
self.scale_images()
self.font3 = pygame.font.Font(
FONT_PATH_ARIMO, 55)
self.OBJECT_LIST = [
self.red_circle,
self.red_square,
self.blue_circle,
self.blue_square]
self.objectlist = []
self.last = Element(self)
self.objectlist.append(self.last)
self.lastright = self.lastleft = 0
self.collision = False
self.coin_miss = False
self.lastleft = self.lastright = Element(self)
self.sound = True
def draw_cars(self):
self.screen.blit(pygame.transform.rotate(
self.leftcar, self.left_angle), (self.leftcar_x, 550))
self.screen.blit(pygame.transform.rotate(
self.rightcar, self.right_angle), (self.rightcar_x, 550))
def draw_objects(self):
for object in self.objectlist:
object.display(self)
def draw_score(self):
score_object = self.font3.render(_(str(self.score)), 1, (WHITE))
self.screen.blit(score_object, (780, 20))
def render(self):
self.screen.fill(BLACK)
self.screen.blit(self.background, (350, 0))
self.draw_cars()
self.draw_objects()
self.draw_score()
def handle_events(self):
while Gtk.events_pending():
Gtk.main_iteration()
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT and not self.is_left_pressed:
self.left_moved = True
self.is_left_pressed = True
elif event.key == pygame.K_RIGHT and not self.is_right_pressed:
self.right_moved = True
self.is_right_pressed = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
self.is_left_pressed = False
elif event.key == pygame.K_RIGHT:
self.is_right_pressed = False
def update_tick_counter(self):
self.tick_counter += 1
if self.tick_counter > TICK_COUNTER_THRESHOLD:
self.tick_counter = 0
def handle_collision(self):
if self.collision or self.coin_miss:
screen = Scorescreen(self)
return_code = screen.run(self, self.score)
if return_code == 0:
self.state = WELCOME # Home button is pressed from scorescreen
self.initialize()
def create_object_list(self):
if self.tick_counter == 10 or self.tick_counter == 35:
self.soon_to_draw = Element(self)
if self.last.x_to_draw < 530:
while True:
soon_to_draw = Element(self)
if soon_to_draw.x_to_draw > 530 and \
soon_to_draw.object != self.lastright.object:
break
self.last = self.lastright = soon_to_draw
self.objectlist.append(self.lastright)
else:
while True:
soon_to_draw = Element(self)
if soon_to_draw.x_to_draw < 530 and \
soon_to_draw.object != self.lastleft.object:
break
self.last = self.lastleft = soon_to_draw
self.objectlist.append(self.lastleft)
def update_car_angles(self):
if not self.left_moved:
if self.left_angle > 0:
self.left_angle -= ANGLE_SPEED
if self.left_angle < 0:
self.left_angle += ANGLE_SPEED
if not self.right_moved:
if self.right_angle > 0:
self.right_angle -= ANGLE_SPEED
if self.right_angle < 0:
self.right_angle += ANGLE_SPEED
def update_car_positions(self):
if self.left_moved:
if self.left:
self.leftcar_x += self.speed
if self.left_angle > -ANGLE_LIMIT:
self.left_angle -= ANGLE_SPEED
if self.leftcar_x >= 510:
self.left_moved = False
self.left = not self.left
else:
self.leftcar_x -= self.speed
if self.left_angle < ANGLE_LIMIT:
self.left_angle += ANGLE_SPEED
if self.leftcar_x <= 390:
self.left_moved = False
self.left = not self.left
if self.right_moved:
if self.right:
self.rightcar_x -= self.speed
if self.right_angle < ANGLE_LIMIT:
self.right_angle += ANGLE_SPEED
if self.rightcar_x <= 640:
self.right_moved = False
self.right = not self.right
else:
self.rightcar_x += self.speed
if self.right_angle > -ANGLE_LIMIT:
self.right_angle -= ANGLE_SPEED
if self.rightcar_x >= 760:
self.right_moved = False
self.right = not self.right
def update_game_state(self):
self.update_tick_counter()
self.handle_collision()
self.create_object_list()
self.update_car_angles()
self.update_car_positions()
def run(self):
self.initialize()
clock = pygame.time.Clock()
self.running = True
self.music.play(-1)
self.screen = pygame.display.get_surface()
while self.running:
self.render()
if self.state == WELCOME:
screen = Welcomescreen(self)
screen.run(self)
self.state = PLAY
self.handle_events()
self.update_game_state()
pygame.display.update()
clock.tick(FPS)
return
if __name__ == "__main__":
pygame.display.set_mode((1200, 900))
game = Game()
game.run()