-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBattleship(V1).py
998 lines (882 loc) · 42.2 KB
/
Battleship(V1).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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
import pygame
import time
import random
import os
import sys
# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
LIGHTBLUE = (4, 229, 225)
ORANGE = (249, 141, 0)
BLUE = (0, 73, 209)
DARKBLUE = (0,0,128)
GREY = (122,122,122)
#more "specific" colors.
OCEAN = (7,87,91)
WAVE = (102,165,173)
SEA = (47,73,110)
BISCOTI = (235,181,130)
pygame.init()
#start the music
try:
pygame.mixer.music.load('PacificRimTheme.mp3')
pygame.mixer.music.play(-1)
except Exception:
nothing = "nothing"
# Set the width and height of the screen [width, height]
size = (500, 500)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Battleship")
# Loop until the user clicks the close button.
done = False
end = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
#--- title screen setup ---
themenumber = 1
titlescreenshipsX = [27, 67, 107, 147, 187, 147, 187, 227, 267, 67, 67, 67, 307, 347, 387, 147, 147]
titlescreenshipsY = [392, 392, 392, 392, 392, 432, 432, 432, 432, 112, 152, 192, 272, 272, 272, 312, 272]
backgroundcolor = BLUE
gridcolor = DARKBLUE
titlegridX = []
titlegridY = []
compselectionboxX = 464
theme1outline = RED
theme2outline = WHITE
theme3outline = WHITE
theme4outline = WHITE
theme5outline = WHITE
theme6outline = WHITE
gamehelp = False
#--- player setup ---
shipchoice = 1
placershiplen = 5
placemode = True
gridX = []
gridY = []
playershipsX = []
playershipsY = []
shipprogress = 0
finalshipplacement = False
finalshipplacementX = 0
finalshipplacementY = 0
orientation = "none"
selectposition = False
PlacementColor = GREY
directionchosen = False
computer = "off"
listchoiceX = playershipsX
listchoiceY = playershipsY
#--- computer setup ---
computerplacing = True
compshipsX = []
compshipsY = []
directions = []
computerplacing = True
emptyspace = False
restofcomputership = False
directionfalse = False
#--- setup for loops ---
#this loop sets up the main game grid
currentgridX = 25
currentgridY = 25
for i in range(10):
for i in range(10):
gridX.append(currentgridX)
gridY.append(currentgridY)
currentgridX+=45
currentgridX = 25
currentgridY += 45
#this loop sets up the title screen grid
currentgridX = 25
currentgridY = 70
for i in range(10):
for i in range(10):
titlegridX.append(currentgridX)
titlegridY.append(currentgridY)
currentgridX+=40
currentgridX = 25
currentgridY += 40
#--- Main Game setup ---
playershotsX = []
playershotsY = []
playershotcolor = []
compshotsX = []
compshotsY = []
compshotcolor = []
turn = "player 1"
playerhits = 0
comphits = 0
hit = False
alreadyshot = False
compalreadyshot = True
search_and_destroy = False
compshootdirection = "none"
# -------- Title Screen Loop -----------
while not done:
# --- Main event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
end = True
# User pressed down on a key
elif event.type == pygame.KEYDOWN:
# Figure out if it was an arrow key. If so
# adjust speed.
if event.key == pygame.K_LEFT:
if computer == "on":
computer = "off"
compselectionboxX = 435
elif event.key == pygame.K_RIGHT:
if computer == "off":
computer = "on"
compselectionboxX = 463
elif event.key == pygame.K_UP:
if themenumber > 1:
themenumber-=1
elif event.key == pygame.K_DOWN:
if themenumber < 6:
themenumber+=1
# If you want a background image, replace this clear with blit'ing the
# background image.
screen.fill(backgroundcolor)
#Mouse detection
pos = pygame.mouse.get_pos()
mouseX = pos[0]
mouseY = pos[1]
pressed1, pressed2, pressed3 = pygame.mouse.get_pressed()
# --- Game logic should go here
theme1outline, theme2outline, theme3outline, theme4outline, theme5outline, theme6outline = WHITE, WHITE, WHITE, WHITE, WHITE, WHITE
if themenumber == 1:
backgroundcolor = BLUE
gridcolor = DARKBLUE
theme1outline = RED
elif themenumber == 2:
backgroundcolor = DARKBLUE
gridcolor = BLACK
theme2outline = RED
elif themenumber == 3:
backgroundcolor = DARKBLUE
gridcolor = GREEN
theme3outline = RED
elif themenumber == 4:
backgroundcolor = BLACK
gridcolor = DARKBLUE
theme4outline = RED
elif themenumber == 5:
backgroundcolor = OCEAN
gridcolor = WAVE
theme5outline = RED
elif themenumber == 6:
backgroundcolor = SEA
gridcolor = BISCOTI
theme6outline = RED
if pressed1:
if (mouseX >=435) and (mouseX <=493) and (mouseY >= 400) and (mouseY <= 458):
done = True
elif (mouseX >=435) and (mouseX <=493) and (mouseY >= 330) and (mouseY <= 388):
helpdone = False
while helpdone == False:
for event in pygame.event.get():
if event.type == pygame.QUIT:
helpdone = True
if (mouseX >=190) and (mouseX <=310) and (mouseY >= 450) and (mouseY <= 485) and pressed1:
helpdone = True
screen.fill(backgroundcolor)
pos = pygame.mouse.get_pos()
mouseX = pos[0]
mouseY = pos[1]
pressed1, pressed2, pressed3 = pygame.mouse.get_pressed()
#I used "helptext" as a variable over and over again in the interest of not having other variables. The code just changes the variable for each new line and prints it
helptitlefont = pygame.font.SysFont('Calibri', 24, True, False)
helpbodyfont = pygame.font.SysFont('Calibri', 16, True, False)
helptext = helptitlefont.render("For the Title Screen:",True,WHITE)
screen.blit(helptext, [10, 5])
helptext = helpbodyfont.render("Use the LEFT and RIGHT arrow keys to turn the computer player on and",True,WHITE)
screen.blit(helptext, [10, 35])
helptext = helpbodyfont.render("off (if the computer player is off, a second human player can play).",True,WHITE)
screen.blit(helptext, [10, 50])
helptext = helpbodyfont.render("The UP and DOWN keys change the theme (coloring) of the game.",True,WHITE)
screen.blit(helptext, [10, 75])
helptext = helpbodyfont.render("Once you have all the settings you want, hit 'GO!' to start the game.",True,WHITE)
screen.blit(helptext, [10, 100])
helptext = helptitlefont.render("For Setup:",True,WHITE)
screen.blit(helptext, [10, 125])
helptext = helpbodyfont.render("Each side has 5 ships: An Aircraft Carrier (5 spaces), a Battleship",True,WHITE)
screen.blit(helptext, [10, 155])
helptext = helpbodyfont.render("(4 spaces), a Destroyer (3 spaces), a Submarine (3 spaces) and a",True,WHITE)
screen.blit(helptext, [10, 170])
helptext = helpbodyfont.render("Patrol Boat (2 spaces).",True,WHITE)
screen.blit(helptext, [10, 185])
helptext = helpbodyfont.render("For each ship:",True,WHITE)
screen.blit(helptext, [10, 210])
helptext = helpbodyfont.render("1) Click a square to place an end of that ship. (press backspace to undo)",True,WHITE)
screen.blit(helptext, [10, 225])
helptext = helpbodyfont.render("2) Use the arrow keys to choose the direction it's pointed in",True,WHITE)
screen.blit(helptext, [10, 240])
helptext = helpbodyfont.render("3) Press shift to finalize its position and move on to the next ship",True,WHITE)
screen.blit(helptext, [10, 255])
helptext = helpbodyfont.render("NOTE: If there are two players, the setup will switch immediatly from",True,WHITE)
screen.blit(helptext, [10, 280])
helptext = helpbodyfont.render("the first player's Patrol Boat to the second player's Aircraft Carrier",True,WHITE)
screen.blit(helptext, [10, 295])
helptext = helptitlefont.render("For GamePlay:",True,WHITE)
screen.blit(helptext, [10, 320])
helptext = helpbodyfont.render("Once the game starts, players will take turns shooting at each other's",True,WHITE)
screen.blit(helptext, [10, 340])
helptext = helpbodyfont.render("ships. A red circle signifies a hit while a white one a miss. The first",True,WHITE)
screen.blit(helptext, [10, 355])
helptext = helpbodyfont.render("player to sink all of their oppenents ships win!",True,WHITE)
screen.blit(helptext, [10, 370])
helptext = helpbodyfont.render("NOTE: This game currently doesn't contain the capability to say when a",True,WHITE)
screen.blit(helptext, [10, 395])
helptext = helpbodyfont.render("ship has been sunk. At this point, each side just needs to hit every dot",True,WHITE)
screen.blit(helptext, [10, 410])
pygame.draw.rect(screen,ORANGE,[190,450,120,35])
helptext = helptitlefont.render("CONTINUE",True,WHITE)
screen.blit(helptext, [195, 455])
helptext = helpbodyfont.render("Click Here To",True,WHITE)
screen.blit(helptext, [204, 435])
pygame.display.flip()
# --- Screen-clearing code goes here
# Here, we clear the screen to white. Don't put other drawing commands
# above this, or they will be erased with this command.
# --- Drawing code should go here
for i in range(len(gridX)):
pygame.draw.rect(screen,gridcolor,[titlegridX[i],titlegridY[i],40,40], 3)
for i in range(len(titlescreenshipsX)):
pygame.draw.ellipse(screen, GREY, [titlescreenshipsX[i],titlescreenshipsY[i],35,35])
pygame.draw.rect(screen,gridcolor,[430,70,68,400], 3)
#displays the title
startfont = pygame.font.SysFont('Calibri', 47, True, False)
placetext = startfont.render("B A T T L E S H I P",True,WHITE)
screen.blit(placetext, [30, 20])
#displays the computer option switch
startfont = pygame.font.SysFont('Calibri', 13, True, False)
comptext = startfont.render("Computer?",True,WHITE)
screen.blit(comptext, [434, 74])
startfont = pygame.font.SysFont('Calibri', 16, True, False)
if computer == "off":
pygame.draw.rect(screen,RED,[435,92,29,27])
pygame.draw.rect(screen,GREY,[464,92,29,27])
placetext = startfont.render("NO",True,WHITE)
screen.blit(placetext, [438, 98])
else:
pygame.draw.rect(screen,GREEN,[464,92,29,27])
pygame.draw.rect(screen,GREY,[435,92,29,27])
placetext = startfont.render("YES",True,WHITE)
screen.blit(placetext, [467, 98])
#displays the theme options
startfont = pygame.font.SysFont('Calibri', 16, True, False)
themetext = startfont.render("Themes",True,WHITE)
screen.blit(themetext, [438, 125])
#theme1
pygame.draw.rect(screen,BLUE,[435,145,29,27])
pygame.draw.rect(screen,DARKBLUE,[464,145,29,27])
pygame.draw.rect(screen,theme1outline,[435,145,58,27], 2)
#theme2
pygame.draw.rect(screen,DARKBLUE,[435,175,29,27])
pygame.draw.rect(screen,BLACK,[464,175,29,27])
pygame.draw.rect(screen,theme2outline,[435,175,58,27], 2)
#theme3
pygame.draw.rect(screen,DARKBLUE,[435,205,29,27])
pygame.draw.rect(screen,GREEN,[464,205,29,27])
pygame.draw.rect(screen,theme3outline,[435,205,58,27], 2)
#theme4
pygame.draw.rect(screen,BLACK,[435,235,29,27])
pygame.draw.rect(screen,DARKBLUE,[464,235,29,27])
pygame.draw.rect(screen,theme4outline,[435,235,58,27], 2)
#theme5
pygame.draw.rect(screen,OCEAN,[435,265,29,27])
pygame.draw.rect(screen,WAVE,[464,265,29,27])
pygame.draw.rect(screen,theme5outline,[435,265,58,27], 2)
#theme6
pygame.draw.rect(screen,SEA,[435,295,29,27])
pygame.draw.rect(screen,BISCOTI,[464,295,29,27])
pygame.draw.rect(screen,theme6outline,[435,295,58,27], 2)
#displays the game help option
pygame.draw.rect(screen,ORANGE,[435,330,58,58])
startfont = pygame.font.SysFont('Calibri', 25, True, False)
helptext = startfont.render("HELP",True,WHITE)
screen.blit(helptext, [437, 347])
#displays the start game button
pygame.draw.rect(screen,GREEN,[435,400,58,58])
GOtext = startfont.render("GO!",True,WHITE)
screen.blit(GOtext, [444, 417])
# --- Go ahead and update the screen with what we've drawn.
pygame.display.flip()
#killswitch
if shipchoice>5:
done = True
# --- Limit to 60 frames per second
clock.tick(60)
time.sleep(0.25)
if end != True:
done = False
# -------- Setup Program Loop ----------
while not done:
# --- Main event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
end = True
# User pressed down on a key
elif event.type == pygame.KEYDOWN:
# Figure out if it was an arrow key. If so
# adjust speed.
if event.key == pygame.K_LEFT:
if (finalshipplacement == True) and (shipplacementX-(45*placershiplen)>-25):
orientation = "left"
directionchosen = True
elif event.key == pygame.K_RIGHT:
if (finalshipplacement == True) and (shipplacementX+(45*placershiplen)<490):
orientation = "right"
directionchosen = True
elif event.key == pygame.K_UP:
if (finalshipplacement == True) and (shipplacementY-(45*placershiplen)>-25):
orientation = "up"
directionchosen = True
elif event.key == pygame.K_DOWN:
if (finalshipplacement == True) and (shipplacementY+(45*placershiplen)<490):
orientation = "down"
directionchosen = True
elif event.key == pygame.K_BACKSPACE:
finalshipplacement = False
orientation = "none"
directionchosen = False
elif (event.key == pygame.K_RSHIFT) or (event.key == pygame.K_LSHIFT):
if selectposition == True and directionchosen == True:
if (shipchoice == 1) or (conflict == False):
#tells the program to move to the next ship in the roster
shipchoice+=1
#the following code adds the ships individual points to a list depending on its directio
listchoiceX.append(shipplacementX)
listchoiceY.append(shipplacementY)
if orientation == "right":
finalshipplacementY = shipplacementY
for i in range(placershiplen-1):
finalshipplacementX = (shipplacementX+((i+1)*45))
listchoiceX.append(finalshipplacementX)
listchoiceY.append(finalshipplacementY)
if orientation == "left":
finalshipplacementY = shipplacementY
for i in range(placershiplen-1):
finalshipplacementX = (shipplacementX-((i+1)*45))
listchoiceX.append(finalshipplacementX)
listchoiceY.append(finalshipplacementY)
if orientation == "up":
finalshipplacementX = shipplacementX
for i in range(placershiplen-1):
finalshipplacementY = (shipplacementY-((i+1)*45))
listchoiceX.append(finalshipplacementX)
listchoiceY.append(finalshipplacementY)
if orientation == "down":
finalshipplacementX = shipplacementX
for i in range(placershiplen-1):
finalshipplacementY = (shipplacementY+((i+1)*45))
listchoiceX.append(finalshipplacementX)
listchoiceY.append(finalshipplacementY)
selectposition = False
else:
#this makes the dots on the current ship flash red to inform the user that they can't place a ship in that position
PlacementColor = RED
#the next two lines essentially reset the code for the next ship
orientation = "none"
finalshipplacement = False
# If you want a background image, replace this clear with blit'ing the
# background image.
screen.fill(backgroundcolor)
#Mouse detection
pos = pygame.mouse.get_pos()
mouseX = pos[0]
mouseY = pos[1]
pressed1, pressed2, pressed3 = pygame.mouse.get_pressed()
# --- Game logic should go here
#Set the conflicting ship position variable to False
conflict = False
"""code that displays the current ship being placed"""
if shipchoice == 1:
placership = "Aircraft Carrier (5 spaces)"
placershiplen = 5
elif shipchoice == 2:
placership = "Battleship (4 spaces)"
placershiplen = 4
elif shipchoice == 3:
placership = "Destroyer (3 spaces)"
placershiplen = 3
elif shipchoice == 4:
placership = "Submarine (3 spaces)"
placershiplen = 3
elif shipchoice == 5:
placership = "Patrol Boat (2 spaces)"
placershiplen = 2
if placemode == True:
startfont = pygame.font.SysFont('Calibri', 12, True, False)
placetext = startfont.render("Please place your "+placership,True,WHITE)
screen.blit(placetext, [25, 12])
placetext = startfont.render("orientation: "+orientation,True,WHITE)
screen.blit(placetext, [390, 12])
if (finalshipplacement == False):
directionchosen = False
#code that follows the mouse
for i in range(len(gridX)):
if (mouseX-gridX[i]<45) and (mouseX-gridX[i]>0) and (mouseY-gridY[i]<45) and (mouseY-gridY[i]>0):
pygame.draw.ellipse(screen, PlacementColor, [gridX[i]+2,gridY[i]+2,40,40])
shipplacementX = gridX[i]+2
shipplacementY = gridY[i]+2
if pressed1:
for i in range(len(listchoiceX)):
if (listchoiceX[i] == shipplacementX) and (listchoiceY[i] == shipplacementY):
conflict = True
if conflict == False:
finalshipplacement = True
selectposition = True
#if the mouse is pressed on a point:
if (finalshipplacement == True):
if shipchoice == 1:
conflict = False
#the following code displays the current ship in the certain potential position and also gets those points in case the user selects that position
pygame.draw.ellipse(screen, PlacementColor, [shipplacementX,shipplacementY,40,40])
if orientation == "right":
for i in range(placershiplen-1):
drawshipplacementX = (shipplacementX+((i+1)*45))
pygame.draw.ellipse(screen, PlacementColor, [drawshipplacementX,shipplacementY,40,40])
for i in range(len(listchoiceX)):
if (listchoiceX[i] == drawshipplacementX) and (listchoiceY[i] == shipplacementY):
conflict = True
if orientation == "left":
for i in range(placershiplen-1):
drawshipplacementX = (shipplacementX-((i+1)*45))
pygame.draw.ellipse(screen, PlacementColor, [drawshipplacementX,shipplacementY,40,40])
for i in range(len(listchoiceX)):
if (listchoiceX[i] == drawshipplacementX) and (listchoiceY[i] == shipplacementY):
conflict = True
if orientation == "up":
for i in range(placershiplen-1):
drawshipplacementY = (shipplacementY-((i+1)*45))
pygame.draw.ellipse(screen, PlacementColor, [shipplacementX,drawshipplacementY,40,40])
for i in range(len(listchoiceX)):
if (listchoiceX[i] == shipplacementX) and (listchoiceY[i] == drawshipplacementY):
conflict = True
if orientation == "down":
for i in range(placershiplen-1):
drawshipplacementY = (shipplacementY+((i+1)*45))
pygame.draw.ellipse(screen, PlacementColor, [shipplacementX,drawshipplacementY,40,40])
for i in range(len(listchoiceX)):
if (listchoiceX[i] == shipplacementX) and (listchoiceY[i] == drawshipplacementY):
conflict = True
#resets the dot colors after they flash red:
PlacementColor = GREY
# --- Screen-clearing code goes here
# Here, we clear the screen to white. Don't put other drawing commands
# above this, or they will be erased with this command.
# --- Drawing code should go here
for i in range(len(gridX)):
pygame.draw.rect(screen,gridcolor,[gridX[i],gridY[i],45,45], 3)
for i in range(len(listchoiceX)):
pygame.draw.ellipse(screen, GREY, [listchoiceX[i],listchoiceY[i],40,40])
# --- Go ahead and update the screen with what we've drawn.
pygame.display.flip()
#killswitch
if shipchoice>5 and (turn == "player 2" or computer == "on"):
done = True
print ("1")
elif shipchoice>5 and computer == "off":
print("2")
turn = "player 2"
shipchoice = 1
listchoiceX = compshipsX
listchoiceY = compshipsY
# --- Limit to 60 frames per second
clock.tick(60)
# -------- Computer Setup Loop ---------
if end != True:
done = False
if computer == "on":
while not done:
# --- Main event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
end = True
# --- Game logic should go here
#Set the conflicting ship position variable to False
conflict = False
#set the ship being placed back to the first one
shipchoice = 1
#this while loop entirely dictates the position of the computer's ships
while computerplacing == True:
"""code for tracking each ship"""
if shipchoice == 1:
placership = "Aircraft Carrier (5 spaces)"
placershiplen = 5
elif shipchoice == 2:
placership = "Battleship (4 spaces)"
placershiplen = 4
elif shipchoice == 3:
placership = "Destroyer (3 spaces)"
placershiplen = 3
elif shipchoice == 4:
placership = "Submarine (3 spaces)"
placershiplen = 3
elif shipchoice == 5:
placership = "Patrol Boat (2 spaces)"
placershiplen = 2
emptyspace = False
restofcomputership = False
#this while loop in particular finds a starting point for the current ship that isn't already taken. This loop does not however confirm that the rest of the ship can be placed in the surrounding area
while emptyspace == False:
emptyspace = True
computershipstartpointX = (random.randint(0,9)*45)+27 #this plus 27 exists because of both the 25 pixel boundary and the 2 pixel spacing between the dots and their box
computershipstartpointY = (random.randint(0,9)*45)+27 #the "rand.int(0,9)*45 is code that randomly chooses one of the 10 boxes in each axis
if shipchoice != 1:
#this for loop sees of the box is already taken and potentially repeats the loop to find a new one
for i in range(len(compshipsX)):
if (compshipsX[i] == computershipstartpointX) and (compshipsY[i] == computershipstartpointY):
emptyspace = False
#this while loop is reponsible for placing the rest of each individual ship
while restofcomputership == False:
#this large if statement checks to see if the right direction is free of other ships
if (computershipstartpointX+(placershiplen*45)<475):
if shipchoice != 1:
for i in range(placershiplen-1):
compshipplacementX = (computershipstartpointX+((i+1)*45))
for i in range(len(compshipsX)):
if (compshipsX[i] == compshipplacementX) and (compshipsY[i] == computershipstartpointY):
directionfalse = True
if directionfalse == False:
directions.append("right")
directionfalse = False
#this large if statement checks to see if the left direction is free of other ships
if (computershipstartpointX-(placershiplen*45)>-25):
if shipchoice != 1:
for i in range(placershiplen-1):
compshipplacementX = (computershipstartpointX-((i+1)*45))
for i in range(len(compshipsX)):
if (compshipsX[i] == compshipplacementX) and (compshipsY[i] == computershipstartpointY):
directionfalse = True
if directionfalse == False:
directions.append("left")
directionfalse = False
#this large if statement checks to see if the up direction is free of other ships
if (computershipstartpointY-(placershiplen*45)>-25):
if shipchoice != 1:
for i in range(placershiplen-1):
compshipplacementY = (computershipstartpointY-((i+1)*45))
for i in range(len(compshipsX)):
if (compshipsX[i] == computershipstartpointX) and (compshipsY[i] == compshipplacementY):
directionfalse = True
if directionfalse == False:
directions.append("up")
directionfalse = False
#this large if statement checks to see if the down direction is free of other ships
if (computershipstartpointY+(placershiplen*45)<475):
if shipchoice != 1:
for i in range(placershiplen-1):
compshipplacementY = (computershipstartpointY+((i+1)*45))
for i in range(len(compshipsX)):
if (compshipsX[i] == computershipstartpointX) and (compshipsY[i] == compshipplacementY):
directionfalse = True
if directionfalse == False:
directions.append("down")
directionfalse = False
#if any of the directions are free, this code randomly chooses one of the available directions and places the ship along that direction. If there are no available directions, the whole while loop then repeats and finds another starting point
if len(directions)>0:
choice = random.randint(1,len(directions))
placementchoice = directions[choice-1]
compshipsX.append(computershipstartpointX)
compshipsY.append(computershipstartpointY)
if placementchoice == "right":
finalcomputershipplacementY = computershipstartpointY
for i in range(placershiplen-1):
finalcomputershipplacementX = (computershipstartpointX+((i+1)*45))
compshipsX.append(finalcomputershipplacementX)
compshipsY.append(finalcomputershipplacementY)
if placementchoice == "left":
finalcomputershipplacementY = computershipstartpointY
for i in range(placershiplen-1):
finalcomputershipplacementX = (computershipstartpointX-((i+1)*45))
compshipsX.append(finalcomputershipplacementX)
compshipsY.append(finalcomputershipplacementY)
if placementchoice == "up":
finalcomputershipplacementX = computershipstartpointX
for i in range(placershiplen-1):
finalcomputershipplacementY = (computershipstartpointY-((i+1)*45))
compshipsX.append(finalcomputershipplacementX)
compshipsY.append(finalcomputershipplacementY)
if placementchoice == "down":
finalcomputershipplacementX = computershipstartpointX
for i in range(placershiplen-1):
finalcomputershipplacementY = (computershipstartpointY+((i+1)*45))
compshipsX.append(finalcomputershipplacementX)
compshipsY.append(finalcomputershipplacementY)
#the following print commands are useful to see how the computer finds and chooses coordinates
del directions[:]
print (directions)
print (placementchoice)
print (str(compshipsX)+" "+str(compshipsY))
print (str(computershipstartpointX)+" "+str(computershipstartpointY))
#these 3 lines reset the loop for the next ship
del directions[:]
restofcomputership = True
shipchoice+=1
#killswitch
if shipchoice>5:
computerplacing = False
done = True
# -------- Main Program Loop -----------
turn = "player 1"
if end != True:
done = False
while not done:
# --- Main event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
end = True
# If you want a background image, replace this clear with blit'ing the
# background image.
screen.fill(backgroundcolor)
#the grid
for i in range(len(gridX)):
pygame.draw.rect(screen,gridcolor,[gridX[i],gridY[i],45,45], 3)
# --- Game logic should go here
enemylocation = False
if turn == "player 1":
# If you want a background image, replace this clear with blit'ing the
# background image.
hit = False
fired = False
pos = pygame.mouse.get_pos()
mouseX = pos[0]
mouseY = pos[1]
pressed1, pressed2, pressed3 = pygame.mouse.get_pressed()
#code that follows the mouse
for i in range(len(gridX)):
if (mouseX-gridX[i]<45) and (mouseX-gridX[i]>0) and (mouseY-gridY[i]<45) and (mouseY-gridY[i]>0):
pygame.draw.ellipse(screen, RED, [gridX[i]+2,gridY[i]+2,40,40])
shotX = gridX[i]+2
shotY = gridY[i]+2
#placing a hit code
if pressed1:
fired = True
alreadyshot = False
for i in range(len(playershotsX)):
if (shotX == playershotsX[i]) and (shotY == playershotsY[i]):
alreadyshot = True
fired = False
if alreadyshot == False:
playershotsX.append(shotX)
playershotsY.append(shotY)
for i in range(len(compshipsX)):
if (compshipsX[i] == shotX) and (compshipsY[i] == shotY):
playerhits+=1
hit = True
playershotcolor.append(RED)
if hit == False:
playershotcolor.append(WHITE)
for i in range(len(playershotsX)):
pygame.draw.ellipse(screen, playershotcolor[i], [playershotsX[i],playershotsY[i],40,40])
pygame.display.flip()
if turn == "player 2":
print ("shots taken: "+str(len(compshotsX)))
if computer == "on":
#a contingency in case the search_and_destroy protocol goes haywire
if len(directions) == 0:
search_and_destroy = False
compalreadyshot = True
hit = False
fired = True
if search_and_destroy == False:
while compalreadyshot == True:
compalreadyshot = False
currentcompshotsX = (random.randint(0,9)*45)+27 #this plus 27 exists because of both the 25 pixel boundary and the 2 pixel spacing between the dots and their box
currentcompshotsY = (random.randint(0,9)*45)+27 #the "rand.int(0,9)*45 is code that randomly chooses one of the 10 boxes in each axis
for i in range(len(compshotsX)):
if (compshotsX[i] == currentcompshotsX) and (compshotsY[i] == currentcompshotsY):
compalreadyshot = True
compshotsX.append(currentcompshotsX)
compshotsY.append(currentcompshotsY)
for i in range(len(playershipsX)):
if (currentcompshotsX == playershipsX[i]) and (currentcompshotsY == playershipsY[i]):
comphits += 1
hit = True
compshotcolor.append(RED)
#setting up the search_and_destroy protocol
search_and_destroy = True
hitcompshotX = currentcompshotsX
hitcompshotY = currentcompshotsY
if (hitcompshotX-45>25):
directions.append("left")
if (hitcompshotX+45<475):
directions.append("right")
if (hitcompshotY-45>25):
directions.append("up")
if (hitcompshotY+45<475):
directions.append("down")
if hit == False:
compshotcolor.append(WHITE)
elif search_and_destroy == True:
compalreadyshot = False
hit = False
print (directions)
print (compshootdirection)
if compshootdirection == "none":
hitcompshotX = currentcompshotsX
hitcompshotY = currentcompshotsY
choice = random.randint(1,len(directions))
print ("choice: "+str(choice-1))
compshootdirection = directions[choice-1]
if compshootdirection == "up" and (hitcompshotY-45>25):
hitcompshotY-=45
elif compshootdirection == "down" and (hitcompshotY+45<475):
hitcompshotY+=45
elif compshootdirection == "left" and (hitcompshotX-45>25):
hitcompshotX-=45
elif compshootdirection == "right" and (hitcompshotX+45<475):
hitcompshotX+=45
else:
#if this else statement is ever activated, it means that the intended shot square is outside the grid, and the computer must recalibarate (therefore, fired = False because the computer cannot fire)
fired = False
directions.pop(choice-1)
compshootdirection = "none"
print (compshootdirection)
#this if statement exists in relation to the last comment
if fired == True:
for i in range(len(compshotsX)):
if (compshotsX[i] == hitcompshotX) and (compshotsY[i] == hitcompshotY):
compalreadyshot = True
if compalreadyshot == True:
print ("activated!")
hitcompshotX = currentcompshotsX
hitcompshotY = currentcompshotsY
directions.remove(compshootdirection)
compshootdirection = "none"
fired = False
if compalreadyshot == False:
compshotsX.append(hitcompshotX)
compshotsY.append(hitcompshotY)
for i in range(len(playershipsX)):
if (hitcompshotX == playershipsX[i]) and (hitcompshotY == playershipsY[i]):
hit = True
if hit == True:
compshotcolor.append(RED)
comphits += 1
#this code prevents the computer from shooting in any other directions once the directions of the ship is found
if compshootdirection == "up" or compshootdirection == "down":
try:
directions.remove("left")
except Exception:
nothing = 0
try:
directions.remove("right")
except Exception:
nothing = 0
if compshootdirection == "right" or compshootdirection == "left":
try:
directions.remove("up")
except Exception:
nothing = 0
try:
directions.remove("down")
except Exception:
nothing = 0
if hit == False:
compshotcolor.append(WHITE)
directions.remove(compshootdirection)
compshootdirection = "none"
hitcompshotX = currentcompshotsX
hitcompshotY = currentcompshotsY
print (directions)
print ("length of directions: "+str(len(directions)))
print ("hitcompshotX: "+str(hitcompshotX))
print ("hitcompshotY: "+str(hitcompshotY))
print ("directions: "+str(compshootdirection))
print ("hit? "+str(hit))
print ("already shot? "+str(compalreadyshot))
print (comphits)
print (" ")
elif computer == "off":
hit = False
pos = pygame.mouse.get_pos()
mouseX = pos[0]
mouseY = pos[1]
pressed1, pressed2, pressed3 = pygame.mouse.get_pressed()
#code that follows the mouse
for i in range(len(gridX)):
if (mouseX-gridX[i]<45) and (mouseX-gridX[i]>0) and (mouseY-gridY[i]<45) and (mouseY-gridY[i]>0):
pygame.draw.ellipse(screen, RED, [gridX[i]+2,gridY[i]+2,40,40])
shotX = gridX[i]+2
shotY = gridY[i]+2
#placing a hit code
if pressed1:
fired = True
alreadyshot = False
for i in range(len(compshotsX)):
if (shotX == compshotsX[i]) and (shotY == compshotsY[i]):
alreadyshot = True
if alreadyshot == False:
compshotsX.append(shotX)
compshotsY.append(shotY)
for i in range(len(playershipsX)):
if (playershipsX[i] == shotX) and (playershipsY[i] == shotY):
comphits+=1
hit = True
compshotcolor.append(RED)
if hit == False:
compshotcolor.append(WHITE)
for i in range(len(compshotsX)):
pygame.draw.ellipse(screen, compshotcolor[i], [compshotsX[i],compshotsY[i],40,40])
for i in range(len(compshotsX)):
pygame.draw.ellipse(screen, compshotcolor[i], [compshotsX[i],compshotsY[i],40,40])
pygame.display.flip()
# --- Screen-clearing code goes here
# Here, we clear the screen to white. Don't put other drawing commands
# above this, or they will be erased with this command.
# --- Drawing code should go here
#turn switch
if turn == "player 2" and fired == True:
time.sleep(0.25)
turn = "player 1"
fired = False
for i in range(250):
pygame.draw.rect(screen, BLACK, [250-i,250-i,(2*i),(2*i)])
pygame.display.flip()
time.sleep(0.0025)
if turn == "player 1" and fired == True:
time.sleep(0.25)
turn = "player 2"
fired = False
for i in range(250):
pygame.draw.rect(screen, BLACK, [250-i,250-i,(2*i),(2*i)])
pygame.display.flip()
time.sleep(0.0025)
#winning/kill switch
if playerhits >= 17:
win = "player 1"
done = True
startfont = pygame.font.SysFont('Calibri', 24, True, False)
placetext = startfont.render("Congrats Player 1",True,WHITE)
screen.blit(placetext, [190, 240])
pygame.display.flip()
time.sleep(2)
if comphits >= 17:
win = "player 2"
done = True
startfont = pygame.font.SysFont('Calibri', 24, True, False)
placetext = startfont.render("Congrats Player 2",True,WHITE)
screen.blit(placetext, [190, 240])
pygame.display.flip()
time.sleep(2)
# --- Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# --- Limit to 60 frames per second
clock.tick(60)
# Close the window and quit.
if end == False:
os.execl(sys.executable, sys.executable, *sys.argv)
wait = input()
time.sleep(8)
pygame.quit()