-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_keyboard_v2_with_controller.py
executable file
·321 lines (248 loc) · 8.96 KB
/
game_keyboard_v2_with_controller.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import random
import socket
from time import sleep
import pygame
# pygame.mixer.pre_init(frequency=45100)
pygame.init()
# pygame.mixer.init(frequency=45100) 421`
# os.system('python3 /home/shitij/Desktop/game/controller.py')
display_width = 1280
display_height = 920
border_startX = 20
border_startY = 20
border_width = display_width - (border_startX * 2)
border_height = display_height - (border_startY * 2)
screen = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('A Space Game By Shitij Shailendra Agrawal')
background = pygame.transform.scale(pygame.image.load('./asset/background.jpeg'), (display_width, display_height))
hero_left_images = [
pygame.image.load('./asset/hero-left1.png'),
pygame.image.load('./asset/hero-left2.png'),
pygame.image.load('./asset/hero-left3.png'),
pygame.image.load('./asset/hero-left4.png'),
pygame.image.load('./asset/hero-left5.png')
]
hero_right_images = [
pygame.image.load('./asset/hero-right1.png'),
pygame.image.load('./asset/hero-right2.png'),
pygame.image.load('./asset/hero-right3.png'),
pygame.image.load('./asset/hero-right4.png'),
pygame.image.load('./asset/hero-right5.png')
]
hero_center_image = [
pygame.image.load('./asset/hero-stand1.png'),
pygame.image.load('./asset/hero-stand2.png'),
pygame.image.load('./asset/hero-stand3.png'),
pygame.image.load('./asset/hero-stand2.png'),
pygame.image.load('./asset/hero-stand3.png')
]
hero = hero_center_image[0]
wilan = pygame.image.load('./asset/vilian.png')
blast = pygame.image.load('./asset/blast.png')
score = 0
hero_life = 100
bullet_width = 1
bullet_height = 10
bullet_speed = 10
bullet_color = (0, 0, 0)
bullets = []
total_wilan = 20
wilan_minimum_speed = 1
wilan_maximum_speed = 3
wilans_speed = [random.randint(wilan_minimum_speed, wilan_maximum_speed) for _ in range(total_wilan)]
wilans_positions = [[], [random.randint(-10, border_startY) for _ in range(total_wilan)]]
incrment_x = (border_width - wilan.get_rect().size[0]) / total_wilan
temp = border_startX
for ele in range(len(wilans_positions[1])):
wilans_positions[0].append(random.randint(temp, int(temp + incrment_x)))
temp = int(temp + incrment_x)
hero_positionX = (border_startX + border_width) / 2
hero_positionY = (border_startY + border_height) - hero.get_rect().size[1]
game_over = False
def play_blast_audio():
pygame.mixer.music.load("./audio/blast.mp3")
pygame.mixer.music.set_volume(0.7)
pygame.mixer.music.play()
def play_fire_audio():
pygame.mixer.music.load("./audio/fire.mp3")
pygame.mixer.music.set_volume(0.7)
pygame.mixer.music.play()
def display_score():
myfont = pygame.font.SysFont('Comic Sans MS', 50)
textsurface = myfont.render('Score : ' + str(score), False, (255, 255, 255))
screen.blit(textsurface, (0, 0))
def display_hero_life():
myfont = pygame.font.SysFont('Comic Sans MS', 50)
textsurface = myfont.render('Life : ' + str(hero_life), False, (255, 255, 255))
screen.blit(textsurface, (int(display_width / 2), 0))
def draw_border():
# noinspection PyArgumentList
pygame.draw.rect(screen, (255, 0, 0), [border_startX, border_startY, border_width, border_height], 2)
def mov_hero_to_left():
displacement = 15
global hero_positionX
global border_startX
if not (hero_positionX - displacement) < border_startX:
hero_positionX = hero_positionX - displacement
def mov_hero_to_right():
displacement = 15
global hero_positionX
global border_startX
if not ((hero_positionX + hero.get_rect().size[0]) + displacement) > (border_startX + border_width):
hero_positionX = hero_positionX + displacement
def mov_hero_to_up():
displacement = 15
global hero_positionY
global border_startY
if not (hero_positionY - displacement) < border_startX:
hero_positionY = hero_positionY - displacement
def mov_hero_to_down():
displacement = 15
global hero_positionY
global border_startY
if not ((hero_positionY + hero.get_rect().size[1]) + displacement) > (border_startY + border_height):
hero_positionY = hero_positionY + displacement
def fire():
play_fire_audio()
center_x = int(hero_positionX + (hero.get_rect().size[0] / 2))
pygame.draw.rect(screen, bullet_color, [center_x, hero_positionY - bullet_height, bullet_width, bullet_height])
bullets.append([center_x, hero_positionY - bullet_height, bullet_width, bullet_height])
def add_new_wilan():
wilans_positions[0].append(random.randint(border_startX, border_width - wilan.get_rect().size[0]))
wilans_positions[1].append(random.randint(-10, border_startY))
wilans_speed.append(random.randint(wilan_minimum_speed, wilan_maximum_speed))
def destory_old_wilans():
for position in range(len(wilans_positions[1])):
try:
if wilans_positions[1][position] > (border_startY + border_height):
del wilans_positions[0][position]
del wilans_positions[1][position]
del wilans_speed[position]
except Exception as ex:
print("destory_old_wilans : ", ex)
def game_Over():
for img in range(1, 9, 1):
screen.blit(pygame.image.load('./asset/blast' + str(img) + '.png'), (int(hero_positionX), int(hero_positionY)))
pygame.display.update()
play_blast_audio()
sleep(1)
screen.blit(
pygame.transform.scale(pygame.image.load('./asset/gameOver.png'), (int(border_width), int(border_height / 2))),
(border_startX, border_startY / 2))
pygame.display.update()
sleep(5)
def display_wilans():
global game_over
destory_old_wilans()
while len(wilans_positions[0]) < total_wilan:
add_new_wilan()
for pos in range(len(wilans_positions[0])):
if (hero_positionY <= wilans_positions[1][pos] <= hero_positionY +
hero.get_rect().size[1]) or (
wilans_positions[1][pos] + wilan.get_rect().size[1] >= hero_positionY and wilans_positions[1][pos] +
wilan.get_rect().size[1] <= hero_positionY + hero.get_rect().size[1]) and not game_over:
if ((hero_positionX + 10) <= wilans_positions[0][pos] <= (
hero_positionX + hero.get_rect().size[0]) - 10) or (
(hero_positionX + 10) <= wilans_positions[0][pos] + wilan.get_rect().size[0] <= (
hero_positionX + hero.get_rect().size[0]) - 10):
global hero_life
if hero_life == 0:
game_over = True
break
else:
hero_life -= 1
if game_over:
break
wilans_positions[1][pos] += wilans_speed[pos]
screen.blit(wilan, (wilans_positions[0][pos], wilans_positions[1][pos]))
def show_animation(x_position, y_position):
screen.blit(blast, (x_position, y_position))
def process_bullets():
global score
for bullet in bullets:
for position in range(len(wilans_positions[0])):
try:
if wilans_positions[0][position] <= bullet[0] <= wilans_positions[0][position] + wilan.get_rect().size[0]:
if wilans_positions[1][position] <= bullet[1] <= wilans_positions[1][position] + wilan.get_rect().size[1]:
play_blast_audio()
show_animation(wilans_positions[0][position], wilans_positions[1][position])
score = score + 1
bullets.remove(bullet)
del wilans_positions[0][position]
del wilans_positions[1][position]
del wilans_speed[position]
continue
except Exception as ex:
print("process_bullets : ", ex)
if bullet[1] <= border_startY:
try:
bullets.remove(bullet)
except Exception as ex:
print("process_bullets : ", ex)
else:
bullet[1] = bullet[1] - bullet_speed
pygame.draw.rect(screen, (255, 0, 255), [bullet[0], bullet[1] - bullet_speed, bullet[2], bullet[3]])
def update():
screen.blit(background, (0, 0))
process_bullets()
screen.blit(hero, (hero_positionX, hero_positionY))
display_wilans()
draw_border()
display_score()
display_hero_life()
def init():
pygame.mixer.init()
pygame.font.init()
screen.blit(background, (0, 0))
screen.blit(hero, (hero_positionX, hero_positionY))
screen.blit(wilan, (100, 10))
draw_border()
init()
current_events = {pygame.K_UP: pygame.KEYUP, pygame.K_DOWN: pygame.KEYUP, pygame.K_RIGHT: pygame.KEYUP, pygame.K_LEFT: pygame.KEYUP, pygame.K_SPACE: pygame.KEYUP}
image_count = 0
while not game_over:
try:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
pygame.quit()
request = b"GET / HTTP/1.1\nHost: 192.168.147.100:80\n\n"
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("192.168.43.100", 80))
s.send(request)
recv = s.recv(3)
while len(recv) > 0 and not game_over:
data = int(s.recv(3))
if image_count > len(hero_right_images) - 1:
image_count = 0
if data == 255:
image_count = 0
hero = hero_center_image[0]
if data == 247 or data == 246:
hero = hero_center_image[image_count]
mov_hero_to_up()
image_count += 1
if data == 251:
hero = hero_center_image[image_count]
mov_hero_to_down()
image_count += 1
if data == 254:
hero = hero_right_images[image_count]
mov_hero_to_right()
image_count += 1
if data == 253:
hero = hero_left_images[image_count]
mov_hero_to_left()
image_count += 1
if data == 127:
hero = hero_center_image[0]
image_count = 0
fire()
update()
pygame.display.update()
except Exception as e:
print("main : ", e)
update()
pygame.display.update()
game_Over()
pygame.quit()