-
Notifications
You must be signed in to change notification settings - Fork 0
/
battleship.py
137 lines (117 loc) · 4.35 KB
/
battleship.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
import pygame, sys, battleshipattributes
from pygame.locals import *
from battleshipattributes import *
#Initiating Pygame
pygame.init()
#Clock and Framerate
clock = pygame.time.Clock()
FPS = 30
#Screen width and height
SCRWIDTH = 480
SCRHEIGHT = 640
#Colors
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
#Setting up the screen
screen = pygame.display.set_mode((SCRWIDTH, SCRHEIGHT))
#screen.fill(WHITE)
player1 = Player(True, 1, screen)
player2 = Player(False, 2, screen)
titleSequence(screen)
startScreen(screen)
upNext = 2
limboMode = False
font = pygame.font.SysFont("none", 24)
#smallFont = pygame.font.SysFont("none", 16)
text2 = font.render("Click to begin turn", True,(255,255,255))
#continueprompt = smallFont.render("Press ENTER to deploy", True, (0,0,0))
while True:
#msElapsed = clock.tick(FPS)
#Checking for game events'
tileClicked = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONUP:
if player2.getTurn() == True:
for row in player1.getButtonTiles():
for tile in row:
feels = tile.wasClicked(pygame.mouse.get_pos())
if feels == True:
tileClicked = True
if player1.getTurn() == True:
for row in player2.getButtonTiles():
for tile in row:
feels = tile.wasClicked(pygame.mouse.get_pos())
if feels == True:
tileClicked = True
if limboMode == True:
if upNext == 2:
player2.flipTurn()
upNext = 1
limboMode = False
else:
player1.flipTurn()
upNext = 2
limboMode = False
if tileClicked == True:
if player1.getTurn() == True:
player1.flipTurn()
limboMode = True
elif player2.getTurn() == True:
player2.flipTurn()
limboMode = True
screen.fill(BLACK)
player1.drawTiles()
player2.drawTiles()
if not limboMode:
if upNext == 1:
text = font.render("Player 2's turn!", True, (255,255,255))
else:
text = font.render("Player 1's turn!", True, (255,255,255))
screen.blit(text,(480/2 - text.get_width() / 2, 640 / 2))
else:
text = font.render("Player " + str(upNext) + " is up next! Click to continue", True, (255,255,255))
screen.blit(text,(480/2 - text.get_width() / 2, 640 / 2))
drawGrid(screen)
pygame.display.update()
if player1.allShipsDestroyed():
loopFinished = False
font = pygame.font.SysFont("none", 24)
text = font.render("The humans destroyed all alien spaceships!", True,(255,255,255))
while loopFinished == False:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONUP:
loopFinished = True
screen.fill((0,0,0))
screen.blit(text, (480/2 - text.get_width() / 2, 640 / 2))
pygame.display.update()
upNext = 2
limboMode = False
player1 = Player(True, 1, screen)
player2 = Player(False, 2, screen)
if player2.allShipsDestroyed():
loopFinished = False
font = pygame.font.SysFont("none", 24)
text = font.render("The aliens destroyed all human ships!", True,(255,255,255))
while loopFinished == False:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONUP:
loopFinished = True
screen.fill((0,0,0))
screen.blit(text, (480/2 - text.get_width() / 2, 640 / 2))
pygame.display.update()
upNext = 2
limboMode = False
player1 = Player(True, 1, screen)
player2 = Player(False, 2, screen)