-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions.py
105 lines (93 loc) · 3.38 KB
/
functions.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
# -*- coding: utf-8 -*-
"""
Created on Tue May 24 14:57:26 2022
@author: Orane
"""
import pygame as pg
pg.init()
vert = (63,128,70)
vertclair = (64, 255, 64)
vertfonce = (119, 221, 119)
rouge = (185, 15, 11)
rougeclair = (255, 64, 64)
rougefonce = (221, 119, 119)
bleu = (0, 68, 125)
bleuclair = (64, 64, 255)
bleufonce = (119, 119, 221)
jaune = (241, 191, 0)
jaunepale = (246, 255, 41)
r_orange = (255, 85, 64)
noir = (0, 0, 0)
def written(text,font,size,bold, color, pos,screen): #create written text
myfont = pg.font.SysFont(font, size)
TextSurf = myfont.render(text, bold, color)
TextRect = TextSurf.get_rect()
TextRect.center = pos
screen.blit(TextSurf, TextRect)#"collage" des textes sur l'écran
pg.display.flip()
def button(pos, long, larg, color,borderad,screen):
rect_object = pg.Rect(pos[0], pos[1], long, larg)
pg.draw.rect(screen, color, rect_object, borderad)
pg.display.flip()
return(rect_object)
def Grid(l,h,a,s,cell):
'''Create a square grid of length a, cell dimention cell, and with s space between the borders and the grid'''
#window
screen = pg.display.set_mode((l, h))
pg.display.set_caption("Battleship")
screen.fill(bleuclair)
pg.display.flip()
###GRID
#to center the position of the square grid for any window size
if h<l:
ecartx = abs(l-h)/2
ecarty = 0
else:
ecartx = 0
ecarty = abs(l-h)/2
#cells
for i in range(11):
pg.draw.line(screen, (0,0,0), (s+ecartx, i*cell+s+ecarty), (a-s+ecartx, i*cell+s+ecarty))
pg.draw.line(screen, (0,0,0), (i*cell+s+ecartx, s+ecarty), (i*cell+s+ecartx, a-s+ecarty))
pg.display.flip()
#writing and typography
t = 15
font = pg.font.SysFont("comicsansms", t)
alph = ["A","B","C","D","E","F","G","H","I","J"]
for i in range(10):
t1 = font.render(str(i+1), True, (0, 0, 0))
screen.blit(t1, (ecartx, ecarty+s+(i+1/2)*cell-t/2))
t2 = font.render(alph[i], True, (0, 0, 0))
screen.blit(t2, (ecartx+s+cell*(i+1/2), ecarty))
pg.display.flip()
return (s+ecartx, a-s+ecartx, s+ecarty, a-s+ecarty) #coordinates of the grid
'''
def Placmt(l, h, color, player, font, size,screen):
long = l
larg = 200
borderad = 0
pos = ((l-long)/2, (h-larg)/2)
button(pos, long, larg, color, borderad, screen)
written(player,font,size,True, noir,(l/2,h/2), screen)
written("Place your boats as you wish",font,size-30,False, noir,(l/2,h/2+40), screen)
pg.display.flip()'''
def PosClicCenterCell(x,y,cell, coord, listclicpos, xcentercell, ycentercell):
validclic = False
if (coord[0] <= x and x <= coord[1]) and (coord[2] <= y and y <= coord[3]): #in the grid
i=0
j=0
while abs(x-xcentercell[i]) > cell/2:
i += 1
while abs(y-ycentercell[j]) > cell/2:
j += 1
if (abs(x-xcentercell[i]) == cell/2) or (abs(y-ycentercell[j]) > cell/2):
print("Don't clic on a line !!")
elif (xcentercell[i],ycentercell[j]) in listclicpos:
print("You already clicked here !!")
else:
x = xcentercell[i]
y = ycentercell[j]
validclic = True
else: #not in grid
print("Clic in the grid !!")
return x,y, validclic