-
Notifications
You must be signed in to change notification settings - Fork 0
/
battleship1.py
175 lines (129 loc) · 5.19 KB
/
battleship1.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
# # B's Battle Ship
############################################
#This version is broken! Do not run
# Import appropriate modules , initialize program, and set constants
import pygame
from pygame.locals import *
import random
import sys
import math
pygame.init()
pygame.font.init()
canvas_size = (1024,768) # canvas will be resizable
center = [int(canvas_size[0]/2),int(canvas_size[1]/2)]
running = True
select = False
select_pos = [0,0]
# Colors for game
GREEN = (0,128,0)
WHITE = (255,255,255)
RED = (255,0,0)
BLUE = (0, 0, 255)
BLACK = (0,0,0)
# Fonts for Game
Bf = pygame.font.SysFont("Fonts\times.ttf", 80, False, False )
sf = pygame.font.SysFont("Fonts\times.ttf", 40, False, False )
# Class Definitions
class CircleShip:
def __init__(self,pos,radius):
self.pos = pos # center of the circle
self.rad = radius
self.color = WHITE
def __str__(self):
specs = "Center (" + str(self.pos[0]) + "," + str(self.pos[1]) + ")"
return specs
def draw_circle(self,color):
pygame.draw.circle(canvas,self.color, self.pos,self.rad)
def select_circle(self,pos):
return dist(pos,self.pos) < self.rad
class RectShip:
def __init__(self,pos,size):
self.pos = pos
self.color = WHITE
self.size = size # dimensions of rectangle
def __str__(self):
specs = "Position (" + str(self.pos[0]) + "," + str(self.pos[1]) + ")"
return specs
def draw_rect(self,color=WHITE):
[h,k] = self.pos
list = [[h-150,k - 150],[h+150,k-150],[h+150,k+150],[h-150,k+150]]
pygame.draw.polygon(canvas,color,list,1)
# Helper Functions
def text(string, font = sf, color = WHITE):
### returns surface consisting of text in the font and color specified. May then be blitted.)
return font.render(string,0,color)
def dist(p1,p2):
### returns distance between two points
return math.sqrt((p1[0]-p2[0])**2 + (p1[1]-p2[1])**2)
# Event handler will receive event from queue and determine outcomes
def findevent():
global canvas, running, canvas_size, center, message, circ, select, select_pos, rect, cursor_pos
new_cursor_pos = pygame.mouse.get_pos()
for event in list(pygame.event.get()):
if event.type == pygame.QUIT: # Close window or hit 'x' to quit the program
running = False
pygame.quit()
sys.exit(0)
if event.type == pygame.KEYDOWN:
if event.key == K_x:
running = False
pygame.quit()
sys.exit(0)
if event.type==VIDEORESIZE: # Drag to resize the screen
canvas_size = event.dict['size']
if select == False and event.type == MOUSEBUTTONDOWN and dist(new_cursor_pos,circ.pos) < circ. rad: # Turn on mouse to move circle
select = True
circ.color = RED
cursor_pos = new_cursor_pos
# move_pos = [0,0]
# for i in range(0,2):
# move_pos[i] = new_cursor_pos[i] - cursor_pos[i]
# circ.pos[i] += move_pos[i]
# cursor_pos = new_cursor_pos
#canvas=pygame.display.set_mode(canvas_size,DOUBLEBUF|RESIZABLE)
#canvas.fill(BLUE)
#center = [int(canvas_size[0]/2),int(canvas_size[1]/2)]
#[h,k] = center
#message = "center is [" + str(h) + ", " + str(k) + "]"
#rect = [[h-150,k - 150],[h+150,k-150],[h+150,k+150],[h-150,k+150]]
#pygame.draw.polygon(canvas,WHITE,rect,1)
#circ.draw_circle(circ.color)
if event.type == MOUSEBUTTONUP:
circ.color = WHITE
select = False
def draw_handler():
global circ, canvas, select, cursor_pos, canvas_size
if select == True:
move_pos = [0,0]
# cir.color = RED
new_cursor_pos = pygame.mouse.get_pos()
for i in range(0,2):
move_pos[i] = new_cursor_pos[i] - cursor_pos[i]
circ.pos[i] += move_pos[i]
cursor_pos = new_cursor_pos
canvas=pygame.display.set_mode(canvas_size,DOUBLEBUF|RESIZABLE)
canvas.fill(BLUE)
center = [int(canvas_size[0]/2),int(canvas_size[1]/2)]
[h,k] = center
message = "center is [" + str(h) + ", " + str(k) + "]"
rect = [[h-150,k - 150],[h+150,k-150],[h+150,k+150],[h-150,k+150]]
pygame.draw.polygon(canvas,WHITE,rect,1)
circ.draw_circle(circ.color)
canvas.blit(text(message),(50,50))
pygame.display.flip()
pygame.event.clear()
# Start frame
canvas = pygame.display.set_mode(canvas_size,pygame.RESIZABLE)
pygame.display.set_caption("B's Battleship Game")
canvas.fill(BLUE)
### initialize test ships rect and circ
ctr =[center[0]-150,center[0]-150] # Initial left top corner of the ship rect
circ = CircleShip(center,100)
circ.draw_circle(WHITE)
rect = [ctr,[ctr[0]+300,ctr[1]],[ctr[0]+300,ctr[1]+300],[ctr[0],ctr[1]+300] ]
pygame.draw.polygon(canvas,WHITE,rect,1)
# cursor_pos = pygame.mouse.get_pos()
pygame.display.flip()
while running:
findevent()
draw_handler()